use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class Modification method deepCopy.
protected Sequence deepCopy(Sequence inSeq) throws XPathException {
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
final Serializer serializer = context.getBroker().borrowSerializer();
serializer.setReceiver(receiver);
try {
final Sequence out = new ValueSequence();
for (final SequenceIterator i = inSeq.iterate(); i.hasNext(); ) {
Item item = i.nextItem();
if (item.getType() == Type.DOCUMENT) {
if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
final NodeHandle root = (NodeHandle) ((NodeProxy) item).getOwnerDocument().getDocumentElement();
item = new NodeProxy(root);
} else {
item = (Item) ((Document) item).getDocumentElement();
}
}
if (Type.subTypeOf(item.getType(), Type.NODE)) {
if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
final int last = builder.getDocument().getLastNode();
final NodeProxy p = (NodeProxy) item;
serializer.toReceiver(p, false, false);
if (p.getNodeType() == Node.ATTRIBUTE_NODE) {
item = builder.getDocument().getLastAttr();
} else {
item = builder.getDocument().getNode(last + 1);
}
} else {
((org.exist.dom.memtree.NodeImpl) item).deepCopy();
}
}
out.add(item);
}
return out;
} catch (final SAXException | DOMException e) {
throw new XPathException(this, e.getMessage(), e);
} finally {
context.getBroker().returnSerializer(serializer);
context.popDocumentContext();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class ExceptTest method createInMemoryDocument.
private Document createInMemoryDocument() {
final MemTreeBuilder memtree = new MemTreeBuilder();
memtree.startDocument();
memtree.startElement(new QName("m1", XMLConstants.NULL_NS_URI), null);
memtree.startElement(new QName("m2", XMLConstants.NULL_NS_URI), null);
memtree.characters("test data");
memtree.endElement();
memtree.endElement();
memtree.endDocument();
return memtree.getDocument();
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class ValueSequenceTest method sortInDocumentOrder.
@Test
public void sortInDocumentOrder() throws EXistException, PermissionDeniedException, AuthenticationException {
final ValueSequence seq = new ValueSequence(true);
seq.keepUnOrdered(true);
// in-memory doc
final MemTreeBuilder memtree = new MemTreeBuilder();
memtree.startDocument();
memtree.startElement(new QName("m1", XMLConstants.NULL_NS_URI), null);
memtree.startElement(new QName("m2", XMLConstants.NULL_NS_URI), null);
memtree.characters("test data");
memtree.endElement();
memtree.endElement();
memtree.endDocument();
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final Subject admin = pool.getSecurityManager().authenticate("admin", "");
try (final DBBroker broker = pool.get(Optional.of(admin))) {
// persistent doc
final Collection sysCollection = broker.getCollection(SecurityManager.SECURITY_COLLECTION_URI);
final DocumentImpl doc = sysCollection.getDocument(broker, XmldbURI.create("config.xml"));
final NodeProxy docProxy = new NodeProxy(doc);
final NodeProxy nodeProxy = new NodeProxy(doc, ((NodeImpl) doc.getFirstChild()).getNodeId());
seq.add(memtree.getDocument());
seq.add(docProxy);
seq.add((org.exist.dom.memtree.NodeImpl) memtree.getDocument().getFirstChild());
seq.add(nodeProxy);
// call sort
seq.sortInDocumentOrder();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class Sync method startSync.
private Sequence startSync(final String target, final String collectionPath, final Map<String, Sequence> options) throws XPathException {
final Date startDate = options.get(AFTER_OPT).hasOne() ? ((DateTimeValue) options.get(AFTER_OPT)).getDate() : null;
final boolean prune = ((BooleanValue) options.get(PRUNE_OPT)).getValue();
final List<String> excludes = new ArrayList<>(Collections.emptyList());
for (final SequenceIterator si = options.get(EXCLUDES_OPT).iterate(); si.hasNext(); ) {
excludes.add(si.nextItem().getStringValue());
}
final Path p = FileModuleHelper.getFile(target);
context.pushDocumentContext();
final MemTreeBuilder output = context.getDocumentBuilder();
final Path targetDir;
try {
if (p.isAbsolute()) {
targetDir = p;
} else {
final Optional<Path> home = context.getBroker().getConfiguration().getExistHome();
targetDir = FileUtils.resolve(home, target);
}
output.startDocument();
output.startElement(FILE_SYNC_ELEMENT, null);
output.addAttribute(FILE_COLLECTION_ATTRIBUTE, collectionPath);
output.addAttribute(FILE_DIR_ATTRIBUTE, targetDir.toAbsolutePath().toString());
final String rootTargetAbsPath = targetDir.toAbsolutePath().toString();
final String separator = rootTargetAbsPath.endsWith(File.separator) ? "" : File.separator;
syncCollection(XmldbURI.create(collectionPath), rootTargetAbsPath + separator, targetDir, startDate, prune, excludes, output);
output.endElement();
output.endDocument();
} catch (final PermissionDeniedException | LockException e) {
throw new XPathException(this, e);
} finally {
context.popDocumentContext();
}
return output.getDocument();
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class Deploy method statusReport.
private Sequence statusReport(final Optional<String> target) {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
final AttributesImpl attrs = new AttributesImpl();
if (target.isPresent()) {
attrs.addAttribute("", "result", "result", "CDATA", "ok");
attrs.addAttribute("", "target", "target", "CDATA", target.get());
} else {
attrs.addAttribute("", "result", "result", "CDATA", "fail");
}
builder.startElement(STATUS_ELEMENT, attrs);
builder.endElement();
return builder.getDocument().getNode(1);
} finally {
context.popDocumentContext();
}
}
Aggregations