Example usage for javax.xml.bind.util JAXBResult JAXBResult

List of usage examples for javax.xml.bind.util JAXBResult JAXBResult

Introduction

In this page you can find the example usage for javax.xml.bind.util JAXBResult JAXBResult.

Prototype

public JAXBResult(Unmarshaller _unmarshaller) throws JAXBException 

Source Link

Document

Creates a new instance that uses the specified Unmarshaller to unmarshal an object.

Usage

From source file:Main.java

public static synchronized Object deserialize(Source source, InputStream xsltSource, Class cls)
        throws TransformerConfigurationException, JAXBException, TransformerException {
    Object obj = null;//from  w  w w  .j  av  a2s .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/*from  ww w .  j  av  a 2 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   w w  w  .j  a  v a  2 s .  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// w  w w  . j a v  a 2  s .  c om
 * @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   ww w.j a  va  2s.  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.XmlUtils.java

/**
 * Prepare a JAXB transformation result for some given context.
 * @param context The JAXB context./*  w w  w  .ja  v a  2s.  c o m*/
 * @return The result data structure created.
 * @throws Docx4JException In case of configuration errors.
 */
public static JAXBResult prepareJAXBResult(final JAXBContext context) throws Docx4JException {

    final JAXBResult result;
    try {
        final Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setEventHandler(new JaxbValidationEventHandler());
        result = new JAXBResult(unmarshaller);

    } catch (JAXBException e) {
        throw new Docx4JException("Error preparing empty JAXB result", e);
    }
    return result;
}