當前位置: 首頁>>代碼示例>>Java>>正文


Java TransformerFactory.newTransformer方法代碼示例

本文整理匯總了Java中javax.xml.transform.TransformerFactory.newTransformer方法的典型用法代碼示例。如果您正苦於以下問題:Java TransformerFactory.newTransformer方法的具體用法?Java TransformerFactory.newTransformer怎麽用?Java TransformerFactory.newTransformer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.transform.TransformerFactory的用法示例。


在下文中一共展示了TransformerFactory.newTransformer方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testTransformer

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
@Test
public void testTransformer() throws TransformerException {
    String xml = "<?xml version='1.0'?><root/>";
    ReaderStub.used = false;

    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    InputSource in = new InputSource(new StringReader(xml));
    SAXSource source = new SAXSource(in);
    StreamResult result = new StreamResult(new StringWriter());

    transformer.transform(source, result);

    assertTrue(ReaderStub.used);

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:Bug4515660.java

示例2: writeXml

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/** 
 * Write out the non-default properties in this configuration to the given
 * {@link Writer}.
 * 
 * @param out the writer to write to.
 */
public void writeXml(Writer out) throws IOException {
  Document doc = asXmlDocument();

  try {
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(out);
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();

    // Important to not hold Configuration log while writing result, since
    // 'out' may be an HDFS stream which needs to lock this configuration
    // from another thread.
    transformer.transform(source, result);
  } catch (TransformerException te) {
    throw new IOException(te);
  }
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:24,代碼來源:Configuration.java

示例3: test

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
@Test
public void test() {
    try {
        String xml = "<html><body><a href='http://example.com/bla?x&amp;y'>" + "</a></body></html>";

        Source src = new StreamSource(new StringReader(xml));
        StringWriter stringWriter = new StringWriter();
        Result res = new StreamResult(stringWriter);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "html");
        t.transform(src, res);

        // System.out.println("s = " + stringWriter.toString());
        Assert.assertTrue(stringWriter.toString().contains("&amp;"));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:Bug6559595.java

示例4: testCheckElementContentWhitespace

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
 * Test for the isIgnoringElementContentWhitespace and the
 * setIgnoringElementContentWhitespace. The xml file has all kinds of
 * whitespace,tab and newline characters, it uses the MyNSContentHandler
 * which does not invoke the characters callback when this
 * setIgnoringElementContentWhitespace is set to true.
 * @throws Exception If any errors occur.
 */
@Test
public void testCheckElementContentWhitespace() throws Exception {
    String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
    String outputFile = USER_DIR + "dbfactory02.out";
    MyErrorHandler eh = MyErrorHandler.newInstance();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);
    assertFalse(dbf.isIgnoringElementContentWhitespace());
    dbf.setIgnoringElementContentWhitespace(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setErrorHandler(eh);
    Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
    assertFalse(eh.isErrorOccured());
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tfactory = TransformerFactory.newInstance();
    Transformer transformer = tfactory.newTransformer();
    SAXResult saxResult = new SAXResult();
    try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
        saxResult.setHandler(handler);
        transformer.transform(domSource, saxResult);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:32,代碼來源:DocumentBuilderFactoryTest.java

示例5: getStringFromDocument

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
private static String getStringFromDocument(Document document) throws TransformerException {
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
   }
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:10,代碼來源:GradebookUtil.java

示例6: write

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
 * XML書き込み
 *
 * @param document xml document
 * @throws IOException IOエラー
 * @throws TransformerException XML書き込みエラー
 */
private void write(final Document document) throws IOException, TransformerException {
	TransformerFactory transformerFactory = TransformerFactory.newInstance();
	Transformer transformer = transformerFactory.newTransformer();
	transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
	transformer
			.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://cobertura.sourceforge.net/xml/coverage-04.dtd");
	try (BufferedWriter bufferedWriter = Files.newBufferedWriter(this.reportPath)) {
		transformer.transform(new DOMSource(document), new StreamResult(bufferedWriter));
	}
}
 
開發者ID:future-architect,項目名稱:uroborosql,代碼行數:19,代碼來源:CoberturaCoverageHandler.java

示例7: process

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
@Override
    public void process(ProcessingContext<Corpus> ctx, Corpus corpus) throws ModuleException {
        try {
        	Logger logger = getLogger(ctx);
            TransformerFactory factory = TransformerFactory.newInstance();
            InputStream is = xslTransform.getInputStream();
            Source transformerSource = new StreamSource(is);
            Transformer transformer = factory.newTransformer(transformerSource);
            is.close();
            transformer.setErrorListener(new ListingErrorHandler());

            libraryResolver = getLibraryResolver(ctx);
            Evaluator roots = libraryResolver.resolveNullable(this.roots);
            Evaluator fileName = this.fileName.resolveExpressions(libraryResolver);
            EvaluationContext evalCtx = new EvaluationContext(logger);

            for (Element root : Iterators.loop(getRoots(roots, evalCtx, corpus))) {
                transformer.reset();

                Document doc = XMLUtils.docBuilder.newDocument();
                doc.setUserData(ELEMENT_USER_DATA, root, null);
//				org.w3c.dom.Element elt = getElementProxy(doc, root);
//				doc.appendChild(elt);
                Source source = new DOMSource(doc);

                String fileNameString = fileName.evaluateString(evalCtx, root);
//                System.out.println("creating file: " + fileNameString);
                File outFile = new File(outDir, fileNameString);
                Result result = new StreamResult(outFile);

                transformer.transform(source, result);
            }
        } catch (DOMException | TransformerException | IOException e) {
            rethrow(e);
        }
    }
 
開發者ID:Bibliome,項目名稱:alvisnlp,代碼行數:37,代碼來源:XMLWriter2ForINIST.java

示例8: writeFile

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
private void writeFile(String title, ArrayList<String> list) throws IOException, ParserConfigurationException, TransformerException {
    System.out.println("GetEmoticons.writeFile");
    System.out.println("title = " + title);
    System.out.println("list = " + list);
    if (title == null || list == null || list.isEmpty()) return;
    String fileName = title.replaceAll("\\W", "") + ".xml";
    File file = new File("C:\\github\\AsciiGenerator\\app\\src\\main\\assets\\emoticons\\" + fileName);
    if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    org.w3c.dom.Document document = documentBuilder.newDocument();
    org.w3c.dom.Element root = document.createElement("root");
    document.appendChild(root);

    org.w3c.dom.Element name = document.createElement("name");
    name.appendChild(document.createTextNode(title));
    root.appendChild(name);

    org.w3c.dom.Element data = document.createElement("data");

    for (String s : list) {
        org.w3c.dom.Element item = document.createElement("item");
        item.appendChild(document.createTextNode(s));
        data.appendChild(item);
    }
    root.appendChild(data);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource domSource = new DOMSource(document);
    StreamResult streamResult = new StreamResult(file);
    transformer.transform(domSource, streamResult);
}
 
開發者ID:tranleduy2000,項目名稱:ascii_generate,代碼行數:34,代碼來源:GetEmoticons.java

示例9: testQueueConfigurationParser

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
* test xml generation 
* @throws ParserConfigurationException
* @throws Exception 
*/
 @Test (timeout=5000)
 public void testQueueConfigurationParser()
     throws ParserConfigurationException, Exception {
   JobQueueInfo info = new JobQueueInfo("root", "rootInfo");
   JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info");
   JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info");

   info.addChild(infoChild1);
   info.addChild(infoChild2);
   DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
       .newInstance();
   DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
   
   
   Document document = builder.newDocument();
   

   // test QueueConfigurationParser.getQueueElement 
   Element e = QueueConfigurationParser.getQueueElement(document, info);
   // transform result to string for check
   DOMSource domSource = new DOMSource(e);
   StringWriter writer = new StringWriter();
   StreamResult result = new StreamResult(writer);
   TransformerFactory tf = TransformerFactory.newInstance();
   Transformer transformer = tf.newTransformer();
   transformer.transform(domSource, result);
   String str= writer.toString();
   assertTrue(str
       .endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
 }
 
開發者ID:naver,項目名稱:hadoop,代碼行數:36,代碼來源:TestQueueConfigurationParser.java

示例10: transformer03

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
 * This tests if newTransformer(DOMSource) method returns Transformer.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void transformer03() throws Exception {
    TransformerFactory tfactory = TransformerFactory.newInstance();

    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 = tfactory.newTransformer(domSource);
    assertNotNull(transformer);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:TransformerTest.java

示例11: 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

示例12: marshalSamlXmlObject

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
 * Marshal the saml xml object to raw xml.
 *
 * @param object the object
 * @param writer the writer
 * @return the xml string
 */
public String marshalSamlXmlObject(final XMLObject object, final StringWriter writer)  {
    try {
        final MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
        final Marshaller marshaller = marshallerFactory.getMarshaller(object);
        if (marshaller == null) {
            throw new IllegalArgumentException("Cannot obtain marshaller for object " + object.getElementQName());
        }
        final Element element = marshaller.marshall(object);
        element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", SAMLConstants.SAML20_NS);
        element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#");

        final TransformerFactory transFactory = TransformerFactory.newInstance();
        final Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(new DOMSource(element), new StreamResult(writer));
        return writer.toString();
    } catch (final Exception e) {
        throw new IllegalStateException("An error has occurred while marshalling SAML object to xml", e);
    }
}
 
開發者ID:yuweijun,項目名稱:cas-server-4.2.1,代碼行數:29,代碼來源:AbstractSamlObjectBuilder.java

示例13: newFormatingTransformer

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
 * Creates a XML transformer with the following properties:<br />
 * <br />
 * character encoding: UTF-8<br />
 * output method: xml<br />
 * output version: 1.0<br />
 * indention size: 2
 */
public static Transformer newFormatingTransformer()
        throws TransformerConfigurationException {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(
            "{http://xml.apache.org/xalan}indent-amount", "2");
    return transformer;
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:22,代碼來源:Transformers.java

示例14: getIdentityTransformer

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
/**
 * Gets the shared instance of the identity transformer.
 */
public Transformer getIdentityTransformer() {
    try {
        if(identityTransformer==null) {
            TransformerFactory tf = XmlFactory.createTransformerFactory(model.options.disableXmlSecurity);
            identityTransformer = tf.newTransformer();
        }
        return identityTransformer;
    } catch (TransformerConfigurationException e) {
        throw new Error(e); // impossible
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:BGMBuilder.java

示例15: ignoreAttTest

import javax.xml.transform.TransformerFactory; //導入方法依賴的package包/類
@Test
public void ignoreAttTest() throws TransformerConfigurationException {
    /* Create a TransformFactory instance */
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    /* Create and init a StreamSource instance */
    StreamSource source = new StreamSource(new StringReader(XSL));

    transformerFactory.newTransformer(source);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:Bug4511326.java


注:本文中的javax.xml.transform.TransformerFactory.newTransformer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。