Changeset 1004 for trunk/docx4j/src/main/java/org/docx4j/XmlUtils.java
- Timestamp:
- 01/09/10 18:15:02 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/XmlUtils.java
r999 r1004 58 58 import org.docx4j.openpackaging.exceptions.Docx4JException; 59 59 60 import org.dom4j.DocumentException;61 import org.dom4j.Element;62 import org.dom4j.io.DOMWriter;63 import org.dom4j.io.OutputFormat;64 import org.dom4j.io.XMLWriter;65 60 import org.w3c.dom.Attr; 66 61 import org.w3c.dom.Document; … … 74 69 private static Logger log = Logger.getLogger(XmlUtils.class); 75 70 76 /** Make a dom4j element into something JAXB can unmarshall */77 @Deprecated78 private static java.io.InputStream getInputStreamFromDom4jEl(Element el) {79 80 // Write it to an output stream81 java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();82 OutputFormat format = OutputFormat.createPrettyPrint();83 try {84 XMLWriter xmlWriter = new XMLWriter( out, format );85 xmlWriter.write(el);86 xmlWriter.flush();87 } catch (IOException e) {88 e.printStackTrace();89 return null;90 }91 92 byte[] bytes = out.toByteArray();93 94 // Now return an input stream95 return new java.io.ByteArrayInputStream(bytes);96 97 }98 71 99 72 public static String TRANSFORMER_FACTORY_ORIGINAL; … … 181 154 } 182 155 183 /** Unmarshal a Dom4j element as JAXB object using JAXBContext Context.jc */ 184 @Deprecated 185 public static Object unmarshalDom4jDoc(org.dom4j.Document doc) { 186 187 JAXBContext jc = Context.jc; 188 189 Object o = null; 190 try { 191 org.dom4j.io.DOMWriter writer = new org.dom4j.io.DOMWriter(); 192 org.w3c.dom.Document w3cDoc = writer.write(doc); 193 194 // JAXBContext jc = JAXBContext 195 // .newInstance("org.docx4j.jaxb.document"); 196 Unmarshaller u = jc.createUnmarshaller(); 197 u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 198 199 o = u.unmarshal( w3cDoc ); 200 201 System.out.println("unmarshalled "); 202 203 } catch (Exception ex) { 204 ex.printStackTrace(); 205 } 206 return o; 207 } 208 209 /** Unmarshal a Dom4j element as an object in the package org.docx4j.jaxb.document. 210 * 211 * Since most of our types don't have an XmlRootElement, here we use 212 * the version of the unmarshal method that takes the 'expectedType' argument. 213 * See https://jaxb.dev.java.net/guide/_XmlRootElement_and_unmarshalling.html 214 * 215 * */ 216 @Deprecated 217 public static <T> JAXBElement<T> unmarshalDom4jEl(Element el, Class<T> declaredType) { 218 219 JAXBElement<T> o = null; 220 //Object o = null; 221 try { 222 223 JAXBContext jc = Context.jc; 224 225 Unmarshaller u = jc.createUnmarshaller(); 226 227 //u.setSchema(null); 228 //u.setValidating( false ); 229 230 u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler()); 231 232 // // Convert dom4j el to W3C 233 // org.dom4j.io.DOMWriter writer = new org.dom4j.io.DOMWriter(); 234 // org.w3c.dom.Document w3cDoc = writer.write(el); // Only takes Document objects :( 235 236 o = u.unmarshal(new StreamSource(org.docx4j.XmlUtils.getInputStreamFromDom4jEl(el)), declaredType); 237 238 System.out.println("unmarshalled "); 239 240 } catch (Exception ex) { 241 ex.printStackTrace(); 242 } 243 return o; 244 } 156 245 157 246 158 /** Unmarshal an InputStream as an object in the package org.docx4j.jaxb.document. … … 287 199 } 288 200 289 /** Marshal to a Dom4j document */290 @Deprecated291 public static org.dom4j.Document marshaltoDom4jDocument(Object o) {292 // TODO - refactor this.293 try {294 JAXBContext jc = Context.jc;295 296 Marshaller marshaller=jc.createMarshaller();297 298 javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();299 dbf.setNamespaceAware(true);300 org.w3c.dom.Document doc = dbf.newDocumentBuilder().newDocument();301 NamespacePrefixMapperUtils.setProperty(marshaller,302 NamespacePrefixMapperUtils.getPrefixMapper());303 marshaller.marshal(o, doc);304 305 // Now convert the W3C document to a dom4j document306 org.dom4j.io.DOMReader xmlReader = new org.dom4j.io.DOMReader();307 308 /* Should be able to do ..309 *310 * dom4j has DocumentResult that extends Result, so you can do:311 312 DocumentResult dr = new DocumentResult();313 marshaller.marshal( object, dr );314 o = dr.getDocument();315 316 *317 *318 */319 return xmlReader.read(doc);320 } catch (JAXBException e) {321 // TODO Auto-generated catch block322 e.printStackTrace();323 } catch (ParserConfigurationException e) {324 // TODO Auto-generated catch block325 e.printStackTrace();326 }327 return null;328 }329 201 330 202 /** Marshal to a String */
Note: See TracChangeset
for help on using the changeset viewer.
