Search in sources :

Example 6 with OutputFormat

use of org.dom4j.io.OutputFormat in project pentaho-platform by pentaho.

the class XactionUtil method executeXml.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String executeXml(RepositoryFile file, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, IPentahoSession userSession) throws Exception {
    try {
        HttpSessionParameterProvider sessionParameters = new HttpSessionParameterProvider(userSession);
        HttpRequestParameterProvider requestParameters = new HttpRequestParameterProvider(httpServletRequest);
        Map parameterProviders = new HashMap();
        // $NON-NLS-1$
        parameterProviders.put("request", requestParameters);
        // $NON-NLS-1$
        parameterProviders.put("session", sessionParameters);
        List messages = new ArrayList();
        IParameterProvider requestParams = new HttpRequestParameterProvider(httpServletRequest);
        // $NON-NLS-1$
        httpServletResponse.setContentType("text/xml");
        httpServletResponse.setCharacterEncoding(LocaleHelper.getSystemEncoding());
        // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
        boolean forcePrompt = "true".equalsIgnoreCase(requestParams.getStringParameter("prompt", "false"));
        OutputStream contentStream = new ByteArrayOutputStream();
        SimpleOutputHandler outputHandler = new SimpleOutputHandler(contentStream, false);
        IRuntimeContext runtime = null;
        try {
            runtime = executeInternal(file, requestParams, httpServletRequest, outputHandler, parameterProviders, userSession, forcePrompt, messages);
            Document responseDoc = SoapHelper.createSoapResponseDocument(runtime, outputHandler, contentStream, messages);
            OutputFormat format = OutputFormat.createCompactFormat();
            format.setSuppressDeclaration(true);
            // $NON-NLS-1$
            format.setEncoding("utf-8");
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            XMLWriter writer = new XMLWriter(outputStream, format);
            writer.write(responseDoc);
            writer.flush();
            // $NON-NLS-1$
            return outputStream.toString("utf-8");
        } finally {
            if (runtime != null) {
                runtime.dispose();
            }
        }
    } catch (Exception e) {
        // $NON-NLS-1$
        logger.warn(Messages.getInstance().getString("XactionUtil.XML_OUTPUT_NOT_SUPPORTED"));
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) OutputFormat(org.dom4j.io.OutputFormat) SimpleOutputHandler(org.pentaho.platform.engine.core.output.SimpleOutputHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) ServletException(javax.servlet.ServletException) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) IOException(java.io.IOException) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) HttpSessionParameterProvider(org.pentaho.platform.web.http.session.HttpSessionParameterProvider) HttpRequestParameterProvider(org.pentaho.platform.web.http.request.HttpRequestParameterProvider) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext)

Example 7 with OutputFormat

use of org.dom4j.io.OutputFormat in project pentaho-platform by pentaho.

the class XactionUtil method doParameter.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static String doParameter(final RepositoryFile file, IParameterProvider parameterProvider, final IPentahoSession userSession) throws IOException {
    ActionSequenceJCRHelper helper = new ActionSequenceJCRHelper();
    final IActionSequence actionSequence = helper.getActionSequence(file.getPath(), PentahoSystem.loggingLevel, RepositoryFilePermission.READ);
    final Document document = DocumentHelper.createDocument();
    try {
        final Element parametersElement = document.addElement("parameters");
        // noinspection unchecked
        final Map<String, IActionParameter> params = actionSequence.getInputDefinitionsForParameterProvider(IParameterProvider.SCOPE_REQUEST);
        for (final Map.Entry<String, IActionParameter> entry : params.entrySet()) {
            final String paramName = entry.getKey();
            final IActionParameter paramDef = entry.getValue();
            final String value = paramDef.getStringValue();
            final Class type;
            // defined as constant (string)
            if (IActionParameter.TYPE_LIST.equalsIgnoreCase(paramDef.getType())) {
                type = String[].class;
            } else {
                type = String.class;
            }
            final String label = paramDef.getSelectionDisplayName();
            final String[] values;
            if (StringUtils.isEmpty(value)) {
                values = new String[0];
            } else {
                values = new String[] { value };
            }
            createParameterElement(parametersElement, paramName, type, label, "user", "parameters", values);
        }
        createParameterElement(parametersElement, "path", String.class, null, "system", "system", new String[] { file.getPath() });
        createParameterElement(parametersElement, "prompt", String.class, null, "system", "system", new String[] { "yes", "no" });
        createParameterElement(parametersElement, "instance-id", String.class, null, "system", "system", new String[] { parameterProvider.getStringParameter("instance-id", null) });
        // no close, as far as I know tomcat does not like it that much ..
        OutputFormat format = OutputFormat.createCompactFormat();
        format.setSuppressDeclaration(true);
        // $NON-NLS-1$
        format.setEncoding("utf-8");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(outputStream, format);
        writer.write(document);
        writer.flush();
        return outputStream.toString("utf-8");
    } catch (Exception e) {
        logger.warn(Messages.getInstance().getString("HttpWebService.ERROR_0003_UNEXPECTED"), e);
        return null;
    }
}
Also used : IActionSequence(org.pentaho.platform.api.engine.IActionSequence) Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) ServletException(javax.servlet.ServletException) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) IOException(java.io.IOException) ActionSequenceJCRHelper(org.pentaho.platform.engine.services.ActionSequenceJCRHelper) IActionParameter(org.pentaho.platform.api.engine.IActionParameter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with OutputFormat

use of org.dom4j.io.OutputFormat in project pentaho-platform by pentaho.

the class XmlDom4JHelper method saveDom.

public static void saveDom(final Document doc, final OutputStream outputStream, String encoding, boolean suppressDeclaration, boolean prettyPrint) throws IOException {
    OutputFormat format = prettyPrint ? OutputFormat.createPrettyPrint() : OutputFormat.createCompactFormat();
    format.setSuppressDeclaration(suppressDeclaration);
    if (encoding != null) {
        format.setEncoding(encoding.toLowerCase());
        if (!suppressDeclaration) {
            doc.setXMLEncoding(encoding.toUpperCase());
        }
    }
    XMLWriter writer = new XMLWriter(outputStream, format);
    writer.write(doc);
    writer.flush();
}
Also used : OutputFormat(org.dom4j.io.OutputFormat) XMLWriter(org.dom4j.io.XMLWriter)

Example 9 with OutputFormat

use of org.dom4j.io.OutputFormat in project free-framework by a601942905git.

the class XmlUtil method createEmptyXmlFile.

/**
 * @方法功能描述:生成空的xml文件头
 * @方法名:createEmptyXmlFile
 * @param xmlPath
 * @返回类型:Document
 * @时间:2011-4-14下午12:44:20
 */
public static Document createEmptyXmlFile(String xmlPath) {
    if (xmlPath == null || xmlPath.equals(""))
        return null;
    XMLWriter output;
    Document document = DocumentHelper.createDocument();
    OutputFormat format = OutputFormat.createPrettyPrint();
    try {
        output = new XMLWriter(new FileWriter(xmlPath), format);
        output.write(document);
        output.close();
    } catch (IOException e) {
        return null;
    }
    return document;
}
Also used : FileWriter(java.io.FileWriter) OutputFormat(org.dom4j.io.OutputFormat) IOException(java.io.IOException) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 10 with OutputFormat

use of org.dom4j.io.OutputFormat in project tmdm-studio-se by Talend.

the class XmlUtil method formatXmlSource.

public static String formatXmlSource(String xmlSource) {
    SAXReader reader = new SAXReader();
    StringReader stringReader = new StringReader(xmlSource);
    StringWriter writer = null;
    XMLWriter xmlwriter = null;
    String result = xmlSource;
    try {
        Document document = reader.read(stringReader);
        writer = new StringWriter();
        OutputFormat format = OutputFormat.createPrettyPrint();
        // $NON-NLS-1$
        format.setEncoding("UTF-8");
        format.setIndentSize(4);
        format.setSuppressDeclaration(true);
        xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(document);
        result = writer.toString();
    } catch (Exception e) {
    } finally {
        try {
            if (stringReader != null)
                stringReader.close();
            if (xmlwriter != null)
                xmlwriter.close();
            if (writer != null)
                writer.close();
        } catch (Exception e) {
        }
    }
    return result;
}
Also used : StringWriter(java.io.StringWriter) SAXReader(org.dom4j.io.SAXReader) StringReader(java.io.StringReader) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter) DocumentException(org.dom4j.DocumentException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4