1 package com.sun.tools.xjc.runtime; 2 3 import javax.xml.bind.ValidationEvent; 4 import javax.xml.bind.helpers.ValidationEventImpl; 5 import javax.xml.bind.helpers.ValidationEventLocatorImpl; 6 7 import org.xml.sax.Attributes ; 8 import org.xml.sax.ContentHandler ; 9 import org.xml.sax.SAXException ; 10 11 22 public class UnmarshallingEventHandlerAdaptor implements UnmarshallingEventHandler { 23 24 protected final UnmarshallingContext context; 25 26 27 protected final ContentHandler handler; 28 29 public UnmarshallingEventHandlerAdaptor(UnmarshallingContext _ctxt,ContentHandler _handler) throws SAXException { 30 this.context = _ctxt; 31 this.handler = _handler; 32 33 try { 35 handler.setDocumentLocator(context.getLocator()); 36 handler.startDocument(); 37 declarePrefixes( context.getAllDeclaredPrefixes() ); 38 } catch( SAXException e ) { 39 error(e); 40 } 41 } 42 43 public Object owner() { 44 return null; 45 } 46 47 48 private int depth = 0; 50 51 public void enterAttribute(String uri, String local, String qname) throws SAXException { 52 } 53 54 public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException { 55 depth++; 56 context.pushAttributes(atts,true); 57 try { 58 declarePrefixes(context.getNewlyDeclaredPrefixes()); 59 handler.startElement(uri,local,qname,atts); 60 } catch( SAXException e ) { 61 error(e); 62 } 63 } 64 65 public void leaveAttribute(String uri, String local, String qname) throws SAXException { 66 } 67 68 public void leaveElement(String uri, String local, String qname) throws SAXException { 69 try { 70 handler.endElement(uri,local,qname); 71 undeclarePrefixes(context.getNewlyDeclaredPrefixes()); 72 } catch( SAXException e ) { 73 error(e); 74 } 75 context.popAttributes(); 76 77 depth--; 78 if(depth==0) { 79 try { 81 undeclarePrefixes(context.getAllDeclaredPrefixes()); 82 handler.endDocument(); 83 } catch( SAXException e ) { 84 error(e); 85 } 86 context.popContentHandler(); 87 } 88 } 89 90 private void declarePrefixes( String [] prefixes ) throws SAXException { 91 for( int i=prefixes.length-1; i>=0; i-- ) 92 handler.startPrefixMapping( 93 prefixes[i], 94 context.getNamespaceURI(prefixes[i]) ); 95 } 96 97 private void undeclarePrefixes( String [] prefixes ) throws SAXException { 98 for( int i=prefixes.length-1; i>=0; i-- ) 99 handler.endPrefixMapping( prefixes[i] ); 100 } 101 102 public void text(String s) throws SAXException { 103 try { 104 handler.characters(s.toCharArray(),0,s.length()); 105 } catch( SAXException e ) { 106 error(e); 107 } 108 } 109 110 private void error( SAXException e ) throws SAXException { 111 context.handleEvent(new ValidationEventImpl( 112 ValidationEvent.ERROR, 113 e.getMessage(), 114 new ValidationEventLocatorImpl(context.getLocator()), 115 e 116 ), false); 117 } 118 119 public void leaveChild(int nextState) throws SAXException { 120 } 121 } 122 | Popular Tags |