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