KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > embedding > ExampleObj2XML


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: ExampleObj2XML.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package embedding;
21
22 //Hava
23
import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 //JAXP
27
import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.TransformerException JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.Result JavaDoc;
32 import javax.xml.transform.stream.StreamResult JavaDoc;
33
34 import embedding.model.ProjectMember;
35 import embedding.model.ProjectTeam;
36
37
38 /**
39  * This class demonstrates the conversion of an arbitrary object file to an
40  * XML file.
41  */

42 public class ExampleObj2XML {
43
44     /**
45      * Converts a ProjectTeam object to XML.
46      * @param team the ProjectTeam object
47      * @param xml the target XML file
48      * @throws IOException In case of an I/O problem
49      * @throws TransformerException In case of a XSL transformation problem
50      */

51     public void convertProjectTeam2XML(ProjectTeam team, File JavaDoc xml)
52                 throws IOException JavaDoc, TransformerException JavaDoc {
53                     
54         //Setup XSLT
55
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
56         Transformer JavaDoc transformer = factory.newTransformer();
57         /* Note:
58            We use the identity transformer, no XSL transformation is done.
59            The transformer is basically just used to serialize the
60            generated document to XML. */

61     
62         //Setup input
63
Source JavaDoc src = team.getSourceForProjectTeam();
64     
65         //Setup output
66
Result JavaDoc res = new StreamResult JavaDoc(xml);
67
68         //Start XSLT transformation
69
transformer.transform(src, res);
70     }
71
72
73     /**
74      * Creates a sample ProjectTeam instance for this demo.
75      * @return ProjectTeam the newly created ProjectTeam instance
76      */

77     public static ProjectTeam createSampleProjectTeam() {
78         ProjectTeam team = new ProjectTeam();
79         team.setProjectName("Rule the Galaxy");
80         team.addMember(new ProjectMember(
81                 "Emperor Palpatine", "lead", "palpatine@empire.gxy"));
82         team.addMember(new ProjectMember(
83                 "Lord Darth Vader", "Jedi-Killer", "vader@empire.gxy"));
84         team.addMember(new ProjectMember(
85                 "Grand Moff Tarkin", "Planet-Killer", "tarkin@empire.gxy"));
86         team.addMember(new ProjectMember(
87                 "Admiral Motti", "Death Star operations", "motti@empire.gxy"));
88         return team;
89     }
90
91
92     /**
93      * Main method.
94      * @param args command-line arguments
95      */

96     public static void main(String JavaDoc[] args) {
97         try {
98             System.out.println("FOP ExampleObj2XML\n");
99             System.out.println("Preparing...");
100             
101             //Setup directories
102
File JavaDoc baseDir = new File JavaDoc(".");
103             File JavaDoc outDir = new File JavaDoc(baseDir, "out");
104             outDir.mkdirs();
105
106             //Setup input and output
107
File JavaDoc xmlfile = new File JavaDoc(outDir, "ResultObj2XML.xml");
108
109             System.out.println("Input: a ProjectTeam object");
110             System.out.println("Output: XML (" + xmlfile + ")");
111             System.out.println();
112             System.out.println("Serializing...");
113
114             ExampleObj2XML app = new ExampleObj2XML();
115             app.convertProjectTeam2XML(createSampleProjectTeam(), xmlfile);
116             
117             System.out.println("Success!");
118         } catch (Exception JavaDoc e) {
119             e.printStackTrace(System.err);
120             System.exit(-1);
121         }
122     }
123 }
124
Popular Tags