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;
}
}
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;
}
}
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();
}
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;
}
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;
}
Aggregations