use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.
the class XqueryApiTest method serialize.
private String serialize(final Sequence sequence) throws ApiException {
try (final DBBroker broker = server.getBrokerPool().getBroker();
final StringWriter writer = new StringWriter()) {
final XQuerySerializer serializer = new XQuerySerializer(broker, new Properties(), writer);
serializer.serialize(sequence);
return writer.toString();
} catch (final EXistException | IOException | SAXException | XPathException e) {
throw new ApiException(e.getMessage(), e);
}
}
use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.
the class LogFunction method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
// Force adaptive serialization
final Properties props = new Properties();
props.setProperty(OutputKeys.METHOD, "adaptive");
SequenceIterator i;
final String calledAs = getName().getLocalPart();
// Find query parameter with actual data
switch(calledAs) {
case FUNCTION_LOG:
i = args[1].unorderedIterator();
if (args[1].isEmpty()) {
return (Sequence.EMPTY_SEQUENCE);
}
break;
case FUNCTION_LOGAPP:
i = args[2].unorderedIterator();
if (args[2].isEmpty()) {
return (Sequence.EMPTY_SEQUENCE);
}
break;
default:
i = args[0].unorderedIterator();
if (args[0].isEmpty()) {
return (Sequence.EMPTY_SEQUENCE);
}
break;
}
// Add line of the log statement
final StringBuilder buf = new StringBuilder();
buf.append("(");
buf.append("Line: ");
buf.append(this.getLine());
// Add source information to the log statement when provided
if (getSource() != null) {
buf.append(' ').append(getSource().pathOrShortIdentifier());
}
buf.append(") ");
// Iterate over all input
while (i.hasNext()) {
final Item next = i.nextItem();
if (next instanceof StringValue) {
// Use simple string value
buf.append(next.getStringValue());
} else {
// Serialize data
try (StringWriter writer = new StringWriter()) {
XQuerySerializer xqs = new XQuerySerializer(context.getBroker(), props, writer);
xqs.serialize(next.toSequence());
buf.append(writer.toString());
} catch (IOException | SAXException e) {
throw new XPathException(this, e.getMessage());
}
}
}
// Finally write the log
switch(calledAs) {
case FUNCTION_LOG:
final String loglevel = args[0].getStringValue().toLowerCase();
writeLog(buf, loglevel, logger);
break;
case FUNCTION_LOG_SYSTEM_OUT:
System.out.println(buf);
break;
case FUNCTION_LOG_SYSTEM_ERR:
System.err.println(buf);
break;
case FUNCTION_LOGAPP:
{
final String loglevelapp = args[0].getStringValue().toLowerCase();
final String logname = args[1].getStringValue();
// Use specific logger when provided
final Logger logger = (logname == null || logname.isEmpty()) ? LOG : LogManager.getLogger(logname);
writeLog(buf, loglevelapp, logger);
break;
}
}
return (Sequence.EMPTY_SEQUENCE);
}
use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.
the class RESTServer method writeResultXML.
private void writeResultXML(final HttpServletResponse response, final DBBroker broker, final Sequence results, final int howmany, final int start, final boolean typed, final Properties outputProperties, final boolean wrap, final long compilationTime, final long executionTime) throws BadRequestException {
// serialize the results to the response output stream
outputProperties.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
try {
// set output headers
final String encoding = outputProperties.getProperty(OutputKeys.ENCODING);
if (!response.containsHeader("Content-Type")) {
String mimeType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
if (mimeType != null) {
final int semicolon = mimeType.indexOf(';');
if (semicolon != Constants.STRING_NOT_FOUND) {
mimeType = mimeType.substring(0, semicolon);
}
if (wrap) {
mimeType = "application/xml";
}
response.setContentType(mimeType + "; charset=" + encoding);
}
}
if (wrap) {
outputProperties.setProperty("method", "xml");
}
final Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
final XQuerySerializer serializer = new XQuerySerializer(broker, outputProperties, writer);
// Marshaller.marshall(broker, results, start, howmany, serializer.getContentHandler());
serializer.serialize(results, start, howmany, wrap, typed, compilationTime, executionTime);
writer.flush();
writer.close();
} catch (final SAXException e) {
LOG.warn(e);
throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
} catch (final Exception e) {
LOG.warn(e.getMessage(), e);
throw new BadRequestException("Error while serializing xml: " + e.toString(), e);
}
}
use of org.exist.util.serializer.XQuerySerializer in project exist by eXist-db.
the class EXistSequence method serialize.
@Override
public void serialize(final OutputStream out, final SerialParameters params) throws ToolsException {
final Properties props = params == null ? null : makeOutputProperties(params);
props.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
final String encoding = props.getProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
try (final Writer writer = new OutputStreamWriter(new CloseShieldOutputStream(out), encoding)) {
final XQuerySerializer xqSerializer = new XQuerySerializer(context.getBroker(), props, writer);
xqSerializer.serialize(sequence);
} catch (final SAXException | IOException | XPathException e) {
throw new ToolsException("A problem occurred while serializing the node set: " + e.getMessage(), e);
}
}
Aggregations