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 java.util.ArrayList;
011
012import org.xml.sax.Attributes;
013import org.xml.sax.ContentHandler;
014import org.xml.sax.Locator;
015import org.xml.sax.SAXException;
016
017/**
018 * Receives SAX2 events and send the equivalent events to
019 * {@link com.sun.xml.bind.serializer.XMLSerializer}
020 * 
021 * @author
022 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
023 */
024public class ContentHandlerAdaptor implements ContentHandler {
025
026    /** Stores newly declared prefix-URI mapping. */
027    private final ArrayList prefixMap = new ArrayList();
028    
029    /** Events will be sent to this object. */
030    private final XMLSerializer serializer;
031    
032    private final StringBuffer text = new StringBuffer();
033    
034    
035    public ContentHandlerAdaptor( XMLSerializer _serializer ) {
036        this.serializer = _serializer;
037    }
038    
039    
040
041    public void startDocument() throws SAXException {
042        prefixMap.clear();
043    }
044
045    public void endDocument() throws SAXException {
046    }
047
048    public void startPrefixMapping(String prefix, String uri) throws SAXException {
049        prefixMap.add(prefix);
050        prefixMap.add(uri);
051    }
052
053    public void endPrefixMapping(String prefix) throws SAXException {
054    }
055
056    public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
057        throws SAXException {
058        
059        flushText();
060
061        int len = atts.getLength();
062        
063        serializer.startElement(namespaceURI,localName);
064        // declare namespace events
065        for( int i=0; i<len; i++ ) {
066            String qname = atts.getQName(i);
067            int idx = qname.indexOf(':');
068            String prefix = (idx==-1)?qname:qname.substring(0,idx);
069            
070            serializer.getNamespaceContext().declareNamespace(
071                atts.getURI(i), prefix, true );
072        }
073        for( int i=0; i<prefixMap.size(); i+=2 ) {
074            String prefix = (String)prefixMap.get(i); 
075            serializer.getNamespaceContext().declareNamespace(
076                (String)prefixMap.get(i+1),
077                prefix,
078                prefix.length()!=0 );
079        }
080        
081        serializer.endNamespaceDecls();
082        // fire attribute events
083        for( int i=0; i<len; i++ ) {
084            serializer.startAttribute( atts.getURI(i), atts.getLocalName(i) );
085            serializer.text(atts.getValue(i),null);
086            serializer.endAttribute();
087        }
088        prefixMap.clear();
089        serializer.endAttributes();
090    }
091
092    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
093        flushText();
094        serializer.endElement();
095    }
096    
097    private void flushText() throws SAXException {
098        if( text.length()!=0 ) {
099            serializer.text(text.toString(),null);
100            text.setLength(0);
101        }
102    }
103
104    public void characters(char[] ch, int start, int length) throws SAXException {
105        text.append(ch,start,length);
106    }
107
108    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
109        text.append(ch,start,length);
110    }
111
112
113
114    public void setDocumentLocator(Locator locator) {
115    }
116    
117    public void processingInstruction(String target, String data) throws SAXException {
118    }
119
120    public void skippedEntity(String name) throws SAXException {
121    }
122
123}