01: /*
02: * User: Michael Rettig
03: * Date: Sep 15, 2002
04: * Time: 8:57:05 PM
05: */
06: package net.sourceforge.jaxor.parser;
07:
08: import net.sourceforge.jaxor.util.JavaBean;
09: import net.sourceforge.jaxor.util.MethodNotFound;
10: import org.xml.sax.Attributes;
11: import org.xml.sax.Locator;
12:
13: import java.util.StringTokenizer;
14:
15: public class XmlProxy {
16:
17: private final JavaBean _obj;
18: private final Locator _locator;
19:
20: public XmlProxy(Object obj, Locator locator) {
21: _obj = new JavaBean(obj);
22: _locator = locator;
23: }
24:
25: public void setAll(Attributes attributes) {
26: for (int i = 0; i < attributes.getLength(); i++) {
27: String key = attributes.getQName(i);
28: String value = attributes.getValue(i);
29: set(key, value);
30: }
31: }
32:
33: public void add(String name, String value) {
34: _obj.add(name, value);
35: }
36:
37: public void set(String name, String value) {
38: // NOTE: only adding getJavaName here so that attribute
39: // handling will work. Doesn't seem to be required for add()
40: // since elements are handled by aliases in the DynamicParser.
41: // atownley 02-April-2003
42:
43: _obj.set(getJavaName(name), value);
44: }
45:
46: private static String getJavaName(String s) {
47: String res = null;
48: int idx = s.indexOf("-");
49:
50: if (idx != -1) {
51: StringBuffer buf = new StringBuffer();
52:
53: // StringTokenizer because String.split() not in 1.3
54: StringTokenizer tokenizer = new StringTokenizer(s, "-");
55:
56: if (tokenizer.hasMoreTokens()) {
57: buf.append(tokenizer.nextToken());
58: }
59:
60: while (tokenizer.hasMoreTokens()) {
61: String tok = tokenizer.nextToken();
62: char c0 = tok.charAt(0);
63: String s2 = tok.substring(1);
64: buf.append(Character.toUpperCase(c0));
65: buf.append(s2);
66: }
67:
68: res = buf.toString();
69: } else {
70: res = s;
71: }
72:
73: return res;
74: }
75:
76: public Object getObject() {
77: return _obj.getWrappedObject();
78: }
79:
80: public void setElement(Object object, String name) {
81: try {
82: _obj.add(name, object);
83: return;
84: } catch (MethodNotFound e) {
85: }
86: try {
87: _obj.add(DynamicParser.getNameFromClass(object.getClass()),
88: object);
89: return;
90: } catch (MethodNotFound e) {
91: }
92: throw new ParseException("Failed to set element: " + name
93: + " on object: " + getObject(), _locator);
94: }
95: }
|