View Javadoc

1   package org.xrn.core;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   
6   import javax.xml.bind.JAXBContext;
7   import javax.xml.bind.Unmarshaller;
8   import javax.xml.parsers.DocumentBuilder;
9   import javax.xml.parsers.DocumentBuilderFactory;
10  import javax.xml.parsers.FactoryConfigurationError;
11  import javax.xml.parsers.ParserConfigurationException;
12  
13  import org.apache.tools.ant.BuildException;
14  import org.w3c.dom.Attr;
15  import org.w3c.dom.Document;
16  import org.w3c.dom.Element;
17  import org.w3c.dom.Node;
18  import org.xrn.xsd2java.MultipleReleaseNotes;
19  import org.xrn.xsd2java.ReleaseNotes;
20  import org.xrn.xsd2java.impl.ReleaseNotesImpl;
21  
22  /***
23   * @author Edourd
24   * 
25   * TODO To change the template for this generated type comment go to Window -
26   * Preferences - Java - Code Style - Code Templates
27   */
28  public class Reader
29  {
30  
31    /***
32     * @return a newly created document builder
33     * @throws org.apache.tools.ant.BuildException if the runtime cannot create such a builder
34     */
35    private DocumentBuilder createADocumentBuilder()
36            throws BuildException
37    {
38      DocumentBuilder document_builder = null;
39      try
40      {
41        DocumentBuilderFactory document_builder_factory = DocumentBuilderFactory.newInstance();
42        document_builder_factory.setNamespaceAware(true);
43        document_builder_factory.setValidating(true);
44        document_builder = document_builder_factory.newDocumentBuilder();
45      }
46      catch (ParserConfigurationException parser_configuration_exception)
47      {
48        throw new BuildException("Cannot create an XML document builder", parser_configuration_exception);
49      }
50      catch (FactoryConfigurationError factory_configuration_error)
51      {
52        throw new BuildException("Cannot create an XML document builder", factory_configuration_error);
53      }
54      return document_builder;
55    }
56    
57    private void test1()
58    {
59  
60      final String xrn_namespace_uri = "http://xmlreleasenotes.free.fr";
61      Document document = createADocumentBuilder().newDocument();
62      Element top_element = document.createElementNS(xrn_namespace_uri, "xrn:MultipleReleaseNotes");
63      document.appendChild(top_element);
64      Attr xrn_namespace_attribute = document.createAttribute("xmlns:xrn");
65      xrn_namespace_attribute.setValue(xrn_namespace_uri);
66      Attr xsi_namespace_attribute = document.createAttribute("xmlns:xsi");
67      xsi_namespace_attribute.setValue("http://www.w3.org/2001/XMLSchema-instance");
68      Attr xrn_version_attribute = document.createAttribute("XRNVersion");
69      xrn_version_attribute.setValue("0.18.1");
70      top_element.setAttributeNode(xrn_namespace_attribute);
71      top_element.setAttributeNode(xsi_namespace_attribute);
72      top_element.setAttributeNode(xrn_version_attribute);
73  
74      Element fake_element = document.createElementNS(xrn_namespace_uri, "xrn:ReleaseNotes");
75      fake_element.setAttribute("ID", "toto");
76      fake_element.setAttribute("version", "0.18.1");
77      top_element.appendChild(fake_element);
78      
79      MultipleReleaseNotes multiple_release_notes = null;
80      try
81      {
82        JAXBContext jContext = JAXBContext.newInstance("org.xrn.xsd2java");
83        Unmarshaller unmarshaller = jContext.createUnmarshaller();
84        // The validation is disabled in order to make sure that the unmarshaller works
85        unmarshaller.setValidating(false);
86        multiple_release_notes = (MultipleReleaseNotes) unmarshaller
87            .unmarshal(document);
88      }
89      catch (Exception exception)
90      {
91        exception.printStackTrace();
92        return;
93      }
94      System.out.println(multiple_release_notes.getXRNVersion());
95    }
96    
97    private void test2()
98    {
99  
100     //final File file = new
101     // File("V://XMLReleaseNotes//setup//ReleaseNotes.xml");
102     final File file = new File("V://XMLReleaseNotes//temp//template//0.xml");
103 
104     MultipleReleaseNotes releaseNotes = null;
105     try
106     {
107       JAXBContext jContext = JAXBContext.newInstance("org.xrn.xsd2java");
108       Unmarshaller unmarshaller = jContext.createUnmarshaller();
109       unmarshaller.setValidating(true);
110       releaseNotes = (MultipleReleaseNotes) unmarshaller
111           .unmarshal(new FileInputStream(file));
112     }
113     catch (Exception exception)
114     {
115       exception.printStackTrace();
116       return;
117     }
118     MultipleReleaseNotes multiple_release_notes = releaseNotes;
119     System.out.println(multiple_release_notes.getXRNVersion());
120 
121   }
122   
123   public static void main(String[] args)
124   {
125     Reader reader = new Reader();
126     reader.test1();
127     return;
128   }
129   
130 }