Search in sources :

Example 66 with TransformerFactory

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;
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 67 with TransformerFactory

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;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Enumeration(java.util.Enumeration) StreamResult(javax.xml.transform.stream.StreamResult) Calendar(java.util.Calendar) StreamSource(javax.xml.transform.stream.StreamSource) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 68 with TransformerFactory

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Server(com.zimbra.cs.account.Server) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Provisioning(com.zimbra.cs.account.Provisioning)

Example 69 with TransformerFactory

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 70 with TransformerFactory

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);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)257 Transformer (javax.xml.transform.Transformer)221 StreamResult (javax.xml.transform.stream.StreamResult)198 DOMSource (javax.xml.transform.dom.DOMSource)157 TransformerException (javax.xml.transform.TransformerException)86 StringWriter (java.io.StringWriter)77 StreamSource (javax.xml.transform.stream.StreamSource)77 Document (org.w3c.dom.Document)67 Source (javax.xml.transform.Source)56 IOException (java.io.IOException)55 File (java.io.File)47 DocumentBuilder (javax.xml.parsers.DocumentBuilder)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 Element (org.w3c.dom.Element)36 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35 Result (javax.xml.transform.Result)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 StringReader (java.io.StringReader)28