use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class IndexingTest method irregularilyStructured.
private void irregularilyStructured(boolean getContentAsDOM) throws XMLDBException, ParserConfigurationException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
Database database = null;
final String testName = "IrregularilyStructured";
startTime = System.currentTimeMillis();
Collection coll = DatabaseManager.getCollection(baseURI, username, password);
XMLResource resource = (XMLResource) coll.createResource(name, XMLResource.RESOURCE_TYPE);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
effectiveSiblingCount = populate(doc);
resource.setContentAsDOM(doc);
coll.storeResource(resource);
coll.close();
coll = DatabaseManager.getCollection(baseURI, username, password);
resource = (XMLResource) coll.getResource(name);
Node n;
if (getContentAsDOM) {
n = resource.getContentAsDOM();
} else {
String s = (String) resource.getContent();
byte[] bytes = s.getBytes(UTF_8);
UnsynchronizedByteArrayInputStream bais = new UnsynchronizedByteArrayInputStream(bytes);
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
n = db.parse(bais);
}
Element documentElement = null;
if (n instanceof Element) {
documentElement = (Element) n;
} else if (n instanceof Document) {
documentElement = ((Document) n).getDocumentElement();
}
assertions(documentElement);
coll.removeResource(resource);
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class BinaryResourceUpdateTest method updateBinary_windows.
// with same docname test fails for windows
@Test
public void updateBinary_windows() throws XMLDBException, URISyntaxException {
for (int i = 0; i < REPEAT; i++) {
BinaryResource binaryResource = (BinaryResource) testCollection.createResource("test.xml", "BinaryResource");
binaryResource.setContent(Paths.get(binFile.toURI()));
testCollection.storeResource(binaryResource);
Resource resource = testCollection.getResource("test.xml");
assertNotNull(resource);
XMLResource xmlResource = (XMLResource) testCollection.createResource("test.xml", "XMLResource");
xmlResource.setContent(Paths.get(xmlFile.toURI()));
testCollection.storeResource(xmlResource);
resource = testCollection.getResource("test.xml");
assertNotNull(resource);
}
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class DOMTest method _test4.
private void _test4(boolean getContentAsDOM) throws TransformerException, ParserConfigurationException, XMLDBException, IOException, SAXException {
Collection coll = existEmbeddedServer.getRoot();
XMLResource resource = (XMLResource) coll.createResource(name, XMLResource.RESOURCE_TYPE);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element rootElem = doc.createElement("element");
Element propertyElem = doc.createElement("property");
propertyElem.setAttribute("key", "value");
propertyElem.appendChild(doc.createTextNode("text"));
rootElem.appendChild(propertyElem);
doc.appendChild(rootElem);
resource.setContentAsDOM(doc);
coll.storeResource(resource);
coll.close();
coll = DatabaseManager.getCollection(XmldbURI.LOCAL_DB, "admin", "");
resource = (XMLResource) coll.getResource(name);
Node n;
if (getContentAsDOM) {
n = resource.getContentAsDOM();
} else {
String s = (String) resource.getContent();
byte[] bytes;
bytes = s.getBytes(UTF_8);
try (final UnsynchronizedByteArrayInputStream bais = new UnsynchronizedByteArrayInputStream(bytes)) {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
n = db.parse(bais);
}
}
Transformer t = TransformerFactory.newInstance().newTransformer();
DOMSource source = new DOMSource(n);
SAXResult result = new SAXResult(new DOMTest.SAXHandler());
t.transform(source, result);
coll.removeResource(resource);
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class DOMTest method test2.
/**
* - create and fill a simple document via DOM and JAXP
* - store it with setContentAsDOM()
* - simple access via getContentAsDOM()
*/
@Test
public void test2() throws XMLDBException, InstantiationException, IllegalAccessException, ClassNotFoundException, ParserConfigurationException, IOException {
for (int i = 0; i < 2; i++) {
XMLResource resource = (XMLResource) existEmbeddedServer.getRoot().getResource(name);
if (resource == null) {
resource = (XMLResource) existEmbeddedServer.getRoot().createResource(name, XMLResource.RESOURCE_TYPE);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element rootElem = doc.createElement("element");
doc.appendChild(rootElem);
resource.setContentAsDOM(doc);
existEmbeddedServer.getRoot().storeResource(resource);
resource = (XMLResource) existEmbeddedServer.getRoot().getResource(name);
}
String s = (String) resource.getContent();
Node content = resource.getContentAsDOM();
}
existEmbeddedServer.restart();
XMLResource resource = (XMLResource) existEmbeddedServer.getRoot().getResource(name);
existEmbeddedServer.getRoot().removeResource(resource);
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class LocalXMLResourceDOMTest method setup.
@BeforeClass
public static void setup() throws XMLDBException {
final CollectionManagementService cms = (CollectionManagementService) existEmbeddedServer.getRoot().getService("CollectionManagementService", "1.0");
final Collection coll = cms.createCollection(TestConstants.TEST_COLLECTION_URI.lastSegment().toString());
final XMLResource r = (XMLResource) coll.createResource(TEST_RESOURCE_NAME, XMLResource.RESOURCE_TYPE);
r.setContent("<properties><property key=\"type\">Table</property><test/></properties><!-- comment -->");
coll.storeResource(r);
}
Aggregations