use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class FunGeometricProperties method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Sequence result = null;
Sequence nodes = args[0];
if (nodes.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
try {
Geometry geometry = null;
String sourceCRS = null;
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");
}
String propertyName = null;
if (isCalledAs("getWKT")) {
propertyName = "WKT";
} else if (isCalledAs("getWKB")) {
propertyName = "WKB";
} else if (isCalledAs("getMinX")) {
propertyName = "MINX";
} else if (isCalledAs("getMaxX")) {
propertyName = "MAXX";
} else if (isCalledAs("getMinY")) {
propertyName = "MINY";
} else if (isCalledAs("getMaxY")) {
propertyName = "MAXY";
} else if (isCalledAs("getCentroidX")) {
propertyName = "CENTROID_X";
} else if (isCalledAs("getCentroidY")) {
propertyName = "CENTROID_Y";
} else if (isCalledAs("getArea")) {
propertyName = "AREA";
} else if (isCalledAs("getEPSG4326WKT")) {
propertyName = "EPSG4326_WKT";
} else if (isCalledAs("getEPSG4326WKB")) {
propertyName = "EPSG4326_WKB";
} else if (isCalledAs("getEPSG4326MinX")) {
propertyName = "EPSG4326_MINX";
} else if (isCalledAs("getEPSG4326MaxX")) {
propertyName = "EPSG4326_MAXX";
} else if (isCalledAs("getEPSG4326MinY")) {
propertyName = "EPSG4326_MINY";
} else if (isCalledAs("getEPSG4326MaxY")) {
propertyName = "EPSG4326_MAXY";
} else if (isCalledAs("getEPSG4326CentroidX")) {
propertyName = "EPSG4326_CENTROID_X";
} else if (isCalledAs("getEPSG4326CentroidY")) {
propertyName = "EPSG4326_CENTROID_Y";
} else if (isCalledAs("getEPSG4326Area")) {
propertyName = "EPSG4326_AREA";
} else if (isCalledAs("getSRS")) {
propertyName = "SRS_NAME";
} else if (isCalledAs("getGeometryType")) {
propertyName = "GEOMETRY_TYPE";
} else if (isCalledAs("isClosed")) {
propertyName = "IS_CLOSED";
} else if (isCalledAs("isSimple")) {
propertyName = "IS_SIMPLE";
} else if (isCalledAs("isValid")) {
propertyName = "IS_VALID";
} else {
logger.error("Unknown spatial property: {}", getName().getLocalPart());
throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
}
NodeValue geometryNode = (NodeValue) nodes.itemAt(0);
if (geometryNode.getImplementationType() == NodeValue.PERSISTENT_NODE) {
// The node should be indexed : get its property
result = indexWorker.getGeometricPropertyForNode(context, (NodeProxy) geometryNode, propertyName);
hasUsedIndex = true;
} else {
// builds the geometry
sourceCRS = ((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");
}
// Transform the geometry to EPSG:4326 if relevant
if (propertyName.contains("EPSG4326")) {
geometry = indexWorker.transformGeometry(geometry, sourceCRS, "EPSG:4326");
if (isCalledAs("getEPSG4326WKT")) {
result = new StringValue(wktWriter.write(geometry));
} else if (isCalledAs("getEPSG4326WKB")) {
byte[] data = wkbWriter.write(geometry);
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
} else if (isCalledAs("getEPSG4326MinX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
} else if (isCalledAs("getEPSG4326MaxX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
} else if (isCalledAs("getEPSG4326MinY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
} else if (isCalledAs("getEPSG4326MaxY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
} else if (isCalledAs("getEPSG4326CentroidX")) {
result = new DoubleValue(geometry.getCentroid().getX());
} else if (isCalledAs("getEPSG4326CentroidY")) {
result = new DoubleValue(geometry.getCentroid().getY());
} else if (isCalledAs("getEPSG4326Area")) {
result = new DoubleValue(geometry.getArea());
}
} else if (isCalledAs("getWKT")) {
result = new StringValue(wktWriter.write(geometry));
} else if (isCalledAs("getWKB")) {
byte[] data = wkbWriter.write(geometry);
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream(data));
} else if (isCalledAs("getMinX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinX());
} else if (isCalledAs("getMaxX")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxX());
} else if (isCalledAs("getMinY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMinY());
} else if (isCalledAs("getMaxY")) {
result = new DoubleValue(geometry.getEnvelopeInternal().getMaxY());
} else if (isCalledAs("getCentroidX")) {
result = new DoubleValue(geometry.getCentroid().getX());
} else if (isCalledAs("getCentroidY")) {
result = new DoubleValue(geometry.getCentroid().getY());
} else if (isCalledAs("getArea")) {
result = new DoubleValue(geometry.getArea());
} else if (isCalledAs("getSRS")) {
result = new StringValue(((Element) geometryNode).getAttribute("srsName"));
} else if (isCalledAs("getGeometryType")) {
result = new StringValue(geometry.getGeometryType());
} else if (isCalledAs("isClosed")) {
result = new BooleanValue(!geometry.isEmpty());
} else if (isCalledAs("isSimple")) {
result = new BooleanValue(geometry.isSimple());
} else if (isCalledAs("isValid")) {
result = new BooleanValue(geometry.isValid());
} else {
logger.error("Unknown spatial property: {}", getName().getLocalPart());
throw new XPathException("Unknown spatial property: " + getName().getLocalPart());
}
}
} catch (SpatialIndexException e) {
logger.error(e.getMessage());
throw new XPathException(e);
}
}
return result;
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class ElementImpl method readNamespaceDecls.
public static void readNamespaceDecls(final List<String[]> namespaces, final Value value, final DocumentImpl document, final NodeId nodeId) {
final byte[] data = value.data();
int offset = value.start();
final int end = offset + value.getLength();
final byte idSizeType = (byte) (data[offset] & 0x03);
final boolean hasNamespace = (data[offset] & 0x10) == 0x10;
offset += StoredNode.LENGTH_SIGNATURE_LENGTH;
offset += LENGTH_ELEMENT_CHILD_COUNT;
offset += NodeId.LENGTH_NODE_ID_UNITS;
offset += nodeId.size();
offset += LENGTH_ATTRIBUTES_COUNT;
offset += Signatures.getLength(idSizeType);
if (hasNamespace) {
offset += LENGTH_NS_ID;
int prefixLen = ByteConversion.byteToShort(data, offset);
offset += LENGTH_PREFIX_LENGTH;
offset += prefixLen;
}
if (end > offset) {
final byte[] pfxData = new byte[end - offset];
System.arraycopy(data, offset, pfxData, 0, end - offset);
final InputStream bin = new UnsynchronizedByteArrayInputStream(pfxData);
final DataInputStream in = new DataInputStream(bin);
try {
final short prefixCount = in.readShort();
String prefix;
short nsId;
for (int i = 0; i < prefixCount; i++) {
prefix = in.readUTF();
nsId = in.readShort();
namespaces.add(new String[] { prefix, document.getBrokerPool().getSymbols().getNamespace(nsId) });
}
} catch (final IOException e) {
LOG.error(e);
}
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class XPathUtil method javaObjectToXPath.
public static final Sequence javaObjectToXPath(Object obj, XQueryContext context, boolean expandChars) throws XPathException {
if (obj == null) {
// return Sequence.EMPTY_SEQUENCE;
return null;
} else if (obj instanceof Sequence) {
return (Sequence) obj;
} else if (obj instanceof String) {
final StringValue v = new StringValue((String) obj);
return (expandChars ? v.expand() : v);
} else if (obj instanceof Boolean) {
return BooleanValue.valueOf(((Boolean) obj));
} else if (obj instanceof Float) {
return new FloatValue(((Float) obj));
} else if (obj instanceof Double) {
return new DoubleValue(((Double) obj));
} else if (obj instanceof Short) {
return new IntegerValue(((Short) obj), Type.SHORT);
} else if (obj instanceof Integer) {
return new IntegerValue(((Integer) obj), Type.INT);
} else if (obj instanceof Long) {
return new IntegerValue(((Long) obj), Type.LONG);
} else if (obj instanceof byte[]) {
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new UnsynchronizedByteArrayInputStream((byte[]) obj));
} else if (obj instanceof ResourceSet) {
final Sequence seq = new AVLTreeNodeSet();
try {
final DBBroker broker = context.getBroker();
for (final ResourceIterator it = ((ResourceSet) obj).getIterator(); it.hasMoreResources(); ) {
seq.add(getNode(broker, (XMLResource) it.nextResource()));
}
} catch (final XMLDBException xe) {
throw new XPathException("Failed to convert ResourceSet to node: " + xe.getMessage());
}
return seq;
} else if (obj instanceof XMLResource) {
return getNode(context.getBroker(), (XMLResource) obj);
} else if (obj instanceof Node) {
context.pushDocumentContext();
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
streamer.setContentHandler(receiver);
streamer.serialize((Node) obj, false);
if (obj instanceof Document) {
return builder.getDocument();
} else {
return builder.getDocument().getNode(1);
}
} catch (final SAXException e) {
throw new XPathException("Failed to transform node into internal model: " + e.getMessage());
} finally {
context.popDocumentContext();
SerializerPool.getInstance().returnObject(streamer);
}
} else if (obj instanceof List<?>) {
boolean createNodeSequence = true;
for (Object next : ((List<?>) obj)) {
if (!(next instanceof NodeProxy)) {
createNodeSequence = false;
break;
}
}
Sequence seq = createNodeSequence ? new AVLTreeNodeSet() : new ValueSequence();
for (Object o : ((List<?>) obj)) {
seq.add((Item) javaObjectToXPath(o, context, expandChars));
}
return seq;
} else if (obj instanceof NodeList) {
context.pushDocumentContext();
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
streamer.setContentHandler(receiver);
final ValueSequence seq = new ValueSequence();
final NodeList nl = (NodeList) obj;
int last = builder.getDocument().getLastNode();
for (int i = 0; i < nl.getLength(); i++) {
final Node n = nl.item(i);
streamer.serialize(n, false);
final NodeImpl created = builder.getDocument().getNode(last + 1);
seq.add(created);
last = builder.getDocument().getLastNode();
}
return seq;
} catch (final SAXException e) {
throw new XPathException("Failed to transform node into internal model: " + e.getMessage());
} finally {
context.popDocumentContext();
SerializerPool.getInstance().returnObject(streamer);
}
} else if (obj instanceof Object[]) {
boolean createNodeSequence = true;
final Object[] array = (Object[]) obj;
for (Object arrayItem : array) {
if (!(arrayItem instanceof NodeProxy)) {
createNodeSequence = false;
break;
}
}
Sequence seq = createNodeSequence ? new AVLTreeNodeSet() : new ValueSequence();
for (Object arrayItem : array) {
seq.add((Item) javaObjectToXPath(arrayItem, context, expandChars));
}
return seq;
} else {
return new JavaObjectValue(obj);
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class DocumentImplTest method checkNamespaces_exist.
@Test
public void checkNamespaces_exist() throws IOException, SAXException, ParserConfigurationException {
final DocumentImpl doc;
try (final InputStream is = new UnsynchronizedByteArrayInputStream(DOC_WITH_NAMESPACES.getBytes(UTF_8))) {
doc = parseExist(is);
}
final ElementImpl elem = (ElementImpl) doc.getDocumentElement();
final NamedNodeMap attrs = elem.getAttributes();
assertEquals(1, attrs.getLength());
// assertEquals(2, attrs.getLength());
int index = 0;
final Attr attr1 = (Attr) attrs.item(index++);
assertEquals(NodeImpl.NAMESPACE_NODE, attr1.getNodeType());
assertTrue(attr1 instanceof NamespaceNode);
assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, attr1.getNamespaceURI());
assertEquals(null, attr1.getPrefix());
assertEquals(XMLConstants.XMLNS_ATTRIBUTE, attr1.getLocalName());
assertEquals(XMLConstants.XMLNS_ATTRIBUTE, attr1.getNodeName());
assertEquals("http://exist-db.org/xquery/repo", attr1.getValue());
// final Attr attr2 = (Attr)attrs.item(index++);
// assertEquals(NodeImpl.NAMESPACE_NODE, attr2.getNodeType());
// assertTrue(attr2 instanceof NamespaceNode);
// assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, attr2.getNamespaceURI());
// assertEquals(XMLConstants.XMLNS_ATTRIBUTE, attr2.getPrefix());
// assertEquals("repo", attr2.getLocalName());
// assertEquals(XMLConstants.XMLNS_ATTRIBUTE + ":repo", attr2.getNodeName());
// assertEquals("http://exist-db.org/xquery/repo", attr2.getValue());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class MemtreeTest method getTreeLevel.
@Test
public void getTreeLevel() throws IOException, ParserConfigurationException, SAXException {
final DocumentImpl doc;
try (final InputStream is = new UnsynchronizedByteArrayInputStream(XML.getBytes(UTF_8))) {
doc = parse(is);
}
// depth of the document node
assertEquals(0, doc.getTreeLevel(0));
// depth of <!-- comment before doc 1 -->
assertEquals(1, doc.getTreeLevel(1));
// depth of <?pi-before-doc-1?>
assertEquals(1, doc.getTreeLevel(2));
// depth of <!-- comment before doc 2 -->
assertEquals(1, doc.getTreeLevel(3));
// depth of <?pi-before-doc-2?>
assertEquals(1, doc.getTreeLevel(4));
// depth of doc-element
assertEquals(1, doc.getTreeLevel(5));
// depth of doc-element/text()[1]
assertEquals(2, doc.getTreeLevel(6));
// depth of <!-- comment before e1 -->
assertEquals(2, doc.getTreeLevel(7));
// depth of doc-element/text()[2]
assertEquals(2, doc.getTreeLevel(8));
// depth of e1
assertEquals(2, doc.getTreeLevel(9));
// depth of e1/text()[1]
assertEquals(3, doc.getTreeLevel(10));
// depth of <?pi-before-e1_1?>
assertEquals(3, doc.getTreeLevel(11));
// depth of e1/text()[2]
assertEquals(3, doc.getTreeLevel(12));
// depth of e1_1
assertEquals(3, doc.getTreeLevel(13));
// depth of e1_1/text()[1]
assertEquals(4, doc.getTreeLevel(14));
// depth of e1/text()[3]
assertEquals(3, doc.getTreeLevel(15));
// depth of e1_2
assertEquals(3, doc.getTreeLevel(16));
// depth of e1_2/text()[1]
assertEquals(4, doc.getTreeLevel(17));
// depth of e1/text()[4]
assertEquals(3, doc.getTreeLevel(18));
// depth of doc-element/text()[3]
assertEquals(2, doc.getTreeLevel(19));
// depth of <!-- comment after e1 -->
assertEquals(2, doc.getTreeLevel(20));
// depth of doc-element/text()[4]
assertEquals(2, doc.getTreeLevel(21));
// depth of <?pi-after-doc-1?>
assertEquals(1, doc.getTreeLevel(22));
// depth of <!-- comment after doc 1 -->
assertEquals(1, doc.getTreeLevel(23));
// depth of <?pi-after-doc-2?>
assertEquals(1, doc.getTreeLevel(24));
// depth of <!-- comment after doc 2 -->
assertEquals(1, doc.getTreeLevel(25));
// depth of <?pi-after-doc-3?>
assertEquals(1, doc.getTreeLevel(26));
}
Aggregations