use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class XQueryUpdateTest method append.
@Test
public void append() throws EXistException, PermissionDeniedException, XPathException, SAXException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
String query = " declare variable $i external;\n" + " update insert\n" + " <product id='id{$i}' num='{$i}'>\n" + " <description>Description {$i}</description>\n" + " <price>{$i + 1.0}</price>\n" + " <stock>{$i * 10}</stock>\n" + " </product>\n" + " into /products";
XQueryContext context = new XQueryContext(pool);
CompiledXQuery compiled = xquery.compile(context, query);
for (int i = 0; i < ITEMS_TO_APPEND; i++) {
context.declareVariable("i", Integer.valueOf(i));
xquery.execute(broker, compiled, null);
}
Sequence seq = xquery.execute(broker, "/products", null);
assertEquals(seq.getItemCount(), 1);
final Serializer serializer = broker.borrowSerializer();
try {
serializer.serialize((NodeValue) seq.itemAt(0));
} finally {
broker.returnSerializer(serializer);
}
seq = xquery.execute(broker, "//product", null);
assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
seq = xquery.execute(broker, "//product[price > 0.0]", null);
assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class XQueryUpdateTest method appendAttributes.
@Test
public void appendAttributes() throws EXistException, PermissionDeniedException, XPathException, SAXException, LockException, IOException {
append();
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
XQuery xquery = pool.getXQueryService();
String query = " declare variable $i external;\n" + " update insert\n" + " attribute name { concat('n', $i) }\n" + " into //product[@num = $i]";
XQueryContext context = new XQueryContext(pool);
CompiledXQuery compiled = xquery.compile(context, query);
for (int i = 0; i < ITEMS_TO_APPEND; i++) {
context.declareVariable("i", Integer.valueOf(i));
xquery.execute(broker, compiled, null);
}
Sequence seq = xquery.execute(broker, "/products", null);
assertEquals(seq.getItemCount(), 1);
final Serializer serializer = broker.borrowSerializer();
try {
serializer.serialize((NodeValue) seq.itemAt(0));
seq = xquery.execute(broker, "//product", null);
assertEquals(ITEMS_TO_APPEND, seq.getItemCount());
seq = xquery.execute(broker, "//product[@name = 'n20']", null);
assertEquals(1, seq.getItemCount());
store(broker, "attribs.xml", "<test attr1='aaa' attr2='bbb'>ccc</test>");
query = "update insert attribute attr1 { 'eee' } into /test";
// testing duplicate attribute ...
xquery.execute(broker, query, null);
seq = xquery.execute(broker, "doc('" + TEST_COLLECTION + "/attribs.xml')/test[@attr1 = 'eee']", null);
assertEquals(1, seq.getItemCount());
serializer.serialize((NodeValue) seq.itemAt(0));
} finally {
broker.returnSerializer(serializer);
}
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class MatchListenerTest method queryResult2String.
private String queryResult2String(DBBroker broker, Sequence seq, int index) throws SAXException, XPathException {
Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, "no");
props.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setProperties(props);
return serializer.serialize((NodeValue) seq.itemAt(index));
} finally {
broker.returnSerializer(serializer);
}
}
use of org.exist.storage.serializers.Serializer in project exist by eXist-db.
the class LuceneMatchListenerTest method queryResult2String.
private String queryResult2String(final DBBroker broker, final Sequence seq, final boolean indent) throws SAXException, XPathException {
final Properties props = new Properties();
props.setProperty(OutputKeys.INDENT, indent ? "yes" : "no");
props.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setProperties(props);
return serializer.serialize((NodeValue) seq.itemAt(0));
} finally {
broker.returnSerializer(serializer);
}
}
use of org.exist.storage.serializers.Serializer 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