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.1KB

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