1 package org.sapia.util.xml.confix; 2 3 4 import org.jdom.Attribute; 7 import org.jdom.Document; 8 import org.jdom.Element; 9 import org.jdom.JDOMException; 10 11 import org.jdom.input.SAXBuilder; 12 import org.jdom.output.XMLOutputter; 13 14 import org.sapia.util.xml.ProcessingException; 17 import org.xml.sax.InputSource ; 18 19 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 26 import java.util.Iterator ; 27 28 29 50 public class JDOMProcessor extends AbstractXMLProcessor { 51 55 60 public JDOMProcessor(ObjectFactoryIF anObjectFactory) { 61 super(anObjectFactory); 62 } 63 64 68 76 public Object process(InputStream is) throws ProcessingException { 77 try { 78 SAXBuilder builder = new SAXBuilder(); 80 Document doc = builder.build(is); 81 82 Object aResult = process(null, doc.getRootElement()); 84 85 return aResult; 86 } catch (IOException ioe) { 87 String aMessage = "Error parsing the XML of the input stream."; 88 throw new ProcessingException(aMessage, ioe); 89 } catch (JDOMException je) { 90 String aMessage = "Error parsing the XML of the input stream."; 91 throw new ProcessingException(aMessage, je); 92 } finally { 93 try { 94 if (is != null) { 95 is.close(); 96 } 97 } catch (IOException ioe) { 98 String aMessage = "Error closing the input stream to process."; 99 100 throw new ProcessingException(aMessage, ioe); 101 } 102 } 103 } 104 105 114 public void process(Object root, InputStream is) throws ProcessingException { 115 try { 116 SAXBuilder builder = new SAXBuilder(); 118 Document doc = builder.build(is); 119 120 Object aResult = process(root, doc.getRootElement()); 122 } catch (IOException ioe) { 123 String aMessage = "Error parsing the XML of the input stream."; 124 throw new ProcessingException(aMessage, ioe); 125 } catch (JDOMException je) { 126 String aMessage = "Error parsing the XML of the input stream."; 127 throw new ProcessingException(aMessage, je); 128 } finally { 129 try { 130 if (is != null) { 131 is.close(); 132 } 133 } catch (IOException ioe) { 134 String aMessage = "Error closing the input stream to process."; 135 136 throw new ProcessingException(aMessage, ioe); 137 } 138 } 139 } 140 141 145 152 public Object process(Object aParent, Element anElement) 153 throws ProcessingException { 154 return process(aParent, anElement, null); 155 } 156 157 private Object process(Object aParent, Element anElement, String setterName) 158 throws ProcessingException { 159 String aName = anElement.getName(); 160 161 if (setterName == null) { 162 setterName = aName; 163 } 164 165 CreationStatus status = null; 166 167 try { 168 status = getObjectFactory().newObjectFor(anElement.getNamespace() 169 .getPrefix(), 170 anElement.getNamespace().getURI(), aName, aParent); 171 } catch (ObjectCreationException oce) { 172 if (aParent == null) { 173 String aMessage = "Unable to create an object for the element " + 174 anElement; 175 176 throw new ProcessingException(aMessage, oce); 177 } 178 179 if ((aParent != null) && 180 (containsMethod("set", aParent, aName) || 181 containsMethod("add", aParent, aName)) && 182 (anElement.getChildren().size() == 1)) { 183 Element child = (Element) anElement.getChildren().get(0); 184 process(aParent, child, setterName); 185 186 return aParent; 187 } 188 189 try { 190 String aValue = anElement.getTextTrim(); 191 192 invokeSetter(aParent.getClass().getName(), aParent, aName, aValue); 193 194 return aParent; 195 } catch (ConfigurationException ce) { 196 String aMessage = 197 "Unable to create an object nor to call a setter for the element " + 198 anElement; 199 oce.printStackTrace(); 200 throw new ProcessingException(aMessage, ce); 201 } 202 } 203 204 String text = anElement.getTextTrim(); 205 206 if (text.length() > 0) { 207 try { 208 invokeSetter(aName, status.getCreated(), "Text", text); 209 } catch (ConfigurationException ce) { 210 String aMessage = "The object '" + aName + 211 "' does not accept free text"; 212 213 throw new ProcessingException(aMessage, ce); 214 } 215 } 216 217 try { 218 for (Iterator it = anElement.getAttributes().iterator(); it.hasNext();) { 220 Attribute attr = (Attribute) it.next(); 221 222 invokeSetter(aName, status.getCreated(), attr.getName(), attr.getValue()); 223 } 224 225 for (Iterator it = anElement.getChildren().iterator(); it.hasNext();) { 227 Element child = (Element) it.next(); 228 229 if (status.getCreated() instanceof JDOMHandlerIF) { 230 ((JDOMHandlerIF) status.getCreated()).handleElement(child); 231 } else if(status.getCreated() instanceof XMLConsumer) { 232 XMLConsumer cons = (XMLConsumer)status.getCreated(); 233 XMLOutputter outputter = new XMLOutputter(); 234 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 235 try{ 236 Element clone = (Element)child.clone(); 237 outputter.output(new Document(clone.detach()), bos); 238 ByteArrayInputStream in = new ByteArrayInputStream (bos.toByteArray()); 239 InputSource is = new InputSource (in); 240 cons.consume(is); 241 }catch(Exception e){ 242 e.printStackTrace(); 243 throw new ProcessingException("Could not pipe content of element: " + child.getQualifiedName() + " to XMLConsumer", e); 244 } 245 } else { 246 process(status.getCreated(), child); 247 } 248 } 249 250 if (status.getCreated() instanceof ObjectCreationCallback) { 253 status._created = ((ObjectCreationCallback) status.getCreated()).onCreate(); 254 } 255 256 if ((aParent != null) && !status.wasAssigned() && 258 !(status.getCreated() instanceof NullObject)) { 259 assignToParent(aParent, status.getCreated(), setterName); 260 } 261 262 if (status.getCreated() instanceof NullObject) { 263 return null; 264 } 265 266 return status.getCreated(); 267 } catch (ConfigurationException ce) { 268 String aMessage = "Unable to process the content of the element " + 269 aName; 270 271 throw new ProcessingException(aMessage, ce); 272 } 273 } 274 } 275 | Popular Tags |