1 package org.sapia.util.xml.confix; 2 3 4 import org.sapia.util.xml.XmlUtil; 7 import org.sapia.util.xml.parser.HandlerContextIF; 8 import org.sapia.util.xml.parser.HandlerStateIF; 9 10 import org.xml.sax.Attributes ; 13 import org.xml.sax.SAXException ; 14 15 16 27 public class ConfixHandlerState implements HandlerStateIF { 28 32 33 private boolean _isParsing; 34 35 36 private boolean _isParsingChild; 37 38 39 private SAXProcessor _theProcessor; 40 41 42 private StringBuffer _theContent; 43 44 45 private Object _theParentObject; 46 47 48 private Object _theCurrentObject; 49 50 51 private boolean _isCurrentObjectAssigned; 52 53 57 60 public ConfixHandlerState(SAXProcessor aProcessor, Object aParentObject) { 61 if (aParentObject == null) { 62 throw new IllegalArgumentException ("The parent object passed in is null"); 63 } 64 65 _theContent = new StringBuffer (); 66 _theProcessor = aProcessor; 67 _theParentObject = aParentObject; 68 } 69 70 73 public ConfixHandlerState(SAXProcessor aProcessor) { 74 _theContent = new StringBuffer (); 75 _theProcessor = aProcessor; 76 } 77 78 82 88 public Object getResult() { 89 if (_isParsing == true) { 90 throw new IllegalStateException ("This handler state is currently parsing"); 91 } 92 93 return _theCurrentObject; 94 } 95 96 100 110 public void startElement(HandlerContextIF aContext, String anUri, 111 String aLocalName, String aQualifiedName, Attributes someAttributes) 112 throws SAXException { 113 if (!_isParsing) { 114 _isParsing = true; 115 116 CreationStatus aStatus = null; 117 String prefix = XmlUtil.extractPrefix(aQualifiedName); 118 119 try { 120 aStatus = _theProcessor.getObjectFactory().newObjectFor(prefix, 122 anUri, aLocalName, _theParentObject); 123 124 _theCurrentObject = aStatus.getCreated(); 125 _isCurrentObjectAssigned = aStatus.wasAssigned(); 126 127 for (int i = 0; i < someAttributes.getLength(); i++) { 129 String anAttributeName = someAttributes.getLocalName(i); 130 String anAttributeValue = someAttributes.getValue(i); 131 132 AbstractXMLProcessor.invokeSetter(aLocalName, _theCurrentObject, 133 anAttributeName, anAttributeValue); 134 } 135 } catch (ObjectCreationException oce) { 136 if (_theParentObject == null) { 137 String aMessage = "Unable to create an object for the element " + 138 aQualifiedName; 139 140 throw new SAXException (aMessage, oce); 141 } 142 } catch (ConfigurationException ce) { 143 String aMessage = "Unable to process the content of the element " + 144 aLocalName; 145 146 throw new SAXException (aMessage, ce); 147 } 148 } else { 149 HandlerStateIF aChildHandler; 150 151 if (_theCurrentObject instanceof HandlerStateIF) { 152 aChildHandler = (HandlerStateIF) _theCurrentObject; 153 } else { 154 aChildHandler = new ConfixHandlerState(_theProcessor, _theCurrentObject); 155 } 156 157 _isParsingChild = true; 158 aContext.setCurrentState(aChildHandler, anUri, aLocalName, 159 aQualifiedName, someAttributes); 160 } 161 } 162 163 172 public void endElement(HandlerContextIF aContext, String anUri, 173 String aLocalName, String aQualifiedName) throws SAXException { 174 if (_isParsingChild) { 175 _isParsingChild = false; 176 } else { 177 String aValue = _theContent.toString().trim(); 178 179 if (aValue.length() > 0) { 180 if (_theCurrentObject != null) { 181 try { 182 AbstractXMLProcessor.invokeSetter(aLocalName, _theCurrentObject, 183 "Text", aValue); 184 } catch (ConfigurationException ce) { 185 String aMessage = "The object " + 186 _theCurrentObject.getClass().getName() + 187 " does not accept free text"; 188 189 throw new SAXException (aMessage, ce); 190 } 191 } else { 192 try { 193 AbstractXMLProcessor.invokeSetter(_theParentObject.getClass() 194 .getName(), 195 _theParentObject, aLocalName, aValue); 196 _isCurrentObjectAssigned = true; 197 } catch (ConfigurationException ce) { 198 String aMessage = "Unable to process the content of the element " + 199 aLocalName; 200 201 throw new SAXException (aMessage, ce); 202 } 203 } 204 } else if (_theCurrentObject == null) { 205 String aMessage = "Unable to create an object for the element " + 206 aQualifiedName; 207 208 throw new SAXException (aMessage); 209 } 210 211 if (_theCurrentObject instanceof ObjectCreationCallback) { 212 try { 213 _theCurrentObject = ((ObjectCreationCallback) _theCurrentObject).onCreate(); 214 } catch (ConfigurationException e) { 215 throw new SAXException ("Could not create object", e); 216 } 217 } 218 219 if ((_theParentObject != null) && !_isCurrentObjectAssigned && 221 !(_theCurrentObject instanceof NullObject)) { 222 try { 223 AbstractXMLProcessor.assignToParent(_theParentObject, 224 _theCurrentObject, aLocalName); 225 } catch (ConfigurationException ce) { 226 StringBuffer aBuffer = new StringBuffer ( 227 "Unable to assign the object ").append(_theCurrentObject) 228 .append(" to the parent ") 229 .append(_theParentObject.getClass() 230 .getName()); 231 232 throw new SAXException (aBuffer.toString(), ce); 233 } 234 } 235 236 if (_theCurrentObject instanceof NullObject) { 237 _theCurrentObject = null; 238 } 239 240 _isParsing = false; 241 aContext.removeCurrentState(anUri, aLocalName, aQualifiedName); 242 } 243 } 244 245 254 public void characters(HandlerContextIF aContext, char[] someChars, 255 int anOffset, int length) throws SAXException { 256 _theContent.append(someChars, anOffset, length); 257 } 258 259 268 public void ignorableWhitespace(HandlerContextIF aContext, char[] someChars, 269 int anOffset, int aLength) throws SAXException { 270 } 272 } 273 | Popular Tags |