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

The following examples show how to use javax.xml.transform.dom.DOMResult#setNode() . 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: DomSerializer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 2
Source File: DomSerializer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 3
Source File: DomSerializer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 4
Source File: Bug5072946.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test3() throws Exception {
    SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    Transformer t = sf.newTransformer();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder parser = dbf.newDocumentBuilder();
    Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml"));

    DOMResult r = new DOMResult();

    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());

    Node n = r.getNode().getFirstChild();
    r.setNode(n);
    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());
    Assert.assertSame(r.getNode(), n);

    r.setNextSibling(r.getNode().getFirstChild());
    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());
    Assert.assertSame(r.getNode(), n);
}
 
Example 5
Source File: DomSerializer.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 6
Source File: DomSerializer.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 7
Source File: DomSerializer.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 8
Source File: DomSerializer.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 9
Source File: DomSerializer.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public DomSerializer(DOMResult domResult) {
    Node node = domResult.getNode();

    if (node == null) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.newDocument();
            domResult.setNode(doc);
            serializer = new SaxSerializer(new Dom2SaxAdapter(doc),null,false);
        } catch (ParserConfigurationException pce) {
            throw new TxwException(pce);
        }
    } else {
        serializer = new SaxSerializer(new Dom2SaxAdapter(node),null,false);
    }
}
 
Example 10
Source File: DOMValidatorHelper.java    From JDKSourceCode1.8 with MIT License 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 = JdkXmlUtils.getDOMFactory(
                    fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
            DocumentBuilder builder = factory.newDocumentBuilder();
            result.setNode(builder.newDocument());
        }
        catch (ParserConfigurationException e) {
            throw new SAXException(e);
        }
    }
    fDOMValidatorHandler = fDOMResultBuilder;
    fDOMResultBuilder.setDOMResult(result);
    fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
 
Example 11
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 12
Source File: DOMValidatorHelper.java    From openjdk-jdk8u 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 = JdkXmlUtils.getDOMFactory(
                    fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
            DocumentBuilder builder = factory.newDocumentBuilder();
            result.setNode(builder.newDocument());
        }
        catch (ParserConfigurationException e) {
            throw new SAXException(e);
        }
    }
    fDOMValidatorHandler = fDOMResultBuilder;
    fDOMResultBuilder.setDOMResult(result);
    fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
 
Example 13
Source File: DOMValidatorHelper.java    From openjdk-jdk8u-backup 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 = JdkXmlUtils.getDOMFactory(
                    fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
            DocumentBuilder builder = factory.newDocumentBuilder();
            result.setNode(builder.newDocument());
        }
        catch (ParserConfigurationException e) {
            throw new SAXException(e);
        }
    }
    fDOMValidatorHandler = fDOMResultBuilder;
    fDOMResultBuilder.setDOMResult(result);
    fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
 
Example 14
Source File: DOMValidatorHelper.java    From Bytecoder with Apache License 2.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 = JdkXmlUtils.getDOMFactory(
                    fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
            DocumentBuilder builder = factory.newDocumentBuilder();
            result.setNode(builder.newDocument());
        }
        catch (ParserConfigurationException e) {
            throw new SAXException(e);
        }
    }
    fDOMValidatorHandler = fDOMResultBuilder;
    fDOMResultBuilder.setDOMResult(result);
    fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
 
Example 15
Source File: DOMValidatorHelper.java    From openjdk-8 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: DOMValidatorHelper.java    From openjdk-jdk9 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 17
Source File: DOMValidatorHelper.java    From TencentKona-8 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 = JdkXmlUtils.getDOMFactory(
                    fComponentManager.getFeature(JdkXmlUtils.OVERRIDE_PARSER));
            DocumentBuilder builder = factory.newDocumentBuilder();
            result.setNode(builder.newDocument());
        }
        catch (ParserConfigurationException e) {
            throw new SAXException(e);
        }
    }
    fDOMValidatorHandler = fDOMResultBuilder;
    fDOMResultBuilder.setDOMResult(result);
    fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
}
 
Example 18
Source File: Bug5073477.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test1() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder parser = dbf.newDocumentBuilder();
    Document dom = parser.parse(Bug5073477.class.getResourceAsStream("Bug5073477.xml"));

    DOMResult r = new DOMResult();

    r.setNode(dom.getDocumentElement());
    r.setNextSibling(r.getNode().getFirstChild());
}
 
Example 19
Source File: DOMValidatorHelper.java    From jdk1.8-source-analysis with Apache License 2.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 20
Source File: AbstractMarshaller.java    From spring4-understanding with Apache License 2.0 3 votes vote down vote up
/**
 * Template method for handling {@code DOMResult}s.
 * <p>This implementation delegates to {@code marshalDomNode}.
 * @param graph the root of the object graph to marshal
 * @param domResult the {@code DOMResult}
 * @throws XmlMappingException if the given object cannot be marshalled to the result
 * @throws IllegalArgumentException if the {@code domResult} is empty
 * @see #marshalDomNode(Object, org.w3c.dom.Node)
 */
protected void marshalDomResult(Object graph, DOMResult domResult) throws XmlMappingException {
	if (domResult.getNode() == null) {
		domResult.setNode(buildDocument());
	}
	marshalDomNode(graph, domResult.getNode());
}