Search in sources :

Example 6 with DOMSerializer

use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.

the class RemoteXMLResource method setContentAsDOM.

@Override
public void setContentAsDOM(final Node root) throws XMLDBException {
    Properties properties = getProperties();
    try {
        VirtualTempPath tempFile = new VirtualTempPath(getInMemorySize(properties), TemporaryFileManager.getInstance());
        try (OutputStream out = tempFile.newOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, UTF_8)) {
            final DOMSerializer xmlout = new DOMSerializer(osw, properties);
            final short type = root.getNodeType();
            if (type == Node.ELEMENT_NODE || type == Node.DOCUMENT_FRAGMENT_NODE || type == Node.DOCUMENT_NODE) {
                xmlout.serialize(root);
            } else {
                throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "invalid node type");
            }
        }
        setContent(tempFile);
    } catch (final TransformerException | IOException ioe) {
        freeResources();
        throw new XMLDBException(ErrorCodes.VENDOR_ERROR, ioe.getMessage(), ioe);
    }
}
Also used : VirtualTempPath(org.exist.util.io.VirtualTempPath) DOMSerializer(org.exist.util.serializer.DOMSerializer) OutputStream(java.io.OutputStream) XMLDBException(org.xmldb.api.base.XMLDBException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException)

Example 7 with DOMSerializer

use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.

the class XMLUtil method dump.

public static final String dump(final DocumentFragment fragment) {
    final StringWriter writer = new StringWriter();
    final DOMSerializer serializer = new DOMSerializer(writer, null);
    try {
        serializer.serialize(fragment);
    } catch (final TransformerException e) {
    // Nothing to do ?
    }
    return writer.toString();
}
Also used : DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) TransformerException(javax.xml.transform.TransformerException)

Example 8 with DOMSerializer

use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.

the class TestEXistXMLSerialize method serialize3.

@Test
public void serialize3() throws ParserConfigurationException, SAXException, IOException, XMLDBException, TransformerException, URISyntaxException {
    Collection testCollection = DatabaseManager.getCollection(XmldbURI.LOCAL_DB + "/" + TEST_COLLECTION);
    Document doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(Paths.get(testFile.toURI()).toFile());
    XMLResource resource = (XMLResource) testCollection.createResource(null, "XMLResource");
    resource.setContentAsDOM(doc);
    testCollection.storeResource(resource);
    resource = (XMLResource) testCollection.getResource(resource.getId());
    assertNotNull(resource);
    Node node = resource.getContentAsDOM();
    StringWriter writer = new StringWriter();
    Properties outputProperties = new Properties();
    outputProperties.setProperty("indent", "yes");
    DOMSerializer serializer = new DOMSerializer(writer, outputProperties);
    serializer.serialize(node);
}
Also used : DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) Node(org.w3c.dom.Node) Collection(org.xmldb.api.base.Collection) Document(org.w3c.dom.Document) Properties(java.util.Properties) XMLResource(org.xmldb.api.modules.XMLResource) Test(org.junit.Test)

Aggregations

DOMSerializer (org.exist.util.serializer.DOMSerializer)8 StringWriter (java.io.StringWriter)6 TransformerException (javax.xml.transform.TransformerException)4 Properties (java.util.Properties)3 Test (org.junit.Test)3 Document (org.w3c.dom.Document)3 OutputStreamWriter (java.io.OutputStreamWriter)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 InputSource (org.xml.sax.InputSource)2 XMLDBException (org.xmldb.api.base.XMLDBException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 ServletException (javax.servlet.ServletException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1