Search in sources :

Example 91 with Transformer

use of javax.xml.transform.Transformer in project azure-tools-for-java by Microsoft.

the class XMLHelper method main.

public static void main(String[] argv) {
    if (argv.length < 4) {
        System.out.println("[Failed] Please specify the arguments $XML_FILE $XPATH $NEW_VALUE $JOIN_OR_REPLACE.");
        System.exit(1);
    }
    String FILEPATH = argv[0];
    String XPATH = argv[1];
    String NEW_VALUE = argv[2];
    String CHANGETYPE = argv[3];
    boolean DISPLAY_LOG = false;
    if (argv.length > 4 && argv[4].toLowerCase().equals("true")) {
        DISPLAY_LOG = true;
    }
    try {
        System.out.println("Starting to modify XML " + FILEPATH);
        // Read the content from xml file
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(FILEPATH);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        XPathExpression expression = xpath.compile(XPATH);
        Node targetNode = (Node) expression.evaluate(doc, XPathConstants.NODE);
        String oldValue = targetNode.getNodeValue();
        String newValue = "";
        if (CHANGETYPE.equals("JOIN")) {
            newValue = oldValue + NEW_VALUE;
        }
        if (DISPLAY_LOG) {
            System.out.println("The old value is " + oldValue);
            System.out.println("The new value is " + newValue);
        }
        targetNode.setNodeValue(newValue);
        // Write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(FILEPATH));
        transformer.transform(source, result);
        System.out.println("Modify XML Finished.");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.exit(0);
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) 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) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) IOException(java.io.IOException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

Example 92 with Transformer

use of javax.xml.transform.Transformer in project azure-tools-for-java by Microsoft.

the class AIParserXMLUtility method saveXMLFile.

/**
	 * save XML file and saves XML document.
	 * 
	 * @param fileName
	 * @param doc
	 * @return XML document or <B>null</B> if error occurred
	 * @throws IOException
	 * @throws WindowsAzureInvalidProjectOperationException
	 */
protected static boolean saveXMLFile(String fileName, Document doc) throws IOException, Exception {
    File xmlFile = null;
    FileOutputStream fos = null;
    Transformer transformer;
    try {
        xmlFile = new File(fileName);
        fos = new FileOutputStream(xmlFile);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        transformer = transFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult destination = new StreamResult(fos);
        // transform source into result will do save
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, destination);
    } catch (Exception excp) {
        Activator.getDefault().log(excp.getMessage(), excp);
        throw new Exception(String.format("%s%s", Messages.saveErrMsg, excp.getMessage()));
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
    return true;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) File(java.io.File) IOException(java.io.IOException)

Example 93 with Transformer

use of javax.xml.transform.Transformer in project lwjgl by LWJGL.

the class StandalonePublisher method doWriteDocument.

/**
	 * @param content
	 * @param sr
	 * @throws TransformerFactoryConfigurationError
	 * @throws TransformerConfigurationException
	 * @throws TransformerException
	 */
private void doWriteDocument(Document content, StreamResult sr) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    Properties oprops = new Properties();
    oprops.put(OutputKeys.METHOD, "xml");
    oprops.put(OutputKeys.INDENT, "yes");
    t.setOutputProperties(oprops);
    t.transform(new DOMSource(content), sr);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Properties(java.util.Properties)

Example 94 with Transformer

use of javax.xml.transform.Transformer in project oxCore by GluuFederation.

the class Response method printDocument.

public void printDocument(OutputStream out) throws IOException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(xmlDoc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) OutputStreamWriter(java.io.OutputStreamWriter)

Example 95 with Transformer

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

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