1:
31: package ;
32:
33: import ;
34: import ;
35: import ;
36:
37: import ;
38: import ;
39: import ;
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48:
53: public class AttributeReadHandler extends PropertyReadHandler
54: {
55: private String encoding;
56: private String className;
57: private Object value;
58: private CharacterEntityParser entityParser;
59: private String namespace;
60:
61: public AttributeReadHandler()
62: {
63: this.entityParser = CharacterEntityParser.createXMLEntityParser();
64: }
65:
66:
71: protected void doneParsing() throws SAXException
72: {
73: super.doneParsing();
74: try
75: {
76: final String result = getResult();
77: if ("base64".equals(encoding))
78: {
79: final byte[] data = Base64.decode(result.trim().toCharArray());
80: final ByteArrayInputStream bin = new ByteArrayInputStream(data);
81: final ObjectInputStream oin = new ObjectInputStream(bin);
82: value = oin.readObject();
83: }
84: else
85: {
86: if (className != null)
87: {
88: final ClassLoader cl = ObjectUtilities.getClassLoader
89: (AttributeReadHandler.class);
90: final Class c = cl.loadClass(className);
91: ConverterRegistry.toPropertyValue
92: (entityParser.decodeEntities(result), c);
93: }
94: else
95: {
96: ConverterRegistry.toPropertyValue
97: (entityParser.decodeEntities(result), String.class);
98: }
99: }
100: }
101: catch (BeanException e)
102: {
103: throw new ParseException("Unable to set attribute '" + getName() + "'", e, getLocator());
104: }
105: catch (ClassNotFoundException e)
106: {
107: throw new ParseException("Unable to set attribute '" + getName() + "'", e, getLocator() );
108: }
109: catch (IOException e)
110: {
111: throw new ParseException("Unable to set attribute '" + getName() + "'", e, getLocator());
112: }
113: }
114:
115:
121: protected void startParsing(final Attributes attrs) throws SAXException
122: {
123: super.startParsing(attrs);
124: namespace = attrs.getValue(getUri(), "namespace-uri");
125: if (namespace == null)
126: {
127: namespace = getNamespace();
128: }
129:
130: className = attrs.getValue(getUri(), "class");
131: if (className == null)
132: {
133: className = "java.lang.String";
134: }
135: encoding = attrs.getValue(getUri(), "encoding");
136: if (encoding == null)
137: {
138: encoding = "text";
139: }
140: else if (("text".equals(encoding) == false) && "base64".equals(encoding) == false)
141: {
142: Log.warn ("Invalid value for attribute 'encoding'. Defaulting to 'text'");
143: encoding = "text";
144: }
145: }
146:
147: public String getNamespace()
148: {
149: return namespace;
150: }
151:
152:
157: public Object getObject()
158: {
159: return value;
160: }
161: }