use of org.exist.util.serializer.DOMStreamer in project exist by eXist-db.
the class LocalXMLResource method getContentAsSAX.
@Override
public void getContentAsSAX(final ContentHandler handler) throws XMLDBException {
// case 1: content is an external DOM node
if (root != null && !(root instanceof NodeValue)) {
try {
final String option = collection.getProperty(Serializer.GENERATE_DOC_EVENTS, "false");
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
streamer.setContentHandler(handler);
streamer.setLexicalHandler(lexicalHandler);
streamer.serialize(root, option.equalsIgnoreCase("true"));
} finally {
SerializerPool.getInstance().returnObject(streamer);
}
} catch (final Exception e) {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, e.getMessage(), e);
}
} else {
withDb((broker, transaction) -> {
try {
// case 2: content is an atomic value
if (value != null) {
value.toSAX(broker, handler, getProperties());
// case 3: content is an internal node or a document
} else {
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setUser(user);
serializer.setProperties(getProperties());
serializer.setSAXHandlers(handler, lexicalHandler);
if (root != null) {
serializer.toSAX((NodeValue) root);
} else if (proxy != null) {
serializer.toSAX(proxy);
} else {
read(broker, transaction).apply((document, broker1, transaction1) -> {
try {
serializer.toSAX(document);
return null;
} catch (final SAXException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
}
} finally {
broker.returnSerializer(serializer);
}
}
return null;
} catch (final SAXException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
}
}
use of org.exist.util.serializer.DOMStreamer in project exist by eXist-db.
the class MutableCollection method validateXMLResource.
@Deprecated
@Override
public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final Node node) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
return validateXMLResourceInternal(transaction, broker, name, getConfiguration(broker), (info) -> {
info.setDOMStreamer(new DOMStreamer());
info.getDOMStreamer().serialize(node, true);
});
}
use of org.exist.util.serializer.DOMStreamer 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);
}
}
Aggregations