~ubuntu-branches/ubuntu/saucy/libxalan2-java/saucy-security

« back to all changes in this revision

Viewing changes to samples/translets/JAXPTransletMultipleTransformations.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2008-04-27 10:20:03 UTC
  • mfrom: (1.1.3 upstream) (3.1.11 hardy)
  • Revision ID: james.westby@ubuntu.com-20080427102003-qy06c2if0qayzwlg
Tags: 2.7.1-2
* Build-Depends on default-jdk-builddep. Closes: #477893
* Clarified debian/copyright.
* Don't use '-1' in Build-Depends.
* Updated watch file to match upstream correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2001-2004 The Apache Software Foundation.
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
/*
17
 
 * $Id: JAXPTransletMultipleTransformations.java,v 1.10 2004/02/17 19:13:22 minchau Exp $
18
 
 */
19
 
import java.io.FileNotFoundException;
20
 
import java.io.FileOutputStream;
21
 
 
22
 
import java.util.Properties;
23
 
 
24
 
import javax.xml.transform.Templates;
25
 
import javax.xml.transform.Transformer;
26
 
import javax.xml.transform.TransformerException;
27
 
import javax.xml.transform.TransformerFactory;
28
 
import javax.xml.transform.stream.StreamResult;
29
 
import javax.xml.transform.stream.StreamSource;
30
 
 
31
 
 
32
 
/**
33
 
 * Using the TrAX/JAXP 1.1 interface to compile a translet and use it 
34
 
 * to perform multiple transformations. The translet implements 
35
 
 * the Templates interface. If you want to use the translet to perform a 
36
 
 * single transformation, see JAXPTransletOneTransformation.java.
37
 
 * 
38
 
 * 
39
 
 * @author Donald Leslie
40
 
 */
41
 
public class JAXPTransletMultipleTransformations 
42
 
{
43
 
 static void doTransform(Templates translet, String xmlInURI, String htmlOutURI)
44
 
        throws TransformerException, FileNotFoundException     
45
 
  {
46
 
    // For each transformation, instantiate a new Transformer, and perform
47
 
    // the transformation from a StreamSource to a StreamResult;
48
 
    Transformer transformer = translet.newTransformer();
49
 
    transformer.transform( new StreamSource(xmlInURI),
50
 
                           new StreamResult(new FileOutputStream(htmlOutURI)));
51
 
  }
52
 
 
53
 
  public static void main(String argv[])        
54
 
  { 
55
 
    // Set the TransformerFactory system property to generate and use translets.
56
 
    // Note: To make this sample more flexible, load properties from a properties file.
57
 
    // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl"
58
 
    String key = "javax.xml.transform.TransformerFactory";
59
 
    String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
60
 
    Properties props = System.getProperties();
61
 
    props.put(key, value);
62
 
    
63
 
    System.setProperties(props);
64
 
 
65
 
    String xslInURI = "todo.xsl";
66
 
    
67
 
    try
68
 
    {
69
 
      // Instantiate the TransformerFactory, and use it along with a SteamSource
70
 
      // XSL stylesheet to create a translet as a Templates object.
71
 
      TransformerFactory tFactory = TransformerFactory.newInstance();
72
 
      Templates translet = tFactory.newTemplates(new StreamSource(xslInURI));
73
 
    
74
 
      // Perform each transformation
75
 
      doTransform(translet, "todo.xml", "todo.html");
76
 
      System.out.println("Produced todo.html");
77
 
    
78
 
      doTransform(translet, "todotoo.xml", "todotoo.html");
79
 
      System.out.println("Produced todotoo.html");
80
 
    }
81
 
    catch (Exception e)
82
 
    {
83
 
      e.printStackTrace();
84
 
    }    
85
 
  } 
86
 
}
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one
 
3
 * or more contributor license agreements. See the NOTICE file
 
4
 * distributed with this work for additional information
 
5
 * regarding copyright ownership. The ASF licenses this file
 
6
 * to you under the Apache License, Version 2.0 (the  "License");
 
7
 * you may not use this file except in compliance with the License.
 
8
 * You may obtain a copy of the License at
 
9
 *
 
10
 *     http://www.apache.org/licenses/LICENSE-2.0
 
11
 *
 
12
 * Unless required by applicable law or agreed to in writing, software
 
13
 * distributed under the License is distributed on an "AS IS" BASIS,
 
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
15
 * See the License for the specific language governing permissions and
 
16
 * limitations under the License.
 
17
 */
 
18
/*
 
19
 * $Id: JAXPTransletMultipleTransformations.java 470245 2006-11-02 06:34:33Z minchau $
 
20
 */
 
21
import java.io.FileNotFoundException;
 
22
import java.io.FileOutputStream;
 
23
 
 
24
import java.util.Properties;
 
25
 
 
26
import javax.xml.transform.Templates;
 
27
import javax.xml.transform.Transformer;
 
28
import javax.xml.transform.TransformerException;
 
29
import javax.xml.transform.TransformerFactory;
 
30
import javax.xml.transform.stream.StreamResult;
 
31
import javax.xml.transform.stream.StreamSource;
 
32
 
 
33
 
 
34
/**
 
35
 * Using the TrAX/JAXP 1.1 interface to compile a translet and use it 
 
36
 * to perform multiple transformations. The translet implements 
 
37
 * the Templates interface. If you want to use the translet to perform a 
 
38
 * single transformation, see JAXPTransletOneTransformation.java.
 
39
 * 
 
40
 * 
 
41
 * @author Donald Leslie
 
42
 */
 
43
public class JAXPTransletMultipleTransformations 
 
44
{
 
45
 static void doTransform(Templates translet, String xmlInURI, String htmlOutURI)
 
46
        throws TransformerException, FileNotFoundException     
 
47
  {
 
48
    // For each transformation, instantiate a new Transformer, and perform
 
49
    // the transformation from a StreamSource to a StreamResult;
 
50
    Transformer transformer = translet.newTransformer();
 
51
    transformer.transform( new StreamSource(xmlInURI),
 
52
                           new StreamResult(new FileOutputStream(htmlOutURI)));
 
53
  }
 
54
 
 
55
  public static void main(String argv[])        
 
56
  { 
 
57
    // Set the TransformerFactory system property to generate and use translets.
 
58
    // Note: To make this sample more flexible, load properties from a properties file.
 
59
    // The setting for the Xalan Transformer is "org.apache.xalan.processor.TransformerFactoryImpl"
 
60
    String key = "javax.xml.transform.TransformerFactory";
 
61
    String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
 
62
    Properties props = System.getProperties();
 
63
    props.put(key, value);
 
64
    
 
65
    System.setProperties(props);
 
66
 
 
67
    String xslInURI = "todo.xsl";
 
68
    
 
69
    try
 
70
    {
 
71
      // Instantiate the TransformerFactory, and use it along with a SteamSource
 
72
      // XSL stylesheet to create a translet as a Templates object.
 
73
      TransformerFactory tFactory = TransformerFactory.newInstance();
 
74
      Templates translet = tFactory.newTemplates(new StreamSource(xslInURI));
 
75
    
 
76
      // Perform each transformation
 
77
      doTransform(translet, "todo.xml", "todo.html");
 
78
      System.out.println("Produced todo.html");
 
79
    
 
80
      doTransform(translet, "todotoo.xml", "todotoo.html");
 
81
      System.out.println("Produced todotoo.html");
 
82
    }
 
83
    catch (Exception e)
 
84
    {
 
85
      e.printStackTrace();
 
86
    }    
 
87
  } 
 
88
}