use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class UpdateReplaceTest method replaceOnlyChildWhereParentHasNoAttributes.
@Test
public void replaceOnlyChildWhereParentHasNoAttributes() throws XMLDBException {
final String testDocName = "replaceOnlyChildWhereParentHasNoAttributes.xml";
final String testDoc = "<Test><Content><A/></Content></Test>";
final String updateQuery = "let $content := doc('/db/test/" + testDocName + "')/Test/Content\n" + " let $legacy := $content/A\n" + " return\n" + " update replace $legacy with <AA/>,\n" + " doc('/db/test/" + testDocName + "')/Test";
final XQueryService xqueryService = storeXMLStringAndGetQueryService(testDocName, testDoc);
final ResourceSet result = xqueryService.query(updateQuery);
assertNotNull(result);
assertEquals(1, result.getSize());
final Resource res1 = result.getResource(0);
assertNotNull(res1);
assertEquals(XMLResource.RESOURCE_TYPE, res1.getResourceType());
final Document doc = ((XMLResource) res1).getContentAsDOM().getOwnerDocument();
final Source actual = Input.fromDocument(doc).build();
final Source expected = Input.fromString("<Test><Content><AA/></Content></Test>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class UpdateReplaceTest method replaceFirstChildWhereParentHasNoAttributes.
@Test
public void replaceFirstChildWhereParentHasNoAttributes() throws XMLDBException {
final String testDocName = "replaceFirstChildWhereParentHasNoAttributes.xml";
final String testDoc = "<Test><Content><A/><A/></Content></Test>";
final String updateQuery = "let $content := doc('/db/test/" + testDocName + "')/Test/Content\n" + " let $legacy := $content/A[1]\n" + " return\n" + " update replace $legacy with <AA/>,\n" + " doc('/db/test/" + testDocName + "')/Test";
final XQueryService xqueryService = storeXMLStringAndGetQueryService(testDocName, testDoc);
final ResourceSet result = xqueryService.query(updateQuery);
assertNotNull(result);
assertEquals(1, result.getSize());
final Resource res1 = result.getResource(0);
assertNotNull(res1);
assertEquals(XMLResource.RESOURCE_TYPE, res1.getResourceType());
final Document doc = ((XMLResource) res1).getContentAsDOM().getOwnerDocument();
final Source actual = Input.fromDocument(doc).build();
final Source expected = Input.fromString("<Test><Content><AA/><A/></Content></Test>").build();
final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class StressTest method fetchDb.
private void fetchDb() throws XMLDBException {
final XPathQueryService xquery = (XPathQueryService) testCol.getService("XPathQueryService", "1.0");
final ResourceSet result = xquery.query("for $n in collection('" + XmldbURI.ROOT_COLLECTION + "/test')//* return local-name($n)");
for (int i = 0; i < result.getSize(); i++) {
final Resource r = result.getResource(i);
final String tag = r.getContent().toString();
final ResourceSet result2 = xquery.query("//" + tag);
assertEquals(result2.getSize(), 1);
}
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class XUpdateTest method removeDocument.
private void removeDocument() throws XMLDBException {
final Resource document = col.getResource(XUPDATE_FILE);
col.removeResource(document);
}
use of org.xmldb.api.base.Resource in project exist by eXist-db.
the class NewResourceDialog method createResource.
private void createResource(final ResourceType resourceType, final String filename, final String moduleNamespace, final String moduleNamespacePrefix) {
final StringBuilder resourceContentBuilder = new StringBuilder();
try (final InputStream is = getClass().getResourceAsStream(resourceType.getTemplatePath());
final Reader reader = new InputStreamReader(is)) {
final char[] buf = new char[1024];
int read = -1;
while ((read = reader.read(buf)) > -1) {
resourceContentBuilder.append(buf, 0, read);
}
} catch (final IOException ioe) {
ClientFrame.showErrorMessage(ioe.getMessage(), ioe);
}
final String resourceContent;
if (resourceType == ResourceType.XQUERY_LIBRARY) {
resourceContent = resourceContentBuilder.toString().replaceAll("\\$NS", moduleNamespace).replaceAll("\\$PREFIX", moduleNamespacePrefix);
} else {
resourceContent = resourceContentBuilder.toString();
}
try {
final String resName = URIUtils.urlEncodeUtf8((isNullOrEmpty(filename) ? DEFAULT_FILENAME : filename) + "." + resourceType.getFileExtension());
final String resType = resourceType == ResourceType.XML_DOCUMENT ? XMLResource.RESOURCE_TYPE : BinaryResource.RESOURCE_TYPE;
final Collection collection = client.current;
final Resource resource = collection.createResource(resName, resType);
resource.setContent(resourceContent);
((EXistResource) resource).setMimeType(resourceType.getMimeType());
collection.storeResource(resource);
collection.close();
client.reloadCollection();
} catch (final XMLDBException xmldbe) {
ClientFrame.showErrorMessage(xmldbe.getMessage(), xmldbe);
}
}
Aggregations