Search in sources :

Example 61 with Transformer

use of javax.xml.transform.Transformer in project opennms by OpenNMS.

the class QuickBaseAPITest method XXXtestCreateLead.

public void XXXtestCreateLead() {
    PrintWriter out = null;
    try {
        out = new PrintWriter(System.out);
        out.println("Welcome to QuickBase\n");
        QuickBaseClient qdb = createClient();
        qdb.findDbByName("TPMG Support");
        HashMap tables = (HashMap) qdb.grantedDBs(false, false, true);
        if (tables == null) {
            out.println("No tables belong to this user.");
        }
        Set tableNames = tables.keySet();
        String tableName = "";
        String tableDbid = "";
        for (Iterator it = tableNames.iterator(); it.hasNext(); ) {
            tableName = (String) it.next();
            tableDbid = (String) tables.get(tableName);
            out.println("Name: " + tableName + " DBID: " + tableDbid);
            Document schema = qdb.getSchema(tableDbid);
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(new DOMSource(schema), new StreamResult(out));
            NodeList fields = schema.getElementsByTagName("field");
            out.println("The QuickBase application " + tableName + " has " + fields.getLength() + " fields.");
            out.println("The fields are listed below.");
            for (int i = 0; i < fields.getLength(); i++) {
                out.println("Field ID: " + fields.item(i).getAttributes().getNamedItem("id").getNodeValue());
                out.println("Field Type: " + fields.item(i).getAttributes().getNamedItem("type").getNodeValue());
                out.println("Field Label: " + fields.item(i).getChildNodes().item(0).getNodeValue());
            }
        }
    } catch (QuickBaseException qdbe) {
        System.err.println("Exception in main " + qdbe.toString() + " error code: " + qdbe.getErrorCode());
        qdbe.printStackTrace();
    } catch (Throwable e) {
        System.err.println("Exception in main " + e.toString());
        e.printStackTrace();
    } finally {
        if (null != out) {
            out.flush();
            out.close();
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Set(java.util.Set) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) QuickBaseException(com.intuit.quickbase.util.QuickBaseException) QuickBaseClient(com.intuit.quickbase.util.QuickBaseClient) Document(org.w3c.dom.Document) Iterator(java.util.Iterator) PrintWriter(java.io.PrintWriter)

Example 62 with Transformer

use of javax.xml.transform.Transformer in project opennms by OpenNMS.

the class OnmsPdfViewResolver method resolveView.

@Override
public void resolveView(ServletRequest request, ServletResponse response, Preferences preferences, Object viewData) throws Exception {
    InputStream is = new ByteArrayInputStream(((String) viewData).getBytes(StandardCharsets.UTF_8));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FopFactory fopFactory = FopFactory.newInstance();
    fopFactory.setStrictValidation(false);
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
    TransformerFactory tfact = TransformerFactory.newInstance();
    Transformer transformer = tfact.newTransformer();
    Source src = new StreamSource(is);
    Result res = new SAXResult(fop.getDefaultHandler());
    transformer.transform(src, res);
    byte[] contents = out.toByteArray();
    response.setContentLength(contents.length);
    response.getOutputStream().write(contents);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) FOUserAgent(org.apache.fop.apps.FOUserAgent) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FopFactory(org.apache.fop.apps.FopFactory) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 63 with Transformer

use of javax.xml.transform.Transformer in project tdi-studio-se by Talend.

the class FeaturesModel method internalGetContent.

private InputStream internalGetContent() throws Exception {
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element features = document.createElement("features");
    features.setAttribute("xmlns", "http://karaf.apache.org/xmlns/features/v1.0.0");
    features.setAttribute("name", getArtifactId());
    document.appendChild(features);
    Element feature = document.createElement("feature");
    feature.setAttribute("name", getArtifactId());
    feature.setAttribute("version", getVersion());
    features.appendChild(feature);
    // add sub features
    for (FeatureModel fm : subFeatures) {
        Element subFeature = document.createElement("feature");
        if (null != fm.getVersion() && !fm.getVersion().isEmpty()) {
            subFeature.setAttribute("version", fm.getVersion());
        }
        subFeature.appendChild(document.createTextNode(fm.getArtifactId()));
        feature.appendChild(subFeature);
    }
    // add sub bundles
    for (BundleModel bm : subBundles) {
        Element bundle = document.createElement("bundle");
        bundle.appendChild(document.createTextNode(toBundleString(bm)));
        feature.appendChild(bundle);
    }
    if (null == contexts || contexts.isEmpty()) {
        // add config
        Element config = document.createElement("config");
        config.setAttribute("name", configName);
        StringBuilder sb = new StringBuilder("talendcontext=\"");
        for (int i = 0; i < contextList.length; i++) {
            if (i != 0) {
                sb.append(',');
            }
            sb.append(contextList[i]);
        }
        sb.append('"');
        config.appendChild(document.createTextNode(sb.toString()));
        feature.appendChild(config);
    } else {
        // add contexts config
        for (Map.Entry<String, Map<String, String>> context : contexts.entrySet()) {
            Element config = document.createElement("config");
            config.setAttribute("name", name + ".talendcontext." + context.getKey());
            StringBuilder sb = new StringBuilder("\n");
            for (Map.Entry<String, String> property : context.getValue().entrySet()) {
                sb.append(property.getKey());
                sb.append('=');
                sb.append(property.getValue());
                sb.append('\n');
            }
            config.appendChild(document.createTextNode(sb.toString()));
            feature.appendChild(config);
        }
    }
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(os));
    return new ByteArrayInputStream(os.toByteArray());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) Map(java.util.Map)

Example 64 with Transformer

use of javax.xml.transform.Transformer in project zm-mailbox by Zimbra.

the class QuotedTextUtil method getHtml.

/**
     * Convert the DOM document back to HTML string
     *
     * @param document
     * @return String the String representation of the DOM document
     */
private String getHtml(Document document) {
    try {
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(new DOMSource(document), result);
        return writer.toString();
    } catch (TransformerException e) {
        ZimbraLog.soap.warn("Exception in converting DOM to html", e);
    }
    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 65 with Transformer

use of javax.xml.transform.Transformer in project zm-mailbox by Zimbra.

the class XsdCleaner method getTranformer.

public static Transformer getTranformer() {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        return transformer;
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        LOG.error("XsdCleaner:Problem getting XML Transformer", e);
    }
    return null;
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Transformer (javax.xml.transform.Transformer)449 StreamResult (javax.xml.transform.stream.StreamResult)354 DOMSource (javax.xml.transform.dom.DOMSource)272 TransformerFactory (javax.xml.transform.TransformerFactory)222 TransformerException (javax.xml.transform.TransformerException)175 StringWriter (java.io.StringWriter)153 Document (org.w3c.dom.Document)114 IOException (java.io.IOException)105 StreamSource (javax.xml.transform.stream.StreamSource)98 Source (javax.xml.transform.Source)97 DocumentBuilder (javax.xml.parsers.DocumentBuilder)70 File (java.io.File)66 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)64 SAXException (org.xml.sax.SAXException)62 Element (org.w3c.dom.Element)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)57 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 StringReader (java.io.StringReader)52 Result (javax.xml.transform.Result)52