use of javax.xml.transform.TransformerFactory in project adempiere by adempiere.
the class ADServiceImpl method getStringFromDocument.
/*<r>
<f colname="AD_Client_ID">1000000</f>
</r>
<AD_Client_ID>1000000</AD_Client_ID>
*/
// method to convert Document to String
public String getStringFromDocument(Document doc) {
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch (TransformerException ex) {
ex.printStackTrace();
return null;
}
}
use of javax.xml.transform.TransformerFactory in project adempiere by adempiere.
the class XSLTProcessor method run.
/**
* Run
* @param request
* @param xslStream
* @param xmlStream
* @return xml
* @throws Exception
*/
public static StringBuffer run(HttpServletRequest request, String xslStream, String xmlStream) throws Exception {
Calendar myCal = Calendar.getInstance();
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(new StringReader(xslStream)));
Enumeration e = request.getParameterNames();
StringBuffer tStrHTML = new StringBuffer();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
transformer.setParameter(name, request.getParameter(name));
}
OutputStream out = new ByteArrayOutputStream();
transformer.transform(new StreamSource(new StringReader(xmlStream)), new StreamResult(out));
tStrHTML.append(out.toString());
if (request.getParameter("debug") != null) {
Calendar myCal2 = Calendar.getInstance();
long timeDiff = (myCal2.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000) + (myCal2.get(Calendar.MINUTE) * 60 * 1000) + (myCal2.get(Calendar.SECOND) * 1000) + myCal2.get(Calendar.MILLISECOND);
timeDiff = timeDiff - ((myCal.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000) + (myCal.get(Calendar.MINUTE) * 60 * 1000) + (myCal.get(Calendar.SECOND) * 1000) + myCal.get(Calendar.MILLISECOND));
tStrHTML.append("<!-- XSLT Processing done in: " + timeDiff + " ms -->\n");
System.out.println("XSLT Processing:" + timeDiff);
}
return tStrHTML;
}
use of javax.xml.transform.TransformerFactory in project zm-mailbox by Zimbra.
the class AutoDiscoverServlet method createResponseDocForEws.
private static String createResponseDocForEws(String displayName, String email, String serviceUrl, Account acct) throws Exception {
Provisioning prov = Provisioning.getInstance();
Server server = prov.getServer(acct);
String cn = server.getCn();
String name = server.getName();
String acctId = acct.getId();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.newDocument();
Element root = xmlDoc.createElementNS(NS, "Autodiscover");
root.setAttribute("xmlns", NS);
root.setAttribute("xmlns:xsi", XSI_NS);
root.setAttribute("xmlns:xsd", XSD_NS);
xmlDoc.appendChild(root);
//Add the response element.
Element response = xmlDoc.createElementNS(NS_OUTLOOK, "Response");
root.appendChild(response);
//User
Element user = xmlDoc.createElement("User");
Element displayNameElm = xmlDoc.createElement("DisplayName");
displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
user.appendChild(displayNameElm);
Element emailAddr = xmlDoc.createElement("EmailAddress");
emailAddr.appendChild(xmlDoc.createTextNode(email));
user.appendChild(emailAddr);
Element depId = xmlDoc.createElement("DeploymentId");
depId.appendChild(xmlDoc.createTextNode(acctId));
user.appendChild(depId);
response.appendChild(user);
//Action
Element account = xmlDoc.createElement("Account");
Element acctType = xmlDoc.createElement("AccountType");
acctType.appendChild(xmlDoc.createTextNode("email"));
account.appendChild(acctType);
response.appendChild(account);
Element action = xmlDoc.createElement("Action");
action.appendChild(xmlDoc.createTextNode("settings"));
account.appendChild(action);
Element protocol = xmlDoc.createElement("Protocol");
account.appendChild(protocol);
Element type = xmlDoc.createElement("Type");
type.appendChild(xmlDoc.createTextNode("EXCH"));
protocol.appendChild(type);
Element ews = xmlDoc.createElement("EwsUrl");
protocol.appendChild(ews);
ews.appendChild(xmlDoc.createTextNode(serviceUrl));
Element as = xmlDoc.createElement("ASUrl");
protocol.appendChild(as);
as.appendChild(xmlDoc.createTextNode(serviceUrl));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(xmlDoc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
writer.flush();
String xml = writer.toString();
writer.close();
return "<?xml version=\"1.0\"?>\n" + xml;
}
use of javax.xml.transform.TransformerFactory in project zm-mailbox by Zimbra.
the class AutoDiscoverServlet method createResponseDoc.
//<Autodiscover xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
// <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006">
// <Culture>en:en</Culture>
// <User>
// <DisplayName>Demo User One</DisplayName>
// <EMailAddress>user1@sudipto-mpro.local</EMailAddress>
// </User>
// <Action>
// <Settings>
// <Server>
// <Type>MobileSync</Type>
// <Url>https://sudipto-mpro.local/Microsoft-Server-ActiveSync</Url>
// <Name>https://sudipto-mpro.local/Microsoft-Server-ActiveSync</Name>
// </Server>
// </Settings>
// </Action>
// </Response>
//</Autodiscover>
//
//
//Error Case:
//<Autodiscover xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
// <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/mobilesync/responseschema/2006">
// <Culture>en:en</Culture>
// <User>
// <DisplayName>Demo User One</DisplayName>
// <EMailAddress>user1@sudipto-mpro.local</EMailAddress>
// </User>
// <Action>
// <Error>
// <Status>1</Status>
// <Message>Active Directory currently not available</Message>
// <DebugData>UserMailbox</DebugData>
// </Error>
// </Action>
// </Response>
//</Autodiscover>
//
private static String createResponseDoc(String displayName, String email, String serviceUrl) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document xmlDoc = builder.newDocument();
Element root = xmlDoc.createElementNS(NS, "Autodiscover");
root.setAttribute("xmlns", NS);
root.setAttribute("xmlns:xsi", XSI_NS);
root.setAttribute("xmlns:xsd", XSD_NS);
xmlDoc.appendChild(root);
//Add the response element.
Element response = xmlDoc.createElementNS(NS_MOBILE, "Response");
root.appendChild(response);
//Add culture to to response
Element culture = xmlDoc.createElement("Culture");
culture.appendChild(xmlDoc.createTextNode("en:en"));
response.appendChild(culture);
//User
Element user = xmlDoc.createElement("User");
Element displayNameElm = xmlDoc.createElement("DisplayName");
displayNameElm.appendChild(xmlDoc.createTextNode(displayName));
user.appendChild(displayNameElm);
Element emailAddr = xmlDoc.createElement("EMailAddress");
emailAddr.appendChild(xmlDoc.createTextNode(email));
user.appendChild(emailAddr);
response.appendChild(user);
//Action
Element action = xmlDoc.createElement("Action");
Element settings = xmlDoc.createElement("Settings");
Element server = xmlDoc.createElement("Server");
Element type = xmlDoc.createElement("Type");
type.appendChild(xmlDoc.createTextNode("MobileSync"));
server.appendChild(type);
Element url = xmlDoc.createElement("Url");
url.appendChild(xmlDoc.createTextNode(serviceUrl));
server.appendChild(url);
Element name = xmlDoc.createElement("Name");
name.appendChild(xmlDoc.createTextNode(serviceUrl));
server.appendChild(name);
settings.appendChild(server);
action.appendChild(settings);
response.appendChild(action);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(xmlDoc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
writer.flush();
String xml = writer.toString();
writer.close();
// return str.toString();
return "<?xml version=\"1.0\"?>\n" + xml;
}
use of javax.xml.transform.TransformerFactory in project poi by apache.
the class RecordGenerator method transform.
/**
* <p>Executes an XSL transformation. This process transforms an XML input
* file into a text output file controlled by an XSLT specification.</p>
*
* @param in the XML input file
* @param out the text output file
* @param xslt the XSLT specification, i.e. an XSL style sheet
* @throws FileNotFoundException
* @throws TransformerException
*/
private static void transform(final File in, final File out, final File xslt) throws FileNotFoundException, TransformerException {
final StreamSource ss = new StreamSource(xslt);
final TransformerFactory tf = TransformerFactory.newInstance();
final Transformer t;
try {
t = tf.newTransformer(ss);
} catch (TransformerException ex) {
System.err.println("Error compiling XSL style sheet " + xslt);
throw ex;
}
final Properties p = new Properties();
p.setProperty(OutputKeys.METHOD, "text");
t.setOutputProperties(p);
final Result result = new StreamResult(out);
t.transform(new StreamSource(in), result);
}
Aggregations