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