| 1 8 package net.sf.dozer.util.mapping.vo.jaxb.employee.impl.runtime; 9 10 import java.util.StringTokenizer ; 11 12 import javax.xml.bind.Element; 13 import javax.xml.bind.ParseConversionEvent; 14 import javax.xml.bind.ValidationEvent; 15 import javax.xml.bind.helpers.ParseConversionEventImpl; 16 import javax.xml.bind.helpers.ValidationEventImpl; 17 import javax.xml.bind.helpers.ValidationEventLocatorImpl; 18 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.SAXException ; 21 22 import com.sun.xml.bind.JAXBAssertionError; 23 import com.sun.xml.bind.unmarshaller.Messages; 24 25 37 public abstract class AbstractUnmarshallingEventHandlerImpl implements UnmarshallingEventHandler 38 { 39 public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt, 40 String _stateTextTypes ) { 41 42 this.context = _ctxt; 43 this.stateTextTypes = _stateTextTypes; 44 } 45 public final UnmarshallingContext context; 46 47 51 private final String stateTextTypes; 52 53 public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException { 60 unexpectedEnterElement(uri,local,qname,atts); 61 } 62 public void leaveElement(String uri, String local, String qname) throws SAXException { 63 unexpectedLeaveElement(uri,local,qname); 64 } 65 public final void text(String text) throws SAXException { 66 if(isListState()) { 67 71 StringTokenizer tokens = new StringTokenizer (text); 72 if( tokens.countTokens()==1 ) { 73 handleText(text); 74 } else { 75 while(tokens.hasMoreTokens()) 76 context.getCurrentHandler().text(tokens.nextToken()); 79 } 80 } else { 81 handleText(text); 83 } 84 } 85 protected void handleText(String s) throws SAXException { 86 unexpectedText(s); 87 } 88 public void enterAttribute(String uri, String local, String qname) throws SAXException { 89 unexpectedEnterAttribute(uri,local,qname); 90 } 91 public void leaveAttribute(String uri, String local, String qname) throws SAXException { 92 unexpectedLeaveAttribute(uri,local,qname); 93 } 94 public void leaveChild(int nextState) throws SAXException { 95 this.state = nextState; 96 } 97 98 99 102 protected final boolean isListState() { 103 return stateTextTypes.charAt(state)=='L'; 104 } 105 106 107 108 public int state; 109 110 111 112 113 119 protected void handleUnexpectedTextException( String text, RuntimeException e ) throws SAXException { 120 reportError( Messages.format(Messages.UNEXPECTED_TEXT,text), e, true ); 122 } 123 124 127 protected void handleGenericException( Exception e ) throws SAXException { 128 reportError( e.getMessage(), e, false ); 129 } 130 131 132 protected final void dump() { 133 System.err.println("state is :"+state); 134 } 135 private void reportError( String msg, boolean canRecover ) throws SAXException { 136 reportError( msg, null, canRecover ); 137 } 138 private void reportError( String msg, Exception nested, boolean canRecover ) throws SAXException { 139 context.handleEvent( new ValidationEventImpl( 140 canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, 141 msg, 142 new ValidationEventLocatorImpl(context.getLocator()), 143 nested ), canRecover ); 144 } 145 protected final void unexpectedEnterElement( String uri, String local, String qname, Attributes atts ) throws SAXException { 146 reportError( Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri, local ), true ); 148 context.pushContentHandler(new Discarder(context),state); 150 context.getCurrentHandler().enterElement(uri,local,qname,atts); 151 } 152 protected final void unexpectedLeaveElement( String uri, String local, String qname ) throws SAXException { 153 reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local ), false ); 154 } 155 protected final void unexpectedEnterAttribute( String uri, String local, String qname ) throws SAXException { 156 reportError( Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri, local ), false ); 157 } 158 protected final void unexpectedLeaveAttribute( String uri, String local, String qname ) throws SAXException { 159 reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local ), false ); 160 } 161 protected final void unexpectedText( String str ) throws SAXException { 162 str = str.replace('\r',' ').replace('\n',' ').replace('\t',' ').trim(); 164 165 reportError( Messages.format(Messages.UNEXPECTED_TEXT, str ), true ); 166 } 167 protected final void unexpectedLeaveChild() throws SAXException { 168 dump(); 172 throw new JAXBAssertionError( 173 Messages.format( Messages.UNEXPECTED_LEAVE_CHILD ) ); 174 } 175 179 protected void handleParseConversionException(Exception e) throws SAXException { 180 if( e instanceof RuntimeException ) 181 throw (RuntimeException )e; 183 ParseConversionEvent pce = new ParseConversionEventImpl( 185 ValidationEvent.ERROR, e.getMessage(), 186 new ValidationEventLocatorImpl(context.getLocator()), e ); 187 context.handleEvent(pce,true); 188 } 189 190 191 private UnmarshallingEventHandler spawnChild( Class clazz, int memento ) { 197 198 UnmarshallableObject child; 199 try { 200 child = (UnmarshallableObject)clazz.newInstance(); 201 } catch (InstantiationException e) { 202 throw new InstantiationError (e.getMessage()); 203 } catch (IllegalAccessException e) { 204 throw new IllegalAccessError (e.getMessage()); 205 } 206 207 UnmarshallingEventHandler handler = child.createUnmarshaller(context); 208 context.pushContentHandler(handler,memento); 209 return handler; 210 } 211 212 protected final Object spawnChildFromEnterElement(Class clazz, int memento, String uri, String local, String qname, Attributes atts) 213 throws SAXException { 214 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 215 ueh.enterElement(uri,local,qname,atts); 216 return ueh.owner(); 217 } 218 219 protected final Object spawnChildFromEnterAttribute(Class clazz, int memento, String uri, String local, String qname) 220 throws SAXException { 221 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 222 ueh.enterAttribute(uri,local,qname); 223 return ueh.owner(); 224 } 225 226 protected final Object spawnChildFromText(Class clazz, int memento, String value) 227 throws SAXException { 228 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 229 ueh.text(value); 230 return ueh.owner(); 231 } 232 233 protected final Object spawnChildFromLeaveElement(Class clazz, int memento, String uri, String local, String qname) 235 throws SAXException { 236 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 237 ueh.leaveElement(uri,local,qname); 238 return ueh.owner(); 239 } 240 241 protected final Object spawnChildFromLeaveAttribute(Class clazz, int memento, String uri, String local, String qname) 242 throws SAXException { 243 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 244 ueh.leaveAttribute(uri,local,qname); 245 return ueh.owner(); 246 } 247 248 protected final Element spawnWildcard( int memento, String uri, String local, String qname, Attributes atts ) 249 throws SAXException { 250 UnmarshallingEventHandler ueh = context.getGrammarInfo().createUnmarshaller(uri,local,context); 251 252 if(ueh!=null) { 253 context.pushContentHandler(ueh,memento); 254 ueh.enterElement(uri,local,qname,atts); 255 return (Element)ueh.owner(); 256 } else { 257 context.pushContentHandler( new Discarder(context), memento ); 260 context.getCurrentHandler().enterElement(uri,local,qname,atts); 261 return null; } 263 } 264 265 271 272 protected final void spawnHandlerFromEnterElement( 273 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname, Attributes atts ) 274 throws SAXException { 275 276 context.pushContentHandler(unm,memento); 277 unm.enterElement(uri,local,qname,atts); 278 } 279 280 protected final void spawnHandlerFromEnterAttribute( 281 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) 282 throws SAXException { 283 284 context.pushContentHandler(unm,memento); 285 unm.enterAttribute(uri,local,qname); 286 } 287 288 protected final void spawnHandlerFromFromText( 289 UnmarshallingEventHandler unm, int memento, String value) 290 throws SAXException { 291 292 context.pushContentHandler(unm,memento); 293 unm.text(value); 294 } 295 296 protected final void spawnHandlerFromLeaveElement( 297 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) 298 throws SAXException { 299 300 context.pushContentHandler(unm,memento); 301 unm.leaveElement(uri,local,qname); 302 } 303 304 protected final void spawnHandlerFromLeaveAttribute( 305 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) 306 throws SAXException { 307 308 context.pushContentHandler(unm,memento); 309 unm.leaveAttribute(uri,local,qname); 310 } 311 312 protected final void spawnHandlerFromText( 313 UnmarshallingEventHandler unm, int memento, String text ) 314 throws SAXException { 315 316 context.pushContentHandler(unm,memento); 317 unm.text(text); 318 } 319 320 321 protected final void revertToParentFromEnterElement(String uri,String local, String qname,Attributes atts) 327 throws SAXException { 328 context.popContentHandler(); 329 context.getCurrentHandler().enterElement(uri,local,qname,atts); 330 } 331 protected final void revertToParentFromLeaveElement(String uri,String local, String qname) 332 throws SAXException { 333 context.popContentHandler(); 334 context.getCurrentHandler().leaveElement(uri,local,qname); 335 } 336 protected final void revertToParentFromEnterAttribute(String uri,String local, String qname) 337 throws SAXException { 338 context.popContentHandler(); 339 context.getCurrentHandler().enterAttribute(uri,local,qname); 340 } 341 protected final void revertToParentFromLeaveAttribute(String uri,String local, String qname) 342 throws SAXException { 343 context.popContentHandler(); 344 context.getCurrentHandler().leaveAttribute(uri,local,qname); 345 } 346 protected final void revertToParentFromText(String value) 347 throws SAXException { 348 context.popContentHandler(); 349 context.getCurrentHandler().text(value); 350 } 351 } 352 | Popular Tags |