1 package com.sun.tools.xjc.runtime; 2 3 import java.util.StringTokenizer ; 4 5 import javax.xml.bind.Element; 6 import javax.xml.bind.ParseConversionEvent; 7 import javax.xml.bind.ValidationEvent; 8 import javax.xml.bind.helpers.ParseConversionEventImpl; 9 import javax.xml.bind.helpers.ValidationEventImpl; 10 import javax.xml.bind.helpers.ValidationEventLocatorImpl; 11 12 import org.xml.sax.Attributes ; 13 import org.xml.sax.SAXException ; 14 15 import com.sun.xml.bind.JAXBAssertionError; 16 import com.sun.xml.bind.unmarshaller.Messages; 17 18 30 public abstract class AbstractUnmarshallingEventHandlerImpl implements UnmarshallingEventHandler 31 { 32 public AbstractUnmarshallingEventHandlerImpl(UnmarshallingContext _ctxt, 33 String _stateTextTypes ) { 34 35 this.context = _ctxt; 36 this.stateTextTypes = _stateTextTypes; 37 } 38 public final UnmarshallingContext context; 39 40 44 private final String stateTextTypes; 45 46 public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException { 53 unexpectedEnterElement(uri,local,qname,atts); 54 } 55 public void leaveElement(String uri, String local, String qname) throws SAXException { 56 unexpectedLeaveElement(uri,local,qname); 57 } 58 public final void text(String text) throws SAXException { 59 if(isListState()) { 60 64 StringTokenizer tokens = new StringTokenizer (text); 65 if( tokens.countTokens()==1 ) { 66 handleText(text); 67 } else { 68 while(tokens.hasMoreTokens()) 69 context.getCurrentHandler().text(tokens.nextToken()); 72 } 73 } else { 74 handleText(text); 76 } 77 } 78 protected void handleText(String s) throws SAXException { 79 unexpectedText(s); 80 } 81 public void enterAttribute(String uri, String local, String qname) throws SAXException { 82 unexpectedEnterAttribute(uri,local,qname); 83 } 84 public void leaveAttribute(String uri, String local, String qname) throws SAXException { 85 unexpectedLeaveAttribute(uri,local,qname); 86 } 87 public void leaveChild(int nextState) throws SAXException { 88 this.state = nextState; 89 } 90 91 92 95 protected final boolean isListState() { 96 return stateTextTypes.charAt(state)=='L'; 97 } 98 99 100 101 public int state; 102 103 104 105 106 112 protected void handleUnexpectedTextException( String text, RuntimeException e ) throws SAXException { 113 reportError( Messages.format(Messages.UNEXPECTED_TEXT,text), e, true ); 115 } 116 117 120 protected void handleGenericException( Exception e ) throws SAXException { 121 reportError( e.getMessage(), e, false ); 122 } 123 124 125 protected final void dump() { 126 System.err.println("state is :"+state); 127 } 128 private void reportError( String msg, boolean canRecover ) throws SAXException { 129 reportError( msg, null, canRecover ); 130 } 131 private void reportError( String msg, Exception nested, boolean canRecover ) throws SAXException { 132 context.handleEvent( new ValidationEventImpl( 133 canRecover? ValidationEvent.ERROR : ValidationEvent.FATAL_ERROR, 134 msg, 135 new ValidationEventLocatorImpl(context.getLocator()), 136 nested ), canRecover ); 137 } 138 protected final void unexpectedEnterElement( String uri, String local, String qname, Attributes atts ) throws SAXException { 139 reportError( Messages.format(Messages.UNEXPECTED_ENTER_ELEMENT, uri, local ), true ); 141 context.pushContentHandler(new Discarder(context),state); 143 context.getCurrentHandler().enterElement(uri,local,qname,atts); 144 } 145 protected final void unexpectedLeaveElement( String uri, String local, String qname ) throws SAXException { 146 reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ELEMENT, uri, local ), false ); 147 } 148 protected final void unexpectedEnterAttribute( String uri, String local, String qname ) throws SAXException { 149 reportError( Messages.format(Messages.UNEXPECTED_ENTER_ATTRIBUTE, uri, local ), false ); 150 } 151 protected final void unexpectedLeaveAttribute( String uri, String local, String qname ) throws SAXException { 152 reportError( Messages.format(Messages.UNEXPECTED_LEAVE_ATTRIBUTE, uri, local ), false ); 153 } 154 protected final void unexpectedText( String str ) throws SAXException { 155 str = str.replace('\r',' ').replace('\n',' ').replace('\t',' ').trim(); 157 158 reportError( Messages.format(Messages.UNEXPECTED_TEXT, str ), true ); 159 } 160 protected final void unexpectedLeaveChild() throws SAXException { 161 dump(); 165 throw new JAXBAssertionError( 166 Messages.format( Messages.UNEXPECTED_LEAVE_CHILD ) ); 167 } 168 172 protected void handleParseConversionException(Exception e) throws SAXException { 173 if( e instanceof RuntimeException ) 174 throw (RuntimeException )e; 176 ParseConversionEvent pce = new ParseConversionEventImpl( 178 ValidationEvent.ERROR, e.getMessage(), 179 new ValidationEventLocatorImpl(context.getLocator()), e ); 180 context.handleEvent(pce,true); 181 } 182 183 184 private UnmarshallingEventHandler spawnChild( Class clazz, int memento ) { 190 191 UnmarshallableObject child; 192 try { 193 child = (UnmarshallableObject)clazz.newInstance(); 194 } catch (InstantiationException e) { 195 throw new InstantiationError (e.getMessage()); 196 } catch (IllegalAccessException e) { 197 throw new IllegalAccessError (e.getMessage()); 198 } 199 200 UnmarshallingEventHandler handler = child.createUnmarshaller(context); 201 context.pushContentHandler(handler,memento); 202 return handler; 203 } 204 205 protected final Object spawnChildFromEnterElement(Class clazz, int memento, String uri, String local, String qname, Attributes atts) 206 throws SAXException { 207 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 208 ueh.enterElement(uri,local,qname,atts); 209 return ueh.owner(); 210 } 211 212 protected final Object spawnChildFromEnterAttribute(Class clazz, int memento, String uri, String local, String qname) 213 throws SAXException { 214 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 215 ueh.enterAttribute(uri,local,qname); 216 return ueh.owner(); 217 } 218 219 protected final Object spawnChildFromText(Class clazz, int memento, String value) 220 throws SAXException { 221 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 222 ueh.text(value); 223 return ueh.owner(); 224 } 225 226 protected final Object spawnChildFromLeaveElement(Class clazz, int memento, String uri, String local, String qname) 228 throws SAXException { 229 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 230 ueh.leaveElement(uri,local,qname); 231 return ueh.owner(); 232 } 233 234 protected final Object spawnChildFromLeaveAttribute(Class clazz, int memento, String uri, String local, String qname) 235 throws SAXException { 236 UnmarshallingEventHandler ueh = spawnChild(clazz,memento); 237 ueh.leaveAttribute(uri,local,qname); 238 return ueh.owner(); 239 } 240 241 protected final Element spawnWildcard( int memento, String uri, String local, String qname, Attributes atts ) 242 throws SAXException { 243 UnmarshallingEventHandler ueh = context.getGrammarInfo().createUnmarshaller(uri,local,context); 244 245 if(ueh!=null) { 246 context.pushContentHandler(ueh,memento); 247 ueh.enterElement(uri,local,qname,atts); 248 return (Element)ueh.owner(); 249 } else { 250 context.pushContentHandler( new Discarder(context), memento ); 253 context.getCurrentHandler().enterElement(uri,local,qname,atts); 254 return null; } 256 } 257 258 264 265 protected final void spawnHandlerFromEnterElement( 266 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname, Attributes atts ) 267 throws SAXException { 268 269 context.pushContentHandler(unm,memento); 270 unm.enterElement(uri,local,qname,atts); 271 } 272 273 protected final void spawnHandlerFromEnterAttribute( 274 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) 275 throws SAXException { 276 277 context.pushContentHandler(unm,memento); 278 unm.enterAttribute(uri,local,qname); 279 } 280 281 protected final void spawnHandlerFromFromText( 282 UnmarshallingEventHandler unm, int memento, String value) 283 throws SAXException { 284 285 context.pushContentHandler(unm,memento); 286 unm.text(value); 287 } 288 289 protected final void spawnHandlerFromLeaveElement( 290 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) 291 throws SAXException { 292 293 context.pushContentHandler(unm,memento); 294 unm.leaveElement(uri,local,qname); 295 } 296 297 protected final void spawnHandlerFromLeaveAttribute( 298 UnmarshallingEventHandler unm, int memento, String uri, String local, String qname) 299 throws SAXException { 300 301 context.pushContentHandler(unm,memento); 302 unm.leaveAttribute(uri,local,qname); 303 } 304 305 protected final void spawnHandlerFromText( 306 UnmarshallingEventHandler unm, int memento, String text ) 307 throws SAXException { 308 309 context.pushContentHandler(unm,memento); 310 unm.text(text); 311 } 312 313 314 protected final void revertToParentFromEnterElement(String uri,String local, String qname,Attributes atts) 320 throws SAXException { 321 context.popContentHandler(); 322 context.getCurrentHandler().enterElement(uri,local,qname,atts); 323 } 324 protected final void revertToParentFromLeaveElement(String uri,String local, String qname) 325 throws SAXException { 326 context.popContentHandler(); 327 context.getCurrentHandler().leaveElement(uri,local,qname); 328 } 329 protected final void revertToParentFromEnterAttribute(String uri,String local, String qname) 330 throws SAXException { 331 context.popContentHandler(); 332 context.getCurrentHandler().enterAttribute(uri,local,qname); 333 } 334 protected final void revertToParentFromLeaveAttribute(String uri,String local, String qname) 335 throws SAXException { 336 context.popContentHandler(); 337 context.getCurrentHandler().leaveAttribute(uri,local,qname); 338 } 339 protected final void revertToParentFromText(String value) 340 throws SAXException { 341 context.popContentHandler(); 342 context.getCurrentHandler().text(value); 343 } 344 } 345 | Popular Tags |