001//
002// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v1.0.6-01/24/2006 06:15 PM(kohsuke)-fcs 
003// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
004// Any modifications to this file will be lost upon recompilation of the source schema. 
005// Generated on: 2012.10.03 at 04:27:47 AM CEST 
006//
007
008package org.jdtaus.mojo.resource.model.impl.runtime;
009
010import javax.xml.bind.ValidationEvent;
011import javax.xml.bind.helpers.ValidationEventImpl;
012import javax.xml.bind.helpers.ValidationEventLocatorImpl;
013
014import org.xml.sax.Attributes;
015import org.xml.sax.ContentHandler;
016import org.xml.sax.SAXException;
017
018/**
019 * Redirects events to another SAX ContentHandler.
020 * 
021 * <p>
022 * Note that the SAXException returned by the ContentHandler is
023 * unreported. So we have to catch them and report it, then rethrow
024 * it if necessary. 
025 * 
026 * @author
027 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
028 */
029public class UnmarshallingEventHandlerAdaptor implements UnmarshallingEventHandler {
030    
031    protected final UnmarshallingContext context;
032
033    /** This handler will receive SAX events. */
034    protected final ContentHandler handler;
035
036    public UnmarshallingEventHandlerAdaptor(UnmarshallingContext _ctxt,ContentHandler _handler) throws SAXException {
037        this.context = _ctxt;
038        this.handler = _handler;
039        
040        // emulate the start of documents
041        try {
042            handler.setDocumentLocator(context.getLocator());
043            handler.startDocument();
044            declarePrefixes( context.getAllDeclaredPrefixes() );
045        } catch( SAXException e ) {
046            error(e);
047        }
048    }
049
050    public Object owner() {
051        return null;
052    }
053
054
055    // nest level of elements.
056    private int depth = 0;
057        
058    public void enterAttribute(String uri, String local, String qname) throws SAXException {
059    }
060
061    public void enterElement(String uri, String local, String qname, Attributes atts) throws SAXException {
062        depth++;
063        context.pushAttributes(atts,true);
064        try {
065            declarePrefixes(context.getNewlyDeclaredPrefixes());
066            handler.startElement(uri,local,qname,atts);
067        } catch( SAXException e ) {
068            error(e);
069        }
070    }
071
072    public void leaveAttribute(String uri, String local, String qname) throws SAXException {
073    }
074
075    public void leaveElement(String uri, String local, String qname) throws SAXException {
076        try {
077            handler.endElement(uri,local,qname);
078            undeclarePrefixes(context.getNewlyDeclaredPrefixes());
079        } catch( SAXException e ) {
080            error(e);
081        }
082        context.popAttributes();
083        
084        depth--;
085        if(depth==0) {
086            // emulate the end of the document
087            try {
088                undeclarePrefixes(context.getAllDeclaredPrefixes());
089                handler.endDocument();
090            } catch( SAXException e ) {
091                error(e);
092            }
093            context.popContentHandler();
094        }
095    }
096    
097    private void declarePrefixes( String[] prefixes ) throws SAXException {
098        for( int i=prefixes.length-1; i>=0; i-- )
099            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}