Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » validator » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.impl.validator;
   17   
   18   import org.apache.xmlbeans.impl.common.XMLNameHelper;
   19   import org.apache.xmlbeans.impl.common.GenericXmlInputStream;
   20   import org.apache.xmlbeans.impl.common.ValidatorListener.Event;
   21   import org.apache.xmlbeans.impl.common.ValidatorListener;
   22   import org.apache.xmlbeans.impl.common.XmlWhitespace;
   23   import org.apache.xmlbeans.impl.schema.BuiltinSchemaTypeSystem;
   24   import org.apache.xmlbeans.SchemaType;
   25   import org.apache.xmlbeans.SchemaTypeLoader;
   26   import org.apache.xmlbeans.XmlCursor;
   27   import org.apache.xmlbeans.XmlError;
   28   import org.apache.xmlbeans.XmlOptions;
   29   import org.apache.xmlbeans.XMLStreamValidationException;
   30   import java.util.Map;
   31   import java.util.AbstractCollection;
   32   import java.util.Iterator;
   33   import java.util.Collections;
   34   
   35   import org.apache.xmlbeans.xml.stream.Attribute;
   36   import org.apache.xmlbeans.xml.stream.AttributeIterator;
   37   import org.apache.xmlbeans.xml.stream.CharacterData;
   38   import org.apache.xmlbeans.xml.stream.StartElement;
   39   import org.apache.xmlbeans.xml.stream.XMLEvent;
   40   import org.apache.xmlbeans.xml.stream.XMLInputStream;
   41   import org.apache.xmlbeans.xml.stream.XMLName;
   42   import org.apache.xmlbeans.xml.stream.XMLStreamException;
   43   import javax.xml.namespace.QName;
   44   
   45   /**
   46    * @deprecated XMLInputStream was deprecated by XMLStreamReader from STaX - jsr173 API.
   47    */
   48   public final class ValidatingXMLInputStream
   49       extends GenericXmlInputStream implements Event
   50   {
   51       public ValidatingXMLInputStream (
   52           XMLInputStream xis,
   53           SchemaTypeLoader typeLoader, SchemaType sType, XmlOptions options )
   54               throws XMLStreamException
   55       {
   56           _source = xis;
   57   
   58           // Figure out the root type
   59   
   60           options = XmlOptions.maskNull( options );
   61           
   62           SchemaType type = (SchemaType) options.get( XmlOptions.DOCUMENT_TYPE );
   63   
   64           if (type == null)
   65               type = sType;
   66   
   67           if (type == null)
   68           {
   69               type = BuiltinSchemaTypeSystem.ST_ANY_TYPE;
   70   
   71               xis = xis.getSubStream();
   72   
   73               if (xis.skip( XMLEvent.START_ELEMENT ))
   74               {
   75                   SchemaType docType =
   76                       typeLoader.findDocumentType(
   77                           XMLNameHelper.getQName( xis.next().getName() ) );
   78   
   79                   if (docType != null)
   80                       type = docType;
   81               }
   82   
   83               xis.close();
   84           }
   85   
   86           // Create the validator
   87   
   88           _validator =
   89               new Validator(
   90                   type, null, typeLoader, options, new ExceptionXmlErrorListener() );
   91   
   92           nextEvent( ValidatorListener.BEGIN );
   93       }
   94   
   95       // TODO - this is the quick and dirty impl of streaming validation,
   96       // may objects are created (like strings) which can be optimized
   97       
   98       protected XMLEvent nextEvent ( ) throws XMLStreamException
   99       {
  100           XMLEvent e = _source.next();
  101   
  102           if (e == null)
  103           {
  104               if (!_finished)
  105               {
  106                   flushText();
  107                   nextEvent( ValidatorListener.END );
  108                   _finished = true;
  109               }
  110           }
  111           else
  112           {
  113               switch ( e.getType() )
  114               {
  115               case XMLEvent.CHARACTER_DATA :
  116               case XMLEvent.SPACE :
  117               {
  118                   CharacterData cd = (CharacterData) e;
  119   
  120                   if (cd.hasContent())
  121                       _text.append( cd.getContent() );
  122   
  123                   break;
  124               }
  125               case XMLEvent.START_ELEMENT :
  126               {
  127                   StartElement se = (StartElement) e;
  128                   
  129                   flushText();
  130   
  131                   // Used for prefix to namespace mapping
  132                   _startElement = se;
  133   
  134                   // Prepare the xsi:* values
  135                   
  136                   AttributeIterator attrs = se.getAttributes();
  137   
  138                   while ( attrs.hasNext() )
  139                   {
  140                       Attribute attr = attrs.next();
  141   
  142                       XMLName attrName = attr.getName();
  143   
  144                       if ("http://www.w3.org/2001/XMLSchema-instance".equals(
  145                               attrName.getNamespaceUri() ))
  146                       {
  147                           String local = attrName.getLocalName();
  148   
  149                           if (local.equals( "type" ))
  150                               _xsiType = attr.getValue();
  151                           else if (local.equals( "nil" ))
  152                               _xsiNil = attr.getValue();
  153                           else if (local.equals( "schemaLocation" ))
  154                               _xsiLoc = attr.getValue();
  155                           else if (local.equals( "noNamespaceSchemaLocation" ))
  156                               _xsiNoLoc = attr.getValue();
  157                       }
  158                   }
  159   
  160                   // Emit the START
  161   
  162                   // TODO - should delay the aquisition of the name
  163                   _name = e.getName();
  164   
  165                   nextEvent( ValidatorListener.BEGIN );
  166                   
  167                   // Emit the attrs
  168                   
  169                   attrs = se.getAttributes();
  170   
  171                   while ( attrs.hasNext() )
  172                   {
  173                       Attribute attr = attrs.next();
  174   
  175                       XMLName attrName = attr.getName();
  176   
  177                       if ("http://www.w3.org/2001/XMLSchema-instance".equals(
  178                               attrName.getNamespaceUri() ))
  179                       {
  180                           String local = attrName.getLocalName();
  181   
  182                           if (local.equals( "type" ))
  183                               continue;
  184                           else if (local.equals( "nil" ))
  185                               continue;
  186                           else if (local.equals( "schemaLocation" ))
  187                               continue;
  188                           else if (local.equals( "noNamespaceSchemaLocation" ))
  189                               continue;
  190                       }
  191   
  192                       // TODO - God, this is lame :-)
  193   
  194                       _text.append( attr.getValue() );
  195                       _name = attr.getName();
  196                       
  197                       nextEvent( ValidatorListener.ATTR );
  198                   }
  199   
  200                   clearText();
  201   
  202                   _startElement = null;
  203   
  204                   break;
  205               }
  206   
  207               case XMLEvent.END_ELEMENT :
  208               {
  209                   flushText();
  210                   
  211                   nextEvent( ValidatorListener.END );
  212   
  213                   break;
  214               }
  215               }
  216           }
  217   
  218           return e;
  219       }
  220   
  221       private void clearText ( )
  222       {
  223           _text.delete( 0, _text.length() );
  224       }
  225       
  226       private void flushText ( ) throws XMLStreamException
  227       {
  228           if (_text.length() > 0)
  229           {
  230               nextEvent( ValidatorListener.TEXT );
  231               clearText();
  232           }
  233       }
  234       
  235       public String getNamespaceForPrefix ( String prefix )
  236       {
  237           if (_startElement == null)
  238               return null;
  239   
  240           Map map = _startElement.getNamespaceMap();
  241   
  242           if (map == null)
  243               return null;
  244   
  245           return (String) map.get( prefix );
  246       }
  247   
  248       public XmlCursor getLocationAsCursor ( )
  249       {
  250           return null;
  251       }
  252   
  253       public javax.xml.stream.Location getLocation()
  254       {
  255           try
  256           {
  257               final org.apache.xmlbeans.xml.stream.Location xeLoc = _source.peek().getLocation();
  258   
  259               if (xeLoc==null)
  260                   return null;
  261   
  262               javax.xml.stream.Location loc = new javax.xml.stream.Location()
  263               {
  264                   public int getLineNumber()
  265                   { return xeLoc.getLineNumber(); }
  266   
  267                   public int getColumnNumber()
  268                   { return xeLoc.getColumnNumber(); }
  269   
  270                   public int getCharacterOffset()
  271                   { return -1;}
  272   
  273                   public String getPublicId()
  274                   { return xeLoc.getPublicId(); }
  275   
  276                   public String getSystemId()
  277                   { return xeLoc.getSystemId(); }
  278               };
  279   
  280               return loc;
  281           }
  282           catch (XMLStreamException e)
  283           {
  284               return null;
  285           }
  286       }
  287   
  288       public String getXsiType ( )
  289       {
  290           return _xsiType;
  291       }
  292       
  293       public String getXsiNil ( )
  294       {
  295           return _xsiNil;
  296       }
  297   
  298       public String getXsiLoc ( )
  299       {
  300           return _xsiLoc;
  301       }
  302   
  303       public String getXsiNoLoc ( )
  304       {
  305           return _xsiNoLoc;
  306       }
  307   
  308       public QName getName ( )
  309       {
  310           return XMLNameHelper.getQName( _name );
  311       }
  312   
  313       public String getText ( )
  314       {
  315           return _text.toString();
  316       }
  317   
  318       public String getText ( int wsr )
  319       {
  320           return XmlWhitespace.collapse( _text.toString(), wsr );
  321       }
  322   
  323       public boolean textIsWhitespace ( )
  324       {
  325           for ( int i = 0 ; i < _text.length() ; i++ )
  326           {
  327               switch ( _text.charAt( i ) )
  328               {
  329                   case ' ':
  330                   case '\n':
  331                   case '\r':
  332                   case '\t':
  333                       break;
  334   
  335                   default :
  336                       return false;
  337               }
  338           }
  339           
  340           return true;
  341       }
  342       
  343       private final class ExceptionXmlErrorListener extends AbstractCollection
  344       {
  345           public boolean add(Object o)
  346           {
  347               assert ValidatingXMLInputStream.this._exception == null;
  348               
  349               ValidatingXMLInputStream.this._exception = 
  350                   new XMLStreamValidationException( (XmlError)o );
  351   
  352               return false;
  353           }
  354   
  355           public Iterator iterator()
  356           {
  357               return Collections.EMPTY_LIST.iterator();
  358           }
  359   
  360           public int size()
  361           {
  362               return 0;
  363           }
  364       }
  365   
  366       private void nextEvent ( int kind )
  367           throws XMLStreamException
  368       {
  369           assert _exception == null;
  370           
  371           _validator.nextEvent( kind, this );
  372   
  373           if (_exception != null)
  374               throw _exception;
  375       }
  376       
  377       private XMLStreamValidationException _exception;
  378   
  379       private XMLInputStream _source;
  380       private Validator      _validator;
  381       private StringBuffer   _text = new StringBuffer();
  382       private boolean        _finished;
  383       private String         _xsiType;
  384       private String         _xsiNil;
  385       private String         _xsiLoc;
  386       private String         _xsiNoLoc;
  387       private XMLName        _name;
  388       private StartElement   _startElement;
  389   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » validator » [javadoc | source]