01: /*
02: * XML 2 Java Binding (X2JB) - the excellent Java tool.
03: * Copyright 2007, by Richard Opalka.
04: *
05: * This is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU Lesser General Public License as
07: * published by the Free Software Foundation; either version 2.1 of
08: * the License, or (at your option) any later version.
09: *
10: * This software is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this software; if not see the FSF site:
17: * http://www.fsf.org/ and search for the LGPL License document there.
18: */
19: package innerifaces;
20:
21: import innerifaces.ifaces.RootIface;
22: import org.x2jb.bind.XML2Java;
23: import org.w3c.dom.Document;
24: import javax.xml.parsers.DocumentBuilder;
25: import javax.xml.parsers.DocumentBuilderFactory;
26: import javax.xml.parsers.ParserConfigurationException;
27:
28: /**
29: * Inner interfaces sample
30: *
31: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
32: * @version 1.0
33: */
34: public final class Main {
35:
36: /**
37: * Lookups specified resource on the classpath and returns parsed document instance
38: * @param classpath resource to return
39: */
40: private static Document getDocument(String resource)
41: throws Exception {
42: Document retVal = null;
43: try {
44: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
45: .newInstance();
46: builderFactory.setIgnoringComments(true);
47: builderFactory.setNamespaceAware(true);
48: DocumentBuilder builder = builderFactory
49: .newDocumentBuilder();
50: retVal = builder.parse(Main.class
51: .getResourceAsStream(resource));
52: } catch (ParserConfigurationException e) {
53: e.printStackTrace(System.err);
54: System.exit(1);
55: }
56: return retVal;
57: }
58:
59: public static void main(String[] args) throws Exception {
60: Document parsedDocument = getDocument("/innerifaces.xml");
61: RootIface rootIface = (RootIface) XML2Java.bind(parsedDocument,
62: RootIface.class);
63:
64: // Accessing first inner interface
65: RootIface.NestedIface1 nested1 = rootIface.getNestedInterface();
66: System.out.println("Value of attribute c1:attr is "
67: + nested1.getAttrValue());
68: System.out.println("Value of element c1:elem is "
69: + nested1.getElemValue());
70:
71: // Accessing second inner interface
72: RootIface.NestedIface1.NestedIface2 nested2 = nested1
73: .getNestedInterface();
74: System.out.println("Value of attribute c1:attr is "
75: + nested2.getAttrValue());
76: System.out.println("Value of element c1:elem is "
77: + nested2.getElemValue());
78: }
79:
80: }
|