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


Java Transformer.transform方法代碼示例

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


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

示例1: doTransform

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * Performs the XSLT transformation.
 */
private String doTransform(final Transformer transformer, final Source input, final URIResolver resolver)
{
	final StringWriter writer = new StringWriter();
	final StreamResult output = new StreamResult(writer);

	if( resolver != null )
	{
		transformer.setURIResolver(resolver);
	}

	try
	{
		transformer.transform(input, output);
	}
	catch( final TransformerException ex )
	{
		throw new RuntimeException("Error transforming XSLT", ex);
	}

	return writer.toString();
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:25,代碼來源:XsltServiceImpl.java

示例2: writeXml

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * Write out the non-default properties in this configuration to the given {@link java.io.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:ampool,項目名稱:monarch,代碼行數:23,代碼來源:Configuration.java

示例3: prettyPrintXMLString

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
private static void prettyPrintXMLString(String xml,LogLevel level) {
    if (isValidXML(xml)) {
        log(showline(), level);
        try {
            Source xmlInput = new StreamSource(new StringReader(xml));
            StreamResult xmlOutput = new StreamResult(new StringWriter());
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.transform(xmlInput, xmlOutput);
            log(xmlOutput.getWriter().toString().replaceFirst(">", ">\n"), level);
        } catch (TransformerException e) {
            log("[XML] : Error => Wrong XML Format [ " + e.getMessage() + " ]", LogLevel.ERROR);
        }

        log(showline(), level);
    }
    else{
        log("[XML] : Error => Wrong XML Format ", LogLevel.WARN);
    }
}
 
開發者ID:afiqiqmal,項目名稱:EzyLogger,代碼行數:22,代碼來源:LogPrint.java

示例4: soapMessage2String

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * Gets the SOAP message pretty printed as escaped xml for displaying in
 * browser
 * 
 * @param msg
 * @return
 */
private String soapMessage2String(SOAPMessage msg) {
    if (msg == null)
        return "";

    try (ByteArrayOutputStream streamOut = new ByteArrayOutputStream();) {
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "2");
        Source sc = msg.getSOAPPart().getContent();
        StreamResult result = new StreamResult(streamOut);
        transformer.transform(sc, result);

        String strMessage = streamOut.toString();
        return escapeXmlString(strMessage);
    } catch (Exception e) {
        System.out.println("Exception in printing SOAP message: "
                + e.getMessage());
        return "";
    }

}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:31,代碼來源:VersionHandlerCtmg.java

示例5: printXmlFile

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * Outputs the given XML {@link Document} to the file {@code outFile}.
 *
 * TODO right now reformats the document. Needs to output as-is, respecting white-space.
 *
 * @param doc The document to output. Must not be null.
 * @param outFile The {@link File} where to write the document.
 * @param log A log in case of error.
 * @return True if the file was written, false in case of error.
 */
static boolean printXmlFile(
        @NonNull Document doc,
        @NonNull File outFile,
        @NonNull IMergerLog log) {
    // Quick thing based on comments from http://stackoverflow.com/questions/139076
    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");         //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");                   //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.INDENT, "yes");                       //$NON-NLS-1$
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",     //$NON-NLS-1$
                             "4");                                            //$NON-NLS-1$
        tf.transform(new DOMSource(doc), new StreamResult(outFile));
        return true;
    } catch (TransformerException e) {
        log.error(Severity.ERROR,
                new FileAndLine(outFile.getName(), 0),
                "Failed to write XML file: %1$s",
                e.toString());
        return false;
    }
}
 
開發者ID:tranleduy2000,項目名稱:javaide,代碼行數:33,代碼來源:MergerXmlUtils.java

示例6: nodeToString

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:1991wangliang,項目名稱:pay,代碼行數:29,代碼來源:XmlUtils.java

示例7: xsltprocess

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
public void xsltprocess(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
    // 1. Instantiate a TransformerFactory.
    SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    
    // 2. Use the TransformerFactory to process the stylesheet Source and
    //    generate a Transformer.
    InputStream is = getClass().getResourceAsStream("xmg2pol.xsl");
    Transformer transformer = tFactory.newTransformer (new StreamSource(is));
    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "polarities.dtd,xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    
    // 3. Use the Transformer to transform an XML Source and send the
    //    output to a Result object.
    try {
	    String input = args[0];
	    String output= args[1];
	    SAXSource saxs = new SAXSource(new InputSource(input));
		XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
		saxReader.setEntityResolver(new MyEntityResolver());
		saxs.setXMLReader(saxReader);
	    transformer.transform(saxs, new StreamResult(new OutputStreamWriter(new FileOutputStream(output), "utf-8")));
   	} catch (Exception e) {
   		e.printStackTrace();
   	}
}
 
開發者ID:spetitjean,項目名稱:TuLiPA-frames,代碼行數:26,代碼來源:TransformPolarity.java

示例8: MexEntityResolver

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
public MexEntityResolver(List<? extends Source> wsdls) throws IOException {
    Transformer transformer = XmlUtil.newTransformer();
    for (Source source : wsdls) {
        XMLStreamBufferResult xsbr = new XMLStreamBufferResult();
        try {
            transformer.transform(source, xsbr);
        } catch (TransformerException e) {
            throw new WebServiceException(e);
        }
        String systemId = source.getSystemId();

        //TODO: can we do anything if the given mex Source has no systemId?
        if(systemId != null){
            SDDocumentSource doc = SDDocumentSource.create(JAXWSUtils.getFileOrURL(systemId), xsbr.getXMLStreamBuffer());
            this.wsdls.put(systemId, doc);
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:MexEntityResolver.java

示例9: moduleDoc

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * CLI option: print the documentation for the specified module.
 * @param name
 * @throws TransformerException
 * @throws XPathExpressionException
 * @throws UnsupportedServiceException
 * @throws ServiceInstanciationException
 * @throws AmbiguousAliasException
 */
@CLIOption(value="-moduleDoc", stop=true)
public final void moduleDoc(String name) throws TransformerException, XPathExpressionException, UnsupportedServiceException, AmbiguousAliasException { 
	Document doc = getModuleDocumentation(name);
	// same ClassLoader as this class
	Source xslt = new StreamSource(getClass().getResourceAsStream(noColors ? "alvisnlp-doc2txt.xslt" : "alvisnlp-doc2ansi.xslt"));
	TransformerFactory transformerFactory = TransformerFactory.newInstance();
	Transformer transformer = transformerFactory.newTransformer(xslt);
	transformer.setParameter("name", bundle.getString(DocResourceConstants.MODULE_NAME).toUpperCase(locale));
	transformer.setParameter("synopsis", bundle.getString(DocResourceConstants.SYNOPSIS).toUpperCase(locale));
	transformer.setParameter("description", bundle.getString(DocResourceConstants.MODULE_DESCRIPTION).toUpperCase(locale));
	transformer.setParameter("parameters", bundle.getString(DocResourceConstants.MODULE_PARAMETERS).toUpperCase(locale));
	Source source = new DOMSource(doc);
	Result result = new StreamResult(System.out);
	transformer.transform(source, result);
}
 
開發者ID:Bibliome,項目名稱:alvisnlp,代碼行數:25,代碼來源:AbstractAlvisNLP.java

示例10: domSourceToString

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
protected String domSourceToString() throws SQLException {
    try {
        DOMSource source = new DOMSource(this.asDOMResult.getNode());
        Transformer identity = TransformerFactory.newInstance().newTransformer();
        StringWriter stringOut = new StringWriter();
        Result result = new StreamResult(stringOut);
        identity.transform(source, result);

        return stringOut.toString();
    } catch (Throwable t) {
        SQLException sqlEx = SQLError.createSQLException(t.getMessage(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
        sqlEx.initCause(t);

        throw sqlEx;
    }
}
 
開發者ID:Jugendhackt,項目名稱:OpenVertretung,代碼行數:17,代碼來源:JDBC4MysqlSQLXML.java

示例11: test

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
@Test
public void test() throws TransformerException, ParserConfigurationException, SAXException, IOException {

    Transformer transformer = TransformerFactory.newInstance().newTransformer();

    String out = USER_DIR + "Bug4693341.out";
    StreamResult result = new StreamResult(new File(out));

    String in = XML_DIR + "Bug4693341.xml";
    String golden = GOLDEN_DIR + "Bug4693341.xml";
    File file = new File(in);
    StreamSource source = new StreamSource(file);
    System.out.println(source.getSystemId());

    Files.copy(Paths.get(XML_DIR + "Bug4693341.dtd"),
            Paths.get(USER_DIR + "Bug4693341.dtd"), REPLACE_EXISTING);

    transformer.transform(source, result);

    assertTrue(compareDocumentWithGold(golden, out));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:Bug4693341.java

示例12: convertXml

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * Converts an xml node to a string.
 * 
 * @param node
 * @return
 */
public String convertXml(Node node) {
	try {
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(node);
		StringWriter sw = new StringWriter();
		StreamResult result = new StreamResult(sw);
		transformer.transform(source, result);
		return sw.toString();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
開發者ID:EixoX,項目名稱:jetfuel,代碼行數:20,代碼來源:StringAdapter.java

示例13: transformStringToWriter

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 *  Transforms an XML String using a pre-compiled {@link javax.xml.transform.Transformer}. Use {@link
 *  #getTransformer(String xslFilePath)} to produce a reusable {@link javax.xml.transform.Transformer} for a
 *  given XSL stylesheet. To convert the resulting StringWriter to a String, call StringWriter.toString().
 *
 * @param  xmlString    The XML String to transform.
 * @param  transformer  A pre-compiled {@link javax.xml.transform.Transformer} used to produce transformed
 *      output.
 * @return              A StringWriter containing the transformed content.
 */
public final static StringWriter transformStringToWriter(String xmlString, Transformer transformer) {
	try {
		StringWriter writer = new StringWriter();
		StreamSource source = new StreamSource(new StringReader(xmlString));
		transformer.transform(source, new StreamResult(writer));
		return writer;
	} catch (Throwable e) {
		prtlnErr(e);
		return new StringWriter();
	}
}
 
開發者ID:NCAR,項目名稱:joai-project,代碼行數:22,代碼來源:XSLTransformer.java

示例14: writeXML

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
/**
 * Writes dataset to an XML file using this specified formatter
 * 
 * @param outputFile
 * @param dataset
 * @throws ParserConfigurationException
 * @throws TransformerException
 * @throws FileNotFoundException
 */
public void writeXML(File outputFile, Processable<RecordType> dataset)
		throws ParserConfigurationException, TransformerException,
		FileNotFoundException {

	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	DocumentBuilder builder;

	builder = factory.newDocumentBuilder();
	Document doc = builder.newDocument();
	Element root = createRootElement(doc);

	doc.appendChild(root);

	for (RecordType record : dataset.get()) {
		root.appendChild(createElementFromRecord(record, doc));
	}

	TransformerFactory tFactory = TransformerFactory.newInstance();
	Transformer transformer;
	transformer = tFactory.newTransformer();
	transformer.setOutputProperty(OutputKeys.INDENT, "yes");
	transformer.setOutputProperty(
			"{http://xml.apache.org/xslt}indent-amount", "2");
	DOMSource source = new DOMSource(root);
	StreamResult result = new StreamResult(new FileOutputStream(outputFile));

	transformer.transform(source, result);

}
 
開發者ID:olehmberg,項目名稱:winter,代碼行數:39,代碼來源:XMLFormatter.java

示例15: outputFile

import javax.xml.transform.Transformer; //導入方法依賴的package包/類
private static void outputFile() throws TransformerException
{
	TransformerFactory transformerFactory = TransformerFactory.newInstance();
	Transformer transformer = transformerFactory.newTransformer();
	DOMSource source = new DOMSource(doc);
	StreamResult result = new StreamResult(System.out);
	transformer.transform(source, result);
}
 
開發者ID:Christopher-Chianelli,項目名稱:ccpa,代碼行數:9,代碼來源:TreeOptimizer.java


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