Java Code Examples for javax.xml.transform.dom.DOMResult#getNode()

The following examples show how to use javax.xml.transform.dom.DOMResult#getNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Parameter.java    From iaf with Apache License 2.0 6 votes vote down vote up
private Object transform(Source xmlSource, ParameterValueList pvl) throws ParameterException, TransformerException, IOException {
	TransformerPool pool = getTransformerPool();
	if (TYPE_NODE.equals(getType()) || TYPE_DOMDOC.equals(getType())) {
		
		DOMResult transformResult = new DOMResult();
		pool.transform(xmlSource,transformResult, pvl);
		Node result=transformResult.getNode();
		if (result!=null && TYPE_NODE.equals(getType())) {
			result=result.getFirstChild();
		}			
		if (log.isDebugEnabled()) { if (result!=null) log.debug("Returning Node result ["+result.getClass().getName()+"]["+result+"]: "+ ToStringBuilder.reflectionToString(result)); } 
		return result;

	} 
	return pool.transform(xmlSource, pvl);
}
 
Example 2
Source File: XMLDOMWriterImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new instance of XMLDOMwriterImpl
 * @param result DOMResult object @javax.xml.transform.dom.DOMResult
 */
public XMLDOMWriterImpl(DOMResult result) {

    node = result.getNode();
    if( node.getNodeType() == Node.DOCUMENT_NODE){
        ownerDoc = (Document)node;
        currentNode = ownerDoc;
    }else{
        ownerDoc = node.getOwnerDocument();
        currentNode = node;
    }
    getDLThreeMethods();
    stringBuffer = new StringBuffer();
    needContextPop = new boolean[resizeValue];
    namespaceContext = new NamespaceSupport();
}
 
Example 3
Source File: W3CDomHandler.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public Element getElement(DOMResult r) {
    // JAXP spec is ambiguous about what really happens in this case,
    // so work defensively
    Node n = r.getNode();
    if( n instanceof Document ) {
        return ((Document)n).getDocumentElement();
    }
    if( n instanceof Element )
        return (Element)n;
    if( n instanceof DocumentFragment )
        return (Element)n.getChildNodes().item(0);

    // if the result object contains something strange,
    // it is not a user problem, but it is a JAXB provider's problem.
    // That's why we throw a runtime exception.
    throw new IllegalStateException(n.toString());
}
 
Example 4
Source File: StreamHeader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    try {
        // TODO what about in-scope namespaces
        // Not very efficient consider implementing a stream buffer
        // processor that produces a DOM node from the buffer.
        TransformerFactory tf = XmlUtil.newTransformerFactory();
        Transformer t = tf.newTransformer();
        XMLStreamBufferSource source = new XMLStreamBufferSource(_mark);
        DOMResult result = new DOMResult();
        t.transform(source, result);
        Node d = result.getNode();
        if(d.getNodeType() == Node.DOCUMENT_NODE)
            d = d.getFirstChild();
        SOAPHeader header = saaj.getSOAPHeader();
        if(header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
        Node node = header.getOwnerDocument().importNode(d, true);
        header.appendChild(node);
    } catch (Exception e) {
        throw new SOAPException(e);
    }
}
 
Example 5
Source File: W3CDomHandler.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public Element getElement(DOMResult r) {
    // JAXP spec is ambiguous about what really happens in this case,
    // so work defensively
    Node n = r.getNode();
    if( n instanceof Document ) {
        return ((Document)n).getDocumentElement();
    }
    if( n instanceof Element )
        return (Element)n;
    if( n instanceof DocumentFragment )
        return (Element)n.getChildNodes().item(0);

    // if the result object contains something strange,
    // it is not a user problem, but it is a JAXB provider's problem.
    // That's why we throw a runtime exception.
    throw new IllegalStateException(n.toString());
}
 
Example 6
Source File: DOMResultBuilder.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void setDOMResult(DOMResult result) {
    fCurrentNode = null;
    fFragmentRoot = null;
    fIgnoreChars = false;
    fTargetChildren.clear();
    if (result != null) {
        fTarget = result.getNode();
        fNextSibling = result.getNextSibling();
        fDocument = (fTarget.getNodeType() == Node.DOCUMENT_NODE) ? (Document) fTarget : fTarget.getOwnerDocument();
        fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null;
        fStorePSVI = (fDocument instanceof PSVIDocumentImpl);
        return;
    }
    fTarget = null;
    fNextSibling = null;
    fDocument = null;
    fDocumentImpl = null;
    fStorePSVI = false;
}
 
Example 7
Source File: W3CDomHandler.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public Element getElement(DOMResult r) {
    // JAXP spec is ambiguous about what really happens in this case,
    // so work defensively
    Node n = r.getNode();
    if( n instanceof Document ) {
        return ((Document)n).getDocumentElement();
    }
    if( n instanceof Element )
        return (Element)n;
    if( n instanceof DocumentFragment )
        return (Element)n.getChildNodes().item(0);

    // if the result object contains something strange,
    // it is not a user problem, but it is a JAXB provider's problem.
    // That's why we throw a runtime exception.
    throw new IllegalStateException(n.toString());
}
 
Example 8
Source File: AbstractSchemaValidationTube.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private Document createDOM(SDDocument doc) {
    // Get infoset
    ByteArrayBuffer bab = new ByteArrayBuffer();
    try {
        doc.writeTo(null, resolver, bab);
    } catch (IOException ioe) {
        throw new WebServiceException(ioe);
    }

    // Convert infoset to DOM
    Transformer trans = XmlUtil.newTransformer();
    Source source = new StreamSource(bab.newInputStream(), null); //doc.getURL().toExternalForm());
    DOMResult result = new DOMResult();
    try {
        trans.transform(source, result);
    } catch(TransformerException te) {
        throw new WebServiceException(te);
    }
    return (Document)result.getNode();
}
 
Example 9
Source File: XMLDocumentHelper.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a copy of the specified document.
 *
 * @param doc the {@code Document}
 * @return the copy of this document
 * @throws ConfigurationException if an error occurs
 */
private static Document copyDocument(final Document doc)
        throws ConfigurationException
{
    final Transformer transformer = createTransformer();
    final DOMSource source = new DOMSource(doc);
    final DOMResult result = new DOMResult();
    transform(transformer, source, result);

    return (Document) result.getNode();
}
 
Example 10
Source File: DOMResultAugmentor.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void setDOMResult(DOMResult result) {
    fIgnoreChars = false;
    if (result != null) {
        final Node target = result.getNode();
        fDocument = (target.getNodeType() == Node.DOCUMENT_NODE) ? (Document) target : target.getOwnerDocument();
        fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null;
        fStorePSVI = (fDocument instanceof PSVIDocumentImpl);
        return;
    }
    fDocument = null;
    fDocumentImpl = null;
    fStorePSVI = false;
}
 
Example 11
Source File: Fixture.java    From openCypher with Apache License 2.0 5 votes vote down vote up
public Document xmlResource( String resource ) throws TransformerException, IOException
{
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DOMResult result = new DOMResult();
    URL url = resource( resource );
    transformer.transform( new StreamSource( url.openStream(), url.toString() ), result );
    return (Document) result.getNode();
}
 
Example 12
Source File: DOMResultAugmentor.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void setDOMResult(DOMResult result) {
    fIgnoreChars = false;
    if (result != null) {
        final Node target = result.getNode();
        fDocument = (target.getNodeType() == Node.DOCUMENT_NODE) ? (Document) target : target.getOwnerDocument();
        fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null;
        fStorePSVI = (fDocument instanceof PSVIDocumentImpl);
        return;
    }
    fDocument = null;
    fDocumentImpl = null;
    fStorePSVI = false;
}
 
Example 13
Source File: LogicalMessageImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Source getPayload() {
    assert (!(payloadSrc instanceof DOMSource));
    try {
        Transformer transformer = XmlUtil.newTransformer();
        DOMResult domResult = new DOMResult();
        transformer.transform(payloadSrc, domResult);
        DOMSource dom = new DOMSource(domResult.getNode());
        lm = new DOMLogicalMessageImpl((DOMSource) dom);
        payloadSrc = null;
        return dom;
    } catch (TransformerException te) {
        throw new WebServiceException(te);
    }
}
 
Example 14
Source File: SecureSoapMessages.java    From spring-ws-security-soap-example with MIT License 5 votes vote down vote up
private static final Document toDocument(final SOAPMessage soapMsg)
        throws TransformerConfigurationException, TransformerException,
        SOAPException, IOException {
    final Source src = soapMsg.getSOAPPart().getContent();
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    final DOMResult result = new DOMResult();
    transformer.transform(src, result);
    return (Document) result.getNode();
}
 
Example 15
Source File: DOMValidatorHelper.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets up handler for <code>DOMResult</code>.
 */
private void setupDOMResultHandler(DOMSource source, DOMResult result) throws SAXException {
    // If there's no DOMResult, unset the validator handler
    if (result == null) {
        fDOMValidatorHandler = null;
        fSchemaValidator.setDocumentHandler(null);
        return;
    }
    final Node nodeResult = result.getNode();
    // If the source node and result node are the same use the DOMResultAugmentor.
    // Otherwise use the DOMResultBuilder.
    if (source.getNode() == nodeResult) {
        fDOMValidatorHandler = fDOMResultAugmentor;
        fDOMResultAugmentor.setDOMResult(result);
        fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);
        return;
    }
    if (result.getNode() == null) {
        try {
            DocumentBuilderFactory factory = fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM) ?
                                DocumentBuilderFactory.newInstance() : new DocumentBuilderFactoryImpl();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            result.setNode(builder.newDocument());
        }
        catch (ParserConfigurationException e) {
            throw new SAXException(e);
        }
    }
    fDOMValidatorHandler = fDOMResultBuilder;
    fDOMResultBuilder.setDOMResult(result);
    fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
 
Example 16
Source File: DOMResultAugmentor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void setDOMResult(DOMResult result) {
    fIgnoreChars = false;
    if (result != null) {
        final Node target = result.getNode();
        fDocument = (target.getNodeType() == Node.DOCUMENT_NODE) ? (Document) target : target.getOwnerDocument();
        fDocumentImpl = (fDocument instanceof CoreDocumentImpl) ? (CoreDocumentImpl) fDocument : null;
        fStorePSVI = (fDocument instanceof PSVIDocumentImpl);
        return;
    }
    fDocument = null;
    fDocumentImpl = null;
    fStorePSVI = false;
}
 
Example 17
Source File: YinDomSchemaSource.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
static @NonNull DOMSource transformSource(final Source source) throws TransformerException {
    final DOMResult result = new DOMResult();
    TRANSFORMER_FACTORY.newTransformer().transform(source, result);

    return new DOMSource(result.getNode(), result.getSystemId());
}
 
Example 18
Source File: BooleanAttributes.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
 
Example 19
Source File: BooleanAttributes.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
 
Example 20
Source File: BooleanAttributes.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void test(String mimeType, boolean useStreamMeta,
                        String metaXml, String... boolXpaths)
    throws Exception
{
    BufferedImage img =
        new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta =
        iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document)dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();

    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);

    meta.mergeTree(metaFormat, node);

    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);

    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta) meta = ir.getStreamMetadata();
    else meta = ir.getImageMetadata(0);

    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}