Save This Page
Home » openjdk-7 » com.sun.org.apache.xerces.internal » util » [javadoc | source]
    1   /*
    2    * reserved comment block
    3    * DO NOT REMOVE OR ALTER!
    4    */
    5   /*
    6    * The Apache Software License, Version 1.1
    7    *
    8    *
    9    * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
   10    * reserved.
   11    *
   12    * Redistribution and use in source and binary forms, with or without
   13    * modification, are permitted provided that the following conditions
   14    * are met:
   15    *
   16    * 1. Redistributions of source code must retain the above copyright
   17    *    notice, this list of conditions and the following disclaimer.
   18    *
   19    * 2. Redistributions in binary form must reproduce the above copyright
   20    *    notice, this list of conditions and the following disclaimer in
   21    *    the documentation and/or other materials provided with the
   22    *    distribution.
   23    *
   24    * 3. The end-user documentation included with the redistribution,
   25    *    if any, must include the following acknowledgment:
   26    *       "This product includes software developed by the
   27    *        Apache Software Foundation (http://www.apache.org/)."
   28    *    Alternately, this acknowledgment may appear in the software itself,
   29    *    if and wherever such third-party acknowledgments normally appear.
   30    *
   31    * 4. The names "Xerces" and "Apache Software Foundation" must
   32    *    not be used to endorse or promote products derived from this
   33    *    software without prior written permission. For written
   34    *    permission, please contact apache@apache.org.
   35    *
   36    * 5. Products derived from this software may not be called "Apache",
   37    *    nor may "Apache" appear in their name, without prior written
   38    *    permission of the Apache Software Foundation.
   39    *
   40    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   41    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   42    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   43    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   44    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   45    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   46    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   47    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   48    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   49    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   50    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   51    * SUCH DAMAGE.
   52    * ====================================================================
   53    *
   54    * This software consists of voluntary contributions made by many
   55    * individuals on behalf of the Apache Software Foundation and was
   56    * originally based on software copyright (c) 1999, International
   57    * Business Machines, Inc., http://www.apache.org.  For more
   58    * information on the Apache Software Foundation, please see
   59    * <http://www.apache.org/>.
   60    */
   61   package com.sun.org.apache.xerces.internal.util;
   62   
   63   import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;
   64   import com.sun.org.apache.xerces.internal.jaxp.validation.WrappedSAXException;
   65   import com.sun.org.apache.xerces.internal.xni.QName;
   66   import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
   67   import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
   68   import com.sun.org.apache.xerces.internal.xni.XMLLocator;
   69   import com.sun.org.apache.xerces.internal.xni.XMLString;
   70   import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
   71   import org.xml.sax.Attributes;
   72   import org.xml.sax.ContentHandler;
   73   import org.xml.sax.Locator;
   74   import org.xml.sax.SAXException;
   75   
   76   /**
   77    * Receves SAX {@link ContentHandler} events
   78    * and produces the equivalent {@link XMLDocumentHandler} events.
   79    *
   80    * @author
   81    *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
   82    */
   83   public class SAX2XNI implements ContentHandler, XMLDocumentSource {
   84       public SAX2XNI( XMLDocumentHandler core ) {
   85           this.fCore = core;
   86       }
   87   
   88       private XMLDocumentHandler fCore;
   89   
   90       private final NamespaceSupport nsContext = new NamespaceSupport();
   91       private final SymbolTable symbolTable = new SymbolTable();
   92   
   93   
   94       public void setDocumentHandler(XMLDocumentHandler handler) {
   95           fCore = handler;
   96       }
   97   
   98       public XMLDocumentHandler getDocumentHandler() {
   99           return fCore;
  100       }
  101   
  102   
  103       //
  104       //
  105       // ContentHandler implementation
  106       //
  107       //
  108       public void startDocument() throws SAXException {
  109           try {
  110               nsContext.reset();
  111   
  112               XMLLocator xmlLocator;
  113               if(locator==null)
  114                   // some SAX source doesn't provide a locator,
  115                   // in which case we assume no line information is available
  116                   // and use a dummy locator. With this, downstream components
  117                   // can always assume that they will get a non-null Locator.
  118                   xmlLocator=new SimpleLocator(null,null,-1,-1);
  119               else
  120                   xmlLocator=new LocatorWrapper(locator);
  121   
  122               fCore.startDocument(
  123                       xmlLocator,
  124                       null,
  125                       nsContext,
  126                       null);
  127           } catch( WrappedSAXException e ) {
  128               throw e.exception;
  129           }
  130       }
  131   
  132       public void endDocument() throws SAXException {
  133           try {
  134               fCore.endDocument(null);
  135           } catch( WrappedSAXException e ) {
  136               throw e.exception;
  137           }
  138       }
  139   
  140       public void startElement( String uri, String local, String qname, Attributes att ) throws SAXException {
  141           try {
  142               fCore.startElement(createQName(uri,local,qname),createAttributes(att),null);
  143           } catch( WrappedSAXException e ) {
  144               throw e.exception;
  145           }
  146       }
  147   
  148       public void endElement( String uri, String local, String qname ) throws SAXException {
  149           try {
  150               fCore.endElement(createQName(uri,local,qname),null);
  151           } catch( WrappedSAXException e ) {
  152               throw e.exception;
  153           }
  154       }
  155   
  156       public void characters( char[] buf, int offset, int len ) throws SAXException {
  157           try {
  158               fCore.characters(new XMLString(buf,offset,len),null);
  159           } catch( WrappedSAXException e ) {
  160               throw e.exception;
  161           }
  162       }
  163   
  164       public void ignorableWhitespace( char[] buf, int offset, int len ) throws SAXException {
  165           try {
  166               fCore.ignorableWhitespace(new XMLString(buf,offset,len),null);
  167           } catch( WrappedSAXException e ) {
  168               throw e.exception;
  169           }
  170       }
  171   
  172       public void startPrefixMapping( String prefix, String uri ) {
  173           nsContext.pushContext();
  174           nsContext.declarePrefix(prefix,uri);
  175       }
  176   
  177       public void endPrefixMapping( String prefix ) {
  178           nsContext.popContext();
  179       }
  180   
  181       public void processingInstruction( String target, String data ) throws SAXException {
  182           try {
  183               fCore.processingInstruction(
  184                       symbolize(target),createXMLString(data),null);
  185           } catch( WrappedSAXException e ) {
  186               throw e.exception;
  187           }
  188       }
  189   
  190       public void skippedEntity( String name ) {
  191       }
  192   
  193       private Locator locator;
  194       public void setDocumentLocator( Locator _loc ) {
  195           this.locator = _loc;
  196       }
  197   
  198       /** Creates a QName object. */
  199       private QName createQName(String uri, String local, String raw) {
  200   
  201           int idx = raw.indexOf(':');
  202   
  203           if( local.length()==0 ) {
  204               // if naemspace processing is turned off, local could be "".
  205               // in that case, treat everything to be in the no namespace.
  206               uri = "";
  207               if(idx<0)
  208                   local = raw;
  209               else
  210                   local = raw.substring(idx+1);
  211           }
  212   
  213           String prefix;
  214           if (idx < 0)
  215               prefix = null;
  216           else
  217               prefix = raw.substring(0, idx);
  218   
  219           if (uri != null && uri.length() == 0)
  220               uri = null; // XNI uses null whereas SAX uses the empty string
  221   
  222           return new QName(symbolize(prefix), symbolize(local), symbolize(raw), symbolize(uri));
  223       }
  224   
  225       /** Symbolizes the specified string. */
  226       private String symbolize(String s) {
  227           if (s == null)
  228               return null;
  229           else
  230               return symbolTable.addSymbol(s);
  231       }
  232   
  233       private XMLString createXMLString(String str) {
  234           // with my patch
  235           // return new XMLString(str);
  236   
  237           // for now
  238           return new XMLString(str.toCharArray(), 0, str.length());
  239       }
  240   
  241   
  242       /** only one instance of XMLAttributes is used. */
  243       private final XMLAttributes xa = new XMLAttributesImpl();
  244   
  245       /** Creates an XMLAttributes object. */
  246       private XMLAttributes createAttributes(Attributes att) {
  247           xa.removeAllAttributes();
  248           int len = att.getLength();
  249           for (int i = 0; i < len; i++)
  250               xa.addAttribute(
  251                       createQName(att.getURI(i), att.getLocalName(i), att.getQName(i)),
  252                       att.getType(i),
  253                       att.getValue(i));
  254           return xa;
  255       }
  256   }

Save This Page
Home » openjdk-7 » com.sun.org.apache.xerces.internal » util » [javadoc | source]