1 56 package org.objectstyle.cayenne.xml; 57 58 import java.util.Collection ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import org.jdom.Document; 63 import org.jdom.Element; 64 import org.jdom.output.Format; 65 import org.jdom.output.XMLOutputter; 66 import org.objectstyle.cayenne.CayenneRuntimeException; 67 import org.objectstyle.cayenne.util.Util; 68 69 75 public class XMLEncoder { 76 77 78 protected Element root; 79 80 88 public String encode(Object object, String mappingFile) 89 throws CayenneRuntimeException { 90 91 this.root = new XMLMappingUtil(mappingFile).encode(object); 92 return getXml(); 93 } 94 95 100 public String getXml() { 101 Document doc = new Document(root); 102 103 XMLOutputter serializer = new XMLOutputter(); 106 Format format = Format.getPrettyFormat(); 107 108 format.setOmitDeclaration(true); 109 format.setLineSeparator("\n"); 110 111 serializer.setFormat(format); 112 113 String ret = serializer.outputString(doc); 114 doc.detachRootElement(); 115 116 ret = stripDoubleLineBreaks(ret); 117 118 return ret; 119 } 120 121 private String stripDoubleLineBreaks(String string) { 123 if (Util.isEmptyString(string)) { 124 return string; 125 } 126 127 StringBuffer buffer = new StringBuffer (string.length()); 128 char previous = 0; 129 for (int i = 0; i < string.length(); i++) { 130 char c = string.charAt(i); 131 if (c == previous && previous == '\n') { 132 continue; 133 } 134 135 buffer.append(c); 136 previous = c; 137 } 138 139 return buffer.toString(); 140 } 141 142 150 public void setRoot(String xmlTag, String type) { 151 root = new Element(xmlTag); 152 root.setAttribute("type", type); 153 } 154 155 160 public Element getRoot() { 161 return root; 162 } 163 164 170 public void encodeProperty(String xmlTag, Object property) { 171 Element temp; 172 173 if (property instanceof XMLSerializable) { 174 XMLSerializable element = (XMLSerializable) property; 175 176 Element rootCopy = root; 179 root = null; 180 181 element.encodeAsXML(this); 183 184 temp = root; 189 temp.setName(xmlTag); 190 191 root = rootCopy; 193 } 194 else if (property instanceof Collection ) { 197 Collection c = (Collection ) property; 198 root.addContent(encodeCollection(xmlTag, c)); 199 200 return; 201 } 202 else { 203 temp = new Element(xmlTag); 204 205 temp.setAttribute("type", property.getClass().getName()); 206 temp.setText(property.toString()); 207 } 208 209 if (null != root) { 210 root.addContent(temp); 211 } 212 else { 213 root = temp; 214 } 215 } 216 217 224 protected List encodeCollection(String xmlTag, Collection c) { 225 XMLEncoder encoder = new XMLEncoder(); 229 encoder.setRoot("root", "root"); 230 231 for (Iterator it = c.iterator(); it.hasNext();) { 233 encoder.encodeProperty(xmlTag, it.next()); 234 } 235 236 List ret = encoder.getRoot().removeContent(); 238 239 if (1 == ret.size()) { 243 Element e = (Element) ret.get(0); 244 245 e.setAttribute("forceList", "YES"); 246 } 247 248 return ret; 249 } 250 251 259 public String encodeList(String xmlTag, List dataObjects) { 260 XMLEncoder encoder = new XMLEncoder(); 263 encoder.setRoot(xmlTag + "List", dataObjects.getClass().getName()); 264 265 encoder.getRoot().addContent(encoder.encodeCollection(xmlTag, dataObjects)); 266 267 return encoder.getXml(); 268 } 269 270 280 public String encodeList(String xmlTag, List dataObjects, String mappingUrl) { 281 XMLEncoder encoder = new XMLEncoder(); 284 encoder.setRoot(xmlTag + "List", dataObjects.getClass().getName()); 285 286 final Element tempRoot = encoder.root; 287 288 for (Iterator it = dataObjects.iterator(); it.hasNext();) { 292 encoder.encode(it.next(), mappingUrl); 293 tempRoot.addContent(encoder.root); 294 } 295 296 encoder.root = tempRoot; 297 298 return encoder.getXml(); 299 } 300 } | Popular Tags |