List of usage examples for javax.xml.bind.util JAXBResult getResult
public Object getResult() throws JAXBException
From source file:Main.java
public static synchronized Object deserialize(Source source, InputStream xsltSource, Class cls) throws TransformerConfigurationException, JAXBException, TransformerException { Object obj = null;// w w w. j a v a 2s . c o m JAXBContext jc = JAXBContext.newInstance(cls); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); JAXBResult result = new JAXBResult(jc); transformer.transform(source, result); obj = result.getResult(); } else { obj = jc.createUnmarshaller().unmarshal(source); } return obj; }
From source file:Main.java
/** * * @param elmnt// w ww. java2 s . co m * @param xsltSource * @param cls * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException */ public static synchronized Object deserialize(Element elmnt, InputStream xsltSource, Class cls) throws TransformerConfigurationException, JAXBException, TransformerException { Object obj = null; JAXBContext jc = JAXBContext.newInstance(cls); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); JAXBResult result = new JAXBResult(jc); transformer.transform(new DOMSource(elmnt), result); obj = result.getResult(); } else { obj = jc.createUnmarshaller().unmarshal(elmnt); } return obj; }
From source file:Main.java
/** * * @param source/*from www. j av a 2s. co m*/ * @param xsltSource * @param cls * @return * @throws TransformerConfigurationException * @throws JAXBException * @throws TransformerException */ public static synchronized Object deserialize(InputStream source, InputStream xsltSource, Class cls) throws TransformerConfigurationException, JAXBException, TransformerException { Object obj = null; JAXBContext jc = JAXBContext.newInstance(cls); if (xsltSource != null) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer; transformer = factory.newTransformer(new StreamSource(xsltSource)); JAXBResult result = new JAXBResult(jc); transformer.transform(new StreamSource(source), result); obj = result.getResult(); } else { obj = jc.createUnmarshaller().unmarshal(source); } return obj; }
From source file:com.netflix.subtitles.ttml.TtmlUtils.java
/** * Does TTML document transformation to another TTML document. * * @param tt source TTML document root element * @param transformer transformer//ww w . ja v a 2 s.c o m * @return TTML document after transformation */ public static TtEltype transformTtmlDocument(TtEltype tt, Transformer transformer) { JAXBElement<TtEltype> ttJaxb = new ObjectFactory().createTt(tt); try { JAXBContext jaxbc = createTtmlJaxbContext(); JAXBSource source = new JAXBSource(jaxbc, ttJaxb); JAXBResult result = new JAXBResult(jaxbc); // transform transformer.transform(source, result); return (TtEltype) ((JAXBElement<TtEltype>) result.getResult()).getValue(); } catch (JAXBException | TransformerException e) { throw new ConvertException(e); } }
From source file:org.docx4j.openpackaging.parts.DrawingML.DiagramLayoutPart.java
public static void main(String[] args) throws Exception { // Need the source doc as a DOM for later, and also // as XSLT input javax.xml.parsers.DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w w w.jav a2 s.co m DocumentBuilder docBuilder = dbf.newDocumentBuilder(); Document doc = docBuilder.parse(new File(System.getProperty("user.dir") + "/SmartArt/12hi.xml")); Source myList = new javax.xml.transform.dom.DOMSource(doc); // We need a source layout part // Could get it from a docx, or a glox String layoutSrcFilePath = System.getProperty("user.dir") + "/sample-docs/glox/extracted/chevron1.glox"; OpcPackage opcPackage = OpcPackage.load(new java.io.File(layoutSrcFilePath)); DiagramLayoutPart thisPart = null; for (Entry<PartName, Part> entry : opcPackage.getParts().getParts().entrySet()) { if (entry.getValue().getContentType().equals(ContentTypes.DRAWINGML_DIAGRAM_LAYOUT)) { thisPart = (DiagramLayoutPart) entry.getValue(); break; } } if (thisPart == null) { System.out.println("No SmartArt found in " + layoutSrcFilePath); return; } // Generate XSLT .. can save this for reuse System.out.println("Generating xslt.."); Templates xslLayoutNodeTree = generateLayoutTreeXSLT(thisPart.getJaxbElement()); // Generate hierarchical layout tree String tmpXslStr = generateLayoutTree(myList, xslLayoutNodeTree); System.out.println(tmpXslStr); // Finally, apply your LT2DD to create DiagramData part System.out.println("Creating DiagramData part.."); Templates xsltLT2DD; try { xsltLT2DD = XmlUtils.getTransformerTemplate(new StreamSource(org.docx4j.utils.ResourceUtils.getResource( "org/docx4j/openpackaging/parts/DrawingML/DiagramLayoutTree4pictureOrgChart2DiagramData.xslt"))); } catch (Exception e) { e.printStackTrace(); return; } // ByteArrayOutputStream layoutBAOS3 = new ByteArrayOutputStream(); // Result result = new StreamResult(layoutBAOS3); JAXBResult result = new JAXBResult(Context.jc); java.util.HashMap<String, Object> settings = new java.util.HashMap<String, Object>(); settings.put("list", doc); XmlUtils.transform(new javax.xml.transform.stream.StreamSource(new java.io.StringReader(tmpXslStr)), xsltLT2DD, settings, result); // What did we generate // tmpXslStr = layoutBAOS3.toString("UTF-8"); // System.out.println(tmpXslStr); // Finally, inject this into your DiagramData part // .. first, we need to make the IDs Word friendly. Object ddJaxb = result.getResult(); DiagramDataPart.setFriendlyIds(XmlUtils.unwrap(ddJaxb)); System.out.println(XmlUtils.marshaltoString(ddJaxb, false)); }
From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java
public void setJaxbElement(JAXBResult result) throws JAXBException { setJaxbElement((E) result.getResult()); }
From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java
/** * Unmarshal XML data from the specified InputStream and return the * resulting content tree. Validation event location information may be * incomplete when using this form of the unmarshal API. * * <p>/* w w w . j a v a 2 s .c om*/ * Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>. * * @param is * the InputStream to unmarshal XML data from * @return the newly created root object of the java content tree * * @throws JAXBException * If any unexpected errors occur while unmarshalling */ public E unmarshal(java.io.InputStream is) throws JAXBException { try { /* To avoid possible XML External Entity Injection attack, * we need to configure the processor. * * We use STAX XMLInputFactory to do that. * * createXMLStreamReader(is) is 40% slower than unmarshal(is). * * But it seems to be the best we can do ... * * org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is) * unmarshal(doc) * * ie DOM is 5x slower than unmarshal(is) * */ XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception XMLStreamReader xsr = xif.createXMLStreamReader(is); Unmarshaller u = jc.createUnmarshaller(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); if (is.markSupported()) { // Only fail hard if we know we can restart eventHandler.setContinue(false); } u.setEventHandler(eventHandler); try { jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(xsr)); } catch (UnmarshalException ue) { if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) { /* Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19] Message: The entity "xxe" was referenced, but not declared. at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source) */ log.error(ue.getMessage(), ue); throw ue; } if (is.markSupported()) { // When reading from zip, we use a ByteArrayInputStream, // which does support this. log.info("encountered unexpected content; pre-processing"); eventHandler.setContinue(true); try { Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); is.reset(); JAXBResult result = XmlUtils.prepareJAXBResult(jc); XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result); jaxbElement = (E) XmlUtils.unwrap(result.getResult()); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } else { log.error(ue.getMessage(), ue); log.error(".. and mark not supported"); throw ue; } } } catch (XMLStreamException e1) { log.error(e1.getMessage(), e1); throw new JAXBException(e1); } return jaxbElement; }
From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java
public E unmarshal(org.w3c.dom.Element el) throws JAXBException { try {//from w w w . ja v a 2 s. c o m Unmarshaller u = jc.createUnmarshaller(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); eventHandler.setContinue(false); u.setEventHandler(eventHandler); try { jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(el)); } catch (UnmarshalException ue) { log.info("encountered unexpected content; pre-processing"); try { org.w3c.dom.Document doc; if (el instanceof org.w3c.dom.Document) { doc = (org.w3c.dom.Document) el; } else { // Hope for the best. Dodgy though; what if this is // being used on something deep in the tree? // TODO: revisit doc = el.getOwnerDocument(); } eventHandler.setContinue(true); JAXBResult result = XmlUtils.prepareJAXBResult(jc); Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); XmlUtils.transform(doc, mcPreprocessorXslt, null, result); jaxbElement = (E) XmlUtils.unwrap(result.getResult()); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } return jaxbElement; } catch (JAXBException e) { log.error(e.getMessage(), e); throw e; } }
From source file:org.docx4j.XmlUtils.java
public static Object unmarshal(InputStream is, JAXBContext jc) throws JAXBException { // Guard against XXE XMLInputFactory xif = XMLInputFactory.newInstance(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception XMLStreamReader xsr = null;// w ww.j a va 2 s .c o m try { xsr = xif.createXMLStreamReader(is); } catch (XMLStreamException e) { throw new JAXBException(e); } Object o = null; Unmarshaller u = jc.createUnmarshaller(); JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler(); // if (is.markSupported()) { // // Only fail hard if we know we can restart // eventHandler.setContinue(false); // } u.setEventHandler(eventHandler); try { o = u.unmarshal(xsr); return o; } catch (UnmarshalException ue) { if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) { /* Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19] Message: The entity "xxe" was referenced, but not declared. at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source) */ log.error(ue.getMessage(), ue); throw ue; } if (is.markSupported()) { // When reading from zip, we use a ByteArrayInputStream, // which does support this. log.info("encountered unexpected content; pre-processing"); eventHandler.setContinue(true); try { Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor(); is.reset(); JAXBResult result = XmlUtils.prepareJAXBResult(jc); XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result); return //XmlUtils.unwrap( result.getResult(); } catch (Exception e) { throw new JAXBException("Preprocessing exception", e); } } else { log.error(ue.getMessage(), ue); log.error(".. and mark not supported"); throw ue; } } }