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.BindingException;
22: import org.x2jb.bind.handler.ElementHandler;
23: import org.x2jb.bind.handler.AttributeHandler;
24: import org.w3c.dom.Element;
25: import org.w3c.dom.Attr;
26: import org.w3c.dom.Node;
27: import org.w3c.dom.Text;
28: import customhandler.ifaces.TimeAndLocation;
29:
30: /**
31: * Custom handler sample
32: *
33: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
34: * @version 1.0
35: */
36: public final class Handler implements ElementHandler, AttributeHandler {
37:
38: private static final String DATE = "date";
39: private static final String START_TIME = "start-time";
40: private static final String END_TIME = "end-time";
41: private static final String ROOM = "room";
42:
43: public final Object bind(Element e, Class c)
44: throws BindingException {
45: if (c.equals(TimeAndLocation.class)) {
46: String date = getTextContent(e.getElementsByTagName(DATE)
47: .item(0));
48: String startTime = getTextContent(e.getElementsByTagName(
49: START_TIME).item(0));
50: String endTime = getTextContent(e.getElementsByTagName(
51: END_TIME).item(0));
52: String room = getTextContent(e.getElementsByTagName(ROOM)
53: .item(0));
54: // preparing return value for TimeAndLocation.getValue() method
55: final StringBuffer sb = new StringBuffer()
56: .append(" is on ").append(date).append(
57: ". It starts at ").append(startTime)
58: .append(" in ").append(room).append(
59: " room and ends at ").append(endTime)
60: .append(".");
61: // Note that TimeAndLocation interface contains no binding definitions.
62: // It is not necessary because the interface is created in our custom handler.
63: return new TimeAndLocation() {
64: public String getValue() {
65: return sb.toString();
66: }
67: };
68: }
69:
70: throw new BindingException("Handler doesn't support '"
71: + c.getName() + "' class");
72: }
73:
74: public final Object bind(Attr a, Class c) throws BindingException {
75: if (c.equals(String.class)) {
76: return "My " + a.getValue();
77: }
78:
79: throw new BindingException("Handler doesn't support '"
80: + c.getName() + "' class");
81: }
82:
83: public final Object getDefault(Class c) throws BindingException {
84: return null;
85: }
86:
87: private static String getTextContent(Node e) {
88: Node childNode = e.getFirstChild();
89: if ((childNode == null) || (!(childNode instanceof Text))) {
90: return null;
91: } else {
92: return ((Text) childNode).getData();
93: }
94: }
95:
96: }
|