Search in sources :

Example 26 with TransformerException

use of javax.xml.transform.TransformerException in project uPortal by Jasig.

the class SitemapTest method testStylesheetCompilation.

@Test
public void testStylesheetCompilation() throws IOException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    Resource resource = new ClassPathResource(STYLESHEET_LOCATION);
    Source source = new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString());
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer(source);
        transformer.setParameter(SitemapPortletController.USE_TAB_GROUPS, useTabGroups);
        transformer.setParameter(SitemapPortletController.USER_LANG, "en_US");
        transformer.setParameter(XsltPortalUrlProvider.CURRENT_REQUEST, request);
        transformer.setParameter(XsltPortalUrlProvider.XSLT_PORTAL_URL_PROVIDER, this.xsltPortalUrlProvider);
        Source xmlSource = new StreamSource(new ClassPathResource(XML_LOCATION).getFile());
        CharArrayWriter buffer = new CharArrayWriter();
        TransformerUtils.enableIndenting(transformer);
        transformer.transform(xmlSource, new StreamResult(buffer));
        if (logger.isTraceEnabled()) {
            logger.trace("XML: " + new String(buffer.toCharArray()));
        }
    } catch (TransformerConfigurationException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        logger.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) StreamSource(javax.xml.transform.stream.StreamSource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) MessageSource(org.springframework.context.MessageSource) StaticMessageSource(org.springframework.context.support.StaticMessageSource) CharArrayWriter(java.io.CharArrayWriter) TransformerException(javax.xml.transform.TransformerException) Test(org.junit.Test)

Example 27 with TransformerException

use of javax.xml.transform.TransformerException in project GDSC-SMLM by aherbert.

the class BatchPeakFit method setParameters.

/**
	 * Modify the XML document using the specified values for the given parameter. For each value
	 * call the method recursively for the next parameter. If there are no more parameters
	 * then add the XML document to the xmlSettings.
	 * 
	 * @param parameters
	 *            The list of parameters
	 * @param i
	 *            Parameter to process
	 * @param doc
	 *            The XML document containing all the current parameters
	 * @param xmlSettings
	 *            A list of XML parameter settings
	 */
private void setParameters(ArrayList<ParameterSettings> parameters, int i, Document doc, ArrayList<String> xmlSettings) {
    if (i < parameters.size()) {
        ParameterSettings param = parameters.get(i);
        NodeList nodes = doc.getElementsByTagName(param.name);
        if (nodes.getLength() == 1) {
            // For each value, set the parameter and move to the next
            String[] values = param.value.split(",");
            for (String value : values) {
                Node node = nodes.item(0);
                node.setTextContent(value);
                setParameters(parameters, i + 1, doc, xmlSettings);
            }
        } else {
            // Just move to the next parameter
            setParameters(parameters, i + 1, doc, xmlSettings);
        }
    } else {
        // Add the final XML to the parameters to run
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(doc), new StreamResult(writer));
            xmlSettings.add(writer.getBuffer().toString());
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}
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) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ParameterSettings(gdsc.smlm.ij.settings.ParameterSettings) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) TransformerException(javax.xml.transform.TransformerException)

Example 28 with TransformerException

use of javax.xml.transform.TransformerException in project adempiere by adempiere.

the class ModelExporter method doIt.

/**
	 * Process
	 * 
	 * @return info
	 */
protected String doIt() throws Exception {
    ExportHelper expHelper = new ExportHelper(getCtx(), p_AD_Client_ID);
    MEXPFormat exportFormat = new MEXPFormat(getCtx(), p_EXP_Format_ID, get_TrxName());
    File file = new File(p_FileName);
    Document doc = expHelper.exportRecord(exportFormat, "", MReplicationStrategy.REPLICATION_TABLE, X_AD_ReplicationTable.REPLICATIONTYPE_Merge, ModelValidator.TYPE_AFTER_CHANGE);
    // Save the document to the disk file
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    //   tranFactory.setAttribute("indent-number", 4); //Adempiere-65 change    
    Transformer aTransformer = tranFactory.newTransformer();
    aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
    aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
    Source src = new DOMSource(doc);
    // =================================== Write to String
    Writer writer = new StringWriter();
    Result dest2 = new StreamResult(writer);
    aTransformer.transform(src, dest2);
    // =================================== Write to Disk
    try {
        Result dest = new StreamResult(file);
        aTransformer.transform(src, dest);
        writer.flush();
        writer.close();
    } catch (TransformerException ex) {
        ex.printStackTrace();
        throw ex;
    }
    return "Exported";
}
Also used : MEXPFormat(org.compiere.model.MEXPFormat) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) StringWriter(java.io.StringWriter) File(java.io.File) StringWriter(java.io.StringWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 29 with TransformerException

use of javax.xml.transform.TransformerException 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 30 with TransformerException

use of javax.xml.transform.TransformerException 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

TransformerException (javax.xml.transform.TransformerException)808 Transformer (javax.xml.transform.Transformer)364 StreamResult (javax.xml.transform.stream.StreamResult)362 DOMSource (javax.xml.transform.dom.DOMSource)311 IOException (java.io.IOException)277 TransformerFactory (javax.xml.transform.TransformerFactory)184 Document (org.w3c.dom.Document)161 StringWriter (java.io.StringWriter)159 SAXException (org.xml.sax.SAXException)157 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)156 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)131 Source (javax.xml.transform.Source)100 StreamSource (javax.xml.transform.stream.StreamSource)94 Element (org.w3c.dom.Element)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)83 File (java.io.File)74 Node (org.w3c.dom.Node)65 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 StringReader (java.io.StringReader)59