use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.
the class XMLDBXUpdate method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection c, Sequence[] args, Sequence contextSequence) throws XPathException {
final NodeValue data = (NodeValue) args[1].itemAt(0);
final StringWriter writer = new StringWriter();
final Properties properties = new Properties();
properties.setProperty(OutputKeys.INDENT, "yes");
final DOMSerializer serializer = new ExtendedDOMSerializer(context.getBroker(), writer, properties);
try {
serializer.serialize(data.getNode());
} catch (final TransformerException e) {
logger.debug("Exception while serializing XUpdate document", e);
throw new XPathException(this, "Exception while serializing XUpdate document: " + e.getMessage(), e);
}
final String xupdate = writer.toString();
long modifications = 0;
try {
final XUpdateQueryService service = (XUpdateQueryService) c.getService("XUpdateQueryService", "1.0");
logger.debug("Processing XUpdate request: {}", xupdate);
modifications = service.update(xupdate);
} catch (final XMLDBException e) {
throw new XPathException(this, "Exception while processing xupdate: " + e.getMessage(), e);
}
context.getRootExpression().resetState(false);
return new IntegerValue(modifications);
}
use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.
the class JMXServlet method writeXmlData.
private void writeXmlData(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Element root = null;
final String operation = request.getParameter("operation");
if ("ping".equals(operation)) {
long timeout = 5000;
final String timeoutParam = request.getParameter("t");
if (StringUtils.isNotBlank(timeoutParam)) {
try {
timeout = Long.parseLong(timeoutParam);
} catch (final NumberFormatException e) {
throw new ServletException("timeout parameter needs to be a number. Got: " + timeoutParam);
}
}
final long responseTime = client.ping(BrokerPool.DEFAULT_INSTANCE_NAME, timeout);
if (responseTime == JMXtoXML.PING_TIMEOUT) {
root = client.generateXMLReport(String.format("no response on ping after %sms", timeout), new String[] { "sanity", "locking", "processes", "instances", "memory" });
} else {
root = client.generateXMLReport(null, new String[] { "sanity" });
}
} else if (operation != null && operation.length() > 0) {
final String mbean = request.getParameter("mbean");
if (mbean == null) {
throw new ServletException("to call an operation, you also need to specify parameter 'mbean'");
}
String[] args = request.getParameterValues("args");
try {
root = client.invoke(mbean, operation, args);
if (root == null) {
throw new ServletException("operation " + operation + " not found on " + mbean);
}
} catch (InstanceNotFoundException e) {
throw new ServletException("mbean " + mbean + " not found: " + e.getMessage(), e);
} catch (MalformedObjectNameException | IntrospectionException | ReflectionException | MBeanException e) {
throw new ServletException(e.getMessage(), e);
}
} else {
String[] categories = request.getParameterValues("c");
if (categories == null) {
categories = new String[] { "all" };
}
root = client.generateXMLReport(null, categories);
}
response.setContentType("application/xml");
final Object useAttribute = request.getAttribute("jmx.attribute");
if (useAttribute != null) {
request.setAttribute(useAttribute.toString(), root);
} else {
final Writer writer = new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8);
final DOMSerializer streamer = new DOMSerializer(writer, defaultProperties);
try {
streamer.serialize(root);
} catch (final TransformerException e) {
LOG.error(e.getMessageAndLocation());
throw new ServletException("Error while serializing result: " + e.getMessage(), e);
}
writer.flush();
}
}
use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.
the class DOMTest method documentBuilder.
@Test
public void documentBuilder() throws ParserConfigurationException, SAXException, IOException, TransformerException {
DocumentBuilderReceiver receiver = new DocumentBuilderReceiver();
SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
factory.setNamespaceAware(true);
XMLReader reader = factory.newSAXParser().getXMLReader();
reader.setContentHandler(receiver);
reader.parse(new InputSource(new StringReader(XML)));
Document doc = receiver.getDocument();
Node node = doc.getFirstChild();
assertNotNull(node);
StringWriter writer = new StringWriter();
DOMSerializer serializer = new DOMSerializer(writer, null);
serializer.serialize(node);
writer.toString();
}
use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.
the class JMXtoXML method generateReport.
/**
* Retrieve JMX output for the given categories and return a string of XML. Valid categories are "memory",
* "instances", "disk", "system", "caches", "locking", "processes", "sanity", "all".
*
* @param categories array of categories to include in the report
* @throws TransformerException in case of serialization errors
* @return string containing an XML report
*/
public String generateReport(final String[] categories) throws TransformerException {
final Element root = generateXMLReport(null, categories);
final StringWriter writer = new StringWriter();
final DOMSerializer streamer = new DOMSerializer(writer, defaultProperties);
streamer.serialize(root);
return writer.toString();
}
use of org.exist.util.serializer.DOMSerializer in project exist by eXist-db.
the class DOMSerializerTest method serialize.
@Test
public void serialize() throws ParserConfigurationException, IOException, SAXException, TransformerException, URISyntaxException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
assertNotNull(factory);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
assertNotNull(builder);
try (final InputStream is = SAMPLES.getBiblioSample()) {
Document doc = builder.parse(new InputSource(is));
assertNotNull(doc);
try (final StringWriter writer = new StringWriter()) {
DOMSerializer serializer = new DOMSerializer(writer, null);
serializer.serialize(doc.getDocumentElement());
}
}
}
Aggregations