Search in sources :

Example 1 with CharacterData

use of org.dom4j.CharacterData in project hudson-2.x by hudson.

the class Api method doXml.

/**
     * Exposes the bean as XML.
     */
public void doXml(StaplerRequest req, StaplerResponse rsp, @QueryParameter String xpath, @QueryParameter String wrapper, @QueryParameter int depth) throws IOException, ServletException {
    String[] excludes = req.getParameterValues("exclude");
    if (xpath == null && excludes == null) {
        // serve the whole thing
        rsp.serveExposedBean(req, bean, Flavor.XML);
        return;
    }
    StringWriter sw = new StringWriter();
    // first write to String
    Model p = MODEL_BUILDER.get(bean.getClass());
    p.writeTo(bean, depth, Flavor.XML.createDataWriter(bean, sw));
    // apply XPath
    Object result;
    try {
        Document dom = new SAXReader().read(new StringReader(sw.toString()));
        // apply exclusions
        if (excludes != null) {
            for (String exclude : excludes) {
                List<org.dom4j.Node> list = (List<org.dom4j.Node>) dom.selectNodes(exclude);
                for (org.dom4j.Node n : list) {
                    Element parent = n.getParent();
                    if (parent != null)
                        parent.remove(n);
                }
            }
        }
        if (xpath == null) {
            result = dom;
        } else {
            List list = dom.selectNodes(xpath);
            if (wrapper != null) {
                Element root = DocumentFactory.getInstance().createElement(wrapper);
                for (Object o : list) {
                    if (o instanceof String) {
                        root.addText(o.toString());
                    } else {
                        root.add(((org.dom4j.Node) o).detach());
                    }
                }
                result = root;
            } else if (list.isEmpty()) {
                rsp.setStatus(HttpServletResponse.SC_NOT_FOUND);
                rsp.getWriter().print(Messages.Api_NoXPathMatch(xpath));
                return;
            } else if (list.size() > 1) {
                rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                rsp.getWriter().print(Messages.Api_MultipleMatch(xpath, list.size()));
                return;
            } else {
                result = list.get(0);
            }
        }
    } catch (DocumentException e) {
        throw new IOException2(e);
    }
    OutputStream o = rsp.getCompressedOutputStream(req);
    try {
        if (result instanceof CharacterData) {
            rsp.setContentType("text/plain;charset=UTF-8");
            o.write(((CharacterData) result).getText().getBytes("UTF-8"));
            return;
        }
        if (result instanceof String || result instanceof Number || result instanceof Boolean) {
            rsp.setContentType("text/plain;charset=UTF-8");
            o.write(result.toString().getBytes("UTF-8"));
            return;
        }
        // otherwise XML
        rsp.setContentType("application/xml;charset=UTF-8");
        new XMLWriter(o).write(result);
    } finally {
        o.close();
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) OutputStream(java.io.OutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) StringWriter(java.io.StringWriter) CharacterData(org.dom4j.CharacterData) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) List(java.util.List) IOException2(hudson.util.IOException2)

Aggregations

IOException2 (hudson.util.IOException2)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 List (java.util.List)1 CharacterData (org.dom4j.CharacterData)1 Document (org.dom4j.Document)1 DocumentException (org.dom4j.DocumentException)1 Element (org.dom4j.Element)1 SAXReader (org.dom4j.io.SAXReader)1 XMLWriter (org.dom4j.io.XMLWriter)1