📄 examples.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* $Id: Examples.java 470245 2006-11-02 06:34:33Z minchau $
*/
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Some examples to show how the Simple API for Transformations
* could be used.
*
* Xalan Developers: please see
* xml-xalan/test/java/src/org/apache/qetest/trax/ExamplesTest.java
* when updating this file, and update that test file as well.
*
* @author <a href="mailto:scott_boag@lotus.com">Scott Boag</a>
*/
public class Examples
{
/**
* Method main
*/
public static void main(String argv[])
throws TransformerException, TransformerConfigurationException, IOException, SAXException,
ParserConfigurationException, FileNotFoundException
{
System.out.println("\n\n==== exampleSimple ====");
try {
exampleSimple1("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleSimple2 (see foo.out) ====");
try {
exampleSimple2("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleFromStream ====");
try {
exampleFromStream("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleFromReader ====");
try {
exampleFromReader("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleUseTemplatesObj ====");
try {
exampleUseTemplatesObj("xml/foo.xml", "xml/baz.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleContentHandlerToContentHandler ====");
try {
exampleContentHandlerToContentHandler("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleXMLReader ====");
try {
exampleXMLReader("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleXMLFilter ====");
try {
exampleXMLFilter("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleXMLFilterChain ====");
try {
exampleXMLFilterChain("xml/foo.xml", "xsl/foo.xsl", "xsl/foo2.xsl", "xsl/foo3.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleDOM2DOM ====");
try {
exampleDOM2DOM("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleParam ====");
try {
exampleParam("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleTransformerReuse ====");
try {
exampleTransformerReuse("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleOutputProperties ====");
try {
exampleOutputProperties("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleUseAssociated ====");
try {
exampleUseAssociated("xml/foo.xml");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleContentHandler2DOM ====");
try {
exampleContentHandler2DOM("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleAsSerializer ====");
try {
exampleAsSerializer("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n\n==== exampleContentHandler2DOM ====");
try {
exampleContentHandler2DOM("xml/foo.xml", "xsl/foo.xsl");
} catch( Exception ex ) {
handleException(ex);
}
System.out.println("\n==== done! ====");
}
/**
* Show the simplest possible transformation from system id
* to output stream.
*/
public static void exampleSimple1(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException
{
// Create a transform factory instance.
TransformerFactory tfactory = TransformerFactory.newInstance();
// Create a transformer for the stylesheet.
Transformer transformer
= tfactory.newTransformer(new StreamSource(xslID));
// Transform the source XML to System.out.
transformer.transform( new StreamSource(sourceID),
new StreamResult(new OutputStreamWriter(System.out)));
}
/**
* Show the simplest possible transformation from File
* to a File.
*/
public static void exampleSimple2(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException
{
// Create a transform factory instance.
TransformerFactory tfactory = TransformerFactory.newInstance();
// Create a transformer for the stylesheet.
Transformer transformer
= tfactory.newTransformer(new StreamSource(xslID));
// Transform the source XML to foo.out.
transformer.transform( new StreamSource(new File(sourceID)),
new StreamResult(new File("foo.out")));
}
/**
* Show simple transformation from input stream to output stream.
*/
public static void exampleFromStream(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException
{
// Create a transform factory instance.
TransformerFactory tfactory = TransformerFactory.newInstance();
InputStream xslIS = new BufferedInputStream(new FileInputStream(xslID));
StreamSource xslSource = new StreamSource(xslIS);
// Note that if we don't do this, relative URLs can not be resolved correctly!
xslSource.setSystemId(xslID);
// Create a transformer for the stylesheet.
Transformer transformer = tfactory.newTransformer(xslSource);
InputStream xmlIS = new BufferedInputStream(new FileInputStream(sourceID));
StreamSource xmlSource = new StreamSource(xmlIS);
// Note that if we don't do this, relative URLs can not be resolved correctly!
xmlSource.setSystemId(sourceID);
// Transform the source XML to System.out.
transformer.transform( xmlSource, new StreamResult(new OutputStreamWriter(System.out)));
}
/**
* Show simple transformation from reader to output stream. In general
* this use case is discouraged, since the XML encoding can not be
* processed.
*/
public static void exampleFromReader(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException, UnsupportedEncodingException
{
// Create a transform factory instance.
TransformerFactory tfactory = TransformerFactory.newInstance();
// Note that in this case the XML encoding can not be processed!
Reader xslReader = new BufferedReader(new InputStreamReader(new FileInputStream(xslID), "UTF-8"));
StreamSource xslSource = new StreamSource(xslReader);
// Note that if we don't do this, relative URLs can not be resolved correctly!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -