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 helloworld;
20:
21: import org.x2jb.bind.XML2Java;
22: import helloworld.ifaces.HelloWorld;
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: * Hello world 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: DocumentBuilder builder = builderFactory
48: .newDocumentBuilder();
49: retVal = builder.parse(Main.class
50: .getResourceAsStream(resource));
51: } catch (ParserConfigurationException e) {
52: e.printStackTrace(System.err);
53: System.exit(1);
54: }
55: return retVal;
56: }
57:
58: public static void main(String[] args) throws Exception {
59: Document parsedDocument = getDocument("/helloworld.xml");
60: HelloWorld helloWorld = (HelloWorld) XML2Java.bind(
61: parsedDocument, HelloWorld.class);
62:
63: System.out.println(helloWorld.getMessageFromElement());
64: System.out.println(helloWorld.getMessageFromAttribute());
65: }
66:
67: }
|