001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ExampleObj2XML.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding;
021:
022: //Hava
023: import java.io.File;
024: import java.io.IOException;
025:
026: //JAXP
027: import javax.xml.transform.Transformer;
028: import javax.xml.transform.TransformerFactory;
029: import javax.xml.transform.TransformerException;
030: import javax.xml.transform.Source;
031: import javax.xml.transform.Result;
032: import javax.xml.transform.stream.StreamResult;
033:
034: import embedding.model.ProjectMember;
035: import embedding.model.ProjectTeam;
036:
037: /**
038: * This class demonstrates the conversion of an arbitrary object file to an
039: * XML file.
040: */
041: public class ExampleObj2XML {
042:
043: /**
044: * Converts a ProjectTeam object to XML.
045: * @param team the ProjectTeam object
046: * @param xml the target XML file
047: * @throws IOException In case of an I/O problem
048: * @throws TransformerException In case of a XSL transformation problem
049: */
050: public void convertProjectTeam2XML(ProjectTeam team, File xml)
051: throws IOException, TransformerException {
052:
053: //Setup XSLT
054: TransformerFactory factory = TransformerFactory.newInstance();
055: Transformer transformer = factory.newTransformer();
056: /* Note:
057: We use the identity transformer, no XSL transformation is done.
058: The transformer is basically just used to serialize the
059: generated document to XML. */
060:
061: //Setup input
062: Source src = team.getSourceForProjectTeam();
063:
064: //Setup output
065: Result res = new StreamResult(xml);
066:
067: //Start XSLT transformation
068: transformer.transform(src, res);
069: }
070:
071: /**
072: * Creates a sample ProjectTeam instance for this demo.
073: * @return ProjectTeam the newly created ProjectTeam instance
074: */
075: public static ProjectTeam createSampleProjectTeam() {
076: ProjectTeam team = new ProjectTeam();
077: team.setProjectName("Rule the Galaxy");
078: team.addMember(new ProjectMember("Emperor Palpatine", "lead",
079: "palpatine@empire.gxy"));
080: team.addMember(new ProjectMember("Lord Darth Vader",
081: "Jedi-Killer", "vader@empire.gxy"));
082: team.addMember(new ProjectMember("Grand Moff Tarkin",
083: "Planet-Killer", "tarkin@empire.gxy"));
084: team.addMember(new ProjectMember("Admiral Motti",
085: "Death Star operations", "motti@empire.gxy"));
086: return team;
087: }
088:
089: /**
090: * Main method.
091: * @param args command-line arguments
092: */
093: public static void main(String[] args) {
094: try {
095: System.out.println("FOP ExampleObj2XML\n");
096: System.out.println("Preparing...");
097:
098: //Setup directories
099: File baseDir = new File(".");
100: File outDir = new File(baseDir, "out");
101: outDir.mkdirs();
102:
103: //Setup input and output
104: File xmlfile = new File(outDir, "ResultObj2XML.xml");
105:
106: System.out.println("Input: a ProjectTeam object");
107: System.out.println("Output: XML (" + xmlfile + ")");
108: System.out.println();
109: System.out.println("Serializing...");
110:
111: ExampleObj2XML app = new ExampleObj2XML();
112: app.convertProjectTeam2XML(createSampleProjectTeam(),
113: xmlfile);
114:
115: System.out.println("Success!");
116: } catch (Exception e) {
117: e.printStackTrace(System.err);
118: System.exit(-1);
119: }
120: }
121: }
|