Example usage for org.jdom2.output XMLOutputter XMLOutputter

List of usage examples for org.jdom2.output XMLOutputter XMLOutputter

Introduction

In this page you can find the example usage for org.jdom2.output XMLOutputter XMLOutputter.

Prototype

public XMLOutputter() 

Source Link

Document

This will create an XMLOutputter with a default Format and XMLOutputProcessor .

Usage

From source file:org.kisst.cordys.caas.util.XmlNode.java

License:Open Source License

@Override
public String toString() {
    XMLOutputter out = new XMLOutputter();
    return out.outputString(element);
}

From source file:org.kisst.cordys.caas.util.XmlNode.java

License:Open Source License

public String getPretty() {
    XMLOutputter out = new XMLOutputter();
    out.setFormat(Format.getPrettyFormat());
    String xml = out.outputString(element);
    return xml;//www.j a  v  a  2s  .co m
}

From source file:org.kitodo.docket.ExportXmlLog.java

License:Open Source License

/**
 * This method exports the production metadata as xml to a given stream.
 *
 * @param docketData// w  w w .j  a  va 2  s.c  o m
 *            the docket data to export
 * @param os
 *            the OutputStream to write the contents to
 * @throws IOException
 *             Throws IOException, when document creation fails.
 */
void startExport(DocketData docketData, OutputStream os) throws IOException {
    try {
        Document doc = createDocument(docketData, true);

        XMLOutputter outp = new XMLOutputter();
        outp.setFormat(Format.getPrettyFormat());

        outp.output(doc, os);
        os.close();

    } catch (RuntimeException e) {
        logger.error("Document creation failed.");
        throw new IOException(e);
    }
}

From source file:org.lendingclub.mercator.ucs.UCSClient.java

License:Apache License

public void logDebug(String message, Element element) {

    if (logger.isDebugEnabled()) {
        XMLOutputter out = new XMLOutputter();

        out.setFormat(Format.getPrettyFormat());

        element = element.clone();/*from  ww w. j  a v a 2 s  . co m*/
        if (!Strings.isNullOrEmpty(element.getAttributeValue("outCookie"))) {
            element.setAttribute("outCookie", "**********");
        }
        if (!Strings.isNullOrEmpty(element.getAttributeValue("cookie"))) {
            element.setAttribute("cookie", "**********");
        }
        logger.debug(message + "\n{}", out.outputString(element));
    }
}

From source file:org.lendingclub.mercator.ucs.UCSClient.java

License:Apache License

public Element exec(String command, Map<String, String> args) {
    try {/* ww  w.j  a v a  2s . c om*/
        Element el = new Element(command);

        if (!command.equals("aaaLogout")) {
            String cookie = getToken().getTokenValue();
            if (Strings.isNullOrEmpty(cookie)) {
                throw new MercatorException("client does not have valid session");
            }
            el.setAttribute("cookie", getToken().getTokenValue());
        }

        args.forEach((k, v) -> {
            el.setAttribute(k, v);
        });
        XMLOutputter out = new XMLOutputter();
        String val = target.post(out.outputString(el)).header("Content-type", "application/xml")
                .execute(String.class);

        SAXBuilder saxBuilder = new SAXBuilder();

        Document document = saxBuilder.build(new StringReader(val));

        Element element = document.getRootElement();
        logDebug(command, element);
        String errorCode = element.getAttributeValue("errorCode");
        if (!Strings.isNullOrEmpty(errorCode)) {
            if (errorCode.equals("552")) {
                logout();
                throw UCSRemoteException.fromResponse(element);
            } else if (errorCode.equals("555") && command.equals("aaaLogout")) {
                logger.info("aaaLogout session not found");
            } else {
                throw UCSRemoteException.fromResponse(element);
            }
        }
        return element;
    } catch (IOException | JDOMException e) {
        throw new UCSException(e);
    }
}

From source file:org.mycore.datamodel.ifs.MCRFile.java

License:Open Source License

/**
 * Sets the content of this file from a JDOM xml document.
 * //  w  w  w .j a va 2s .  co  m
 * @param xml
 *            the JDOM xml document that should be stored as file content
 */
public void setContentFrom(Document xml) {
    Objects.requireNonNull(xml, "jdom xml document is null");

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        new XMLOutputter().output(xml, baos);
        baos.close();
    } catch (IOException ignored) {
    }
    setContentFrom(baos.toByteArray());
}

From source file:org.mycore.frontend.basket.MCRBasketPersistence.java

License:Open Source License

/**
 * Writes the basket's content to a persistent file in the given derivate.
 * //from  www .j a va 2s.  c  o  m
 * @param basket the basket to save.
 * @param derivate the derivate holding the file
 * @param basketFile the file holding the basket's data.
 * @throws IOException 
 */
private static void writeBasketToFile(MCRBasket basket, MCRDerivate derivate, MCRPath basketFile)
        throws IOException {
    Document xml = new MCRBasketXMLBuilder(false).buildXML(basket);
    XMLOutputter xout = new XMLOutputter();
    xout.output(xml, Files.newOutputStream(basketFile));
    MCRMetadataManager.updateMCRDerivateXML(derivate);
}

From source file:org.mycore.frontend.editor.MCREditorServlet.java

License:Open Source License

private void sendToDebug(HttpServletResponse res, Document unprocessed, MCREditorSubmission sub)
        throws IOException, UnsupportedEncodingException {
    res.setContentType("text/html; charset=UTF-8");

    PrintWriter pw = res.getWriter();

    pw.println("<html><body><p><pre>");

    for (int i = 0; i < sub.getVariables().size(); i++) {
        MCREditorVariable var = (MCREditorVariable) sub.getVariables().get(i);
        pw.println(var.getPath() + " = " + var.getValue());

        FileItem file = var.getFile();

        if (file != null) {
            pw.println("      is uploaded file " + file.getContentType() + ", " + file.getSize() + " bytes");
        }/*from   w  w  w. j a  v  a  2 s.  c o  m*/
    }

    pw.println("</pre></p><p>");

    XMLOutputter outputter = new XMLOutputter();
    Format fmt = Format.getPrettyFormat();
    fmt.setLineSeparator("\n");
    fmt.setOmitDeclaration(true);
    outputter.setFormat(fmt);

    Element pre = new Element("pre");
    pre.addContent(outputter.outputString(unprocessed));
    outputter.output(pre, pw);

    pre = new Element("pre");
    pre.addContent(outputter.outputString(sub.getXML()));
    outputter.output(pre, pw);

    pw.println("</p></body></html>");
    pw.close();
}

From source file:org.mycore.mir.wizard.MIRWizardServlet.java

License:Open Source License

public void doGetPost(final MCRServletJob job) throws Exception {
    final HttpServletRequest req = job.getRequest();
    final HttpServletResponse res = job.getResponse();

    final String path = req.getPathInfo();

    if (path != null) {
        final StringTokenizer st = new StringTokenizer(path, "/");
        final String request = st.nextToken();

        if ("shutdown".equals(request)) {
            LOGGER.info("Shutdown System....");
            MCRConfiguration.instance().set("MCR.LayoutTransformerFactory.Default.Stylesheets", "");
            MCRConfiguration.instance().set("MCR.Startup.Class", "%MCR.Startup.Class%");
            System.exit(0);/*from w w w  .  j  av a 2s  .  c  o  m*/
        } else {
            LOGGER.info("Request file \"" + request + "\"...");
            getLayoutService().doLayout(job.getRequest(), job.getResponse(),
                    new MCRJDOMContent(MCRURIResolver.instance().resolve("resource:setup/" + request)));
        }
    } else {
        final Document doc = (Document) (job.getRequest().getAttribute("MCRXEditorSubmission"));
        final Element wizXML = doc.getRootElement();

        LOGGER.debug(new XMLOutputter().outputString(wizXML));

        final Element resXML = new Element("wizard");

        if (!MIRWizardRequestFilter.isAuthenticated(req)) {
            final String loginToken = wizXML.getChildTextTrim("login");
            String url = "wizard";

            if (loginToken != null && MIRWizardRequestFilter.getLoginToken(req).equals(loginToken)) {
                LOGGER.info("Authenticate with token \"" + loginToken + "\"...");
                MCRSessionMgr.getCurrentSession().put(MIRWizardStartupHandler.LOGIN_TOKEN, loginToken);
                MCRSessionMgr.getCurrentSession().put("ServerBaseURL",
                        req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort());
            } else {
                LOGGER.info("Redirect to login...");
                url += "/?action=login"
                        + (!MIRWizardRequestFilter.getLoginToken(req).equals(loginToken) ? "&token=invalid"
                                : "");

                // output login token again
                MIRWizardStartupHandler.outputLoginToken(req.getServletContext());
            }
            res.sendRedirect(res.encodeRedirectURL(MCRFrontendUtil.getBaseURL() + url));
            return;
        }

        final Element results = new Element("results");

        final Element commands = MCRURIResolver.instance().resolve("resource:setup/install.xml");

        final MIRWizardCommandChain chain = new MIRWizardCommandChain();

        for (Element command : commands.getChildren("command")) {
            final String cls = command.getAttributeValue("class");
            final String name = command.getAttributeValue("name");
            final String src = command.getAttributeValue("src");

            try {
                final Class<?> cmdCls = Class.forName(cls);
                MIRWizardCommand cmd = null;

                if (name != null) {
                    final Constructor<?> cmdC = cmdCls.getConstructor(String.class);
                    cmd = (MIRWizardCommand) cmdC.newInstance(name);
                } else {
                    final Constructor<?> cmdC = cmdCls.getConstructor();
                    cmd = (MIRWizardCommand) cmdC.newInstance();
                }

                if (src != null) {
                    cmd.setInputXML(MCRURIResolver.instance().resolve(src));
                }

                if (cmd != null) {
                    chain.addCommand(cmd);
                }
            } catch (final ClassNotFoundException | NoSuchMethodException | SecurityException
                    | InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException e) {
                LOGGER.error(e);
            }
        }

        initializeApplication(job);

        LOGGER.info("Execute Wizard Commands...");
        chain.execute(wizXML);
        LOGGER.info("done.");

        for (MIRWizardCommand cmd : chain.getCommands()) {
            if (cmd.getResult() != null)
                results.addContent(cmd.getResult().toElement());
        }

        results.setAttribute("success", Boolean.toString(chain.isSuccess()));

        resXML.addContent(results);

        getLayoutService().doLayout(job.getRequest(), job.getResponse(), new MCRJDOMContent(resXML));
    }
}

From source file:org.mycore.oai.classmapping.MCRClassificationMappingEventHandler.java

License:Open Source License

private void createMapping(MCRObject obj) {
    MCRMetaElement mappings = obj.getMetadata().getMetadataElement("mappings");
    if (mappings != null) {
        oldMappings = mappings.clone();//from  w  w  w  .  j a  v a  2 s .c  o m
        obj.getMetadata().removeMetadataElement("mappings");
    }

    Element currentClassElement = null;
    try {
        Document doc = new Document((Element) obj.getMetadata().createXML().detach());
        XPathExpression<Element> classElementPath = XPathFactory.instance().compile("//*[@categid]",
                Filters.element());
        List<Element> classList = classElementPath.evaluate(doc);
        if (classList.size() > 0) {
            mappings = new MCRMetaElement();
            mappings.setTag("mappings");
            mappings.setClass(MCRMetaClassification.class);
            mappings.setHeritable(false);
            mappings.setNotInherit(true);
            obj.getMetadata().setMetadataElement(mappings);
        }
        for (Element classElement : classList) {
            currentClassElement = classElement;
            MCRCategory categ = DAO.getCategory(new MCRCategoryID(classElement.getAttributeValue("classid"),
                    classElement.getAttributeValue("categid")), 0);
            addMappings(mappings, categ);
        }
    } catch (Exception je) {
        if (currentClassElement == null) {
            LOGGER.error("Error while finding classification elements", je);
        } else {
            LOGGER.error("Error while finding classification elements for "
                    + new XMLOutputter().outputString(currentClassElement), je);
        }
    } finally {
        if (mappings == null || mappings.size() == 0) {
            obj.getMetadata().removeMetadataElement("mappings");
        }
    }
}