use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class DeepCopyFunction method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
final Item a = args[0].itemAt(0);
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
try {
final Properties props = new Properties();
a.toSAX(context.getBroker(), receiver, props);
} catch (final SAXException e) {
throw new XPathException(this, "Cannot Deep-copy Item");
}
builder.endDocument();
return (NodeValue) receiver.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class FnExport method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole()) {
throw (new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to kill a running xquery"));
}
final String dirOrFile = args[0].getStringValue();
boolean incremental = false;
if (args[1].hasOne()) {
incremental = args[1].effectiveBooleanValue();
}
boolean zip = false;
if (args[2].hasOne()) {
zip = args[2].effectiveBooleanValue();
}
try {
MemTreeBuilder builder = null;
if (NAME.equals(getName())) {
context.pushDocumentContext();
builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(EXPORT_ELEMENT, null);
}
try (final Txn transaction = context.getBroker().continueOrBeginTransaction()) {
final SystemExport export = new SystemExport(context.getBroker(), transaction, new Callback(builder), null, true);
export.export(dirOrFile, incremental, zip, null);
transaction.commit();
} catch (final Exception e) {
throw new XPathException(this, "export failed with exception: " + e.getMessage(), e);
}
if (builder == null) {
return Sequence.EMPTY_SEQUENCE;
} else {
builder.endElement();
builder.endDocument();
return (NodeValue) builder.getDocument().getDocumentElement();
}
} finally {
if (NAME.equals(getName())) {
context.popDocumentContext();
}
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class PIConstructor method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (newDocumentContext) {
context.pushDocumentContext();
}
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
final int nodeNr = builder.processingInstruction(target, data);
final NodeImpl node = builder.getDocument().getNode(nodeNr);
return node;
} finally {
if (newDocumentContext) {
context.popDocumentContext();
}
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class NamespaceConstructor method eval.
@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
final MemTreeBuilder builder = context.getDocumentBuilder();
context.proceed(this, builder);
final Sequence prefixSeq = qnameExpr.eval(contextSequence, contextItem);
if (!(prefixSeq.isEmpty() || Type.subTypeOf(prefixSeq.getItemType(), Type.STRING) || prefixSeq.getItemType() == Type.UNTYPED_ATOMIC)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Prefix needs to be xs:string or xs:untypedAtomic");
}
String prefix = XMLConstants.DEFAULT_NS_PREFIX;
if (!prefixSeq.isEmpty()) {
prefix = prefixSeq.getStringValue();
if (!(prefix.length() == 0 || XMLNames.isNCName(prefix))) {
throw new XPathException(this, ErrorCodes.XQDY0074, "Prefix cannot be cast to xs:NCName");
}
}
final Sequence uriSeq = content.eval(contextSequence, contextItem);
final String value = uriSeq.getStringValue();
final String inscopeNsUri = context.getInScopeNamespace(prefix);
if (inscopeNsUri != null && !inscopeNsUri.equals(value)) {
throw new XPathException(this, ErrorCodes.XQDY0102, "Cannot override already defined ns");
}
if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
throw new XPathException(this, ErrorCodes.XQDY0101, "Cannot bind xmlns prefix");
} else if (prefix.equals(XMLConstants.XML_NS_PREFIX) && !value.equals(Namespaces.XML_NS)) {
throw new XPathException(this, ErrorCodes.XQDY0101, "Cannot bind xml prefix to another namespace");
} else if (value.equals(Namespaces.XML_NS) && !prefix.equals("xml")) {
throw new XPathException(this, ErrorCodes.XQDY0101, "Cannot bind prefix to XML namespace");
} else if (value.equals(Namespaces.XMLNS_NS)) {
throw new XPathException(this, ErrorCodes.XQDY0101, "Cannot bind prefix to xmlns namespace");
} else if (value.length() == 0) {
throw new XPathException(this, ErrorCodes.XQDY0101, "Cannot bind prefix to empty or zero-length namespace");
}
// context.declareInScopeNamespace(prefix, value);
final int nodeNr = builder.namespaceNode(prefix, value);
final Sequence result = builder.getDocument().getNamespaceNode(nodeNr);
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class DynamicAttributeConstructor method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
if (newDocumentContext) {
context.pushDocumentContext();
}
NodeImpl node;
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.setReplaceAttributeFlag(replaceAttribute);
context.proceed(this, builder);
final Sequence nameSeq = qnameExpr.eval(contextSequence, contextItem);
if (!nameSeq.hasOne()) {
throw new XPathException(this, "The name expression should evaluate to a single value");
}
final Item qnItem = nameSeq.itemAt(0);
QName qn;
if (qnItem.getType() == Type.QNAME) {
qn = ((QNameValue) qnItem).getQName();
} else
try {
qn = QName.parse(context, nameSeq.getStringValue(), null);
} catch (final QName.IllegalQNameException e) {
throw new XPathException(this, ErrorCodes.XPTY0004, "'" + nameSeq.getStringValue() + "' is not a valid attribute name");
}
// Not in the specs but... makes sense
if (!XMLNames.isName(qn.getLocalPart())) {
throw new XPathException(this, ErrorCodes.XPTY0004, "'" + qn.getLocalPart() + "' is not a valid attribute name");
}
if ("xmlns".equals(qn.getLocalPart()) && qn.getNamespaceURI().isEmpty()) {
throw new XPathException(this, ErrorCodes.XQDY0044, "'" + qn.getLocalPart() + "' is not a valid attribute name");
}
String value;
final Sequence valueSeq = valueExpr.eval(contextSequence, contextItem);
if (valueSeq.isEmpty()) {
value = "";
} else {
final StringBuilder buf = new StringBuilder();
for (final SequenceIterator i = Atomize.atomize(valueSeq).iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
buf.append(next.getStringValue());
if (i.hasNext()) {
buf.append(' ');
}
}
value = buf.toString();
}
value = DynamicAttributeConstructor.normalize(this, qn, value);
node = null;
try {
final int nodeNr = builder.addAttribute(qn, value);
node = builder.getDocument().getAttribute(nodeNr);
} catch (final DOMException e) {
throw new XPathException(this, ErrorCodes.XQDY0025, "element has more than one attribute '" + qn + "'");
}
} finally {
if (newDocumentContext) {
context.popDocumentContext();
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", node);
}
return node;
}
Aggregations