You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleObj2XML.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 1999-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. /* $Id$ */
  17. package embedding;
  18. //Hava
  19. import java.io.File;
  20. import java.io.IOException;
  21. //JAXP
  22. import javax.xml.transform.Transformer;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Result;
  27. import javax.xml.transform.stream.StreamResult;
  28. //Avalon
  29. import org.apache.avalon.framework.ExceptionUtil;
  30. import embedding.model.ProjectMember;
  31. import embedding.model.ProjectTeam;
  32. /**
  33. * This class demonstrates the conversion of an arbitrary object file to an
  34. * XML file.
  35. */
  36. public class ExampleObj2XML {
  37. /**
  38. * Converts a ProjectTeam object to XML.
  39. * @param team the ProjectTeam object
  40. * @param xml the target XML file
  41. * @throws IOException In case of an I/O problem
  42. * @throws TransformerException In case of a XSL transformation problem
  43. */
  44. public void convertProjectTeam2XML(ProjectTeam team, File xml)
  45. throws IOException, TransformerException {
  46. //Setup XSLT
  47. TransformerFactory factory = TransformerFactory.newInstance();
  48. Transformer transformer = factory.newTransformer();
  49. /* Note:
  50. We use the identity transformer, no XSL transformation is done.
  51. The transformer is basically just used to serialize the
  52. generated document to XML. */
  53. //Setup input
  54. Source src = team.getSourceForProjectTeam();
  55. //Setup output
  56. Result res = new StreamResult(xml);
  57. //Start XSLT transformation
  58. transformer.transform(src, res);
  59. }
  60. /**
  61. * Creates a sample ProjectTeam instance for this demo.
  62. * @return ProjectTeam the newly created ProjectTeam instance
  63. */
  64. public static ProjectTeam createSampleProjectTeam() {
  65. ProjectTeam team = new ProjectTeam();
  66. team.setProjectName("Rule the Galaxy");
  67. team.addMember(new ProjectMember(
  68. "Emperor Palpatine", "lead", "palpatine@empire.gxy"));
  69. team.addMember(new ProjectMember(
  70. "Lord Darth Vader", "Jedi-Killer", "vader@empire.gxy"));
  71. team.addMember(new ProjectMember(
  72. "Grand Moff Tarkin", "Planet-Killer", "tarkin@empire.gxy"));
  73. team.addMember(new ProjectMember(
  74. "Admiral Motti", "Death Star operations", "motti@empire.gxy"));
  75. return team;
  76. }
  77. /**
  78. * Main method.
  79. * @param args command-line arguments
  80. */
  81. public static void main(String[] args) {
  82. try {
  83. System.out.println("FOP ExampleObj2XML\n");
  84. System.out.println("Preparing...");
  85. //Setup directories
  86. File baseDir = new File(".");
  87. File outDir = new File(baseDir, "out");
  88. outDir.mkdirs();
  89. //Setup input and output
  90. File xmlfile = new File(outDir, "ResultObj2XML.xml");
  91. System.out.println("Input: a ProjectTeam object");
  92. System.out.println("Output: XML (" + xmlfile + ")");
  93. System.out.println();
  94. System.out.println("Serializing...");
  95. ExampleObj2XML app = new ExampleObj2XML();
  96. app.convertProjectTeam2XML(createSampleProjectTeam(), xmlfile);
  97. System.out.println("Success!");
  98. } catch (Exception e) {
  99. System.err.println(ExceptionUtil.printStackTrace(e));
  100. System.exit(-1);
  101. }
  102. }
  103. }