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 customhandler;
20:
21: import org.x2jb.bind.XML2Java;
22: import customhandler.ifaces.Meetings;
23: import customhandler.ifaces.Meeting;
24: import org.w3c.dom.Document;
25: import javax.xml.parsers.DocumentBuilder;
26: import javax.xml.parsers.DocumentBuilderFactory;
27: import javax.xml.parsers.ParserConfigurationException;
28:
29: /**
30: * Custom handler sample
31: *
32: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
33: * @version 1.0
34: */
35: public final class Main {
36:
37: /**
38: * Lookups specified resource on the classpath and returns parsed document instance
39: * @param classpath resource to return
40: */
41: private static Document getDocument(String resource)
42: throws Exception {
43: Document retVal = null;
44:
45: try {
46: DocumentBuilderFactory builderFactory = DocumentBuilderFactory
47: .newInstance();
48: builderFactory.setIgnoringComments(true);
49: DocumentBuilder builder = builderFactory
50: .newDocumentBuilder();
51: retVal = builder.parse(Main.class
52: .getResourceAsStream(resource));
53: ;
54: } catch (ParserConfigurationException e) {
55: e.printStackTrace(System.err);
56: System.exit(1);
57: }
58: return retVal;
59: }
60:
61: public static void main(String[] args) throws Exception {
62: Document parsedDocument = getDocument("/customhandler.xml");
63: Meetings meetings = (Meetings) XML2Java.bind(parsedDocument,
64: Meetings.class);
65:
66: // display all planned meetings
67: Meeting[] meeting = meetings.get();
68: for (int i = 0; i < meeting.length; i++) {
69: System.out.println(meeting[i].getSubject()
70: + meeting[i].getTimeAndLocation().getValue());
71: }
72: }
73:
74: }
|