
org.eriwen.rtm.RtmCollectionParser.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of groovyrtm Show documentation
Show all versions of groovyrtm Show documentation
Java/Groovy API library for Remember The Milk
The newest version!
/*
* Copyright 2009-2011 Eric Wendelin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eriwen.rtm
import org.eriwen.rtm.model.*
import groovy.util.slurpersupport.GPathResult
/**
* Utility class used to house RTM XML parsing methods
*
* @author @author Eric Wendelin
*/
class RtmCollectionParser {
/**
* Creates a java.util.LinkedHashMap
from XML representing an
* RTM Task. For instance, the XML:
*
*
* {@code
* <rsp stat="ok">
* <transaction id="123"/>
* <list id="100">
* <taskseries id="101" created="2009-05-07T10:19:54Z" modified="2009-05-07T10:19:54Z"
* name="Get Bananas" source="api" location_id="234" url="http://eriwen.com">
* <rrule every="1">FREQ=DAILY;INTERVAL=1</rrule>
* <tags><tag>yay</tag><tag>bananas</tag></tags>
* <participants/>
* <notes/>
* <task id="102" due="2009-05-07T10:19:54Z" has_due_time="1" added="2009-05-07T10:19:54Z"
* completed="" deleted="" priority="2" postponed="0" estimate="4 hours"/>
* </taskseries>
* </list>
* </rsp>
* }
*
*
* Will become:
*
* ['transaction_id':'123', 'name':'Get Bananas',
* 'list_id':'100', 'taskseries_id':'101', 'task_id':'102',
* 'priority': '2', 'due':'2009-05-07T10:19:54Z', 'has_due_time':'1',
* 'completed':'', 'deleted':'', 'estimate':'4 hours', 'repeat':'FREQ=DAILY;INTERVAL=1',
* 'url':'http://eriwen.com', 'location':'234', 'tags':'yay, bananas',
* 'notes':'', 'participants':'']
*/
public Task parseTask(GPathResult taskXml) {
if (!taskXml) {
return null
}
def taskSeries = taskXml.list.taskseries
def task = taskSeries.task
def transactionId = taskXml.transaction ? [email protected]() : ''
return new Task(
transactionId: transactionId,
name: [email protected](),
listId: [email protected](),
taskSeriesId: [email protected](),
taskId: [email protected](),
due: [email protected](),
hasDueTime: task.@has_due_time.toString().equals('1'),
completed: [email protected]().equals(''),
priority: [email protected](),
deleted: [email protected]().equals(''),
postponed: [email protected]().equals('1'),
estimate: [email protected](),
repeat: taskSeries.rrule.text().toString(),
url: [email protected](),
locationId: taskSeries.@location_id.toString(),
tags: taskSeries.tags.tag.collect{ it.text() },
notes: taskSeries.notes.collect{ new Note(id: [email protected](), title: [email protected](), text: it.text() ) },
participants: taskSeries.participants.participant.collect{ it.text() }
)
}
/**
* Creates a java.util.List
of LinkedHashMaps representing RTM
* tasks from the XML returned by the RTM API. For example:
*
*
* {@code
* <rsp stat="ok">
* <tasks>
* <list id="100">
* <taskseries id="101" created="2009-05-07T10:19:54Z" modified="2009-05-07T10:19:54Z"
* name="Get Bananas" source="api">
* <tags/>
* <participants/>
* <notes/>
* <task id="102" due="" has_due_time="0" added="2009-05-07T10:19:54Z"
* completed="" deleted="" priority="N" postponed="0" estimate=""/>
* </taskseries>
* <taskseries id="103" created="2009-05-07T10:19:54Z" modified="2009-05-07T10:19:54Z"
* name="Buy Coffee" source="api">
* <tags>
* <tag>mmm</tag>
* <tag>tasty</tag>
* </tags>
* <participants/>
* <notes/>
* <task id="104" due="" has_due_time="0" added="2009-05-07T10:19:54Z"
* completed="" deleted="" priority="2" postponed="1" estimate="3 hrs"/>
* </taskseries>
* </list>
* <list id="105">
* <taskseries id="106" created="2009-05-07T10:19:54Z" modified="2009-05-07T10:19:54Z"
* name="Do Some Work" source="api">
* <tags/>
* <participants/>
* <notes/>
* <task id="107" due="" has_due_time="0" added="2009-05-07T10:19:54Z"
* completed="" deleted="" priority="3" postponed="0" estimate="4 hrs"/>
* </taskseries>
* </list>
* </tasks>
* </rsp>
* }
*
*
* Will become:
*
* [
* ['name':'Get Bananas', 'list_id':'100', 'taskseries_id':'101', 'task_id':'102', 'priority': 'N', 'due':'', 'has_due_time':'0', 'completed':'', 'deleted':'', 'estimate':'', 'repeat':'', 'url':'', 'location':'', 'tags':'', 'notes':'', 'participants':''],
* ['name':'Buy Coffee', 'list_id':'100', 'taskseries_id':'103', 'task_id':'104', 'priority': '2', 'due':'', 'has_due_time':'0', 'completed':'', 'deleted':'', 'estimate':'3 hrs', 'repeat':'', 'url':'', 'location':'', 'tags':'mmm, good', 'notes':'', 'participants':''],
* ['name':'Do Some Work', 'list_id':'105', 'taskseries_id':'106', 'task_id':'107', 'priority': '3', 'due':'', 'has_due_time':'0', 'completed':'', 'deleted':'', 'estimate':'4 hrs', 'repeat':'', 'url':'', 'location':'', 'tags':'', 'notes':'', 'participants':'']
* ]
*/
public List parseTaskList(GPathResult response) {
if (!response) {
return []
}
def tasksList = []
response.tasks.list.each {
def listId = [email protected]()
it.taskseries.each {
def taskSeries = it
def taskName = [email protected]()
def taskSeriesId = [email protected]()
def repeat = taskSeries.rrule.text().toString()
def url = [email protected]()
def locationId = taskSeries.@location_id.toString()
def tags = taskSeries.tags.tag.collect{ it.text() }
def notes = null //taskSeries.notes.collect{ it.text() }
def participants = taskSeries.participants.participant.collect{ it.text() }
it.task.each {
def task = it
tasksList << new Task(
name: taskName,
listId: listId,
taskSeriesId: taskSeriesId,
repeat: repeat,
url: url,
locationId: locationId,
tags: tags,
notes: notes,
participants: participants,
taskId: [email protected](),
due: [email protected](),
hasDueTime: task.@has_due_time.toString().equals('1'),
completed: [email protected]().equals(''),
priority: [email protected](),
deleted: [email protected]().equals(''),
postponed: [email protected]().equals('1'),
estimate: [email protected]()
)
}
}
}
return tasksList
}
/**
* Creates a java.util.LinkedHashMap
from XML representing an
* RTM Note. For instance, the XML:
*
*
* {@code
* <rsp stat="ok">
* <transaction id="234"/>
* <note id="169624" created="2009-05-07T11:26:49Z" modified="2009-05-07T11:26:49Z" title="Note Title">Note Body</note>
* </rsp>
* }
*
*
* Will become:
*
* ['transaction_id':'234', 'id':'169624', 'title':'Note Title', 'text':'Note Body']
*/
public Note parseNote(GPathResult noteXml) {
if (!noteXml) {
return null
}
def note = noteXml.note
return new Note(
transactionId: [email protected](),
id: [email protected](),
title: [email protected](),
text: note.text().toString()
)
}
/**
* Creates a java.util.LinkedHashMap
from XML representing an
* RTM List. For instance, the XML:
*
*
* {@code
* <rsp stat="ok">
* <transaction id="234"/>
* <list id="123" name="New List" deleted="0" locked="0" archived="0" position="-1" smart="0"/>
* </rsp>
* }
*
*
* Will become:
*
* ['transaction_id':'234', 'id':'123', 'name':'New List', 'deleted':'0',
* 'locked':'0', 'archived':'0', 'position':'-1', 'smart':'0']
*/
public TaskList parseList(GPathResult listXml) {
if (!listXml) {
return null
}
def list = listXml.list
return new TaskList(
transactionId: [email protected](),
id: [email protected](),
name: [email protected](),
deleted: [email protected]().equals('1'),
locked: [email protected]().equals('1'),
archived: [email protected]().equals('1'),
position: [email protected](),
smart: [email protected]().equals('1')
)
}
/**
* Creates a java.util.LinkedHashMap
from XML representing an
* RTM List. For instance, the XML:
*
*
* {@code
* <rsp stat="ok">
* <lists>
* <list id="123" name="New List" deleted="0" locked="0" archived="0" position="-1" smart="0"/>
* <list id="124" name="Other List" deleted="0" locked="0" archived="0" position="0" smart="1"/>
* </lists>
* </rsp>
* }
*
*
* Will become:
* [
* ['id':'123', 'name':'New List', 'deleted':'0', 'locked':'0', 'archived':'0', 'position':'-1', 'smart':'0']
* ['id':'124', 'name':'Other List', 'deleted':'0', 'locked':'0', 'archived':'0', 'position':'0', 'smart':'1']
* ]
*/
public List parseLists(GPathResult listsXml) {
if (!listsXml) {
return null
}
def listsList = []
listsXml.lists.list.each {
def list = it
listsList << new TaskList(
id: [email protected](),
name: [email protected](),
deleted: [email protected]().equals('1'),
locked: [email protected]().equals('1'),
archived: [email protected]().equals('1'),
position: [email protected](),
smart: [email protected]().equals('1')
)
}
return listsList
}
/**
* Creates a java.util.LinkedHashMap
from XML representing RTM
* settings. For instance, the XML:
*
*
* {@code
* <rsp stat="ok">
* <settings>
* <timezone>Australia/Sydney</timezone>
* <dateformat>0</dateformat>
* <timeformat>0</timeformat>
* <defaultlist>123456</defaultlist>
* </settings>
* </rsp>
* }
*
*
* Will become:
*
* ['timezone':'Australia/Sydney', 'dateformat':'0', 'timeformat': '0', 'defaultlist':'123456']
*/
public LinkedHashMap parseSettings(GPathResult settingsXml) {
if (!(settingsXml && [email protected]("ok"))) {
return null
}
def settings = settingsXml.settings
return [
'timezone': settings.timezone.toString(),
'dateformat': settings.dateformat.toString(),
'timeformat': settings.timeformat.toString(),
'defaultlist': settings.defaultlist.toString()
]
}
/**
* Creates a java.util.LinkedHashMap
from XML representing RTM
* Timezones. For instance, the XML:
*
*
* {@code
* <rsp stat="ok">
* <timezones>
* <timezone id="216" name="Asia/Hong_Kong" dst="0" offset="28800" current_offset="25200" />
* <timezone id="217" name="Asia/Hovd" dst="1" offset="28800" current_offset="25200" />
* </timezones>
* </rsp>
* }
*
*
* Will become:
*
* [
* ['id':'216', 'name':'Asia/Hong_Kong', 'dst':'0', 'offset','28800', 'currentOffset';'25200'],
* ['id':'217', 'name':'Asia/Hovd', 'dst':'1', 'offset','28800', 'currentOffset';'25200']
* ]
*/
public List parseTimezones(GPathResult timezonesXml) {
if (!(timezonesXml && [email protected]("ok"))) {
return null
}
def timezones = []
timezonesXml.timezones.timezone.each {
timezones.push(new Timezone(id:[email protected](), name:[email protected](),
dst:[email protected](), offset:[email protected](),
currentOffset:it.@current_offset.toString()))
}
timezones
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy