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 com.x2jb.bind.handler;
20:
21: import org.w3c.dom.Attr;
22: import org.w3c.dom.Node;
23: import org.w3c.dom.Text;
24: import org.w3c.dom.Element;
25: import org.x2jb.bind.handler.AttributeHandler;
26: import org.x2jb.bind.handler.ElementHandler;
27: import org.x2jb.bind.BindingException;
28:
29: /**
30: * Template handler extended by other handlers in this package
31: *
32: * @author <a href="mailto:richard_opalka@yahoo.com">Richard Opalka</a>
33: * @version 1.0
34: */
35: abstract class TemplateHandler implements ElementHandler,
36: AttributeHandler {
37:
38: public Object bind(Attr a, Class clazz) throws BindingException {
39: String value = a.getValue();
40: try {
41: return create(value);
42: } catch (NumberFormatException nfe) {
43: throw new BindingException("Incorrect value '" + value
44: + "'", nfe);
45: }
46: }
47:
48: public Object bind(Element e, Class clazz) throws BindingException {
49: Node childNode = e.getFirstChild();
50: if ((childNode == null) || (!(childNode instanceof Text))) {
51: throw new BindingException("Text node expected");
52: } else {
53: String value = ((Text) childNode).getData();
54: try {
55: return create(value);
56: } catch (NumberFormatException nfe) {
57: throw new BindingException("Incorrect value '" + value
58: + "'", nfe);
59: }
60: }
61: }
62:
63: public Object getDefault(Class clazz) throws BindingException {
64: return null;
65: }
66:
67: protected abstract Object create(String value)
68: throws BindingException;
69:
70: }
|