Example usage for org.dom4j DocumentHelper parseText

List of usage examples for org.dom4j DocumentHelper parseText

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper parseText.

Prototype

public static Document parseText(String text) throws DocumentException 

Source Link

Document

parseText parses the given text as an XML document and returns the newly created Document.

Usage

From source file:org.pentaho.platform.uifoundation.component.xml.InputFormComponent.java

License:Open Source License

@Override
public Document getXmlContent() {

    ActionSequenceJCRHelper actionHelper = new ActionSequenceJCRHelper(getSession());
    IActionSequence actionSequence = actionHelper.getActionSequence(
            ActionInfo.buildSolutionPath(solution, path, actionName), getLoggingLevel(),
            RepositoryFilePermission.READ);

    if (actionSequence == null) {
        // TODO log this
        error(Messages.getInstance().getString("InputForm.ERROR_0004_ACTION_NOT_FOUND") + solution + path //$NON-NLS-1$
                + actionName);/*from w  w  w .j  a va2  s .c om*/
        return null;
    }

    List actions = actionSequence.getActionDefinitionsAndSequences();
    ISolutionActionDefinition action = (ISolutionActionDefinition) actions.get(0);

    Node node = action.getComponentSection();
    if (node == null) {
        error(Messages.getInstance().getString("InputForm.ERROR_0005_INBOX_DEFINITION_MISSING") + solution //$NON-NLS-1$
                + path + actionName);
        return null;
    }

    if (templateName == null) {

        // see if the template is specified in the action document
        Node templateNode = node.selectSingleNode("//template"); //$NON-NLS-1$
        if (templateNode != null) {
            templateName = templateNode.getText();
        }
        if (templateName == null) {
            error(Messages.getInstance().getString("InputForm.ERROR_0006_TEMPLATE_NOT_SPECIFIED")); //$NON-NLS-1$
            return null;
        }
    }
    Node xFormNode = node.selectSingleNode("//xForm"); //$NON-NLS-1$

    try {

        String actionTitle = actionSequence.getTitle();
        if (actionTitle != null) {
            setXslProperty("title", actionTitle); //$NON-NLS-1$
        }

        String description = actionSequence.getDescription();
        if (description != null) {
            setXslProperty("description", description); //$NON-NLS-1$
        }

        String xFormHtml = XForm.transformSnippet(xFormNode, getSession(), new SolutionURIResolver());
        if (xFormHtml == null) {
            error(Messages.getInstance().getString("InputForm.ERROR_0007_INBOX_DEFINITION_INVALID") + solution //$NON-NLS-1$
                    + path + actionName);
            return null;
        }
        Document document = DocumentHelper.parseText(xFormHtml);
        Node xFormHtmlNode = document.selectSingleNode("//xForm"); //$NON-NLS-1$

        setXslProperty("xForm", xFormHtmlNode.asXML()); //$NON-NLS-1$

        if ((stylesheetName != null) && !"".equals(stylesheetName)) { //$NON-NLS-1$
            setXslProperty("css", stylesheetName); //$NON-NLS-1$
        }
        setXsl("text/html", templateName); //$NON-NLS-1$

        return document;

    } catch (Exception e) {
        return null;
    }

}

From source file:org.pentaho.platform.util.client.ClientUtil.java

License:Open Source License

/**
 * Submits an HTTP result with the provided HTTPMethod and returns a dom4j document of the response
 *
 * @param callMethod//w w  w  .ja va2 s . c  o  m
 * @return
 * @throws ServiceException
 */
public static org.dom4j.Document getResultDom4jDocument(HttpClient client, HttpUriRequest callMethod)
        throws ServiceException {

    try {
        // execute the HTTP call
        HttpResponse httpResponse = client.execute(callMethod);
        final int status = httpResponse.getStatusLine().getStatusCode();
        if (status != HttpStatus.SC_OK) {
            throw new ServiceException("Web service call failed with code " + status); //$NON-NLS-1$
        }
        // get the result as a string
        InputStream in = httpResponse.getEntity().getContent();
        byte[] buffer = new byte[2048];
        int n = in.read(buffer);
        StringBuilder sb = new StringBuilder();
        while (n != -1) {
            sb.append(new String(buffer, 0, n));
            n = in.read(buffer);
        }
        String result = sb.toString();
        // convert to XML
        return DocumentHelper.parseText(result);
    } catch (IOException e) {
        throw new ServiceException(e);
    } catch (DocumentException e) {
        throw new ServiceException(e);
    }

}

From source file:org.pentaho.platform.web.servlet.AdhocWebServiceInteract.java

License:Open Source License

public static void interactiveOutput(final String reportStr, final OutputStream out,
        final IPentahoSession session) {

    try {/*from   w  w  w.  ja  va2 s . c o m*/
        //          System.out.println( "interactiveOutput 1" );
        Document reportXml = DocumentHelper.parseText(reportStr);
        //          System.out.println( "interactiveOutput 2" );
        Document reportDoc = AdhocWebServiceInteractXml.convertXml(reportXml);
        //          System.out.println( "interactiveOutput 3" );
        //          System.out.println( reportDoc.asXML() );

        StringBuffer sb = XmlHelper.transformXml("iwaqr-report.xsl", "adhoc", reportDoc.asXML(), //$NON-NLS-1$//$NON-NLS-2$
                new HashMap<String, String>(), new SolutionURIResolver(session));
        //          System.out.println( "interactiveOutput 4" );

        //          System.out.println( sb.toString() );

        out.write(sb.toString().getBytes());
        //          System.out.println( "interactiveOutput 5" );

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.pentaho.platform.web.servlet.SolutionRepositoryService.java

License:Open Source License

protected void dispatch(final HttpServletRequest request, final HttpServletResponse response,
        final String component, final OutputStream outputStream, String responseEncoding,
        final IPentahoSession userSession, final boolean wrapWithSOAP)
        throws IOException, SolutionRepositoryServiceException, PentahoAccessControlException,
        ParserConfigurationException, TransformerConfigurationException, TransformerException,
        TransformerFactoryConfigurationError, DocumentException {

    ISolutionRepositoryService service = PentahoSystem.get(ISolutionRepositoryService.class, userSession);
    IParameterProvider parameterProvider = new HttpRequestParameterProvider(request);
    org.dom4j.Document doc = null;

    if ("getSolutionRepositoryDoc".equals(component)) { //$NON-NLS-1$
        String[] filters = getFilters(request);
        doc = convertToDom4JDoc(service.getSolutionRepositoryDoc(userSession, filters));
    } else if ("getSolutionRepositoryFileDetails".equals(component)) { //$NON-NLS-1$
        String fullPath = request.getParameter("fullPath"); //$NON-NLS-1$
        doc = convertToDom4JDoc(service.getSolutionRepositoryFileDetails(userSession, fullPath));
    } else if ("createNewFolder".equals(component)) { //$NON-NLS-1$
        String solution = request.getParameter("solution"); //$NON-NLS-1$
        String path = request.getParameter("path"); //$NON-NLS-1$
        String name = request.getParameter("name"); //$NON-NLS-1$
        String desc = request.getParameter("desc"); //$NON-NLS-1$
        boolean result = service.createFolder(userSession, solution, path, name, desc);
        doc = createResultDocument(Boolean.toString(result));
    } else if ("delete".equals(component)) { //$NON-NLS-1$
        String solution = request.getParameter("solution"); //$NON-NLS-1$
        String path = request.getParameter("path"); //$NON-NLS-1$
        String name = request.getParameter("name"); //$NON-NLS-1$
        boolean result = service.delete(userSession, solution, path, name);
        doc = createResultDocument(Boolean.toString(result));
    } else if ("setAcl".equals(component)) { //$NON-NLS-1$
        String solution = parameterProvider.getStringParameter("solution", null); //$NON-NLS-1$ 
        String path = parameterProvider.getStringParameter("path", null); //$NON-NLS-1$ 
        String filename = parameterProvider.getStringParameter("filename", null); //$NON-NLS-1$
        String strAclXml = parameterProvider.getStringParameter("aclXml", null); //$NON-NLS-1$
        service.setAcl(solution, path, filename, strAclXml, userSession);
        doc = WebServiceUtil.createStatusDocument(
                Messages.getInstance().getString("AdhocWebService.ACL_UPDATE_SUCCESSFUL")); //$NON-NLS-1$
    } else if ("getAcl".equals(component)) { //$NON-NLS-1$
        String solution = parameterProvider.getStringParameter("solution", null); //$NON-NLS-1$ 
        String path = parameterProvider.getStringParameter("path", null); //$NON-NLS-1$ 
        String filename = parameterProvider.getStringParameter("filename", null); //$NON-NLS-1$
        try {//w  w w. ja va2  s.  co m
            doc = DocumentHelper.parseText(service.getAclXml(solution, path, filename, userSession));
        } catch (DocumentException e) {
            throw new RuntimeException(Messages.getInstance()
                    .getErrorString("HttpWebService.UNRECOGNIZED_COMPONENT_REQUEST", component)); //$NON-NLS-1$
        }
    } else {
        throw new RuntimeException(Messages.getInstance()
                .getErrorString("HttpWebService.UNRECOGNIZED_COMPONENT_REQUEST", component)); //$NON-NLS-1$
    }
    if (doc != null) {
        doc = wrapWithSOAP ? SoapHelper.createSoapResponseDocument(doc) : doc;
        XmlDom4JHelper.saveDom(doc, outputStream, responseEncoding, true);
    }

}

From source file:org.pentaho.platform.web.servlet.SolutionRepositoryService.java

License:Open Source License

public org.dom4j.Document convertToDom4JDoc(final org.w3c.dom.Document doc)
        throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError,
        DocumentException {/*w  w  w.  ja  v a  2  s . com*/
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new StringWriter());
    TransformerFactory.newInstance().newTransformer().transform(source, result);
    String theXML = result.getWriter().toString();
    org.dom4j.Document dom4jDoc = DocumentHelper.parseText(theXML);
    return dom4jDoc;
}

From source file:org.pentaho.pms.ui.QueryBuilderDialog.java

License:Open Source License

private void viewMql() {
    try {/*  w  ww  .j  av  a  2s  .  com*/
        MQLQuery mqlQuery = editor.getMqlQuery();
        if (mqlQuery != null) {
            Document document = DocumentHelper.parseText(mqlQuery.getXML());
            TextDialog textDialog = new TextDialog(getShell(), "MQL Query", //$NON-NLS-1$
                    prettyPrint(document).getRootElement().asXML()); //$NON-NLS-2$
            textDialog.open();
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:org.pentaho.pms.ui.QueryBuilderDialog.java

License:Open Source License

public Document prettyPrint(Document document) {
    try {/*from   w  ww . j  a  va2 s .  c  o  m*/
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(document.getXMLEncoding());
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, format);
        // XMLWriter has a bug that is avoided if we reparse the document
        // prior to calling XMLWriter.write()
        writer.write(DocumentHelper.parseText(document.asXML()));
        writer.close();
        document = DocumentHelper.parseText(stringWriter.toString());
    } catch (Exception e) {
        e.printStackTrace();
        return (null);
    }
    return (document);
}

From source file:org.pentaho.supportutility.config.retriever.DataSourceRetriever.java

License:Open Source License

/**
 * writes fetched data to respective Xml
 * //from  w  w  w  . j  a v  a2  s  . c o m
 * @param xml
 *            -string obtained from restful client
 * @param dirName
 *            -directory name of respective file
 * @param fileName
 *            -respective filename
 */
private void writeXml(String xml, String dirName, String fileName) {

    try {
        Document document = DocumentHelper.parseText(xml);
        OutputFormat format = OutputFormat.createPrettyPrint();

        XMLWriter writer = new XMLWriter(new FileWriter(dirName + File.separator + fileName), format);
        writer.write(document);
        writer.close();
    } catch (NullPointerException e) {
        e.getMessage();
    } catch (DocumentException e1) {
        e1.getMessage();
    } catch (IOException e) {
        e.getMessage();
    }

}

From source file:org.pentaho.supportutility.config.retriever.LicenseRetriever.java

License:Open Source License

@Override
protected void readConfiguration(Properties props) {

    String dirName = props.getProperty(SupportUtilConstant.SUPP_INFO_DEST_PATH) + File.separator
            + props.getProperty(SupportUtilConstant.SUPP_INF_DIR) + File.separator
            + SupportUtilConstant.LICENSE_DIR;

    if (createDestDirectory(dirName, null)) {
        // calls restful client to get license detail
        String licenseXml = RestFulClient.restFulService(getServerName(), SupportUtilConstant.LICENSE, props,
                webXMLPath);//ww  w.  j  av  a  2  s  .c  o  m

        try {

            Document document = DocumentHelper.parseText(licenseXml);
            OutputFormat format = OutputFormat.createPrettyPrint();

            XMLWriter writer = new XMLWriter(
                    new FileWriter(dirName + File.separator + SupportUtilConstant.LICENSE_FILE_NAME), format);
            writer.write(document);

            writer.close();
        } catch (DocumentException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

From source file:org.prettyx.Common.XMLParser.java

License:Open Source License

/**
 * parse xml string//from w w w  .j  av a 2 s  .c  o  m
 *
 * @param xmlString
 *              string contains a xml
 * @return elementlist
 */

public static synchronized Map parserXmlFromString(String xmlString) throws Exception {
    elementMap.clear();
    Document document = DocumentHelper.parseText(xmlString);
    Element rootElement = document.getRootElement();
    getElementList(rootElement);

    return elementMap;
}