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