1:
42:
43: package ;
44:
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
65: public class GenericReadHandler extends AbstractXmlReadHandler {
66:
67:
68: private Object object;
69:
70:
71: private GenericObjectFactory objectFactory;
72:
73:
74: private ArrayList objectRefHandlers;
75:
76:
77: private HashMap createdHandler;
78:
79:
84: public GenericReadHandler(final GenericObjectFactory factory) {
85: this.createdHandler = new HashMap();
86: this.objectRefHandlers = new ArrayList();
87: this.objectFactory = factory;
88: }
89:
90:
97: protected void startParsing(final Attributes attrs) throws SAXException {
98:
99: try {
100: final AttributeDefinition[] attribs = this.objectFactory.getAttributeDefinitions();
101: for (int i = 0; i < attribs.length; i++) {
102: final AttributeDefinition attrDef = attribs[i];
103: final String value = attrs.getValue(attrDef.getAttributeName());
104: if (value == null) {
105: continue;
106: }
107: final Object o = attrDef.getHandler().toPropertyValue(value);
108: this.objectFactory.setProperty(attrDef.getPropertyName(), o);
109: }
110: }
111: catch (ObjectDescriptionException ode) {
112: throw new SAXException(ode);
113: }
114: }
115:
116:
126: protected XmlReadHandler getHandlerForChild(final String tagName, final Attributes atts)
127: throws SAXException {
128: try {
129: if (tagName.equals("objectRef")) {
130:
131: final XmlReadHandler handler = new ObjectRefHandler();
132: this.objectRefHandlers.add(handler);
133: return handler;
134: }
135: final XmlReadHandler handler = getRootHandler().createHandler
136: (this.objectFactory.getTypeForTagName(tagName), tagName, atts);
137: if (handler != null) {
138: this.createdHandler.put(tagName, handler);
139: }
140:
141: return handler;
142: }
143: catch (ObjectDescriptionException ode) {
144: Log.debug ("Failed to get handler for child: ", ode);
145: throw new SAXException(ode);
146: }
147: }
148:
149:
156: public Object getObject() throws XmlReaderException {
157:
158: if (this.object != null) {
159: return this.object;
160: }
161: final RootXmlReadHandler rootHandler = getRootHandler();
162: try {
163: for (int i = 0; i < this.objectRefHandlers.size(); i++) {
164: final ObjectRefHandler handler = (ObjectRefHandler) this.objectRefHandlers.get(i);
165: this.objectFactory.setProperty(handler.getPropertyName(), handler.getObject());
166: }
167:
168: final ArrayList lookups = new ArrayList();
169: final LookupDefinition[] lookupDefs = this.objectFactory.getLookupDefinitions();
170: for (int i = 0; i < lookupDefs.length; i++) {
171: final LookupDefinition ldef = lookupDefs[i];
172: lookups.add(ldef.getPropertyName());
173: Log.debug ("lookup object: " + ldef.getPropertyName());
174:
175: final Object value = rootHandler.getHelperObject(ldef.getRegistryKey());
176: if (value == null) {
177:
178: Log.warn ("Failed to lookup object: " + value);
179: }
180: else {
181: this.objectFactory.setProperty(ldef.getPropertyName(), value);
182: }
183: }
184:
185: final ConstructorDefinition[] conDefs = this.objectFactory.getConstructorDefinitions();
186: for (int i = 0; i < conDefs.length; i++) {
187: final ConstructorDefinition cDef = conDefs[i];
188:
189: if (lookups.contains(cDef.getPropertyName())) {
190: continue;
191: }
192: if (this.objectFactory.isPropertyDefinition(cDef.getPropertyName())) {
193: final PropertyDefinition pd = this.objectFactory.getPropertyDefinitionByPropertyName(
194: cDef.getPropertyName()
195: );
196: final XmlReadHandler handler = (XmlReadHandler) this.createdHandler.get(
197: pd.getElementName()
198: );
199: if (handler != null) {
200: this.objectFactory.setProperty(pd.getPropertyName(), handler.getObject());
201: }
202: }
203:
204: }
205:
206: this.object = this.objectFactory.createObject();
207: Object oldValue = null;
208: if (this.objectFactory.getRegisterName() != null) {
209: oldValue = rootHandler.getHelperObject(this.objectFactory.getRegisterName());
210: rootHandler.setHelperObject(this.objectFactory.getRegisterName(), this.object);
211: }
212:
213: final PropertyDefinition[] propertyDefs = this.objectFactory.getPropertyDefinitions();
214: for (int i = 0; i < propertyDefs.length; i++) {
215: final PropertyDefinition pdef = propertyDefs[i];
216: final XmlReadHandler handler = (XmlReadHandler) this.createdHandler.get(
217: pdef.getElementName()
218: );
219: if (handler == null) {
220: continue;
221: }
222: this.objectFactory.setProperty(pdef.getPropertyName(), handler.getObject());
223: }
224:
225: this.objectFactory.writeObjectProperties(this.object);
226:
227: if (this.objectFactory.getRegisterName() != null) {
228: rootHandler.setHelperObject(this.objectFactory.getRegisterName(), oldValue);
229: }
230: }
231: catch (ObjectDescriptionException ode) {
232: Log.error ("Unable to create object.", ode);
233: throw new XmlReaderException("Unable to create object.", ode);
234: }
235: return this.object;
236: }
237:
238: }