use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunNamespaceURI method eval.
@Override
public Sequence eval(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());
}
}
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
// If we have one argument, we take it into account
final Sequence seq;
if (getSignature().getArgumentCount() > 0) {
seq = getArgument(0).eval(contextSequence, contextItem);
} else {
// Otherwise, we take the context sequence and we iterate over it
seq = contextSequence;
}
if (seq == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
final Sequence result;
if (seq.isEmpty()) {
result = AnyURIValue.EMPTY_URI;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node; got: " + Type.getTypeName(item.getType()));
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
String ns = n.getNamespaceURI();
if (ns == null) {
ns = XMLConstants.NULL_NS_URI;
}
result = new AnyURIValue(ns);
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunRoot method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(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 (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());
}
}
Sequence seq;
Sequence result;
Item item;
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
if (contextSequence == null || contextSequence.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
}
// If we have one argumment, we take it into account
if (getSignature().getArgumentCount() > 0) {
seq = getArgument(0).eval(contextSequence, contextItem);
} else // Otherwise, we take the context sequence and we iterate over it
{
seq = contextSequence;
}
if (seq == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
if (seq.isPersistentSet()) {
result = new ExtArrayNodeSet(seq.getItemCount());
} else {
result = new ValueSequence(seq.getItemCount());
}
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
}
final Sequence s = item.toSequence();
if (s.isPersistentSet()) {
final NodeProxy p = s.toNodeSet().get(0);
result.add(new NodeProxy(p.getOwnerDocument()));
} else {
if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
result.add(item);
} else if (item.getType() == Type.DOCUMENT) {
result.add((DocumentImpl) item);
} else {
result.add(((NodeImpl) item).getOwnerDocument());
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunRoundHalfToEven method eval.
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());
}
}
Sequence result;
IntegerValue precision = null;
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
if (getSignature().getArgumentCount() > 1) {
precision = (IntegerValue) getArgument(1).eval(contextSequence, contextItem).itemAt(0).convertTo(Type.INTEGER);
}
final Item item = seq.itemAt(0);
NumericValue value;
if (item instanceof NumericValue) {
value = (NumericValue) item;
} else {
value = (NumericValue) item.convertTo(Type.NUMBER);
}
result = value.round(precision);
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class XUpdateProcessor method startElement.
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
// save accumulated character content
if (inModification && charBuf.length() > 0) {
// String normalized = charBuf.toString();
final String normalized = preserveWhitespace ? charBuf.toString() : charBuf.toString().trim();
if (!normalized.isEmpty()) {
final Text text = doc.createTextNode(charBuf.toString());
final Element last = stack.peek();
if (last == null) {
// LOG.debug("appending text to fragment: " + text.getData());
contents.add(text);
} else {
last.appendChild(text);
}
}
charBuf.setLength(0);
}
if (namespaceURI.equals(XUPDATE_NS)) {
String select = null;
switch(localName) {
case MODIFICATIONS:
startModifications(atts);
return;
case VARIABLE:
// variable declaration
startVariableDecl(atts);
return;
case IF:
if (inModification) {
throw new SAXException("xupdate:if is not allowed inside a modification");
}
select = atts.getValue("test");
final Conditional cond = new Conditional(broker, documentSet, select, namespaces, variables);
conditionals.push(cond);
return;
case VALUE_OF:
if (!inModification) {
throw new SAXException("xupdate:value-of is not allowed outside a modification");
}
break;
case APPEND:
case INSERT_BEFORE:
case INSERT_AFTER:
case REMOVE:
case RENAME:
case UPDATE:
case REPLACE:
if (inModification) {
throw new SAXException("nested modifications are not allowed");
}
select = atts.getValue("select");
if (select == null) {
throw new SAXException(localName + " requires a select attribute");
}
doc = builder.newDocument();
contents = new NodeListImpl();
inModification = true;
break;
case ELEMENT:
case ATTRIBUTE:
case TEXT:
case PROCESSING_INSTRUCTION:
case COMMENT:
if (!inModification) {
throw new SAXException("creation elements are only allowed inside " + "a modification");
}
charBuf.setLength(0);
break;
default:
throw new SAXException("Unknown XUpdate element: " + qName);
}
// start a new modification section
switch(localName) {
case APPEND:
final String child = atts.getValue("child");
modification = new Append(broker, documentSet, select, child, namespaces, variables);
break;
case UPDATE:
modification = new Update(broker, documentSet, select, namespaces, variables);
break;
case INSERT_BEFORE:
modification = new Insert(broker, documentSet, select, Insert.INSERT_BEFORE, namespaces, variables);
break;
case INSERT_AFTER:
modification = new Insert(broker, documentSet, select, Insert.INSERT_AFTER, namespaces, variables);
break;
case REMOVE:
modification = new Remove(broker, documentSet, select, namespaces, variables);
break;
case RENAME:
modification = new Rename(broker, documentSet, select, namespaces, variables);
break;
case REPLACE:
modification = new Replace(broker, documentSet, select, namespaces, variables);
break;
// process commands for node creation
case ELEMENT:
{
String name = atts.getValue("name");
if (name == null) {
throw new SAXException("element requires a name attribute");
}
final int p = name.indexOf(':');
String namespace = null;
String prefix = "";
if (p != Constants.STRING_NOT_FOUND) {
prefix = name.substring(0, p);
if (name.length() == p + 1) {
throw new SAXException("illegal prefix in qname: " + name);
}
name = name.substring(p + 1);
namespace = atts.getValue("namespace");
if (namespace == null) {
namespace = namespaces.get(prefix);
}
if (namespace == null) {
throw new SAXException("no namespace defined for prefix " + prefix);
}
}
Element elem;
if (namespace != null && !namespace.isEmpty()) {
elem = doc.createElementNS(namespace, name);
elem.setPrefix(prefix);
} else {
elem = doc.createElement(name);
}
final Element last = stack.peek();
if (last == null) {
contents.add(elem);
} else {
last.appendChild(elem);
}
stack.push(elem);
this.setWhitespaceHandling(elem);
break;
}
case ATTRIBUTE:
{
final String name = atts.getValue("name");
if (name == null) {
throw new SAXException("attribute requires a name attribute");
}
final int p = name.indexOf(':');
String namespace = null;
if (p != Constants.STRING_NOT_FOUND) {
final String prefix = name.substring(0, p);
if (name.length() == p + 1) {
throw new SAXException("illegal prefix in qname: " + name);
}
namespace = atts.getValue("namespace");
if (namespace == null) {
namespace = namespaces.get(prefix);
}
if (namespace == null) {
throw new SAXException("no namespace defined for prefix " + prefix);
}
}
Attr attrib = namespace != null && !namespace.isEmpty() ? doc.createAttributeNS(namespace, name) : doc.createAttribute(name);
if (stack.isEmpty()) {
for (int i = 0; i < contents.getLength(); i++) {
final Node n = contents.item(i);
String ns = n.getNamespaceURI();
final String nname = ns == null ? n.getNodeName() : n.getLocalName();
if (ns == null) {
ns = XMLConstants.NULL_NS_URI;
}
// check for duplicate attributes
if (n.getNodeType() == Node.ATTRIBUTE_NODE && nname.equals(name) && ns.equals(namespace)) {
throw new SAXException("The attribute " + attrib.getNodeName() + " cannot be specified twice");
}
}
contents.add(attrib);
} else {
final Element last = stack.peek();
if (namespace != null && last.hasAttributeNS(namespace, name) || namespace == null && last.hasAttribute(name)) {
throw new SAXException("The attribute " + attrib.getNodeName() + " cannot be specified " + "twice on the same element");
}
if (namespace != null) {
last.setAttributeNodeNS(attrib);
} else {
last.setAttributeNode(attrib);
}
}
inAttribute = true;
currentNode = attrib;
// process value-of
break;
}
case VALUE_OF:
select = atts.getValue("select");
if (select == null) {
throw new SAXException("value-of requires a select attribute");
}
final Sequence seq = processQuery(select);
if (LOG.isDebugEnabled()) {
LOG.debug("Found {} items for value-of", seq.getItemCount());
}
Item item;
try {
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
if (Type.subTypeOf(item.getType(), Type.NODE)) {
final Node node = NodeSetHelper.copyNode(doc, ((NodeValue) item).getNode());
final Element last = stack.peek();
if (last == null) {
contents.add(node);
} else {
last.appendChild(node);
}
} else {
final String value = item.getStringValue();
characters(value.toCharArray(), 0, value.length());
}
}
} catch (final XPathException e) {
throw new SAXException(e.getMessage(), e);
}
break;
}
} else if (inModification) {
final Element elem = namespaceURI != null && !namespaceURI.isEmpty() ? doc.createElementNS(namespaceURI, qName) : doc.createElement(qName);
Attr a;
for (int i = 0; i < atts.getLength(); i++) {
final String name = atts.getQName(i);
final String nsURI = atts.getURI(i);
if (name.startsWith("xmlns")) {
// Why are these showing up? They are supposed to be stripped out?
} else {
a = nsURI != null ? doc.createAttributeNS(nsURI, name) : doc.createAttribute(name);
a.setValue(atts.getValue(i));
if (nsURI != null) {
elem.setAttributeNodeNS(a);
} else {
elem.setAttributeNode(a);
}
}
}
final Element last = stack.peek();
if (last == null) {
contents.add(elem);
} else {
last.appendChild(elem);
}
stack.push(elem);
this.setWhitespaceHandling(elem);
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class IndexerTest2 method executeQuery.
private String executeQuery() throws EXistException, PermissionDeniedException, SAXException, XPathException, IOException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final StringWriter out = new StringWriter()) {
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final Sequence result = xquery.execute(broker, XQUERY, null);
final Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "yes");
final SAXSerializer serializer = new SAXSerializer(out, props);
serializer.startDocument();
for (final SequenceIterator i = result.iterate(); i.hasNext(); ) {
final Item next = i.nextItem();
next.toSAX(broker, serializer, props);
}
serializer.endDocument();
return out.toString();
}
}
Aggregations