Example usage for javax.xml.transform.dom DOMResult DOMResult

List of usage examples for javax.xml.transform.dom DOMResult DOMResult

Introduction

In this page you can find the example usage for javax.xml.transform.dom DOMResult DOMResult.

Prototype

public DOMResult() 

Source Link

Document

Zero-argument default constructor.

Usage

From source file:net.javacrumbs.smock.common.XmlUtil.java

/**
 * Loads document from source./*from w  w w  .  j  av  a 2s . c  om*/
 * @param source
 * @return
 */
public static Document loadDocument(Source source) {
    DOMResult result = new DOMResult();
    transform(source, result);
    return (Document) result.getNode();
}

From source file:Main.java

public static Node applyXslToDocument2(Source xslt, Source doc, URIResolver resolver,
        Properties transformerProperties, HashMap<String, String> params, String transformerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException, IOException,
        NoSuchMethodException, TransformerConfigurationException, TransformerException {
    TransformerFactory transformerFactory = null;
    if (transformerClassName == null)
        transformerFactory = TransformerFactory.newInstance();
    else {//from w ww. j a  v  a  2s.  c  o  m
        Class transformerClass = Class.forName(transformerClassName);
        Constructor defaultConstructor = transformerClass.getConstructor(null);
        transformerFactory = (TransformerFactory) transformerClass.newInstance();
    }

    if (resolver != null)
        transformerFactory.setURIResolver(resolver);

    Transformer transformer = transformerFactory.newTransformer(xslt);
    if (transformerFactory != null)
        transformer.setOutputProperties(transformerProperties);

    if (params != null) {
        for (Map.Entry<String, String> cursor : params.entrySet()) {
            transformer.setParameter(cursor.getKey(), cursor.getValue());
        }
    }

    DOMResult result = new DOMResult();
    transformer.transform(doc, result);

    return (result.getNode());
}

From source file:Main.java

/**
 * /*w ww .  jav a  2  s. c om*/
 * <B>Purpose:</B> XML transformation using XSL
 * 
 * @param doc
 * @param xslInput
 * @param systemid
 * @return
 * @throws TransformerException
 */
public static Node transform(Document doc, StreamSource xslInput, String systemid) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(xslInput);
    DOMResult domResult = new DOMResult();
    DOMSource xmlDomSource = null;
    xmlDomSource = new DOMSource(doc);
    xmlDomSource.setSystemId(systemid);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
    transformer.transform(xmlDomSource, domResult);
    return domResult.getNode();
}

From source file:no.digipost.api.SdpMeldingSigner.java

public Document sign(final StandardBusinessDocument sbd) {
    try {//from   w w w .  jav a 2s . c  o m
        PrivateKey privateKey = keystoreInfo.getPrivateKey();
        X509Certificate certificate = keystoreInfo.getCertificate();

        DOMResult result = new DOMResult();
        Marshalling.marshal(marshaller, sbd, result);
        Document doc = (Document) result.getNode();
        Marshalling.trimNamespaces(doc);

        XMLSignatureFactory fac = XMLSignatureFactory.getInstance("DOM");
        Reference ref = fac.newReference("", fac.newDigestMethod(DigestMethod.SHA256, null),
                Collections.singletonList(fac.newTransform(Transform.ENVELOPED, (TransformParameterSpec) null)),
                null, null);

        SignedInfo si = fac.newSignedInfo(
                fac.newCanonicalizationMethod(CanonicalizationMethod.EXCLUSIVE, (C14NMethodParameterSpec) null),
                fac.newSignatureMethod(Constants.RSA_SHA256, null), Collections.singletonList(ref));
        KeyInfoFactory kif = fac.getKeyInfoFactory();
        X509Data xd = kif.newX509Data(Collections.singletonList(certificate));
        KeyInfo ki = kif.newKeyInfo(Collections.singletonList(xd));
        XMLSignature signature = fac.newXMLSignature(si, ki);

        Node digitalPostNode = doc.getDocumentElement().getFirstChild().getNextSibling();
        Node avsenderNode = digitalPostNode.getFirstChild();

        DOMSignContext dsc = new DOMSignContext(privateKey, digitalPostNode, avsenderNode);
        signature.sign(dsc);

        doc.normalizeDocument();
        return doc;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (UnrecoverableKeyException e) {
        throw new RuntimeException(e);
    } catch (XMLSignatureException e) {
        throw new RuntimeException(e);
    } catch (InvalidAlgorithmParameterException e) {
        throw new RuntimeException(e);
    } catch (KeyStoreException e) {
        throw new RuntimeException(e);
    } catch (MarshalException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test
public void generateXmlSchema() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2.class,
            org.javelin.sws.ext.bind.jaxb.context3a.MyClassJ3.class);

    final List<DOMResult> results = new LinkedList<DOMResult>();

    ctx.generateSchema(new SchemaOutputResolver() {
        @Override/*from  w  w w.ja  va 2s. c  o  m*/
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            log.info("file: {}, namespace: {}", suggestedFileName, namespaceUri);
            DOMResult result = new DOMResult();
            results.add(result);
            result.setSystemId(suggestedFileName);
            return result;
        }
    });

    for (DOMResult dr : results) {
        log.info("=== {} ===", dr.getSystemId());
        javax.xml.transform.TransformerFactory.newInstance().newTransformer().transform(
                new javax.xml.transform.dom.DOMSource(dr.getNode()),
                new javax.xml.transform.stream.StreamResult(new java.io.PrintWriter(System.out)));
    }
}

From source file:net.javacrumbs.airline.server.VanillaTest.java

@Test
public void testGetFlightsXml() throws AirlineException, DatatypeConfigurationException, TransformerException {
    GetFlightsRequest request = JAXB.unmarshal(getStream("request1.xml"), GetFlightsRequest.class);

    GetFlightsResponse response = endpoint.getFlights(request);

    DOMResult domResponse = new DOMResult();
    JAXB.marshal(response, domResponse);

    XMLUnit.setIgnoreWhitespace(true);//from   w ww .  j  a v  a2s  .  c o m
    XMLAssert.assertXMLEqual(getDocument("response1.xml"), (Document) domResponse.getNode());

}

From source file:com.mindquarry.search.serializer.IndexPostSerializer.java

/**
 * Set the {@link OutputStream} where the requested resource should be
 * serialized./*from   ww  w.j av a  2s .  c  o m*/
 */
public void setOutputStream(OutputStream out) throws IOException {
    super.setOutputStream(out);
    try {
        TransformerHandler handler = this.getTransformerHandler();
        handler.getTransformer().setOutputProperties(this.format);
        res = new DOMResult();
        handler.setResult(res);
        // handler.setResult(new StreamResult(this.output));
        this.setContentHandler(handler);
        this.setLexicalHandler(handler);
    } catch (Exception e) {
        final String message = "Cannot set XMLSerializer outputstream";
        throw new CascadingIOException(message, e);
    }
}

From source file:net.javacrumbs.airline.server.VanillaTest.java

private Document getDocument(String name) throws TransformerException {

    DOMResult result = new DOMResult();
    new TransformerHelper().transform(new StreamSource(getStream(name)), result);
    return (Document) result.getNode();

}

From source file:net.javacrumbs.springws.test.util.DefaultXmlUtil.java

public Document loadDocument(Source source) {
    DOMResult messageContent = new DOMResult();
    transform(source, messageContent);/*from ww w  . ja va 2  s.c  om*/
    return (Document) messageContent.getNode();
}

From source file:Main.java

/**
 * General method for transformation to a DOM Document. Transform a Source
 * document using a Source XSL document and an array of parameters.
 * The parameter array consists of a sequence of pairs of (String parametername)
 * followed by (Object parametervalue) in an Object[].
 * @param doc the XML document to transform.
 * @param xsl the XSL transformation program.
 * @param params the array of transformation parameters.
 * @return the transformed text.// w  w  w  . jav a2 s .  co  m
 */
public static Document getTransformedDocument(Source doc, Source xsl, Object[] params) throws Exception {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(xsl);
    if ((params != null) && (params.length > 1)) {
        for (int i = 0; i < params.length; i = i + 2) {
            transformer.setParameter((String) params[i], params[i + 1]);
        }
    }
    DOMResult domResult = new DOMResult();
    transformer.transform(doc, domResult);
    return (Document) domResult.getNode();
}