javax.xml.transform.Transformer#transform ( )源码实例Demo

下面列出了javax.xml.transform.Transformer#transform ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jpx   文件: XML.java
static Document clone(final Document doc) {
	if (doc == null) return null;

	try {
		final Transformer transformer = TFHolder.INSTANCE.factory
			.newTransformer();

		final DOMSource source = new DOMSource(doc);
		final DOMResult result = new DOMResult();
		transformer.transform(source,result);
		return (Document)result.getNode();
	} catch (TransformerException e) {
		throw (DOMException)
			new DOMException(DOMException.NOT_SUPPORTED_ERR, e.getMessage())
				.initCause(e);
	}
}
 
源代码2 项目: cloudstack   文件: XmlToHtmlConverter.java
public void generateIndividualCommandPages() {
    for (String commandName : allCommandNames) {

        try {

            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource("generatecommands.xsl"));

            transformer.transform
            // Modify this path to the location of the input files on your system.
                    (new javax.xml.transform.stream.StreamSource("apis/" + commandName + ".xml"),
                    // Modify this path with the desired output location.
                            new javax.xml.transform.stream.StreamResult(new FileOutputStream("html/apis/" + commandName + ".html")));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
源代码3 项目: neoscada   文件: XMLBase.java
protected void storeXmlDocument ( final Document doc, final File file ) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException
{
    // create output directory

    file.getParentFile ().mkdirs ();

    // write out xml

    final TransformerFactory transformerFactory = TransformerFactory.newInstance ();

    final Transformer transformer = transformerFactory.newTransformer ();
    transformer.setOutputProperty ( OutputKeys.INDENT, "yes" ); //$NON-NLS-1$

    final DOMSource source = new DOMSource ( doc );
    final StreamResult result = new StreamResult ( file );
    transformer.transform ( source, result );
}
 
源代码4 项目: MogwaiERDesignerNG   文件: XMLUtils.java
public void transform(Document aDocument, Writer aWriter)
		throws TransformerException {

	ApplicationPreferences thePreferences = ApplicationPreferences
			.getInstance();

	Transformer theTransformer = transformerFactory.newTransformer();
	theTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
	theTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
	theTransformer.setOutputProperty(OutputKeys.ENCODING, PlatformConfig
			.getXMLEncoding());
	theTransformer.setOutputProperty(
			"{http://xml.apache.org/xslt}indent-amount", ""
					+ thePreferences.getXmlIndentation());
	theTransformer.transform(new DOMSource(aDocument), new StreamResult(
			aWriter));
}
 
源代码5 项目: eclair   文件: XPathMasker.java
@Override
public String process(String string) {
    if (xPathExpressions.isEmpty()) {
        return string;
    }
    InputStream stream = new ByteArrayInputStream(string.getBytes());
    try {
        Document document = documentBuilderFactory.newDocumentBuilder().parse(stream);
        XPath xPath = xPathfactory.newXPath();
        for (String xPathExpression : xPathExpressions) {
            NodeList nodeList = (NodeList) xPath.compile(xPathExpression).evaluate(document, XPathConstants.NODESET);
            for (int a = 0; a < nodeList.getLength(); a++) {
                nodeList.item(a).setTextContent(replacement);
            }
        }
        StringWriter writer = new StringWriter();
        Transformer transformer = transformerFactory.newTransformer();
        for (Map.Entry<String, String> entry : outputProperties.entrySet()) {
            transformer.setOutputProperty(entry.getKey(), entry.getValue());
        }
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        return writer.getBuffer().toString();
    } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException | TransformerException e) {
        throw new IllegalArgumentException(e);
    }
}
 
源代码6 项目: wandora   文件: AbstractBingExtractor.java
protected String getStringFromDocument(Document doc) {
    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);

        return writer.toString();
    }

    catch(TransformerException ex) {
        ex.printStackTrace();
        return null;
    }
}
 
源代码7 项目: freehealth-connector   文件: ConnectorXmlUtils.java
public static String format(String unformattedXml, Source xslt) {
   try {
      Document doc = parseXmlFile(unformattedXml);
      DOMSource domSource = new DOMSource(doc);
      StringWriter writer = new StringWriter();
      StreamResult result = new StreamResult(writer);
      TransformerFactory tf = TransformerFactory.newInstance();
      Transformer transformer = null;
      if (xslt != null) {
         transformer = tf.newTransformer(xslt);
      } else {
         transformer = tf.newTransformer();
      }

      transformer.setOutputProperty("indent", "yes");
      transformer.setOutputProperty("omit-xml-declaration", "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(1));
      transformer.transform(domSource, result);
      return writer.toString();
   } catch (Exception var8) {
      throw new InstantiationException(var8);
   }
}
 
源代码8 项目: utah-parser   文件: ConfigTests.java
/**
 * Build a Reader for the document - this will contain the XML doc for the parser
 * @return the reader
 * @throws TransformerException
 */
private Reader buildDocReader() throws TransformerException {
  // use the DOM writing tools to write the XML to this document
  StringWriter writer = new StringWriter();
  TransformerFactory tFactory = TransformerFactory.newInstance();
  Transformer transformer = tFactory.newTransformer();

  DOMSource source = new DOMSource(document);
  StreamResult result = new StreamResult(writer);
  transformer.transform(source, result);

  // return this as a reader
  return new StringReader(writer.toString());
}
 
源代码9 项目: openjdk-jdk9   文件: ExternalMetadataReader.java
public static JavaWsdlMappingType transformAndRead(Source src, boolean disableXmlSecurity) throws TransformerException, JAXBException {
    Source xsl = new StreamSource(Util.class.getResourceAsStream(TRANSLATE_NAMESPACES_XSL));
    JAXBResult result = new JAXBResult(jaxbContext(disableXmlSecurity));
    TransformerFactory tf = XmlUtil.newTransformerFactory(!disableXmlSecurity);
    Transformer transformer = tf.newTemplates(xsl).newTransformer();
    transformer.transform(src, result);
    return getJavaWsdlMapping(result.getResult());
}
 
源代码10 项目: dragonwell8_jdk   文件: XSLT.java
public static void main(String[] args) throws TransformerException {
    ByteArrayOutputStream resStream = new ByteArrayOutputStream();
    TransformerFactory trf = TransformerFactory.newInstance();
    Transformer tr = trf.newTransformer(new StreamSource(System.getProperty("test.src", ".") + XSLTRANSFORMER));
    tr.transform(new StreamSource(System.getProperty("test.src", ".") + XMLTOTRANSFORM), new StreamResult(resStream));
    System.out.println("Transformation completed. Result:" + resStream.toString());
    if (!resStream.toString().equals(EXPECTEDRESULT)) {
        throw new RuntimeException("Incorrect transformation result");
    }
}
 
源代码11 项目: TranskribusCore   文件: TrpXPathProcessor.java
public void writeToFile(Document doc, File outFile, boolean doIndent) throws TransformerFactoryConfigurationError, TransformerException {
       Transformer transformer = TransformerFactory.newInstance().newTransformer();
      	transformer.setOutputProperty(OutputKeys.INDENT, doIndent ? "yes" : "no");
       DOMSource source = new DOMSource(doc);
       StreamResult file = new StreamResult(outFile);
       transformer.transform(source, file);
}
 
源代码12 项目: Cynthia   文件: XMLUtil.java
/**
 * 
 * @description:TODO
 * @date:2014-11-11 下午4:11:25
 * @version:v1.0
 * @param document
 * @param encode
 * @param os
 * @throws TransformerException
 */
public static void document2OutputStream(Document document, String encode, OutputStream os) throws TransformerException
{
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty("encoding", encode);
    transformer.setOutputProperty("indent", "yes");

    transformer.transform(source, new StreamResult(os));
}
 
private void performTransform(OutputStream os, IBaseResource resource, String styleSheet) {

        // Input xml data file
        ClassLoader classLoader = getContextClassLoader();

        // Input xsl (stylesheet) file
        String xslInput = classLoader.getResource(styleSheet).getFile();
        log.debug("xslInput = "+xslInput);
        // Set the property to use xalan processor
        System.setProperty("javax.xml.transform.TransformerFactory",
                "org.apache.xalan.processor.TransformerFactoryImpl");

        // try with resources
        try {
            InputStream xml = new ByteArrayInputStream(ctx.newXmlParser().encodeResourceToString(resource).getBytes(StandardCharsets.UTF_8));

            // For Windows repace escape sequence
            xslInput = xslInput.replace("%20"," ");
            log.debug("open fileInputStream for xsl "+xslInput);
            FileInputStream xsl = new FileInputStream(xslInput);

            // Instantiate a transformer factory
            TransformerFactory tFactory = TransformerFactory.newInstance();

            // Use the TransformerFactory to process the stylesheet source and produce a Transformer
            StreamSource styleSource = new StreamSource(xsl);
            Transformer transformer = tFactory.newTransformer(styleSource);

            // Use the transformer and perform the transformation
            StreamSource xmlSource = new StreamSource(xml);
            StreamResult result = new StreamResult(os);
            log.trace("Transforming");
            transformer.transform(xmlSource, result);
        } catch (Exception ex) {
            log.error(ex.getMessage());
            ex.printStackTrace();
            throw new InternalErrorException(ex.getMessage());
        }

    }
 
源代码14 项目: factura-electronica   文件: CFDv2.java
byte[] getOriginalBytes() throws Exception {
    JAXBSource in = new JAXBSource(context, document);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Result out = new StreamResult(baos);
    TransformerFactory factory = tf;
    if (factory == null) {
        factory = TransformerFactory.newInstance();
        factory.setURIResolver(new URIResolverImpl());
    }
    Transformer transformer = factory.newTransformer(new StreamSource(getClass().getResourceAsStream(XSLT)));
    transformer.transform(in, out);
    return baos.toByteArray();
}
 
源代码15 项目: factura-electronica   文件: CFDv33.java
byte[] getOriginalBytes(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Source source = new StreamSource(in);
    Result out = new StreamResult(baos);
    TransformerFactory factory = tf;
    if (factory == null) {
        factory = TransformerFactory.newInstance();
        factory.setURIResolver(new URIResolverImpl());
    }
    Transformer transformer = factory.newTransformer(new StreamSource(getClass().getResourceAsStream(XSLT)));
    transformer.transform(source, out);
    in.close();
    return baos.toByteArray();
}
 
private String convertNodeToText(Node node )throws TransformerException {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter sw = new StringWriter();
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}
 
源代码17 项目: openjdk-jdk8u   文件: XPathNegativeZero.java
private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();

    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);

    t.transform(src, res);
    return writer.toString();
}
 
源代码18 项目: Juicebox   文件: XMLFileWriter.java
private static void writeXML() throws TransformerException {
    DOMSource domSource = new DOMSource(xmlDoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, streamResult);
}
 
源代码19 项目: openjdk-jdk8u   文件: TransformerTest.java
private StringWriter transformResourceToStringWriter(Transformer transformer, String xmlResource) throws TransformerException {
    StringWriter sw = new StringWriter();
    transformer.transform(new StreamSource(getClass().getResource(xmlResource).toString()), new StreamResult(sw));
    return sw;
}
 
源代码20 项目: dss   文件: SimpleCertificateReportFacade.java
public void generateHtmlReport(XmlSimpleCertificateReport simpleCertificateReport, Result result) throws IOException, TransformerException, JAXBException {
	Transformer transformer = SimpleCertificateReportXmlDefiner.getHtmlBootstrap4Templates().newTransformer();
	transformer.transform(new JAXBSource(getJAXBContext(), wrap(simpleCertificateReport)), result);
}