use of org.exist.storage.serializers.Serializer 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.storage.serializers.Serializer in project exist by eXist-db.
the class Modification method deepCopy.
protected Sequence deepCopy(Sequence inSeq) throws XPathException {
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
final Serializer serializer = context.getBroker().borrowSerializer();
serializer.setReceiver(receiver);
try {
final Sequence out = new ValueSequence();
for (final SequenceIterator i = inSeq.iterate(); i.hasNext(); ) {
Item item = i.nextItem();
if (item.getType() == Type.DOCUMENT) {
if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
final NodeHandle root = (NodeHandle) ((NodeProxy) item).getOwnerDocument().getDocumentElement();
item = new NodeProxy(root);
} else {
item = (Item) ((Document) item).getDocumentElement();
}
}
if (Type.subTypeOf(item.getType(), Type.NODE)) {
if (((NodeValue) item).getImplementationType() == NodeValue.PERSISTENT_NODE) {
final int last = builder.getDocument().getLastNode();
final NodeProxy p = (NodeProxy) item;
serializer.toReceiver(p, false, false);
if (p.getNodeType() == Node.ATTRIBUTE_NODE) {
item = builder.getDocument().getLastAttr();
} else {
item = builder.getDocument().getNode(last + 1);
}
} else {
((org.exist.dom.memtree.NodeImpl) item).deepCopy();
}
}
out.add(item);
}
return out;
} catch (final SAXException | DOMException e) {
throw new XPathException(this, e.getMessage(), e);
} finally {
context.getBroker().returnSerializer(serializer);
context.popDocumentContext();
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class StoreResourceTest method replaceXmlDoc.
private void replaceXmlDoc(final Subject execAsUser, final DBBroker.PreserveType preserve, final XmldbURI docName, final String content) throws EXistException, PermissionDeniedException, LockException, IOException, SAXException {
final XmldbURI uri = TEST_COLLECTION_URI.append(docName);
final BrokerPool pool = existWebServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final Txn transaction = pool.getTransactionManager().beginTransaction();
final Collection col = broker.openCollection(uri.removeLastSegment(), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, uri.lastSegment(), new StringInputSource(content), MimeType.XML_TYPE, col);
transaction.commit();
}
// check the replaced document is correct
try (final DBBroker broker = pool.get(Optional.of(execAsUser));
final LockedDocument lockedDoc = broker.getXMLResource(uri, Lock.LockMode.READ_LOCK)) {
final Serializer serializer = broker.borrowSerializer();
try {
final String docXml = serializer.serialize(lockedDoc.getDocument());
final Diff diff = DiffBuilder.compare(Input.fromString(content)).withTest(Input.fromString(docXml)).build();
assertFalse(diff.toString(), diff.hasDifferences());
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class DebuggeeJointImpl method evalution.
@Override
public String evalution(String script) throws Exception {
Database db = compiledXQuery.getContext().getDatabase();
// TODO: account required
try (final DBBroker broker = db.getBroker()) {
XQueryContext context = compiledXQuery.getContext().copyContext();
context.setDebuggeeJoint(null);
context.undeclareGlobalVariable(Debuggee.SESSION);
context.undeclareGlobalVariable(Debuggee.IDEKEY);
XQuery service = broker.getBrokerPool().getXQueryService();
CompiledXQuery compiled = service.compile(broker, context, script);
Sequence resultSequence = service.execute(broker, compiled, null);
SAXSerializer sax = null;
Serializer serializer = broker.getSerializer();
serializer.reset();
try {
sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
Properties outputProps = new Properties();
StringWriter writer = new StringWriter();
sax.setOutput(writer, outputProps);
serializer.setSAXHandlers(sax, sax);
for (SequenceIterator i = resultSequence.iterate(); i.hasNext(); ) {
Item next = i.nextItem();
if (Type.subTypeOf(next.getType(), Type.NODE))
serializer.toSAX((NodeValue) next);
else
writer.write(next.getStringValue());
}
return writer.toString();
} finally {
if (sax != null) {
SerializerPool.getInstance().returnObject(sax);
}
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class PropertyGet method getPropertyString.
protected static StringBuilder getPropertyString(Variable variable, XQueryContext context) {
Sequence value = variable.getValue();
Database db = context.getDatabase();
try (final DBBroker broker = db.getBroker()) {
Serializer serializer = broker.getSerializer();
serializer.reset();
try {
StringBuilder property = new StringBuilder();
if (value.hasOne()) {
String strVal = getPropertyValue(value.itemAt(0), serializer);
String type = Type.subTypeOf(value.getItemType(), Type.NODE) ? "node" : Type.getTypeName(value.getItemType());
property.append("<property name=\"");
property.append(variable.getQName().toString());
property.append("\" fullname=\"");
property.append(variable.getQName().toString());
property.append("\" type=\"");
property.append(type);
property.append("\" size=\"");
property.append(strVal.length());
property.append("\" encoding=\"none\">");
property.append(strVal);
property.append("</property>");
} else {
property.append("<property name=\"");
property.append(variable.getQName().toString());
property.append("\" fullname=\"");
property.append(variable.getQName().toString());
property.append("\" type=\"array\" children=\"true\" numchildren=\"");
property.append(value.getItemCount());
property.append("\">");
int idx = 0;
for (SequenceIterator si = value.iterate(); si.hasNext(); idx++) {
Item item = si.nextItem();
String strVal = getPropertyValue(item, serializer);
String type = Type.subTypeOf(value.getItemType(), Type.NODE) ? "xs:string" : Type.getTypeName(value.getItemType());
property.append("<property name=\"");
property.append(idx);
property.append("\" type=\"");
property.append(type);
property.append("\" size=\"");
property.append(strVal.length());
property.append("\" encoding=\"none\">");
property.append(strVal);
property.append("</property>");
}
property.append("</property>");
}
return property;
} catch (SAXException e) {
e.printStackTrace();
} catch (XPathException e) {
e.printStackTrace();
}
} catch (EXistException e) {
e.printStackTrace();
}
return null;
}
Aggregations