当前位置: 首页>>代码示例>>Java>>正文


Java TransformerFactory.setAttribute方法代码示例

本文整理汇总了Java中javax.xml.transform.TransformerFactory.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java TransformerFactory.setAttribute方法的具体用法?Java TransformerFactory.setAttribute怎么用?Java TransformerFactory.setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.xml.transform.TransformerFactory的用法示例。


在下文中一共展示了TransformerFactory.setAttribute方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getPayload

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Override
public String [] getPayload (final PayloadType type, final String body) {
	final Source input = new StreamSource (new StringReader (body));
	final StringWriter writer = new StringWriter ();
	final StreamResult output = new StreamResult (writer);
	final TransformerFactory transformerFactory = TransformerFactory.newInstance ();
	transformerFactory.setAttribute ("indent-number", 4);
	try {
		final Transformer transformer = transformerFactory.newTransformer ();
		transformer.setOutputProperty (OutputKeys.INDENT, "yes");
		transformer.transform (input, output);
		return output.getWriter ()
			.toString ()
			.split ("\n");
	}
	catch (final TransformerException e) {
		fail (XmlFormatTransformerError.class, "Error while Xml Transformation.", e);
	}
	return new String [] {};
}
 
开发者ID:WasiqB,项目名称:coteafs-services,代码行数:21,代码来源:XmlPayloadLogger.java

示例2: makeTransformer

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * Helper to make an XML Transformer.
 *
 * @param declaration If true, include the XML declaration.
 * @param indent If true, set up the transformer to indent.
 * @return A suitable {@code Transformer}.
 */
public static Transformer makeTransformer(boolean declaration,
                                          boolean indent) {
    Transformer tf = null;
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setAttribute("indent-number", new Integer(2));
        tf = factory.newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.METHOD, "xml");
        if (!declaration) {
            tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        if (indent) {
            tf.setOutputProperty(OutputKeys.INDENT, "yes");
            tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        }
    } catch (TransformerException e) {
        logger.log(Level.WARNING, "Failed to install transformer!", e);
    }
    return tf;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:29,代码来源:Utils.java

示例3: getTransformerFactory

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * Returns an instance of TransformerFactory with either a custom URIResolver
 * or Catalog.
 *
 * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
 * through the factory
 * @param useCatalog the value of USE_CATALOG
 * @param catalog a catalog
 * @param resolver a custom resolver
 * @return an instance of TransformerFactory
 * @throws Exception
 */
TransformerFactory getTransformerFactory(boolean setUseCatalog, boolean useCatalog,
        String catalog, URIResolver resolver)
        throws Exception {

    TransformerFactory factory = TransformerFactory.newInstance();
    if (setUseCatalog) {
        factory.setFeature(XMLConstants.USE_CATALOG, useCatalog);
    }
    if (catalog != null) {
        factory.setAttribute(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
    }

    // use resolver or catalog if resolver = null
    if (resolver != null) {
        factory.setURIResolver(resolver);
    }

    return factory;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:CatalogSupportBase.java

示例4: printXmlDoc

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public String printXmlDoc(Document doc) throws Exception {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);

    TransformerFactory transformerFact = TransformerFactory.newInstance();
    transformerFact.setAttribute("indent-number", new Integer(4));
    Transformer transformer;

    transformer = transformerFact.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");

    transformer.transform(new DOMSource(doc), result);
    return sw.toString();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:Bug6794483Test.java

示例5: NodesToString

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * Transforms a list of nodes into their XML string representation
 *
 * @param ns      List of nodes
 * @param verbose Flag to print a comment with the number of output nodes and before each one
 * @return String containing the XML plaintext representations of the nodes (without a common root)
 * @throws Exception Internal error
 */
public static String NodesToString(List<Node> ns, boolean verbose) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    // Set indentation to two spaces
    tf.setAttribute("indent-number", 2);
    Transformer ts = tf.newTransformer();
    // Standard XML document
    ts.setOutputProperty(OutputKeys.METHOD, "xml");
    ts.setOutputProperty(OutputKeys.STANDALONE, "yes");
    // Encoding
    ts.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    // Indent results
    ts.setOutputProperty(OutputKeys.INDENT, "yes");
    // Do not include xml declaration if there is more than one root node
    if (verbose || (ns.size() != 1)) {
        ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    } else {
        ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    }
    // Join the output of each node
    String output = "";
    if (verbose) {
        output += "<!-- Number of nodes: " + ns.size() + " -->\n";
    }
    for (int i = 0; i < ns.size(); ++i) {
        if (verbose) {
            output += "<!-- Node #" + (i + 1) + " -->\n";
        }
        output += IO.NodeToString(ns.get(i), ts) + "\n";
    }
    return output;
}
 
开发者ID:jsidrach,项目名称:xquery-engine,代码行数:40,代码来源:IO.java

示例6: saveXml

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public void saveXml(OutputStream outputStream, Document doc) throws Exception {
	TransformerFactory transFactory = TransformerFactory.newInstance();
	transFactory.setAttribute("indent-number", new Integer(3));
	Transformer transformer = transFactory.newTransformer();
	transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
	transformer.setOutputProperty(OutputKeys.VERSION, "1.0 "); 
	PrintWriter printWriter = new PrintWriter(outputStream);
	transformer.transform(new DOMSource(doc), new StreamResult(printWriter));
}
 
开发者ID:bsteker,项目名称:bdf2,代码行数:11,代码来源:W3cParser.java

示例7: testDocument

import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
 * @bug 8062518 8153082
 * Verifies that a reference to the DTM created by XSLT document function is
 * actually read from the DTM by an extension function.
 * @param xml Content of xml file to process
 * @param xsl stylesheet content that loads external document {@code externalDoc}
 *        with XSLT 'document' function and then reads it with
 *        DocumentExtFunc.test() function
 * @param externalDoc Content of the external xml document
 * @param expectedResult Expected transformation result
 **/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl,
                         final String externalDoc, final String expectedResult) throws Exception {
    // Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xsl));

    // Create factory and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
    tf.setAttribute(EXTENSION_CLASS_LOADER,
            runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));
    Transformer t = tf.newTransformer( xslsrc );
    t.setErrorListener(tf.getErrorListener());

    // Set URI Resolver to return the newly constructed xml
    // stream source object from xml test string
    t.setURIResolver(new URIResolver() {
        @Override
        public Source resolve(String href, String base)
                throws TransformerException {
            if (href.contains("externalDoc")) {
                return new StreamSource(new StringReader(externalDoc));
            } else {
                return new StreamSource(new StringReader(xml));
            }
        }
    });

    // Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);

    //Transform the xml
    t.transform(src, xmlResultStream);

    // If the document can't be accessed and the bug is in place then
    // reported exception will be thrown during transformation
    System.out.println("Transformation result:"+xmlResultString.toString().trim());

    // Check the result - it should contain two (node name, node values) entries -
    // one for original document, another for a document created with
    // call to 'document' function
    assertEquals(xmlResultString.toString().trim(), expectedResult);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:57,代码来源:XSLTFunctionsTest.java


注:本文中的javax.xml.transform.TransformerFactory.setAttribute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。