1 16 package org.roller.presentation.atomapi; 17 18 import java.text.ParseException ; 19 import java.text.SimpleDateFormat ; 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.jdom.Document; 26 import org.jdom.Element; 27 import org.jdom.Namespace; 28 29 34 64 public class AtomCollection 65 { 66 public static final Namespace ns = 67 Namespace.getNamespace("http://purl.org/atom/app#"); 68 private static SimpleDateFormat df = 69 new SimpleDateFormat ( "yyyy-MM-dd'T'HH:mm:ssZ" ); 70 private String next = null; 71 private List members = new ArrayList (); 72 73 public AtomCollection() 74 { 75 } 76 77 78 public String getNext() 79 { 80 return next; 81 } 82 83 public void setNext(String next) 84 { 85 this.next = next; 86 } 87 88 public List getMembers() 89 { 90 return members; 91 } 92 93 public void setMembers(List members) 94 { 95 this.members = members; 96 } 97 98 public void addMember(Member member) 99 { 100 members.add(member); 101 } 102 103 104 108 public static class Member 109 { 110 private String title; 111 private String href; 112 private String hrefreadonly; 113 private Date updated; 114 115 public Member() 116 { 117 } 118 119 120 public String getTitle() 121 { 122 return title; 123 } 124 125 public void setTitle(String title) 126 { 127 this.title = title; 128 } 129 130 131 public String getHref() 132 { 133 return href; 134 } 135 136 public void setHref(String href) 137 { 138 this.href = href; 139 } 140 141 142 public String getHrefreadonly() 143 { 144 return hrefreadonly; 145 } 146 147 public void setHrefreadonly(String hrefreadonly) 148 { 149 this.hrefreadonly = hrefreadonly; 150 } 151 152 153 public Date getUpdated() 154 { 155 return updated; 156 } 157 158 public void setUpdated(Date updated) 159 { 160 this.updated = updated; 161 } 162 } 163 164 165 public static AtomCollection documentToCollection(Document document) 166 throws Exception 167 { 168 AtomCollection collection = new AtomCollection(); 169 Element root = document.getRootElement(); 170 if (root.getAttribute("next") != null) 171 { 172 collection.setNext(root.getAttribute("next").getValue()); 173 } 174 List mems = root.getChildren("member", ns); 175 Iterator iter = mems.iterator(); 176 while (iter.hasNext()) 177 { 178 Element e = (Element) iter.next(); 179 collection.addMember(AtomCollection.elementToMember(e)); 180 } 181 return collection; 182 } 183 184 185 public static Document collectionToDocument(AtomCollection collection) 186 { 187 Document doc = new Document(); 188 Element root = new Element("collection", ns); 189 doc.setRootElement(root); 190 if (collection.getNext() != null) 191 { 192 root.setAttribute("next", collection.getNext()); 193 } 194 Iterator iter = collection.getMembers().iterator(); 195 while (iter.hasNext()) 196 { 197 Member member = (Member) iter.next(); 198 root.addContent(AtomCollection.memberToElement(member)); 199 } 200 return doc; 201 } 202 203 204 public static Member elementToMember(Element element) throws Exception 205 { 206 Member member = new Member(); 207 member.setTitle(element.getAttribute("title").getValue()); 208 member.setHref(element.getAttribute("href").getValue()); 209 if (element.getAttribute("href") != null) 210 { 211 member.setHref(element.getAttribute("href").getValue()); 212 } 213 member.setUpdated(df.parse(element.getAttribute("updated").getValue())); 214 return member; 215 } 216 217 218 public static Element memberToElement(Member member) 219 { 220 Element element = new Element("member", ns); 221 element.setAttribute("title", member.getTitle()); element.setAttribute("href", member.getHref()); 223 if (member.getHrefreadonly() != null) 224 { 225 element.setAttribute("hrefreadonly", member.getHrefreadonly()); 226 } 227 element.setAttribute("updated", df.format(member.getUpdated())); 228 return element; 229 } 230 231 232 public static class Range { Date start=null; Date end=null; } 233 234 235 public static Range parseRange(String rangeString) throws ParseException 236 { 237 241 Range range = new Range(); 242 String [] split = rangeString.split("="); 243 if (split[1].startsWith("/")) 244 { 245 range.end = df.parse(split[1].split("/")[1]); 247 } 248 else if (split[1].endsWith("/")) 249 { 250 range.start = df.parse(split[1].split("/")[0]); 252 } 253 else 254 { 255 String [] dates = split[1].split("/"); 257 range.start = df.parse(dates[0]); 258 range.end = df.parse(dates[1]); 259 } 260 return range; 261 } 262 } 263 | Popular Tags |