Java Code Examples for javax.xml.transform.Transformer#getOutputProperties()

The following examples show how to use javax.xml.transform.Transformer#getOutputProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DomXmlUtils.java    From DAFramework with MIT License 6 votes vote down vote up
/**
 * 获取一个Transformer对象,由于使用时都做相同的初始化,所以提取出来作为公共方法。
 *
 * @return a Transformer encoding gb2312
 */

public static Transformer newTransformer() {
	try {
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		Properties properties = transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING, "gb2312");
		properties.setProperty(OutputKeys.METHOD, "xml");
		properties.setProperty(OutputKeys.VERSION, "1.0");
		properties.setProperty(OutputKeys.INDENT, "no");
		transformer.setOutputProperties(properties);
		return transformer;
	} catch (TransformerConfigurationException tce) {
		throw new RuntimeException(tce.getMessage());
	}
}
 
Example 2
Source File: TransformerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This tests getOutputProperties() method of Transformer.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void transformer05() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new File(TEST_XSL));
    DOMSource domSource = new DOMSource(document);

    Transformer transformer = TransformerFactory.newInstance().
            newTransformer(domSource);
    Properties prop = transformer.getOutputProperties();

    assertEquals(prop.getProperty("indent"), "yes");
    assertEquals(prop.getProperty("method"), "xml");
    assertEquals(prop.getProperty("encoding"), "UTF-8");
    assertEquals(prop.getProperty("standalone"), "no");
    assertEquals(prop.getProperty("version"), "1.0");
    assertEquals(prop.getProperty("omit-xml-declaration"), "no");
}
 
Example 3
Source File: XmlUtils.java    From alipay-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the Node/Element instance to XML payload.
 *
 * @param node the node/element instance to convert
 * @return the XML payload representing the node/element
 * @throws ApiException problem converting XML to string
 */
public static String childNodeToString(Node node) throws AlipayApiException {
    String payload = null;

    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();

        Properties props = tf.getOutputProperties();
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
        tf.setOutputProperties(props);

        StringWriter writer = new StringWriter();
        tf.transform(new DOMSource(node), new StreamResult(writer));
        payload = writer.toString();
        payload = payload.replaceAll(REG_INVALID_CHARS, " ");
    } catch (TransformerException e) {
        throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
    }

    return payload;
}
 
Example 4
Source File: XmlUtils.java    From alipay-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the Node/Document/Element instance to XML payload.
 *
 * @param node the node/document/element instance to convert
 * @return the XML payload representing the node/document/element
 * @throws ApiException problem converting XML to string
 */
public static String nodeToString(Node node) throws AlipayApiException {
    String payload = null;

    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();

        Properties props = tf.getOutputProperties();
        props.setProperty(OutputKeys.INDENT, LOGIC_YES);
        props.setProperty(OutputKeys.ENCODING, DEFAULT_ENCODE);
        tf.setOutputProperties(props);

        StringWriter writer = new StringWriter();
        tf.transform(new DOMSource(node), new StreamResult(writer));
        payload = writer.toString();
        payload = payload.replaceAll(REG_INVALID_CHARS, " ");
    } catch (TransformerException e) {
        throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
    }

    return payload;
}
 
Example 5
Source File: XmlUtils.java    From alipay-sdk with Apache License 2.0 6 votes vote down vote up
/**
    * Transforms the XML content to XHTML/HTML format string with the XSL.
    *
    * @param payload the XML payload to convert
    * @param xsltFile the XML stylesheet file
    * @return the transformed XHTML/HTML format string
    * @throws ApiException problem converting XML to HTML
    */
   public static String xmlToHtml(String payload, File xsltFile)
throws AlipayApiException {
       String result = null;

       try {
           Source template = new StreamSource(xsltFile);
           Transformer transformer = TransformerFactory.newInstance()
                   .newTransformer(template);

           Properties props = transformer.getOutputProperties();
           props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
           transformer.setOutputProperties(props);

           StreamSource source = new StreamSource(new StringReader(payload));
           StreamResult sr = new StreamResult(new StringWriter());
           transformer.transform(source, sr);

           result = sr.getWriter().toString();
       } catch (TransformerException e) {
           throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
       }

       return result;
   }
 
Example 6
Source File: XmlUtils.java    From pay with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the Node/Element instance to XML payload.
 *
 * @param node the node/element instance to convert
 * @return the XML payload representing the node/element
 * @throws AlipayApiException problem converting XML to string
 */
public static String childNodeToString(Node node) throws AlipayApiException {
    String payload = null;

    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();

        Properties props = tf.getOutputProperties();
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
        tf.setOutputProperties(props);

        StringWriter writer = new StringWriter();
        tf.transform(new DOMSource(node), new StreamResult(writer));
        payload = writer.toString();
        payload = payload.replaceAll(REG_INVALID_CHARS, " ");
    } catch (TransformerException e) {
        throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
    }

    return payload;
}
 
Example 7
Source File: XmlUtils.java    From pay with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the Node/Document/Element instance to XML payload.
 *
 * @param node the node/document/element instance to convert
 * @return the XML payload representing the node/document/element
 * @throws AlipayApiException problem converting XML to string
 */
public static String nodeToString(Node node) throws AlipayApiException {
    String payload = null;

    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();

        Properties props = tf.getOutputProperties();
        props.setProperty(OutputKeys.INDENT, LOGIC_YES);
        props.setProperty(OutputKeys.ENCODING, DEFAULT_ENCODE);
        tf.setOutputProperties(props);

        StringWriter writer = new StringWriter();
        tf.transform(new DOMSource(node), new StreamResult(writer));
        payload = writer.toString();
        payload = payload.replaceAll(REG_INVALID_CHARS, " ");
    } catch (TransformerException e) {
        throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
    }

    return payload;
}
 
Example 8
Source File: XmlUtils.java    From pay with Apache License 2.0 6 votes vote down vote up
/**
    * Transforms the XML content to XHTML/HTML format string with the XSL.
    *
    * @param payload the XML payload to convert
    * @param xsltFile the XML stylesheet file
    * @return the transformed XHTML/HTML format string
    * @throws AlipayApiException problem converting XML to HTML
    */
   public static String xmlToHtml(String payload, File xsltFile)
throws AlipayApiException {
       String result = null;

       try {
           Source template = new StreamSource(xsltFile);
           Transformer transformer = TransformerFactory.newInstance()
                   .newTransformer(template);

           Properties props = transformer.getOutputProperties();
           props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
           transformer.setOutputProperties(props);

           StreamSource source = new StreamSource(new StringReader(payload));
           StreamResult sr = new StreamResult(new StringWriter());
           transformer.transform(source, sr);

           result = sr.getWriter().toString();
       } catch (TransformerException e) {
           throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
       }

       return result;
   }
 
Example 9
Source File: DomXmlUtils.java    From wES with MIT License 6 votes vote down vote up
/**
 * 获取一个Transformer对象,由于使用时都做相同的初始化,所以提取出来作为公共方法。
 *
 * @return a Transformer encoding gb2312
 */

public static Transformer newTransformer() {
	try {
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		Properties properties = transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING, "gb2312");
		properties.setProperty(OutputKeys.METHOD, "xml");
		properties.setProperty(OutputKeys.VERSION, "1.0");
		properties.setProperty(OutputKeys.INDENT, "no");
		transformer.setOutputProperties(properties);
		return transformer;
	} catch (TransformerConfigurationException tce) {
		throw new RuntimeException(tce.getMessage());
	}
}
 
Example 10
Source File: ProjectUtil.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
public static void removeHandset(String file,String name)throws Exception{
	DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
	DocumentBuilder dombuilder=domfac.newDocumentBuilder();
       FileInputStream is=new FileInputStream(file);
       
       Document doc=dombuilder.parse(is);
       NodeList devices = doc.getElementsByTagName("devices");
       NodeList nodeList = doc.getElementsByTagName("device");
       for(int i=0;i<nodeList.getLength();i++){
       	Node deviceNode = nodeList.item(i);
       	if(deviceNode.getTextContent().equals(name)){
       		devices.item(0).removeChild(deviceNode);
       	}
       }
      
       //save
       TransformerFactory tf=TransformerFactory.newInstance();
       Transformer t=tf.newTransformer();
       Properties props=t.getOutputProperties();
       props.setProperty(OutputKeys.ENCODING, "GB2312");
       t.setOutputProperties(props);
       DOMSource dom=new DOMSource(doc);
       StreamResult sr=new StreamResult(file);
       t.transform(dom, sr);
}
 
Example 11
Source File: ProjectUtil.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
public static void addHandset(String file,String name,Hashtable<String,String> attri) throws Exception{
	DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
	DocumentBuilder dombuilder=domfac.newDocumentBuilder();
       FileInputStream is=new FileInputStream(file);
       
       Document doc=dombuilder.parse(is);
       NodeList nodeList = doc.getElementsByTagName("devices");
       if(nodeList != null && nodeList.getLength()>=1){
       	Node deviceNode = nodeList.item(0);
       	Element device = doc.createElement("device"); 
       	device.setTextContent(name);
       	for(Iterator itrName=attri.keySet().iterator();itrName.hasNext();){
   			String attriKey = (String)itrName.next();
   			String attriValue = (String)attri.get(attriKey);
   			device.setAttribute(attriKey, attriValue);
       	}
       	deviceNode.appendChild(device);
       }
      
       //save
       TransformerFactory tf=TransformerFactory.newInstance();
       Transformer t=tf.newTransformer();
       Properties props=t.getOutputProperties();
       props.setProperty(OutputKeys.ENCODING, "GB2312");
       t.setOutputProperties(props);
       DOMSource dom=new DOMSource(doc);
       StreamResult sr=new StreamResult(file);
       t.transform(dom, sr);
}
 
Example 12
Source File: ProjectUtil.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
public static void addApp(String file,String name,Hashtable<String,String> attri) throws Exception{
	DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
	DocumentBuilder dombuilder=domfac.newDocumentBuilder();
       FileInputStream is=new FileInputStream(file);
       
       Document doc=dombuilder.parse(is);
       
       NodeList nodeList = doc.getElementsByTagName("app");
       if(nodeList != null && nodeList.getLength()>=1){
       	Node deviceNode = nodeList.item(0);
       	Element device = doc.createElement("aut"); 
       	device.setTextContent(name);
       	for(Iterator itrName=attri.keySet().iterator();itrName.hasNext();){
   			String attriKey = (String)itrName.next();
   			String attriValue = (String)attri.get(attriKey);
   			device.setAttribute(attriKey, attriValue);
       	}
       	deviceNode.appendChild(device);
       }
      
       //save
       TransformerFactory tf=TransformerFactory.newInstance();
       Transformer t=tf.newTransformer();
       Properties props=t.getOutputProperties();
       props.setProperty(OutputKeys.ENCODING, "GB2312");
       t.setOutputProperties(props);
       DOMSource dom=new DOMSource(doc);
       StreamResult sr=new StreamResult(file);
       t.transform(dom, sr);
}
 
Example 13
Source File: ProjectFile.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
     * Save project file
     *
     * @param aFile File name
     * @throws javax.xml.parsers.ParserConfigurationException
     */
    public void saveProjFile(String aFile) throws ParserConfigurationException {
        _fileName = aFile;

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element root = doc.createElement("MeteoInfo");
        File af = new File(aFile);
        Attr fn = doc.createAttribute("File");
        Attr type = doc.createAttribute("Type");
        fn.setValue(af.getName());
        type.setValue("projectfile");
        root.setAttributeNode(fn);
        root.setAttributeNode(type);
        doc.appendChild(root);

        //Add language element
        //addLanguageElement(doc, root, Thread.CurrentThread.CurrentUICulture.Name);

        //Add LayersLegend content
        _mainForm.getMapDocument().getMapLayout().updateMapFrameOrder();
        _mainForm.getMapDocument().exportProjectXML(doc, root, _fileName);

        //Add MapLayout content
        _mainForm.getMapDocument().getMapLayout().exportProjectXML(doc, root);

        //Save project file            
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(doc);          
            
            Properties properties = transformer.getOutputProperties();
            properties.setProperty(OutputKeys.ENCODING, "UTF-8");
            properties.setProperty(OutputKeys.INDENT, "yes");
            properties.setProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.setOutputProperties(properties);
//            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
//            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            //PrintWriter pw = new PrintWriter(new FileOutputStream(aFile));
            FileOutputStream out = new FileOutputStream(aFile);
            StreamResult result = new StreamResult(out);
            transformer.transform(source, result);
        } catch (TransformerException mye) {
        } catch (IOException exp) {
        }
    }