use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class ExtDocTest method parse.
@Test
public void parse() throws XMLDBException {
final URI docUri = externalDoc.toUri();
final ResourceSet result = existEmbeddedServer.executeQuery("xquery version \"3.1\";\n" + "\n" + "declare namespace output = \"http://www.w3.org/2010/xslt-xquery-serialization\";" + "declare option output:omit-xml-declaration \"yes\";\n" + "\n" + "fn:doc('" + docUri + "')");
assertNotNull(result);
assertEquals(1, result.getSize());
final Resource resource = result.getResource(0);
assertNotNull(resource);
assertEquals(XMLResource.RESOURCE_TYPE, resource.getResourceType());
final Source expectedSource = Input.fromString(docContent).build();
final Source actualSource = Input.fromNode(((XMLResource) resource).getContentAsDOM()).build();
final Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource).checkForSimilar().build();
assertFalse(diff.toString(), diff.hasDifferences());
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class AbstractExtractFunction method processCompressedEntry.
/**
* Processes a compressed entry from an archive
*
* @param name The name of the entry
* @param isDirectory true if the entry is a directory, false otherwise
* @param is an InputStream for reading the uncompressed data of the entry
* @param filterParam is an additional param for entry filtering function
* @param storeParam is an additional param for entry storing function
*
* @return the result of processing the compressed entry.
*
* @throws XPathException if a query error occurs
* @throws XMLDBException if a database error occurs
* @throws IOException if an I/O error occurs
*/
protected Sequence processCompressedEntry(String name, boolean isDirectory, InputStream is, Sequence filterParam, Sequence storeParam) throws IOException, XPathException, XMLDBException {
String dataType = isDirectory ? "folder" : "resource";
// call the entry-filter function
Sequence[] filterParams = new Sequence[3];
filterParams[0] = new StringValue(name);
filterParams[1] = new StringValue(dataType);
filterParams[2] = filterParam;
Sequence entryFilterFunctionResult = entryFilterFunction.evalFunction(contextSequence, null, filterParams);
if (BooleanValue.FALSE == entryFilterFunctionResult.itemAt(0)) {
return Sequence.EMPTY_SEQUENCE;
} else {
Sequence entryDataFunctionResult;
Sequence uncompressedData = Sequence.EMPTY_SEQUENCE;
if (entryDataFunction.getSignature().getReturnType().getPrimaryType() != Type.EMPTY && entryDataFunction.getSignature().getArgumentCount() == 3) {
Sequence[] dataParams = new Sequence[3];
System.arraycopy(filterParams, 0, dataParams, 0, 2);
dataParams[2] = storeParam;
entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
String path = entryDataFunctionResult.itemAt(0).getStringValue();
Collection root = new LocalCollection(context.getSubject(), context.getBroker().getBrokerPool(), new AnyURIValue("/db").toXmldbURI());
if (isDirectory) {
XMLDBAbstractCollectionManipulator.createCollection(root, path);
} else {
Resource resource;
Path file = Paths.get(path).normalize();
name = FileUtils.fileName(file);
path = file.getParent().toAbsolutePath().toString();
Collection target = (path == null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
// copy the input data
final byte[] entryData;
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
baos.write(is);
entryData = baos.toByteArray();
}
try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
NodeValue content = ModuleUtils.streamToXML(context, bis);
resource = target.createResource(name, "XMLResource");
ContentHandler handler = ((XMLResource) resource).setContentAsSAX();
handler.startDocument();
content.toSAX(context.getBroker(), handler, null);
handler.endDocument();
} catch (SAXException e) {
resource = target.createResource(name, "BinaryResource");
resource.setContent(entryData);
}
if (resource != null) {
if (mime != null) {
((EXistResource) resource).setMimeType(mime.getName());
}
target.storeResource(resource);
}
}
} else {
// copy the input data
final byte[] entryData;
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
baos.write(is);
entryData = baos.toByteArray();
}
// try and parse as xml, fall back to binary
try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
uncompressedData = ModuleUtils.streamToXML(context, bis);
} catch (SAXException saxe) {
if (entryData.length > 0) {
try (final InputStream bis = new UnsynchronizedByteArrayInputStream(entryData)) {
uncompressedData = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), bis);
}
}
}
// call the entry-data function
Sequence[] dataParams = new Sequence[4];
System.arraycopy(filterParams, 0, dataParams, 0, 2);
dataParams[2] = uncompressedData;
dataParams[3] = storeParam;
entryDataFunctionResult = entryDataFunction.evalFunction(contextSequence, null, dataParams);
}
return entryDataFunctionResult;
}
}
use of org.xmldb.api.modules.XMLResource in project exist by eXist-db.
the class ExpandTest method expandPersistentDomAttrNs.
@Test
public void expandPersistentDomAttrNs() throws XMLDBException {
final String query = "declare namespace x = \"http://x\";\n" + "util:expand(doc('/db/expand-test/doc4.xml')/doc4/@x:foo)";
final ResourceSet result = existEmbeddedServer.executeQuery(query);
final Resource res = result.getResource(0);
assertEquals(XMLResource.RESOURCE_TYPE, res.getResourceType());
final XMLResource xmlRes = (XMLResource) res;
final Node node = xmlRes.getContentAsDOM();
assertEquals(Node.ATTRIBUTE_NODE, node.getNodeType());
assertEquals("http://x", node.getNamespaceURI());
assertEquals("foo", node.getNodeName());
assertEquals("bar", node.getNodeValue());
}
use of org.xmldb.api.modules.XMLResource 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.modules.XMLResource 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());
}
Aggregations