use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class FunLocalName 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();
}
final Item item;
// the context sequence
if (getArgumentCount() > 0) {
final Sequence seq = getArgument(0).eval(contextSequence);
if (!seq.isEmpty()) {
item = seq.itemAt(0);
} else {
item = null;
}
} else {
if (contextSequence == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
item = contextSequence.itemAt(0);
}
final Sequence result;
if (item == null) {
result = StringValue.EMPTY_STRING;
} else {
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
final String localName = n.getLocalName();
if (localName != null) {
result = new StringValue(localName);
} else {
result = StringValue.EMPTY_STRING;
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class FunName 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 = StringValue.EMPTY_STRING;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
if (n instanceof INode) {
result = new StringValue(((INode) n).getQName().getStringValue());
} else {
result = StringValue.EMPTY_STRING;
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class FunDeepEqual method deepEquals.
public static boolean deepEquals(Item a, Item b, Collator collator) {
try {
if (a.getType() == Type.ARRAY || b.getType() == Type.ARRAY) {
if (a.getType() != b.getType()) {
return false;
}
final ArrayType ar = (ArrayType) a;
final ArrayType br = (ArrayType) b;
if (ar.getSize() != br.getSize()) {
return false;
}
for (int i = 0; i < ar.getSize(); i++) {
if (!deepEqualsSeq(ar.get(i), br.get(i), collator)) {
return false;
}
}
return true;
}
if (a.getType() == Type.MAP || b.getType() == Type.MAP) {
if (a.getType() != b.getType()) {
return false;
}
final AbstractMapType amap = (AbstractMapType) a;
final AbstractMapType bmap = (AbstractMapType) b;
if (amap.size() != bmap.size()) {
return false;
}
for (final IEntry<AtomicValue, Sequence> aentry : amap) {
if (!bmap.contains(aentry.key())) {
return false;
}
if (!deepEqualsSeq(aentry.value(), bmap.get(aentry.key()), collator)) {
return false;
}
}
return true;
}
final boolean aAtomic = Type.subTypeOf(a.getType(), Type.ATOMIC);
final boolean bAtomic = Type.subTypeOf(b.getType(), Type.ATOMIC);
if (aAtomic || bAtomic) {
if (!aAtomic || !bAtomic) {
return false;
}
try {
final AtomicValue av = (AtomicValue) a;
final AtomicValue bv = (AtomicValue) b;
if (Type.subTypeOfUnion(av.getType(), Type.NUMBER) && Type.subTypeOfUnion(bv.getType(), Type.NUMBER)) {
// or if both values are NaN
if (((NumericValue) a).isNaN() && ((NumericValue) b).isNaN()) {
return true;
}
}
return ValueComparison.compareAtomic(collator, av, bv, StringTruncationOperator.NONE, Comparison.EQ);
} catch (final XPathException e) {
return false;
}
}
if (a.getType() != b.getType()) {
return false;
}
final NodeValue nva = (NodeValue) a;
final NodeValue nvb = (NodeValue) b;
if (nva == nvb) {
return true;
}
try {
// since the symbol table is ignored.
if (nva.getImplementationType() != NodeValue.IN_MEMORY_NODE && nva.equals(nvb)) // shortcut!
{
return true;
}
} catch (final XPathException e) {
// apparently incompatible values, do manual comparison
}
final Node na;
final Node nb;
switch(a.getType()) {
case Type.DOCUMENT:
// NodeValue.getNode() doesn't seem to work for document nodes
na = nva instanceof Node ? (Node) nva : ((NodeProxy) nva).getOwnerDocument();
nb = nvb instanceof Node ? (Node) nvb : ((NodeProxy) nvb).getOwnerDocument();
return compareContents(na, nb);
case Type.ELEMENT:
na = nva.getNode();
nb = nvb.getNode();
return compareElements(na, nb);
case Type.ATTRIBUTE:
na = nva.getNode();
nb = nvb.getNode();
return compareNames(na, nb) && safeEquals(na.getNodeValue(), nb.getNodeValue());
case Type.PROCESSING_INSTRUCTION:
case Type.NAMESPACE:
na = nva.getNode();
nb = nvb.getNode();
return safeEquals(na.getNodeName(), nb.getNodeName()) && safeEquals(nva.getStringValue(), nvb.getStringValue());
case Type.TEXT:
case Type.COMMENT:
return safeEquals(nva.getStringValue(), nvb.getStringValue());
default:
throw new RuntimeException("unexpected item type " + Type.getTypeName(a.getType()));
}
} catch (final XPathException e) {
logger.error(e.getMessage());
e.printStackTrace();
return false;
}
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class FunGMLProducers method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence result = null;
try {
AbstractGMLJDBCIndexWorker indexWorker = (AbstractGMLJDBCIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(AbstractGMLJDBCIndex.ID);
if (indexWorker == null) {
logger.error("Unable to find a spatial index worker");
throw new XPathException(this, "Unable to find a spatial index worker");
}
Geometry geometry = null;
String targetSRS = null;
if (isCalledAs("transform")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
String sourceSRS = null;
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
sourceSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
sourceSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
targetSRS = args[1].itemAt(0).getStringValue().trim();
geometry = indexWorker.transformGeometry(geometry, sourceSRS, targetSRS);
}
} else if (isCalledAs("WKTtoGML")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
String wkt = args[0].itemAt(0).getStringValue();
WKTReader wktReader = new WKTReader();
try {
geometry = wktReader.read(wkt);
} catch (ParseException e) {
logger.error(e.getMessage());
throw new XPathException(e);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
targetSRS = args[1].itemAt(0).getStringValue().trim();
}
} else if (isCalledAs("buffer")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
double distance = ((DoubleValue) args[1].itemAt(0)).getDouble();
int quadrantSegments = 8;
int endCapStyle = BufferOp.CAP_ROUND;
if (getArgumentCount() > 2 && Type.subTypeOf(args[2].itemAt(0).getType(), Type.INTEGER))
quadrantSegments = ((IntegerValue) args[2].itemAt(0)).getInt();
if (getArgumentCount() > 3 && Type.subTypeOf(args[3].itemAt(0).getType(), Type.INTEGER))
endCapStyle = ((IntegerValue) args[3].itemAt(0)).getInt();
switch(endCapStyle) {
case BufferOp.CAP_ROUND:
case BufferOp.CAP_BUTT:
case BufferOp.CAP_SQUARE:
// OK
break;
default:
{
logger.error("Invalid line end style");
throw new XPathException("Invalid line end style");
}
}
geometry = geometry.buffer(distance, quadrantSegments, endCapStyle);
}
} else if (isCalledAs("getBbox")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
geometry = geometry.getEnvelope();
}
} else if (isCalledAs("convexHull")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
geometry = geometry.convexHull();
}
} else if (isCalledAs("boundary")) {
if (args[0].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else {
NodeValue geometryNode = (NodeValue) args[0].itemAt(0);
// Try to get the geometry from the index
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
targetSRS = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, "SRS_NAME").getStringValue();
geometry = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode, false);
hasUsedIndex = true;
}
// Otherwise, build it
if (geometry == null) {
targetSRS = ((Element) geometryNode.getNode()).getAttribute("srsName").trim();
geometry = indexWorker.streamNodeToGeometry(context, geometryNode);
}
if (geometry == null) {
logger.error("Unable to get a geometry from the node");
throw new XPathException("Unable to get a geometry from the node");
}
geometry = geometry.getBoundary();
}
} else {
Geometry geometry1 = null;
Geometry geometry2 = null;
if (args[0].isEmpty() && args[1].isEmpty())
result = Sequence.EMPTY_SEQUENCE;
else if (!args[0].isEmpty() && args[1].isEmpty())
result = args[0].itemAt(0).toSequence();
else if (args[0].isEmpty() && !args[1].isEmpty())
result = args[1].itemAt(0).toSequence();
else {
NodeValue geometryNode1 = (NodeValue) args[0].itemAt(0);
NodeValue geometryNode2 = (NodeValue) args[1].itemAt(0);
String srsName1 = null;
String srsName2 = null;
// Try to get the geometries from the index
if (geometryNode1.getImplementationType() == NodeValue.PERSISTENT_NODE) {
srsName1 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode1, "SRS_NAME").getStringValue();
geometry1 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode1, false);
hasUsedIndex = true;
}
if (geometryNode2.getImplementationType() == NodeValue.PERSISTENT_NODE) {
srsName2 = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode2, "SRS_NAME").getStringValue();
geometry2 = indexWorker.getGeometryForNode(context.getBroker(), (NodeProxy) geometryNode2, false);
hasUsedIndex = true;
}
// Otherwise build them
if (geometry1 == null) {
srsName1 = ((Element) geometryNode1.getNode()).getAttribute("srsName").trim();
geometry1 = indexWorker.streamNodeToGeometry(context, geometryNode1);
}
if (geometry2 == null) {
srsName2 = ((Element) geometryNode2.getNode()).getAttribute("srsName").trim();
geometry2 = indexWorker.streamNodeToGeometry(context, geometryNode2);
}
if (geometry1 == null) {
logger.error("Unable to get a geometry from the first node");
throw new XPathException("Unable to get a geometry from the first node");
}
if (geometry2 == null) {
logger.error("Unable to get a geometry from the second node");
throw new XPathException("Unable to get a geometry from the second node");
}
if (srsName1 == null)
throw new XPathException("Unable to get a SRS for the first geometry");
if (srsName2 == null)
throw new XPathException("Unable to get a SRS for the second geometry");
// Transform the second geometry in the SRS of the first one if necessary
if (!srsName1.equalsIgnoreCase(srsName2)) {
geometry2 = indexWorker.transformGeometry(geometry2, srsName1, srsName2);
}
if (isCalledAs("intersection")) {
geometry = geometry1.intersection(geometry2);
} else if (isCalledAs("union")) {
geometry = geometry1.union(geometry2);
} else if (isCalledAs("difference")) {
geometry = geometry1.difference(geometry2);
} else if (isCalledAs("symetricDifference")) {
geometry = geometry1.symDifference(geometry2);
}
targetSRS = srsName1;
}
}
if (result == null) {
String gmlPrefix = context.getPrefixForURI(AbstractGMLJDBCIndexWorker.GML_NS);
if (gmlPrefix == null) {
logger.error("namespace is not defined:" + SpatialModule.PREFIX);
throw new XPathException("'" + AbstractGMLJDBCIndexWorker.GML_NS + "' namespace is not defined");
}
context.pushDocumentContext();
try {
MemTreeBuilder builder = context.getDocumentBuilder();
DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
result = (NodeValue) indexWorker.streamGeometryToElement(geometry, targetSRS, receiver);
} finally {
context.popDocumentContext();
}
}
} catch (SpatialIndexException e) {
logger.error(e.getMessage());
throw new XPathException(e);
}
return result;
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class HighlightMatches method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty())
return Sequence.EMPTY_SEQUENCE;
context.pushDocumentContext();
final Serializer serializer = context.getBroker().borrowSerializer();
try (FunctionReference func = (FunctionReference) args[1].itemAt(0)) {
MemTreeBuilder builder = context.getDocumentBuilder();
NGramIndexWorker index = (NGramIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(NGramIndex.ID);
DocumentBuilderReceiver docBuilder = new DocumentBuilderReceiver(builder);
MatchCallback matchCb = new MatchCallback(func, docBuilder);
ValueSequence result = new ValueSequence();
for (SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
NodeValue v = (NodeValue) i.nextItem();
try {
int nodeNr = builder.getDocument().getLastNode();
if (v.getImplementationType() == NodeValue.IN_MEMORY_NODE) {
((NodeImpl) v).copyTo(context.getBroker(), docBuilder);
} else {
NodeProxy p = (NodeProxy) v;
MatchListener ml = index.getMatchListener(context.getBroker(), p, matchCb);
Receiver receiver;
if (ml == null)
receiver = docBuilder;
else {
ml.setNextInChain(docBuilder);
receiver = ml;
}
serializer.setReceiver(receiver);
serializer.toReceiver((NodeProxy) v, false);
}
result.add(builder.getDocument().getNode(++nodeNr));
} catch (SAXException e) {
LOG.warn(e.getMessage(), e);
throw new XPathException(this, e.getMessage());
}
}
return result;
} finally {
context.getBroker().returnSerializer(serializer);
context.popDocumentContext();
}
}
Aggregations