Search in sources :

Example 1 with DOMSerializer

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);
}
Also used : ExtendedDOMSerializer(org.exist.util.serializer.ExtendedDOMSerializer) NodeValue(org.exist.xquery.value.NodeValue) ExtendedDOMSerializer(org.exist.util.serializer.ExtendedDOMSerializer) DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) XUpdateQueryService(org.xmldb.api.modules.XUpdateQueryService) XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) XMLDBException(org.xmldb.api.base.XMLDBException) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException)

Example 2 with DOMSerializer

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();
    }
}
Also used : ServletException(javax.servlet.ServletException) DOMSerializer(org.exist.util.serializer.DOMSerializer) Element(org.w3c.dom.Element) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 3 with DOMSerializer

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();
}
Also used : InputSource(org.xml.sax.InputSource) DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) Node(org.w3c.dom.Node) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) ExistSAXParserFactory(org.exist.util.ExistSAXParserFactory) Test(org.junit.Test)

Example 4 with DOMSerializer

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();
}
Also used : DOMSerializer(org.exist.util.serializer.DOMSerializer) StringWriter(java.io.StringWriter) Element(org.w3c.dom.Element)

Example 5 with DOMSerializer

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());
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMSerializer(org.exist.util.serializer.DOMSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

DOMSerializer (org.exist.util.serializer.DOMSerializer)8 StringWriter (java.io.StringWriter)6 TransformerException (javax.xml.transform.TransformerException)4 Properties (java.util.Properties)3 Test (org.junit.Test)3 Document (org.w3c.dom.Document)3 OutputStreamWriter (java.io.OutputStreamWriter)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 InputSource (org.xml.sax.InputSource)2 XMLDBException (org.xmldb.api.base.XMLDBException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 Writer (java.io.Writer)1 ServletException (javax.servlet.ServletException)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 SAXParserFactory (javax.xml.parsers.SAXParserFactory)1