Save This Page
Home » openjdk-7 » com.sun.xml.internal » bind » v2 » runtime » [javadoc | source]
    1   /*
    2    * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
    3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
    4    *
    5    * This code is free software; you can redistribute it and/or modify it
    6    * under the terms of the GNU General Public License version 2 only, as
    7    * published by the Free Software Foundation.  Sun designates this
    8    * particular file as subject to the "Classpath" exception as provided
    9    * by Sun in the LICENSE file that accompanied this code.
   10    *
   11    * This code is distributed in the hope that it will be useful, but WITHOUT
   12    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   13    * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   14    * version 2 for more details (a copy is included in the LICENSE file that
   15    * accompanied this code).
   16    *
   17    * You should have received a copy of the GNU General Public License version
   18    * 2 along with this work; if not, write to the Free Software Foundation,
   19    * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   20    *
   21    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
   22    * CA 95054 USA or visit www.sun.com if you need additional information or
   23    * have any questions.
   24    */
   25   
   26   package com.sun.xml.internal.bind.v2.runtime;
   27   
   28   import java.io.IOException;
   29   
   30   import javax.xml.stream.XMLStreamException;
   31   
   32   import com.sun.istack.internal.FinalArrayList;
   33   import com.sun.istack.internal.SAXException2;
   34   
   35   import org.xml.sax.Attributes;
   36   import org.xml.sax.SAXException;
   37   import org.xml.sax.helpers.DefaultHandler;
   38   
   39   /**
   40    * Receives SAX2 events and send the equivalent events to
   41    * {@link XMLSerializer}
   42    *
   43    * @author
   44    *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
   45    */
   46   final class ContentHandlerAdaptor extends DefaultHandler {
   47   
   48       /** Stores newly declared prefix-URI mapping. */
   49       private final FinalArrayList prefixMap = new FinalArrayList();
   50   
   51       /** Events will be sent to this object. */
   52       private final XMLSerializer serializer;
   53   
   54       private final StringBuffer text = new StringBuffer();
   55   
   56   
   57       ContentHandlerAdaptor( XMLSerializer _serializer ) {
   58           this.serializer = _serializer;
   59       }
   60   
   61       public void startDocument() {
   62           prefixMap.clear();
   63       }
   64   
   65       public void startPrefixMapping(String prefix, String uri) {
   66           prefixMap.add(prefix);
   67           prefixMap.add(uri);
   68       }
   69   
   70       public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
   71           throws SAXException {
   72           try {
   73               flushText();
   74   
   75               int len = atts.getLength();
   76   
   77               serializer.startElement(namespaceURI,localName,getPrefix(qName),null);
   78               // declare namespace events
   79               for( int i=0; i<len; i++ ) {
   80                   String qname = atts.getQName(i);
   81                   if(qname.startsWith("xmlns"))
   82                       continue;
   83                   String prefix = getPrefix(qname);
   84   
   85                   serializer.getNamespaceContext().declareNamespace(
   86                       atts.getURI(i), prefix, true );
   87               }
   88               for( int i=0; i<prefixMap.size(); i+=2 ) {
   89                   String prefix = (String)prefixMap.get(i);
   90                   serializer.getNamespaceContext().declareNamespace(
   91                       (String)prefixMap.get(i+1),
   92                       prefix,
   93                       prefix.length()!=0 );
   94               }
   95   
   96               serializer.endNamespaceDecls(null);
   97               // fire attribute events
   98               for( int i=0; i<len; i++ ) {
   99                   // be defensive.
  100                   if(atts.getQName(i).startsWith("xmlns"))
  101                       continue;
  102                   serializer.attribute( atts.getURI(i), atts.getLocalName(i), atts.getValue(i));
  103               }
  104               prefixMap.clear();
  105               serializer.endAttributes();
  106           } catch (IOException e) {
  107               throw new SAXException2(e);
  108           } catch (XMLStreamException e) {
  109               throw new SAXException2(e);
  110           }
  111       }
  112   
  113       private String getPrefix(String qname) {
  114           int idx = qname.indexOf(':');
  115           String prefix = (idx==-1)?qname:qname.substring(0,idx);
  116           return prefix;
  117       }
  118   
  119       public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
  120           try {
  121               flushText();
  122               serializer.endElement();
  123           } catch (IOException e) {
  124               throw new SAXException2(e);
  125           } catch (XMLStreamException e) {
  126               throw new SAXException2(e);
  127           }
  128       }
  129   
  130       private void flushText() throws SAXException, IOException, XMLStreamException {
  131           if( text.length()!=0 ) {
  132               serializer.text(text.toString(),null);
  133               text.setLength(0);
  134           }
  135       }
  136   
  137       public void characters(char[] ch, int start, int length) {
  138           text.append(ch,start,length);
  139       }
  140   }

Save This Page
Home » openjdk-7 » com.sun.xml.internal » bind » v2 » runtime » [javadoc | source]