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   package org.apache.xmlbeans.impl.validator;
   16   
   17   import org.apache.xmlbeans.SchemaType;
   18   import org.apache.xmlbeans.SchemaTypeLoader;
   19   import org.apache.xmlbeans.XmlCursor;
   20   import org.apache.xmlbeans.XmlError;
   21   import org.apache.xmlbeans.XmlOptions;
   22   import org.apache.xmlbeans.impl.common.ValidatorListener;
   23   import org.apache.xmlbeans.impl.common.XmlWhitespace;
   24   import org.apache.xmlbeans.impl.common.QNameHelper;
   25   
   26   import javax.xml.namespace.QName;
   27   import javax.xml.stream.XMLStreamException;
   28   import javax.xml.stream.XMLStreamReader;
   29   import javax.xml.stream.Location;
   30   import javax.xml.stream.events.XMLEvent;
   31   import javax.xml.stream.util.StreamReaderDelegate;
   32   import java.util.ArrayList;
   33   import java.util.Collection;
   34   import java.util.List;
   35   
   36   /**
   37    * This class is a wrapper over a generic XMLStreamReader that provides validation.
   38    * There are 3 cases:
   39    * <br/> 1) the XMLStreamReader represents a document, it contains only one element document
   40    *          - in this case the user schema type should be null or it should be a document SchemaType
   41    * <br/> 2) the XMLStreamReader represents an xml-fragment (content only) - must have at least one user type or xsi:type
   42    * <br/>     a) it has an xsi:type - if user schema type is available it has to be a base type of xsi:type
   43    * <br/>     b) it doesn't have xsi:type - user must provide a schema type
   44    *         otherwise will error and will not do validation
   45    * <br/> 3) the XMLStreamReader represents a global attribute - i.e. user schema type is null and only one attribute
   46    * <br/>
   47    *
   48    * @author Cezar Andrei (cezar.andrei at bea.com)
   49    * Date: Feb 13, 2004
   50    */
   51   public class ValidatingXMLStreamReader
   52       extends StreamReaderDelegate
   53       implements XMLStreamReader
   54   {
   55       public static final String OPTION_ATTTRIBUTE_VALIDATION_COMPAT_MODE = "OPTION_ATTTRIBUTE_VALIDATION_COMPAT_MODE";
   56   
   57       private static final String URI_XSI = "http://www.w3.org/2001/XMLSchema-instance";
   58       private static final QName XSI_TYPE = new QName(URI_XSI, "type");
   59       private static final QName XSI_NIL  = new QName(URI_XSI, "nil");
   60       private static final QName XSI_SL   = new QName(URI_XSI, "schemaLocation");
   61       private static final QName XSI_NSL  = new QName(URI_XSI, "noNamespaceSchemaLocation");
   62   
   63       private SchemaType _contentType;
   64       private SchemaTypeLoader _stl;
   65       private XmlOptions _options;
   66       private Collection _errorListener;
   67       protected Validator _validator;
   68       private final ElementEventImpl _elemEvent;
   69       private final AttributeEventImpl _attEvent;
   70       private final SimpleEventImpl _simpleEvent;
   71       private PackTextXmlStreamReader _packTextXmlStreamReader;
   72   
   73       private int _state;
   74       private final int STATE_FIRSTEVENT = 0;
   75       private final int STATE_VALIDATING = 1;
   76       private final int STATE_ATTBUFFERING = 2;
   77       private final int STATE_ERROR = 3;
   78   
   79       private List _attNamesList;
   80       private List _attValuesList;
   81       private SchemaType _xsiType;
   82   
   83       private int _depth;
   84   
   85       /**
   86        * Default constructor. Use init(...) to set the params.
   87        * See {@link #init}
   88        */
   89       public ValidatingXMLStreamReader()
   90       {
   91           super();
   92           _elemEvent = new ElementEventImpl();
   93           _attEvent = new AttributeEventImpl();
   94           _simpleEvent = new SimpleEventImpl();
   95           _packTextXmlStreamReader = new PackTextXmlStreamReader();
   96       }
   97   
   98       /**
   99        * Used in case of reusing the same ValidatinXMLStreamReader object
  100        * @param xsr The stream to be validated
  101        * @param startWithCurrentEvent Validation will start if true with the current event or if false with the next event in the stream
  102        * @param contentType The schemaType of the content. This can be null for document and global Att validation
  103        * @param stl SchemaTypeLoader context of validation
  104        * @param options Validator options
  105        * @param errorListener Errors and warnings listener
  106        */
  107       public void init(XMLStreamReader xsr, boolean startWithCurrentEvent, SchemaType contentType,
  108                        SchemaTypeLoader stl, XmlOptions options, Collection errorListener)
  109       {
  110           _packTextXmlStreamReader.init(xsr);
  111   
  112   //        setParent(xsr);
  113           setParent(_packTextXmlStreamReader);
  114           _contentType = contentType;
  115           _stl = stl;
  116           _options = options;
  117           _errorListener = errorListener;
  118   //        _elemEvent.setXMLStreamReader(xsr);
  119   //        _attEvent.setXMLStreamReader(xsr);
  120   //        _simpleEvent.setXMLStreamReader(xsr);
  121           _elemEvent.setXMLStreamReader(_packTextXmlStreamReader);
  122           _attEvent.setXMLStreamReader(_packTextXmlStreamReader);
  123           _simpleEvent.setXMLStreamReader(_packTextXmlStreamReader);
  124           _validator = null;
  125           _state = STATE_FIRSTEVENT;
  126           if (_attNamesList!=null)
  127           {
  128               _attNamesList.clear();
  129               _attValuesList.clear();
  130           }
  131           _xsiType = null;
  132           _depth = 0;
  133   
  134           if (startWithCurrentEvent)
  135           {
  136               int evType = getEventType();
  137               validate_event(evType);
  138           }
  139       }
  140   
  141       private static class PackTextXmlStreamReader
  142           extends StreamReaderDelegate
  143           implements XMLStreamReader
  144       {
  145           private boolean _hasBufferedText;
  146           private StringBuffer _buffer = new StringBuffer();
  147           private int _textEventType;
  148   
  149           void init(XMLStreamReader xmlstream)
  150           {
  151               setParent(xmlstream);
  152               _hasBufferedText = false;
  153               _buffer.delete(0, _buffer.length());
  154           }
  155   
  156           public int next()
  157               throws XMLStreamException
  158           {
  159               if (_hasBufferedText)
  160               {
  161                   clearBuffer();
  162                   return super.getEventType();
  163               }
  164   
  165               int evType = super.next();
  166   
  167               if (evType == XMLEvent.CHARACTERS || evType == XMLEvent.CDATA || evType == XMLEvent.SPACE)
  168               {
  169                   _textEventType = evType;
  170                   bufferText();
  171               }
  172   
  173               return evType;
  174           }
  175   
  176           private void clearBuffer()
  177           {
  178               _buffer.delete(0, _buffer.length());
  179               _hasBufferedText = false;
  180           }
  181   
  182           private void bufferText()
  183               throws XMLStreamException
  184           {
  185               if (super.hasText())
  186                   _buffer.append( super.getText());
  187   
  188               _hasBufferedText = true;
  189   
  190               while (hasNext())
  191               {
  192                   int evType = super.next();
  193   
  194                   switch (evType)
  195                   {
  196                   case XMLEvent.CHARACTERS:
  197                   case XMLEvent.CDATA:
  198                   case XMLEvent.SPACE:
  199                       if (super.hasText())
  200                           _buffer.append(super.getText());
  201   
  202                   case XMLEvent.COMMENT:
  203                       //ignore
  204                       continue;
  205                   default:
  206                       return;
  207                   }
  208               }
  209           }
  210   
  211           public String getText()
  212           {
  213               assert _hasBufferedText;
  214               return _buffer.toString();
  215           }
  216   
  217           public int getTextLength()
  218           {
  219               assert _hasBufferedText;
  220               return _buffer.length();
  221           }
  222   
  223           public int getTextStart()
  224           {
  225               assert _hasBufferedText;
  226               return 0;
  227           }
  228   
  229           public char[] getTextCharacters()
  230           {
  231               assert _hasBufferedText;
  232               return _buffer.toString().toCharArray();
  233           }
  234   
  235           public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
  236           {
  237               assert _hasBufferedText;
  238               _buffer.getChars(sourceStart, sourceStart + length, target, targetStart);
  239               return length;
  240           }
  241   
  242           public boolean isWhiteSpace()
  243           {
  244               assert _hasBufferedText;
  245               return XmlWhitespace.isAllSpace(_buffer);
  246           }
  247   
  248           public boolean hasText()
  249           {
  250               if (_hasBufferedText)
  251                   return true;
  252               else
  253                   return super.hasText();
  254           }
  255   
  256           public int getEventType()
  257           {
  258               if (_hasBufferedText)
  259                   return _textEventType;
  260               else
  261                   return super.getEventType();
  262           }
  263       }
  264   
  265       private static class ElementEventImpl
  266           implements ValidatorListener.Event
  267       {
  268           private static final int BUF_LENGTH = 1024;
  269           private char[] _buf = new char[BUF_LENGTH];
  270           private int _length;
  271           private boolean _supportForGetTextCharacters = true;
  272   
  273           private XMLStreamReader _xmlStream;
  274   
  275           private void setXMLStreamReader(XMLStreamReader xsr)
  276           {
  277               _xmlStream = xsr;
  278           }
  279   
  280           // can return null, used only to locate errors
  281           public XmlCursor getLocationAsCursor()
  282           {
  283               return null;
  284           }
  285   
  286           public javax.xml.stream.Location getLocation()
  287           {
  288               return _xmlStream.getLocation();
  289           }
  290   
  291           // fill up chars with the xsi:type attribute value if there is one othervise return false
  292           public String getXsiType() // BEGIN xsi:type
  293           {
  294               return _xmlStream.getAttributeValue(URI_XSI, "type");
  295           }
  296   
  297           // fill up chars with xsi:nill attribute value if any
  298           public String getXsiNil() // BEGIN xsi:nil
  299           {
  300               return _xmlStream.getAttributeValue(URI_XSI, "nil");
  301           }
  302   
  303           // not used curently
  304           public String getXsiLoc() // BEGIN xsi:schemaLocation
  305           {
  306               return _xmlStream.getAttributeValue(URI_XSI, "schemaLocation");
  307           }
  308   
  309           // not used curently
  310           public String getXsiNoLoc() // BEGIN xsi:noNamespaceSchemaLocation
  311           {
  312               return _xmlStream.getAttributeValue(URI_XSI, "noNamespaceSchemaLocation");
  313           }
  314   
  315           // On START and ATTR
  316           public QName getName()
  317           {
  318               // avoid construction of a new QName object after the bug in getName() is fixed.
  319               if (_xmlStream.hasName())
  320                   return new QName(_xmlStream.getNamespaceURI(), _xmlStream.getLocalName());
  321               else
  322                   return null;
  323           }
  324   
  325           // On TEXT and ATTR
  326           public String getText()
  327           {
  328               _length = 0;
  329               addTextToBuffer();
  330               return new String( _buf, 0, _length );
  331   //            return _xmlStream.getText();
  332           }
  333   
  334           public String getText(int wsr)
  335           {
  336               return XmlWhitespace.collapse( _xmlStream.getText(), wsr );
  337           }
  338   
  339           public boolean textIsWhitespace()
  340           {
  341               return _xmlStream.isWhiteSpace();
  342           }
  343   
  344           public String getNamespaceForPrefix(String prefix)
  345           {
  346               return _xmlStream.getNamespaceURI(prefix);
  347           }
  348   
  349           private void addTextToBuffer()
  350           {
  351               int textLength = _xmlStream.getTextLength();
  352               ensureBufferLength(textLength);
  353   
  354               if (_supportForGetTextCharacters)
  355                   try
  356                   {
  357                       _length = _xmlStream.getTextCharacters(0, _buf, _length, textLength);
  358                   }
  359                   catch(Exception e)
  360                   {
  361                       _supportForGetTextCharacters = false;
  362                   }
  363   
  364               if(!_supportForGetTextCharacters)
  365               {
  366                   System.arraycopy(_xmlStream.getTextCharacters(), _xmlStream.getTextStart(), _buf, _length, textLength);
  367                   _length = _length + textLength;
  368               }
  369           }
  370   
  371           private void ensureBufferLength(int lengthToAdd)
  372           {
  373               if (_length + lengthToAdd>_buf.length)
  374               {
  375                   char[] newBuf = new char[_length + lengthToAdd];
  376                   if (_length>0)
  377                       System.arraycopy(_buf, 0, newBuf, 0, _length);
  378                   _buf = newBuf;
  379               }
  380           }
  381       }
  382   
  383       private static final class AttributeEventImpl
  384           implements ValidatorListener.Event
  385       {
  386           private int _attIndex;
  387           private XMLStreamReader _xmlStream;
  388   
  389           private void setXMLStreamReader(XMLStreamReader xsr)
  390           {
  391               _xmlStream = xsr;
  392           }
  393   
  394           // can return null, used only to locate errors
  395           public XmlCursor getLocationAsCursor()
  396           {
  397               return null;
  398           }
  399   
  400           public javax.xml.stream.Location getLocation()
  401           {
  402               return _xmlStream.getLocation();
  403           }
  404   
  405           // fill up chars with the xsi:type attribute value if there is one othervise return false
  406           public String getXsiType() // BEGIN xsi:type
  407           {
  408               throw new IllegalStateException();
  409           }
  410   
  411           // fill up chars with xsi:nill attribute value if any
  412           public String getXsiNil() // BEGIN xsi:nil
  413           {
  414               throw new IllegalStateException();
  415           }
  416   
  417           // not used curently
  418           public String getXsiLoc() // BEGIN xsi:schemaLocation
  419           {
  420               throw new IllegalStateException();
  421           }
  422   
  423           // not used curently
  424           public String getXsiNoLoc() // BEGIN xsi:noNamespaceSchemaLocation
  425           {
  426               throw new IllegalStateException();
  427           }
  428   
  429           // On START and ATTR
  430           public QName getName()
  431           {
  432               assert _xmlStream.isStartElement() : "Not on Start Element.";
  433               String uri = _xmlStream.getAttributeNamespace(_attIndex);
  434               QName qn = new QName(uri==null ? "" : uri, _xmlStream.getAttributeLocalName(_attIndex));
  435               //System.out.println("    Att QName: " + qn);
  436               return qn;
  437           }
  438   
  439           // On TEXT and ATTR
  440           public String getText()
  441           {
  442               assert _xmlStream.isStartElement() : "Not on Start Element.";
  443               return _xmlStream.getAttributeValue(_attIndex);
  444           }
  445   
  446           public String getText(int wsr)
  447           {
  448               assert _xmlStream.isStartElement() : "Not on Start Element.";
  449               return XmlWhitespace.collapse( _xmlStream.getAttributeValue(_attIndex), wsr );
  450           }
  451   
  452           public boolean textIsWhitespace()
  453           {
  454               throw new IllegalStateException();
  455           }
  456   
  457           public String getNamespaceForPrefix(String prefix)
  458           {
  459               assert _xmlStream.isStartElement() : "Not on Start Element.";
  460               return _xmlStream.getNamespaceURI(prefix);
  461           }
  462   
  463           private void setAttributeIndex(int attIndex)
  464           {
  465               _attIndex = attIndex;
  466           }
  467       }
  468   
  469       /**
  470        * This is used as implementation of Event for validating global attributes
  471        * and for pushing the buffered attributes
  472        */
  473       private static final class SimpleEventImpl
  474           implements ValidatorListener.Event
  475       {
  476           private String _text;
  477           private QName  _qname;
  478           private XMLStreamReader _xmlStream;
  479   
  480           private void setXMLStreamReader(XMLStreamReader xsr)
  481           {
  482               _xmlStream = xsr;
  483           }
  484   
  485           // should return null, getLocation will be used, used only to locate errors
  486           public XmlCursor getLocationAsCursor()
  487           { return null; }
  488   
  489           public javax.xml.stream.Location getLocation()
  490           {
  491               return _xmlStream.getLocation();
  492           }
  493   
  494           // fill up chars with the xsi:type attribute value if there is one othervise return false
  495           public String getXsiType() // BEGIN xsi:type
  496           { return null; }
  497   
  498           // fill up chars with xsi:nill attribute value if any
  499           public String getXsiNil() // BEGIN xsi:nil
  500           { return null; }
  501   
  502           // not used curently
  503           public String getXsiLoc() // BEGIN xsi:schemaLocation
  504           { return null; }
  505   
  506           // not used curently
  507           public String getXsiNoLoc() // BEGIN xsi:noNamespaceSchemaLocation
  508           { return null; }
  509   
  510           // On START and ATTR
  511           public QName getName()
  512           { return _qname; }
  513   
  514           // On TEXT and ATTR
  515           public String getText()
  516           {
  517               return _text;
  518           }
  519   
  520           public String getText(int wsr)
  521           {
  522               return XmlWhitespace.collapse( _text, wsr );
  523           }
  524   
  525           public boolean textIsWhitespace()
  526           { return false; }
  527   
  528           public String getNamespaceForPrefix(String prefix)
  529           {
  530               return _xmlStream.getNamespaceURI(prefix);
  531           }
  532       }
  533   
  534       /* public methods in XMLStreamReader */
  535   
  536       public Object getProperty(String s) throws IllegalArgumentException
  537       {
  538           return super.getProperty(s);
  539       }
  540   
  541       public int next() throws XMLStreamException
  542       {
  543           int evType = super.next();
  544           //debugEvent(evType);
  545   
  546           validate_event(evType);
  547   
  548           return evType;
  549       }
  550   
  551       private void validate_event(int evType)
  552       {
  553           if (_state==STATE_ERROR)
  554               return;
  555   
  556           if (_depth<0)
  557               throw new IllegalArgumentException("ValidatingXMLStreamReader cannot go further than the subtree is was initialized on.");
  558   
  559           switch(evType)
  560           {
  561           case XMLEvent.START_ELEMENT:
  562               _depth++;
  563               if (_state == STATE_ATTBUFFERING)
  564                   pushBufferedAttributes();
  565   
  566               if (_validator==null)
  567               {
  568                   // avoid construction of a new QName object after the bug in getName() is fixed.
  569                   QName qname = new QName(getNamespaceURI(), getLocalName());
  570   
  571                   if (_contentType==null)
  572                       _contentType = typeForGlobalElement(qname);
  573   
  574                   if (_state==STATE_ERROR)
  575                       break;
  576   
  577                   initValidator(_contentType);
  578                   _validator.nextEvent(Validator.BEGIN, _elemEvent);
  579               }
  580   
  581               _validator.nextEvent(Validator.BEGIN, _elemEvent);
  582   
  583               int attCount = getAttributeCount();
  584               validate_attributes(attCount);
  585   
  586               break;
  587   
  588           case XMLEvent.ATTRIBUTE:
  589               if (getAttributeCount()==0)
  590                   break;
  591   
  592               if (_state == STATE_FIRSTEVENT || _state == STATE_ATTBUFFERING)
  593               {
  594                   // buffer all Attributes
  595                   for (int i=0; i<getAttributeCount(); i++)
  596                   {
  597                       // avoid construction of a new QName object after the bug in getName() is fixed.
  598                       QName qname = new QName(getAttributeNamespace(i), getAttributeLocalName(i));
  599   
  600                       if (qname.equals(XSI_TYPE))
  601                       {
  602                           String xsiTypeValue = getAttributeValue(i);
  603                           String uri = super.getNamespaceURI(QNameHelper.getPrefixPart(xsiTypeValue));
  604                           QName xsiTypeQname = new QName(uri, QNameHelper.getLocalPart(xsiTypeValue));
  605                           _xsiType = _stl.findType(xsiTypeQname);
  606                       }
  607   
  608                       if (_attNamesList==null)
  609                       {
  610                           _attNamesList = new ArrayList();
  611                           _attValuesList = new ArrayList();
  612                       }
  613                       // skip xsi:type xsi:nil xsi:schemaLocation xsi:noNamespaceSchemaLocation
  614                       if (isSpecialAttribute(qname))
  615                           continue;
  616   
  617                       _attNamesList.add(qname);
  618                       _attValuesList.add(getAttributeValue(i));
  619                   }
  620                   _state = STATE_ATTBUFFERING;
  621               }
  622               else
  623                   throw new IllegalStateException("ATT event must be only at the beggining of the stream.");
  624   
  625               break;
  626   
  627           case XMLEvent.END_ELEMENT:
  628           case XMLEvent.END_DOCUMENT:
  629               _depth--;
  630               if (_state == STATE_ATTBUFFERING)
  631                   pushBufferedAttributes();
  632   
  633               _validator.nextEvent(Validator.END, _elemEvent);
  634               break;
  635   
  636           case XMLEvent.CDATA:
  637           case XMLEvent.CHARACTERS:
  638               if (_state == STATE_ATTBUFFERING)
  639                   pushBufferedAttributes();
  640   
  641               if (_validator==null)
  642               {
  643                   if (_contentType==null)
  644                   {
  645                       if (isWhiteSpace()) // hack/workaround for avoiding errors for parsers that do not generate XMLEvent.SPACE
  646                           break;
  647   
  648                       addError("No content type provided for validation of a content model.");
  649                       _state = STATE_ERROR;
  650                       break;
  651                   }
  652                   initValidator(_contentType);
  653                   _validator.nextEvent(Validator.BEGIN, _simpleEvent);
  654               }
  655   
  656               _validator.nextEvent(Validator.TEXT, _elemEvent);
  657               break;
  658   
  659           case XMLEvent.START_DOCUMENT:
  660               _depth++;
  661               break;
  662   
  663           case XMLEvent.COMMENT:
  664           case XMLEvent.DTD:
  665           case XMLEvent.ENTITY_DECLARATION:
  666           case XMLEvent.ENTITY_REFERENCE:
  667           case XMLEvent.NAMESPACE:
  668           case XMLEvent.NOTATION_DECLARATION:
  669           case XMLEvent.PROCESSING_INSTRUCTION:
  670           case XMLEvent.SPACE:
  671               //ignore
  672               break;
  673   
  674           default:
  675               throw new IllegalStateException("Unknown event type.");
  676           }
  677       }
  678   
  679       private void pushBufferedAttributes()
  680       {
  681           SchemaType validationType = null;
  682   
  683           if (_xsiType!=null)
  684           {
  685               if (_contentType==null)
  686               {
  687                   validationType = _xsiType;
  688               }
  689               else
  690               {
  691                   // we have both _xsiType and _contentType
  692                   if (_contentType.isAssignableFrom(_xsiType))
  693                   {
  694                       validationType = _xsiType;
  695                   }
  696                   else
  697                   {
  698                       addError("Specified type '" + _contentType +
  699                           "' not compatible with found xsi:type '" + _xsiType + "'.");
  700                       _state = STATE_ERROR;
  701                       return;
  702                   }
  703               }
  704           }
  705           else
  706           {
  707               if (_contentType != null)
  708               {
  709                   validationType = _contentType;
  710               }
  711               else if (_attNamesList!=null)
  712               {
  713                   // no xsi:type, no _contentType
  714                   // this is the global attribute case
  715                   validationType = _stl.findAttributeType((QName)_attNamesList.get(0));
  716                   if (validationType==null)
  717                   {
  718                       addError("A schema global attribute with name '" + _attNamesList.get(0) +
  719                           "' could not be found in the current schema type loader.");
  720                       _state = STATE_ERROR;
  721                       return;
  722                   }
  723                   // if _attNamesList.size() > 1 than the validator will add an error
  724               }
  725               else
  726               {
  727                   addError("No content type provided for validation of a content model.");
  728                   _state = STATE_ERROR;
  729                   return;
  730               }
  731           }
  732   
  733           // here validationType is the right type, start pushing all acumulated attributes
  734           initValidator(validationType);
  735           _validator.nextEvent(Validator.BEGIN, _simpleEvent);
  736   
  737           // validate attributes from _attNamesList
  738           validate_attributes(_attNamesList.size());
  739           _attNamesList = null;
  740           _attValuesList = null;
  741   
  742           _state = STATE_VALIDATING;
  743       }
  744   
  745       private boolean isSpecialAttribute(QName qn)
  746       {
  747           if (qn.getNamespaceURI().equals(URI_XSI))
  748               return qn.getLocalPart().equals(XSI_TYPE.getLocalPart()) ||
  749                   qn.getLocalPart().equals(XSI_NIL.getLocalPart()) ||
  750                   qn.getLocalPart().equals(XSI_SL.getLocalPart()) ||
  751                   qn.getLocalPart().equals(XSI_NSL.getLocalPart());
  752   
  753           return false;
  754       }
  755   
  756       /**
  757        * Initializes the validator for the given schemaType
  758        * @param schemaType
  759        */
  760       private void initValidator(SchemaType schemaType)
  761       {
  762           assert schemaType!=null;
  763   
  764           _validator = new Validator(schemaType, null, _stl, _options, _errorListener);
  765       }
  766   
  767       private SchemaType typeForGlobalElement(QName qname)
  768       {
  769           assert qname!=null;
  770   
  771           SchemaType docType = _stl.findDocumentType(qname);
  772   
  773           if (docType==null)
  774           {
  775               addError("Schema document type not found for element '" + qname + "'.");
  776               _state = STATE_ERROR;
  777           }
  778           return docType;
  779       }
  780   
  781       private void addError(String msg)
  782       {
  783           String source = null;
  784           Location location = getLocation();
  785   
  786           if (location != null)
  787           {
  788               source = location.getPublicId();
  789               if (source==null)
  790                   source = location.getSystemId();
  791   
  792               _errorListener.add(XmlError.forLocation(msg, source, location));
  793           }
  794           else
  795               _errorListener.add(XmlError.forMessage(msg));
  796       }
  797   
  798       protected void validate_attributes(int attCount)
  799       {
  800           for(int i=0; i<attCount; i++)
  801           {
  802               validate_attribute(i);
  803           }
  804   
  805           if (_options!=null && _options.hasOption(OPTION_ATTTRIBUTE_VALIDATION_COMPAT_MODE))
  806           {}
  807           else
  808               _validator.nextEvent(Validator.ENDATTRS, _simpleEvent);
  809       }
  810   
  811       protected void validate_attribute(int attIndex)
  812       {
  813           ValidatorListener.Event event;
  814           if (_attNamesList==null)
  815           {
  816               _attEvent.setAttributeIndex(attIndex);
  817               QName qn = _attEvent.getName();
  818               if (isSpecialAttribute(qn))
  819                   return;
  820   
  821               event = _attEvent;
  822           }
  823           else
  824           {
  825               _simpleEvent._qname = (QName)_attNamesList.get(attIndex);
  826               _simpleEvent._text = (String)_attValuesList.get(attIndex);
  827               event = _simpleEvent;
  828           }
  829   
  830           _validator.nextEvent(Validator.ATTR, event);
  831       }
  832   
  833       /**
  834        * @return Returns the validation state up to this point.
  835        * NOTE: At least one START ELEMENT should have been consumed for a valid value to be returned.
  836        */
  837       public boolean isValid()
  838       {
  839           if ( _state==STATE_ERROR || _validator==null)
  840               return false;
  841   
  842           return _validator.isValid();
  843       }
  844   
  845   //    /* for unit testing */
  846   //    public static void main(String[] args) throws FileNotFoundException, XMLStreamException
  847   //    {
  848   //        ValidatingXMLStreamReader valXsr = new ValidatingXMLStreamReader();
  849   //        for( int i = 0; i<args.length; i++)
  850   //        {
  851   //            validate(valXsr, args[i]);
  852   //        }
  853   //    }
  854   //
  855   //    private static void validate(ValidatingXMLStreamReader valXsr, String file)
  856   //        throws XMLStreamException, FileNotFoundException
  857   //    {
  858   //        Collection errors = new ArrayList();
  859   //        XMLStreamReader xsr = XMLInputFactory.newInstance().
  860   //            createXMLStreamReader(new FileInputStream(new File(file)));
  861   //        valXsr.init(xsr, null,
  862   //            XmlBeans.typeLoaderForClassLoader(ValidatingXMLStreamReader.class.getClassLoader()),
  863   //            null,
  864   //            errors);
  865   //
  866   //        while( valXsr.hasNext() )
  867   //        {
  868   //            valXsr.next();
  869   //        }
  870   //
  871   //        System.out.println("File '" + file + "' is: " + (valXsr.isValid() ? "Valid" : "INVALID") + "\t\t\t\t ----------");
  872   //        for (Iterator i = errors.iterator(); i.hasNext(); )
  873   //        {
  874   //            XmlError err = (XmlError)i.next();
  875   //            System.out.println("ERROR " + err.getSeverity() + " " + err.getLine() + ":" + err.getColumn() + " " +
  876   //                err.getMessage() + " ");
  877   //        }
  878   //    }
  879   //
  880   //    private void debugEvent(int evType)
  881   //    {
  882   //        switch(evType)
  883   //        {
  884   //        case XMLEvent.START_ELEMENT:
  885   //            System.out.println("SE     " + _elemEvent.getName());
  886   //            break;
  887   //        case XMLEvent.START_DOCUMENT:
  888   //            System.out.println("SDoc");
  889   //            break;
  890   //        case XMLEvent.END_ELEMENT:
  891   //            System.out.println("EE     " + _elemEvent.getName());
  892   //            break;
  893   //        case XMLEvent.END_DOCUMENT:
  894   //            System.out.println("EDoc");
  895   //            break;
  896   //        case XMLEvent.SPACE:
  897   //            System.out.println("SPACE");
  898   //            break;
  899   //        case XMLEvent.CDATA:
  900   //            System.out.println("CDATA");
  901   //            break;
  902   //        case XMLEvent.CHARACTERS:
  903   //            String c = _elemEvent.getText();
  904   //            System.out.println("TEXT     " + c);
  905   //            break;
  906   //
  907   //        case XMLEvent.ATTRIBUTE:      // global attributes
  908   //            System.out.println("ATT     count: " + _elemEvent._xmlStream.getAttributeCount());
  909   //            for(int i=0; i<_elemEvent._xmlStream.getAttributeCount(); i++)
  910   //            {
  911   //                System.out.println("\t\t" + _elemEvent._xmlStream.getAttributeNamespace(i) + ":" +
  912   //                    _elemEvent._xmlStream.getAttributeLocalName(i) + "  =  " +
  913   //                    _elemEvent._xmlStream.getAttributeValue(i));
  914   //            }
  915   //            break;
  916   //        case XMLEvent.COMMENT:
  917   //            System.out.println("COMMENT");
  918   //            break;
  919   //        case XMLEvent.DTD:
  920   //            System.out.println("DTD");
  921   //            break;
  922   //        case XMLEvent.ENTITY_DECLARATION:
  923   //            System.out.println("ENTITY_DECL");
  924   //            break;
  925   //        case XMLEvent.ENTITY_REFERENCE:
  926   //            System.out.println("ENTITY_REF");
  927   //            break;
  928   //        case XMLEvent.NAMESPACE:
  929   //            System.out.println("NS");
  930   //            break;
  931   //        case XMLEvent.NOTATION_DECLARATION:
  932   //            System.out.println("NOTATION_DECL");
  933   //            break;
  934   //        case XMLEvent.PROCESSING_INSTRUCTION:
  935   //            System.out.println("PI");
  936   //            break;
  937   //        }
  938   //    }
  939   }

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