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);
}
}
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();
}
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);
}
Aggregations