Search in sources :

Example 31 with Transformer

use of javax.xml.transform.Transformer in project intellij-community by JetBrains.

the class XsltDocumentationProvider method getDocumentation.

@Nullable
private String getDocumentation(String name, String type) {
    try {
        final Transformer transformer = getTemplate().newTransformer();
        transformer.setParameter("element", name);
        transformer.setParameter("type", type);
        final StringWriter writer = new StringWriter();
        transformer.transform(new JDOMSource(getDocumentationDocument()), new StreamResult(writer));
        final String s = writer.toString();
        final Matcher matcher = check.matcher(s);
        if (matcher.find()) {
            if (matcher.group(1).equals("true")) {
                return s.replaceFirst("<META.+?>", "");
            }
        }
    } catch (Exception e) {
        Logger.getInstance(getClass().getName()).error(e);
    }
    return null;
}
Also used : Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Matcher(java.util.regex.Matcher) JDOMSource(org.jdom.transform.JDOMSource) JDOMException(org.jdom.JDOMException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 32 with Transformer

use of javax.xml.transform.Transformer in project jangaroo-tools by CoreMedia.

the class PomConverter method writePom.

/**
   * Serializes the given DOM document into a POM file within the given directory.
   */
private static void writePom(Document document, File projectBaseDir) throws MojoExecutionException {
    try {
        File pomFile = new File(projectBaseDir, "pom.xml");
        // keep trailing whitespace because it's not reproduced by the transformer and we want to keep the diff small
        String pom = readFileToString(pomFile);
        String trailingWhitespace = pom.substring(pom.lastIndexOf('>') + 1);
        PrintWriter pomWriter = new PrintWriter(pomFile);
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            // the transformer does not reproduce the new line after the XML declaration, so we do it on our own
            // see https://bugs.openjdk.java.net/browse/JDK-7150637
            transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
            if (document.getXmlEncoding() != null) {
                pomWriter.print("<?xml version=\"");
                pomWriter.print(document.getXmlVersion());
                pomWriter.print("\" encoding=\"");
                pomWriter.print(document.getXmlEncoding());
                pomWriter.println("\"?>");
            }
            transformer.transform(new DOMSource(document), new StreamResult(pomWriter));
            pomWriter.write(trailingWhitespace);
        } finally {
            pomWriter.close();
        }
    } catch (IOException e) {
        throw new MojoExecutionException("error while generating modified POM", e);
    } catch (TransformerException e) {
        throw new MojoExecutionException("error while generating modified POM", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) IOException(java.io.IOException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Example 33 with Transformer

use of javax.xml.transform.Transformer in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<QuantityData> data, OutputStream stream) throws IoParseException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = MimeType.APPLICATION_PDF.getMimeType();
        Fop fop = fopFactory.newFop(mimeType, stream);
        //FopFactory fopFactory = FopFactory.newInstance(cfg);
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        //FopFactory fopFactory = fopFactoryBuilder.build();
        //Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // Create PDF via XSLT transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        StreamSource transformationRule = getTransforamtionRule();
        Transformer transformer = transFact.newTransformer(transformationRule);
        Source source = new StreamSource(document.newInputStream());
        Result result = new SAXResult(fop.getDefaultHandler());
        if (LOGGER.isDebugEnabled()) {
            try {
                File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
                StreamResult debugResult = new StreamResult(tempFile);
                transformer.transform(source, debugResult);
                String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
                LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
            } catch (IOException | TransformerException | XmlException e) {
                LOGGER.error("Could not debug XSL result output!", e);
            }
        }
        // XXX debug, diagram is not embedded
        transformer.transform(source, result);
    } catch (FOPException e) {
        throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
    } catch (SAXException | ConfigurationException | IOException e) {
        throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
    } catch (TransformerConfigurationException e) {
        throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
    } catch (TransformerException e) {
        throw new IoParseException("Could not generate PDF report!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.avalon.framework.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.avalon.framework.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.avalon.framework.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 34 with Transformer

use of javax.xml.transform.Transformer in project OpenClinica by OpenClinica.

the class OpenRosaServices method getUserXml.

private String getUserXml(ServletContext context) throws Exception {
    HashMap<String, String> value = getSubjectContextCacheValue(context);
    String studySubjectOid = value.get("studySubjectOID");
    StudySubject ssBean = ssDao.findByOcOID(studySubjectOid);
    StudyBean study = getStudy(ssBean.getStudy().getOc_oid());
    StudyBean parentStudy = getParentStudy(ssBean.getStudy().getOc_oid());
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    Element root = doc.createElement("root");
    doc.appendChild(root);
    List<UserAccount> users = userAccountDao.findNonRootNonParticipateUsersByStudyId(study.getId(), parentStudy.getId());
    for (UserAccount userAccount : users) {
        Element item = doc.createElement("item");
        Element userName = doc.createElement("user_name");
        userName.appendChild(doc.createTextNode(userAccount.getUserName()));
        Element firstName = doc.createElement("first_name");
        firstName.appendChild(doc.createTextNode(userAccount.getFirstName()));
        Element lastName = doc.createElement("last_name");
        lastName.appendChild(doc.createTextNode(userAccount.getLastName()));
        item.appendChild(userName);
        item.appendChild(firstName);
        item.appendChild(lastName);
        root.appendChild(item);
    }
    DOMSource dom = new DOMSource(doc);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(dom, result);
    String userXml = writer.toString();
    return userXml;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) StringWriter(java.io.StringWriter) StudySubject(org.akaza.openclinica.domain.datamap.StudySubject) DocumentBuilder(javax.xml.parsers.DocumentBuilder) UserAccount(org.akaza.openclinica.domain.user.UserAccount)

Example 35 with Transformer

use of javax.xml.transform.Transformer in project jdk8u_jdk by JetBrains.

the class XSLT method main.

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream resStream = new ByteArrayOutputStream();
    TransformerFactory trf = TransformerFactory.newInstance();
    Transformer tr = trf.newTransformer(new StreamSource(System.getProperty("test.src", ".") + "/" + args[1]));
    String res, expectedRes;
    tr.transform(new StreamSource(System.getProperty("test.src", ".") + "/" + args[0]), new StreamResult(resStream));
    res = resStream.toString();
    System.out.println("Transformation completed. Result:" + res);
    if (!res.replaceAll("\\s", "").equals(args[2]))
        throw new RuntimeException("Incorrect transformation result. Expected:" + args[2] + " Observed:" + res);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Transformer (javax.xml.transform.Transformer)1034 StreamResult (javax.xml.transform.stream.StreamResult)799 DOMSource (javax.xml.transform.dom.DOMSource)644 TransformerFactory (javax.xml.transform.TransformerFactory)511 TransformerException (javax.xml.transform.TransformerException)398 StringWriter (java.io.StringWriter)322 IOException (java.io.IOException)264 Document (org.w3c.dom.Document)260 StreamSource (javax.xml.transform.stream.StreamSource)204 Source (javax.xml.transform.Source)188 File (java.io.File)162 DocumentBuilder (javax.xml.parsers.DocumentBuilder)149 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)145 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)142 Element (org.w3c.dom.Element)136 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)124 SAXException (org.xml.sax.SAXException)112 StringReader (java.io.StringReader)108 Result (javax.xml.transform.Result)106 ByteArrayOutputStream (java.io.ByteArrayOutputStream)104