You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlbeans-cvs@xml.apache.org by ce...@apache.org on 2004/03/23 03:42:45 UTC

cvs commit: xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/values XmlObjectBase.java

cezar       2004/03/22 18:42:45

  Modified:    v2/src/common/org/apache/xmlbeans/impl/common
                        IdentityConstraint.java ValidatorListener.java
               v2/test/src/drt/drtcases MarshalTests.java
               v2/src/xmlstore/org/apache/xmlbeans/impl/store Path.java
                        Saver.java XqrlDelegate.java
               v2/src/xmlpublic/org/apache/xmlbeans QNameSet.java
                        SchemaType.java XmlObject.java
               v2/src/typeimpl/org/apache/xmlbeans/impl/schema
                        SchemaTypeImpl.java
               v2/src/typeimpl/org/apache/xmlbeans/impl/validator
                        ValidatingXMLInputStream.java
                        ValidatingXMLStreamReader.java Validator.java
                        ValidatorUtil.java
               v2/src/typeimpl/org/apache/xmlbeans/impl/values
                        XmlObjectBase.java
  Log:
  - selectChildren and selectAttributes for dynamic getters and some wildcard scenarious.
  - Location in ValidatingXmlStreamReader
  - various bug fixes
  
  CR: Eris
  DRT: passes
  
  Revision  Changes    Path
  1.3       +26 -2     xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common/IdentityConstraint.java
  
  Index: IdentityConstraint.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common/IdentityConstraint.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IdentityConstraint.java	12 Feb 2004 20:06:08 -0000	1.2
  +++ IdentityConstraint.java	23 Mar 2004 02:42:45 -0000	1.3
  @@ -17,6 +17,8 @@
   
   import org.apache.xmlbeans.impl.common.ValidatorListener.Event;
   import javax.xml.namespace.QName;
  +import javax.xml.stream.Location;
  +
   import org.apache.xmlbeans.*;
   import java.util.*;
   
  @@ -119,9 +121,31 @@
           {
               assert event != null;
               
  -            _errorListener.add(
  -                XmlError.forCursor( msg, event.getLocationAsCursor() ) );
  +            _errorListener.add(errorForEvent(msg, XmlError.SEVERITY_ERROR, event));
  +        }
  +    }
  +
  +    public static XmlError errorForEvent(String msg, int severity, Event event)
  +    {
  +        XmlCursor loc = event.getLocationAsCursor();
  +        XmlError error;
  +        if (loc!=null)
  +            error = XmlError.forCursor(msg, severity, loc);
  +        else
  +        {
  +            Location location = event.getLocation();
  +            if (location!=null)
  +            {
  +                error = XmlError.forLocation(msg, severity,
  +                    location.getSystemId(), location.getLineNumber(),
  +                    location.getColumnNumber(), location.getCharacterOffset());
  +            }
  +            else
  +            {
  +                error = XmlError.forMessage(msg, severity);
  +            }
           }
  +        return error;
       }
   
       private void setSavePoint( ConstraintState cs )
  
  
  
  1.3       +2 -0      xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common/ValidatorListener.java
  
  Index: ValidatorListener.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/common/org/apache/xmlbeans/impl/common/ValidatorListener.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ValidatorListener.java	12 Feb 2004 20:06:08 -0000	1.2
  +++ ValidatorListener.java	23 Mar 2004 02:42:45 -0000	1.3
  @@ -17,6 +17,7 @@
   
   import org.apache.xmlbeans.XmlCursor;
   import javax.xml.namespace.QName;
  +import javax.xml.stream.Location;
   
   /**
    * First event must be a BEGIN with no name (to communicate any xsi
  @@ -40,6 +41,7 @@
           public static final int COLLAPSE = 3;
   
           XmlCursor getLocationAsCursor ( );
  +        Location getLocation();
   
           boolean getXsiType  ( Chars chars ); // BEGIN xsi:type
           boolean getXsiNil   ( Chars chars ); // BEGIN xsi:nil
  
  
  
  1.46      +29 -28    xml-xmlbeans/v2/test/src/drt/drtcases/MarshalTests.java
  
  Index: MarshalTests.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/test/src/drt/drtcases/MarshalTests.java,v
  retrieving revision 1.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- MarshalTests.java	18 Mar 2004 23:25:35 -0000	1.45
  +++ MarshalTests.java	23 Mar 2004 02:42:45 -0000	1.46
  @@ -1101,38 +1101,38 @@
   
               // -- this is currently broken --
               //now try unmarshalType...
  -//        final FileInputStream fis = new FileInputStream(instance);
  -//        final XMLStreamReader rdr =
  -//            XMLInputFactory.newInstance().createXMLStreamReader(fis);
  -//        QName schema_type = new QName("http://nosuch.domain.name", "USAddress");
  -//        String java_type = obj.getClass().getName();
  -//
  -//        //not super robust but this should work for valid xml
  -//        while(!rdr.isStartElement()) {
  -//            rdr.next();
  -//        }
  -//
  -//        um.unmarshalType(rdr, schema_type, java_type, opts);
  -//        rdr.close();
  -//        fis.close();
  -//
  -//        reportErrors(errors);
  -//        Assert.assertTrue(errors.isEmpty());
  +        final FileInputStream fis = new FileInputStream(instance);
  +        final XMLStreamReader rdr =
  +            XMLInputFactory.newInstance().createXMLStreamReader(fis);
  +        QName schema_type = new QName("http://nosuch.domain.name", "USAddress");
  +        String java_type = obj.getClass().getName();
  +
  +        //not super robust but this should work for valid xml
  +        while(!rdr.isStartElement()) {
  +            rdr.next();
  +        }
  +
  +        um.unmarshalType(rdr, schema_type, java_type, opts_validation_on);
  +        rdr.close();
  +        fis.close();
  +
  +        reportErrors(errors);
  +        Assert.assertTrue(errors.isEmpty());
   
   
               // -- this is currently broken --
               //now lets try validating our stream over objects
  -//        final Marshaller marshaller = binding_context.createMarshaller();
  -//        final XmlOptions empty_opts = new XmlOptions();
  -//        final XMLStreamReader obj_rdr =
  -//            marshaller.marshal(obj, empty_opts);
  -//        inform("VALIDATION-OBJ: " + obj);
  -//
  -//        final Object obj2 = um.unmarshal(obj_rdr, opts_validation_on);
  -//        inform("obj2="+obj2);
  -//        obj_rdr.close();
  -//        reportErrors(errors);
  -//        Assert.assertTrue(errors.isEmpty());
  +        final Marshaller marshaller = binding_context.createMarshaller();
  +        final XmlOptions empty_opts = new XmlOptions();
  +        final XMLStreamReader obj_rdr =
  +            marshaller.marshal(obj, empty_opts);
  +        inform("VALIDATION-OBJ: " + obj);
  +
  +        final Object obj2 = um.unmarshal(obj_rdr, opts_validation_on);
  +        inform("obj2="+obj2);
  +        obj_rdr.close();
  +        reportErrors(errors);
  +        Assert.assertTrue(!errors.isEmpty());
   
               // depends on reasonable equals methods which we do not have yet
               //Assert.assertEquals(obj, obj2);
  @@ -1142,6 +1142,7 @@
               thread.setContextClassLoader(curr_cl);
           }
       }
  +
   
       private static void reportErrors(List errors)
       {
  
  
  
  1.4       +11 -3     xml-xmlbeans/v2/src/xmlstore/org/apache/xmlbeans/impl/store/Path.java
  
  Index: Path.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlstore/org/apache/xmlbeans/impl/store/Path.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Path.java	12 Feb 2004 20:06:30 -0000	1.3
  +++ Path.java	23 Mar 2004 02:42:45 -0000	1.4
  @@ -161,6 +161,12 @@
                           _xqrlPathCache.put( path.getPathExpr(), path );
                   }
               }
  +
  +            if (path == null)
  +            {
  +                throw new UnsupportedOperationException(
  +                    "This query is too complex to be processed." );
  +            }
           }
   
           return path == null ? null : path.getPathExpr();
  @@ -252,9 +258,11 @@
   
           static Path create ( String pathExpr, String currentNodeVar )
           {
  -            return new XqrlPathImpl(
  -                pathExpr,
  -                XqrlDelegate.compilePath( pathExpr, currentNodeVar ) );
  +            Path.Query p = XqrlDelegate.compilePath( pathExpr, currentNodeVar );
  +            if (p==null)
  +                return null;
  +
  +            return new XqrlPathImpl(pathExpr, p);
           }
   
           protected PathEngine execute (
  
  
  
  1.9       +5 -0      xml-xmlbeans/v2/src/xmlstore/org/apache/xmlbeans/impl/store/Saver.java
  
  Index: Saver.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlstore/org/apache/xmlbeans/impl/store/Saver.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Saver.java	3 Mar 2004 22:56:39 -0000	1.8
  +++ Saver.java	23 Mar 2004 02:42:45 -0000	1.9
  @@ -3773,6 +3773,11 @@
               return new Cursor( getRoot(), _sLoc, _pLoc );
           }
   
  +        public javax.xml.stream.Location getLocation()
  +        {
  +            return null;
  +        }
  +
           public boolean getXsiType ( Chars chars )
           {
               if (_xsiType == null)
  
  
  
  1.3       +13 -10    xml-xmlbeans/v2/src/xmlstore/org/apache/xmlbeans/impl/store/XqrlDelegate.java
  
  Index: XqrlDelegate.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlstore/org/apache/xmlbeans/impl/store/XqrlDelegate.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XqrlDelegate.java	12 Feb 2004 20:06:30 -0000	1.2
  +++ XqrlDelegate.java	23 Mar 2004 02:42:45 -0000	1.3
  @@ -23,18 +23,21 @@
       {
       }
   
  -    static void check ( Object o )
  -    {
  -        if (o == null)
  -        {
  -            throw
  -                new UnsupportedOperationException(
  -                    "This operation requires xqrl.jar" );
  -        }
  -    }
  +//    static void check ( Object o )
  +//    {
  +//        if (o == null)
  +//        {
  +//            throw
  +//                new UnsupportedOperationException(
  +//                    "This operation requires xqrl.jar" );
  +//        }
  +//    }
   
       static Path.Query compilePath ( String path, String currentNodeVar )
       {
  +        if (_compilePath==null)
  +            return null;
  +
           return
               (Path.Query)
                   invoke( _compilePath, new Object[] { path, currentNodeVar } );
  @@ -68,7 +71,7 @@
           {
               throw
                   new UnsupportedOperationException(
  -                    "This operation requires xqrl.jar" );
  +                    "This query is too complex to be processed." );
           }
   
           try
  
  
  
  1.3       +2 -1      xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/QNameSet.java
  
  Index: QNameSet.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/QNameSet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- QNameSet.java	12 Feb 2004 20:06:27 -0000	1.2
  +++ QNameSet.java	23 Mar 2004 02:42:45 -0000	1.3
  @@ -63,6 +63,8 @@
    *   </ul>
    * </li>
    * </ul>
  + *
  + * @see QNameSetBuilder
    */
   public final class QNameSet implements QNameSetSpecification, java.io.Serializable
   {
  @@ -186,7 +188,6 @@
       {
           return new QNameSet(null, Collections.EMPTY_SET, Collections.EMPTY_SET, Collections.singleton(name));
       }
  -
   
       /**
        * Constructs a QNameSetBuilder whose contents are given by
  
  
  
  1.5       +29 -0     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SchemaType.java
  
  Index: SchemaType.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/SchemaType.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SchemaType.java	12 Feb 2004 20:06:27 -0000	1.4
  +++ SchemaType.java	23 Mar 2004 02:42:45 -0000	1.5
  @@ -869,4 +869,33 @@
        * Retruns a SchemaType.Ref pointing to this schema type itself.
        */
       public Ref getRef();
  +
  +    /**
  +     * Returns a QNameSet of elements that may exist in wildcard
  +     * buchets and are not explicitly defined in this schema type.
  +     * Note: In this example:
  +     *  <xs:complexType name="exampleType">
  +     *    <xs:sequence>
  +     *      <xs:element name="someElement" type='xs:string' />
  +     *      <xs:any namespace="##targetNamespace" />
  +     *    </xs:sequence>
  +     *  </xs:complexType>
  +     *  the returned QNameSet will not contain the qname of 'someElement'.
  +     * @return the constructed QNameSet
  +     */
  +    public QNameSet qnameSetForWildcardElements();
  +
  +    /**
  +     * Returns a QNameSet of attributes that may exist in wildcard
  +     * buchets and are not explicitly defined in this schema type.
  +     * Note: In this example:
  +     *  <xs:complexType name="exampleType">
  +     *    ...
  +     *    <xs:attribute name='someAttribute' type='xs:string' />
  +     *    <xs:anyAttribute namespace="##targetNamespace" />
  +     *  </xs:complexType>
  +     *  the returned QNameSet will not contain the qname of 'someAttribute'.
  +     * @return the constructed QNameSet
  +     */
  +    public QNameSet qnameSetForWildcardAttributes();
   }
  
  
  
  1.5       +51 -2     xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlObject.java
  
  Index: XmlObject.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/xmlpublic/org/apache/xmlbeans/XmlObject.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XmlObject.java	12 Feb 2004 20:06:27 -0000	1.4
  +++ XmlObject.java	23 Mar 2004 02:42:45 -0000	1.5
  @@ -19,6 +19,7 @@
   import org.apache.xmlbeans.xml.stream.XMLStreamException;
   
   import javax.xml.stream.XMLStreamReader;
  +import javax.xml.namespace.QName;
   
   import org.w3c.dom.Node;
   
  @@ -182,8 +183,8 @@
        *      }
        * }
        * </pre>
  -     * 
  -     * @param options An object that implements the {@link java.util.Collection 
  +     *
  +     * @param options An object that implements the {@link java.util.Collection
        * Collection} interface.
        */
       boolean validate(XmlOptions options);
  @@ -448,6 +449,54 @@
       static final int GREATER_THAN = 1;
       /** NOT_EQUAL is 2. See {@link #compareValue}. */
       static final int NOT_EQUAL = 2;
  +
  +    /**
  +     * Selects the contents of the children elements with the given name.
  +     * @param elementName The name of the elements to be selected.
  +     * @return Returns the contents of the selected elements.
  +     */
  +    XmlObject[] selectChildren(QName elementName);
  +
  +    /**
  +     * Selects the contents of the children elements with the given name.
  +     * @param elementUri The URI of the elements to be selected.
  +     * @param elementLocalName The local name of the elements to be selected.
  +     * @return Returns the contents of the selected elements.
  +     */
  +    XmlObject[] selectChildren(String elementUri, String elementLocalName);
  +
  +    /**
  +     * Selects the contents of the children elements that are contained in the elementNameSet.
  +     * @param elementNameSet Set of element names to be selected.
  +     * @return Returns the contents of the selected elements.
  +     * @see SchemaType#qnameSetForWildcardElements()
  +     * @see QNameSetBuilder for creating sets of qnames
  +     */
  +    XmlObject[] selectChildren(QNameSet elementNameSet);
  +
  +    /**
  +     * Selects the content of the attribute with the given name.
  +     * @param attributeName The name of the attribute to be selected.
  +     * @return Returns the contents of the selected attribute.
  +     */
  +    XmlObject selectAttribute(QName attributeName);
  +
  +    /**
  +     * Selects the content of the attribute with the given name.
  +     * @param attributeUri The URI of the attribute to be selected.
  +     * @param attributeLocalName The local name of the attribute to be selected.
  +     * @return Returns the content of the selected attribute.
  +     */
  +    XmlObject selectAttribute(String attributeUri, String attributeLocalName);
  +
  +    /**
  +     * Selects the contents of the attributes that are contained in the elementNameSet.
  +     * @param attributeNameSet Set of attribute names to be selected.
  +     * @return Returns the contents of the selected attributes.
  +     * @see SchemaType#qnameSetForWildcardAttributes()
  +     * @see QNameSetBuilder for creating sets of qnames
  +     */
  +    XmlObject[] selectAttributes(QNameSet attributeNameSet);
   
       /**
        * Static factory class for creating new instances.  Note that if
  
  
  
  1.8       +106 -17   xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java
  
  Index: SchemaTypeImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeImpl.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SchemaTypeImpl.java	12 Feb 2004 20:06:21 -0000	1.7
  +++ SchemaTypeImpl.java	23 Mar 2004 02:42:45 -0000	1.8
  @@ -15,34 +15,42 @@
   
   package org.apache.xmlbeans.impl.schema;
   
  -import org.apache.xmlbeans.impl.values.*;
  -import org.apache.xmlbeans.impl.regex.RegularExpression;
  -import org.apache.xmlbeans.impl.common.QNameHelper;
  -import org.apache.xmlbeans.XmlObject;
  -import org.apache.xmlbeans.SchemaGlobalElement;
  +import org.apache.xmlbeans.QNameSet;
  +import org.apache.xmlbeans.QNameSetBuilder;
   import org.apache.xmlbeans.SchemaAnnotation;
  +import org.apache.xmlbeans.SchemaAttributeModel;
   import org.apache.xmlbeans.SchemaComponent;
   import org.apache.xmlbeans.SchemaField;
  -import org.apache.xmlbeans.SchemaType;
  -import org.apache.xmlbeans.SchemaParticle;
   import org.apache.xmlbeans.SchemaGlobalAttribute;
  -import org.apache.xmlbeans.SchemaAttributeModel;
  -import org.apache.xmlbeans.SchemaTypeElementSequencer;
  -import org.apache.xmlbeans.SchemaTypeSystem;
  -import org.apache.xmlbeans.SchemaProperty;
  -import org.apache.xmlbeans.QNameSet;
  +import org.apache.xmlbeans.SchemaGlobalElement;
   import org.apache.xmlbeans.SchemaLocalAttribute;
   import org.apache.xmlbeans.SchemaLocalElement;
  -import org.apache.xmlbeans.XmlAnySimpleType;
  -import org.apache.xmlbeans.StringEnumAbstractBase;
  +import org.apache.xmlbeans.SchemaParticle;
  +import org.apache.xmlbeans.SchemaProperty;
   import org.apache.xmlbeans.SchemaStringEnumEntry;
  +import org.apache.xmlbeans.SchemaType;
  +import org.apache.xmlbeans.SchemaTypeElementSequencer;
   import org.apache.xmlbeans.SchemaTypeLoader;
  +import org.apache.xmlbeans.SchemaTypeSystem;
  +import org.apache.xmlbeans.StringEnumAbstractBase;
  +import org.apache.xmlbeans.XmlAnySimpleType;
  +import org.apache.xmlbeans.XmlObject;
  +import org.apache.xmlbeans.impl.common.QNameHelper;
  +import org.apache.xmlbeans.impl.values.*;
   
  +import javax.xml.namespace.QName;
   import java.lang.reflect.Constructor;
  -import java.util.*;
   import java.math.BigInteger;
  -
  -import javax.xml.namespace.QName;
  +import java.util.ArrayList;
  +import java.util.Arrays;
  +import java.util.Collection;
  +import java.util.Collections;
  +import java.util.HashMap;
  +import java.util.Iterator;
  +import java.util.LinkedHashSet;
  +import java.util.List;
  +import java.util.Map;
  +import java.util.Set;
   
   public final class SchemaTypeImpl implements SchemaType, TypeStoreUserFactory
   {
  @@ -2006,5 +2014,86 @@
   
               return _visitor.testValid(elementName);
           }
  +    }
  +
  +    /**
  +     * Returns a QNameSet of elements that may exist in wildcard
  +     * buchets and are not explicitly defined in this schema type.
  +     * Note: In this example:
  +     *  <xs:complexType name="exampleType">
  +     *    <xs:sequence>
  +     *      <xs:element name="someElement" type='xs:string' />
  +     *      <xs:any namespace="##targetNamespace" />
  +     *    </xs:sequence>
  +     *  </xs:complexType>
  +     *  the returned QNameSet will not contain the qname of 'someElement'.
  +     * @return the constructed QNameSet
  +     */
  +    public QNameSet qnameSetForWildcardElements()
  +    {
  +        SchemaParticle model = this.getContentModel();
  +        QNameSetBuilder wildcardSet = new QNameSetBuilder();
  +        computeWildcardSet(model, wildcardSet);
  +
  +        QNameSetBuilder qnsb = new QNameSetBuilder( wildcardSet );
  +        SchemaProperty[] props = this.getElementProperties();
  +
  +        for (int i = 0; i < props.length; i++)
  +        {
  +            SchemaProperty prop = props[i];
  +            qnsb.remove(prop.getName());
  +        }
  +
  +        return qnsb.toQNameSet();
  +    }
  +
  +    private static void computeWildcardSet(SchemaParticle model, QNameSetBuilder result)
  +    {
  +        for (int i = 0; i < model.countOfParticleChild(); i++)
  +        {
  +            SchemaParticle child = model.getParticleChild(i);
  +            if (child.getParticleType()==SchemaParticle.WILDCARD)
  +            {
  +                QNameSet cws = child.getWildcardSet();
  +                result.addAll(cws);
  +            }
  +            else
  +            {
  +                computeWildcardSet(child, result);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * Returns a QNameSet of attributes that may exist in wildcard
  +     * buchets and are not explicitly defined in this schema type.
  +     * Note: In this example:
  +     *  <xs:complexType name="exampleType">
  +     *    ...
  +     *    <xs:attribute name='someAttribute' type='xs:string' />
  +     *    <xs:anyAttribute namespace="##targetNamespace" />
  +     *  </xs:complexType>
  +     *  the returned QNameSet will not contain the qname of 'someAttribute'.
  +     * @return the constructed QNameSet
  +     */
  +    public QNameSet qnameSetForWildcardAttributes()
  +    {
  +        SchemaAttributeModel model = this.getAttributeModel();
  +        QNameSet wildcardSet = model.getWildcardSet();
  +
  +        if (wildcardSet==null)
  +            return QNameSet.EMPTY;
  +
  +        QNameSetBuilder qnsb = new QNameSetBuilder( wildcardSet );
  +
  +        SchemaProperty[] props = this.getAttributeProperties();
  +
  +        for (int i = 0; i < props.length; i++)
  +        {
  +            SchemaProperty prop = props[i];
  +            qnsb.remove(prop.getName());
  +        }
  +
  +        return qnsb.toQNameSet();
       }
   }
  
  
  
  1.4       +37 -1     xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatingXMLInputStream.java
  
  Index: ValidatingXMLInputStream.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatingXMLInputStream.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ValidatingXMLInputStream.java	12 Feb 2004 20:06:22 -0000	1.3
  +++ ValidatingXMLInputStream.java	23 Mar 2004 02:42:45 -0000	1.4
  @@ -43,6 +43,7 @@
   import org.apache.xmlbeans.xml.stream.XMLStreamException;
   import javax.xml.namespace.QName;
   
  +
   public final class ValidatingXMLInputStream
       extends GenericXmlInputStream implements Event
   {
  @@ -247,7 +248,42 @@
       {
           return null;
       }
  -    
  +
  +    public javax.xml.stream.Location getLocation()
  +    {
  +        try
  +        {
  +            final org.apache.xmlbeans.xml.stream.Location xeLoc = _source.peek().getLocation();
  +
  +            if (xeLoc==null)
  +                return null;
  +
  +            javax.xml.stream.Location loc = new javax.xml.stream.Location()
  +            {
  +                public int getLineNumber()
  +                { return xeLoc.getLineNumber(); }
  +
  +                public int getColumnNumber()
  +                { return xeLoc.getColumnNumber(); }
  +
  +                public int getCharacterOffset()
  +                { return -1;}
  +
  +                public String getPublicId()
  +                { return xeLoc.getPublicId(); }
  +
  +                public String getSystemId()
  +                { return xeLoc.getSystemId(); }
  +            };
  +
  +            return loc;
  +        }
  +        catch (XMLStreamException e)
  +        {
  +            return null;
  +        }
  +    }
  +
       public boolean getXsiType ( Chars chars )
       {
           if (_xsiType == null)
  
  
  
  1.3       +754 -735  xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatingXMLStreamReader.java
  
  Index: ValidatingXMLStreamReader.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatingXMLStreamReader.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ValidatingXMLStreamReader.java	5 Mar 2004 00:57:10 -0000	1.2
  +++ ValidatingXMLStreamReader.java	23 Mar 2004 02:42:45 -0000	1.3
  @@ -1,735 +1,754 @@
  -/*   Copyright 2004 The Apache Software Foundation
  - *
  - *   Licensed under the Apache License, Version 2.0 (the "License");
  - *   you may not use this file except in compliance with the License.
  - *   You may obtain a copy of the License at
  - *
  - *       http://www.apache.org/licenses/LICENSE-2.0
  - *
  - *   Unless required by applicable law or agreed to in writing, software
  - *   distributed under the License is distributed on an "AS IS" BASIS,
  - *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - *   See the License for the specific language governing permissions and
  - *  limitations under the License.
  - */
  -package org.apache.xmlbeans.impl.validator;
  -
  -import org.apache.xmlbeans.SchemaType;
  -import org.apache.xmlbeans.SchemaTypeLoader;
  -import org.apache.xmlbeans.XmlCursor;
  -import org.apache.xmlbeans.XmlError;
  -import org.apache.xmlbeans.XmlOptions;
  -import org.apache.xmlbeans.impl.common.Chars;
  -import org.apache.xmlbeans.impl.common.ValidatorListener;
  -import org.apache.xmlbeans.impl.common.XmlWhitespace;
  -import org.apache.xmlbeans.impl.common.QNameHelper;
  -
  -import javax.xml.namespace.QName;
  -import javax.xml.stream.XMLStreamException;
  -import javax.xml.stream.XMLStreamReader;
  -import javax.xml.stream.events.XMLEvent;
  -import javax.xml.stream.util.StreamReaderDelegate;
  -import java.util.ArrayList;
  -import java.util.Collection;
  -import java.util.List;
  -
  -/**
  - * This class is a wrapper over a generic XMLInputStream that provides validation.
  - * There are 3 cases:
  - * <br/> 1) the XMLStreamReader represents a document, it contains only one element document
  - *          - in this case the user schema type should be null or it should be a document SchemaType
  - * <br/> 2) the XMLStreamReader represents an xml-fragment (content only) - must have at least one user type or xsi:type
  - * <br/>     a) it has an xsi:type - if user schema type is available it has to be a base type of xsi:type
  - * <br/>     b) it doesn't have xsi:type - user must provide a schema type
  - *         otherwise will error and will not do validation
  - * <br/> 3) the XMLStreamReader represents a global attribute - i.e. user schema type is null and only one attribute
  - *
  - * @author Cezar Andrei (cezar.andrei at bea.com)
  - * Date: Feb 13, 2004
  - */
  -public class ValidatingXMLStreamReader
  -    extends StreamReaderDelegate
  -    implements XMLStreamReader
  -{
  -    private static final String URI_XSI = "http://www.w3.org/2001/XMLSchema-instance";
  -    private static final QName XSI_TYPE = new QName(URI_XSI, "type");
  -    private static final QName XSI_NIL  = new QName(URI_XSI, "nil");
  -    private static final QName XSI_SL   = new QName(URI_XSI, "schemaLocation");
  -    private static final QName XSI_NSL  = new QName(URI_XSI, "noNamespaceSchemaLocation");
  -
  -    private SchemaType _contentType;
  -    private SchemaTypeLoader _stl;
  -    private XmlOptions _options;
  -    private Collection _errorListener;
  -    protected Validator _validator;
  -    private final ElementEventImpl _elemEvent;
  -    private final AttributeEventImpl _attEvent;
  -    private final SimpleEventImpl _simpleEvent;
  -
  -    private int _state;
  -    private final int STATE_FIRSTEVENT = 0;
  -    private final int STATE_VALIDATING = 1;
  -    private final int STATE_ATTBUFFERING = 2;
  -    private final int STATE_ERROR = 3;
  -
  -    private List _attNamesList;
  -    private List _attValuesList;
  -    private SchemaType _xsiType;
  -
  -    /**
  -     * Default constructor. Use init(...) to set the params.
  -     * See {@link #init}
  -     */
  -    public ValidatingXMLStreamReader()
  -    {
  -        super();
  -        _elemEvent = new ElementEventImpl();
  -        _attEvent = new AttributeEventImpl();
  -        _simpleEvent = new SimpleEventImpl();
  -    }
  -
  -    /**
  -     * Used in case of reusing the same ValidatinXMLStreamReader object
  -     * @param xsr The stream to be validated
  -     * @param contentType The schemaType of the content. This can be null for document and global Att validation
  -     * @param stl SchemaTypeLoader context of validation
  -     * @param options Validator options
  -     * @param errorListener Errors and warnings listener
  -     */
  -    public void init(XMLStreamReader xsr, SchemaType contentType, SchemaTypeLoader stl,
  -                     XmlOptions options, Collection errorListener)
  -    {
  -        setParent(xsr);
  -        _contentType = contentType;
  -        _stl = stl;
  -        _options = options;
  -        _errorListener = errorListener;
  -        _elemEvent.setXMLStreamReader(xsr);
  -        _attEvent.setXMLStreamReader(xsr);
  -        _simpleEvent.setXMLStreamReader(xsr);
  -        _validator = null;
  -        _state = STATE_FIRSTEVENT;
  -        if (_attNamesList!=null)
  -        {
  -            _attNamesList.clear();
  -            _attValuesList.clear();
  -        }
  -        _xsiType = null;
  -    }
  -
  -    private static class ElementEventImpl
  -        implements ValidatorListener.Event
  -    {
  -        private static final int BUF_LENGTH = 1024;
  -        private char[] _buf = new char[BUF_LENGTH];
  -        private int _length;
  -        private boolean _supportForGetTextCharacters = true;
  -
  -        private XMLStreamReader _xmlStream;
  -
  -        private void setXMLStreamReader(XMLStreamReader xsr)
  -        {
  -            _xmlStream = xsr;
  -        }
  -
  -        // can return null, used only to locate errors
  -        public XmlCursor getLocationAsCursor()
  -        {
  -            return null;
  -        }
  -
  -        // fill up chars with the xsi:type attribute value if there is one othervise return false
  -        public boolean getXsiType(Chars chars) // BEGIN xsi:type
  -        {
  -            chars.string = _xmlStream.getAttributeValue(URI_XSI, "type");
  -            if (chars.string != null)
  -                return true;
  -            return false;
  -        }
  -
  -        // fill up chars with xsi:nill attribute value if any
  -        public boolean getXsiNil(Chars chars) // BEGIN xsi:nil
  -        {
  -            chars.string = _xmlStream.getAttributeValue(URI_XSI, "nil");
  -            if (chars.string != null)
  -                return true;
  -            return false;
  -        }
  -
  -        // not used curently
  -        public boolean getXsiLoc(Chars chars) // BEGIN xsi:schemaLocation
  -        {
  -            chars.string = _xmlStream.getAttributeValue(URI_XSI, "schemaLocation");
  -            if (chars.string != null)
  -                return true;
  -            return false;
  -        }
  -
  -        // not used curently
  -        public boolean getXsiNoLoc(Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
  -        {
  -            chars.string = _xmlStream.getAttributeValue(URI_XSI, "noNamespaceSchemaLocation");
  -            if (chars.string != null)
  -                return true;
  -            return false;
  -        }
  -
  -        // On START and ATTR
  -        public QName getName()
  -        {
  -            // avoid construction of a new QName object after the bug in getName() is fixed.
  -            QName qname = new QName(_xmlStream.getNamespaceURI(), _xmlStream.getLocalName());
  -            return qname;
  -        }
  -
  -        // On TEXT and ATTR
  -        public void getText(Chars chars)
  -        {
  -            chars.string = null;
  -            chars.buffer = _buf;
  -            chars.offset = 0;
  -            _length = 0;
  -            addTextToBuffer();
  -        }
  -
  -        public void getText(Chars chars, int wsr)
  -        {
  -            chars.string = XmlWhitespace.collapse(
  -                    _xmlStream.getText(), wsr );
  -        }
  -
  -        public boolean textIsWhitespace()
  -        {
  -            return _xmlStream.isWhiteSpace();
  -        }
  -
  -        public String getNamespaceForPrefix(String prefix)
  -        {
  -            return _xmlStream.getNamespaceURI(prefix);
  -        }
  -
  -        private void addTextToBuffer()
  -        {
  -            int textLength = _xmlStream.getTextLength();
  -            ensureBufferLength(textLength);
  -
  -            if (_supportForGetTextCharacters)
  -                try
  -                {
  -                    _length = _xmlStream.getTextCharacters(0, _buf, _length, textLength);
  -                }
  -                catch(Exception e)
  -                {
  -                    _supportForGetTextCharacters = false;
  -                }
  -
  -            if(!_supportForGetTextCharacters)
  -            {
  -                System.arraycopy(_xmlStream.getTextCharacters(), _xmlStream.getTextStart(), _buf, _length, textLength);
  -                _length = _length + textLength;
  -            }
  -        }
  -
  -        private void ensureBufferLength(int lengthToAdd)
  -        {
  -            if (_length + lengthToAdd>_buf.length)
  -            {
  -                char[] newBuf = new char[_length + lengthToAdd];
  -                if (_length>0)
  -                    System.arraycopy(_buf, 0, newBuf, 0, _length);
  -                _buf = newBuf;
  -            }
  -        }
  -    }
  -
  -    private static final class AttributeEventImpl
  -        implements ValidatorListener.Event
  -    {
  -        private int _attIndex;
  -        private XMLStreamReader _xmlStream;
  -
  -        private void setXMLStreamReader(XMLStreamReader xsr)
  -        {
  -            _xmlStream = xsr;
  -        }
  -
  -        // can return null, used only to locate errors
  -        public XmlCursor getLocationAsCursor()
  -        {
  -            return null;
  -        }
  -
  -        // fill up chars with the xsi:type attribute value if there is one othervise return false
  -        public boolean getXsiType(Chars chars) // BEGIN xsi:type
  -        {
  -            throw new IllegalStateException();
  -        }
  -
  -        // fill up chars with xsi:nill attribute value if any
  -        public boolean getXsiNil(Chars chars) // BEGIN xsi:nil
  -        {
  -            throw new IllegalStateException();
  -        }
  -
  -        // not used curently
  -        public boolean getXsiLoc(final Chars chars) // BEGIN xsi:schemaLocation
  -        {
  -            throw new IllegalStateException();
  -        }
  -
  -        // not used curently
  -        public boolean getXsiNoLoc(final Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
  -        {
  -            throw new IllegalStateException();
  -        }
  -
  -        // On START and ATTR
  -        public QName getName()
  -        {
  -            assert _xmlStream.isStartElement() : "Not on Start Element.";
  -            String uri = _xmlStream.getAttributeNamespace(_attIndex);
  -            QName qn = new QName(uri==null ? "" : uri, _xmlStream.getAttributeLocalName(_attIndex));
  -            //System.out.println("    Att QName: " + qn);
  -            return qn;
  -        }
  -
  -        // On TEXT and ATTR
  -        public void getText(Chars chars)
  -        {
  -            assert _xmlStream.isStartElement() : "Not on Start Element.";
  -            chars.string = _xmlStream.getAttributeValue(_attIndex);
  -            chars.offset = 0;
  -            //System.out.println("    Att Text   : " + chars.asString() );
  -        }
  -
  -        public void getText(Chars chars, int wsr)
  -        {
  -            assert _xmlStream.isStartElement() : "Not on Start Element.";
  -            chars.string = XmlWhitespace.collapse(
  -                    _xmlStream.getAttributeValue(_attIndex), wsr );
  -            //System.out.println("    Att Text WS: " + chars.string );
  -        }
  -
  -        public boolean textIsWhitespace()
  -        {
  -            throw new IllegalStateException();
  -        }
  -
  -        public String getNamespaceForPrefix(String prefix)
  -        {
  -            assert _xmlStream.isStartElement() : "Not on Start Element.";
  -            return _xmlStream.getNamespaceURI(prefix);
  -        }
  -
  -        private void setAttributeIndex(int attIndex)
  -        {
  -            _attIndex = attIndex;
  -        }
  -    }
  -
  -    /**
  -     * This is used as implementation of Event for validating global attributes
  -     * and for pushing the buffered attributes
  -     */
  -    private static final class SimpleEventImpl
  -        implements ValidatorListener.Event
  -    {
  -        private String _text;
  -        private QName  _qname;
  -        private XMLStreamReader _xmlStream;
  -
  -        private void setXMLStreamReader(XMLStreamReader xsr)
  -        {
  -            _xmlStream = xsr;
  -        }
  -        // can return null, used only to locate errors
  -        public XmlCursor getLocationAsCursor()
  -        { return null; }
  -
  -        // fill up chars with the xsi:type attribute value if there is one othervise return false
  -        public boolean getXsiType(Chars chars) // BEGIN xsi:type
  -        { return false; }
  -
  -        // fill up chars with xsi:nill attribute value if any
  -        public boolean getXsiNil(Chars chars) // BEGIN xsi:nil
  -        { return false; }
  -
  -        // not used curently
  -        public boolean getXsiLoc(final Chars chars) // BEGIN xsi:schemaLocation
  -        { return false; }
  -
  -        // not used curently
  -        public boolean getXsiNoLoc(final Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
  -        { return false; }
  -
  -        // On START and ATTR
  -        public QName getName()
  -        { return _qname; }
  -
  -        // On TEXT and ATTR
  -        public void getText(Chars chars)
  -        {
  -            chars.string = _text;
  -            chars.offset = 0;
  -        }
  -
  -        public void getText(Chars chars, int wsr)
  -        {
  -            chars.string = XmlWhitespace.collapse( _text, wsr );
  -        }
  -
  -        public boolean textIsWhitespace()
  -        { return false; }
  -
  -        public String getNamespaceForPrefix(String prefix)
  -        {
  -            return _xmlStream.getNamespaceURI(prefix);
  -        }
  -    }
  -    /* public methods */
  -
  -    public Object getProperty(String s) throws IllegalArgumentException
  -    {
  -        return super.getProperty(s);
  -    }
  -
  -    public int next() throws XMLStreamException
  -    {
  -        int evType = super.next();
  -        //debugEvent(evType);
  -
  -        if (_state==STATE_ERROR)
  -            return evType;
  -
  -        switch(evType)
  -        {
  -        case XMLEvent.START_ELEMENT:
  -            if (_state == STATE_ATTBUFFERING)
  -                pushBufferedAttributes();
  -
  -            if (_validator==null)
  -            {
  -                // avoid construction of a new QName object after the bug in getName() is fixed.
  -                QName qname = new QName(getNamespaceURI(), getLocalName());
  -
  -                if (_contentType==null)
  -                    _contentType = typeForGlobalElement(qname);
  -
  -                initValidator(_contentType);
  -                _validator.nextEvent(Validator.BEGIN, _elemEvent);
  -            }
  -
  -            _validator.nextEvent(Validator.BEGIN, _elemEvent);
  -
  -            int attCount = getAttributeCount();
  -            for(int i=0; i<attCount; i++)
  -            {
  -                _attEvent.setAttributeIndex(i);
  -                QName qn = _attEvent.getName();
  -                if (isSpecialAttribute(qn))
  -                    continue;
  -
  -                _validator.nextEvent(Validator.ATTR, _attEvent);
  -            }
  -            break;
  -
  -        case XMLEvent.ATTRIBUTE:
  -            if (getAttributeCount()==0)
  -                break;
  -
  -            if (_state == STATE_FIRSTEVENT || _state == STATE_ATTBUFFERING)
  -            {
  -                // buffer all Attributes
  -                for (int i=0; i<getAttributeCount(); i++)
  -                {
  -                    // avoid construction of a new QName object after the bug in getName() is fixed.
  -                    QName qname = new QName(getAttributeNamespace(i), getAttributeLocalName(i));
  -
  -                    if (qname.equals(XSI_TYPE))
  -                    {
  -                        String xsiTypeValue = getAttributeValue(i);
  -                        String uri = super.getNamespaceURI(QNameHelper.getPrefixPart(xsiTypeValue));
  -                        QName xsiTypeQname = new QName(uri, QNameHelper.getLocalPart(xsiTypeValue));
  -                        _xsiType = _stl.findType(xsiTypeQname);
  -                    }
  -
  -                    if (_attNamesList==null)
  -                    {
  -                        _attNamesList = new ArrayList();
  -                        _attValuesList = new ArrayList();
  -                    }
  -                    // skip xsi:type xsi:nil xsi:schemaLocation xsi:noNamespaceSchemaLocation
  -                    if (isSpecialAttribute(qname))
  -                        continue;
  -
  -                    _attNamesList.add(qname);
  -                    _attValuesList.add(getAttributeValue(i));
  -                }
  -                _state = STATE_ATTBUFFERING;
  -            }
  -            else
  -                throw new IllegalStateException("ATT event must be only at the beggining of the stream.");
  -
  -            break;
  -
  -        case XMLEvent.END_ELEMENT:
  -        case XMLEvent.END_DOCUMENT:
  -            if (_state == STATE_ATTBUFFERING)
  -                pushBufferedAttributes();
  -
  -            _validator.nextEvent(Validator.END, _elemEvent);
  -            break;
  -
  -        case XMLEvent.CDATA:
  -        case XMLEvent.CHARACTERS:
  -            if (_state == STATE_ATTBUFFERING)
  -                pushBufferedAttributes();
  -
  -            if (_validator==null)
  -            {
  -                if (_contentType==null)
  -                {
  -                    _errorListener.add(XmlError.forMessage("No content type provided for validation of a content model.",
  -                        XmlError.SEVERITY_ERROR));
  -                    _state = STATE_ERROR;
  -                }
  -                initValidator(_contentType);
  -                _validator.nextEvent(Validator.BEGIN, _elemEvent);
  -            }
  -
  -            _validator.nextEvent(Validator.TEXT, _elemEvent);
  -            break;
  -
  -        case XMLEvent.START_DOCUMENT:
  -        case XMLEvent.COMMENT:
  -        case XMLEvent.DTD:
  -        case XMLEvent.ENTITY_DECLARATION:
  -        case XMLEvent.ENTITY_REFERENCE:
  -        case XMLEvent.NAMESPACE:
  -        case XMLEvent.NOTATION_DECLARATION:
  -        case XMLEvent.PROCESSING_INSTRUCTION:
  -        case XMLEvent.SPACE:
  -            //ignore
  -            break;
  -
  -        default:
  -            throw new IllegalStateException("Unknown event type.");
  -        }
  -
  -        return evType;
  -    }
  -
  -    private void pushBufferedAttributes()
  -    {
  -        SchemaType validationType = null;
  -
  -        if (_xsiType!=null)
  -        {
  -            if (_contentType==null)
  -            {
  -                validationType = _xsiType;
  -            }
  -            else
  -            {
  -                // we have both _xsiType and _contentType
  -                if (_contentType.isAssignableFrom(_xsiType))
  -                {
  -                    validationType = _xsiType;
  -                }
  -                else
  -                {
  -                    _errorListener.add(XmlError.forMessage("Specified type '" + _contentType +
  -                        "' not compatible with found xsi:type '" + _xsiType + "'.", XmlError.SEVERITY_ERROR));
  -                    _state = STATE_ERROR;
  -                    return;
  -                }
  -            }
  -        }
  -        else
  -        {
  -            if (_contentType != null)
  -            {
  -                validationType = _contentType;
  -            }
  -            else if (_attNamesList!=null)
  -            {
  -                // no xsi:type, no _contentType
  -                // this is the global attribute case
  -                validationType = _stl.findAttributeType((QName)_attNamesList.get(0));
  -                if (validationType==null)
  -                {
  -                    _errorListener.add(XmlError.forMessage("A schema global element with name '" + _attNamesList.get(0) +
  -                        "' could not be found in the current schema type loader.", XmlError.SEVERITY_ERROR));
  -                    _state = STATE_ERROR;
  -                    return;
  -                }
  -                // if _attNamesList.size() > 1 than the validator will add an error
  -            }
  -            else
  -            {
  -                _errorListener.add(XmlError.forMessage("No content type provided for validation of a content model.",
  -                    XmlError.SEVERITY_ERROR));
  -                _state = STATE_ERROR;
  -                return;
  -            }
  -        }
  -
  -        // here validationType is the right type, start pushing all acumulated attributes
  -        _validator = new Validator(validationType, null, _stl, _options, _errorListener);
  -        _validator.nextEvent(Validator.BEGIN, _simpleEvent);
  -
  -        for (int i=0; i<_attNamesList.size(); i++)
  -        {
  -            _simpleEvent._qname = (QName)_attNamesList.get(i);
  -            _simpleEvent._text = (String)_attValuesList.get(i);
  -            _validator.nextEvent(Validator.ATTR, _simpleEvent);
  -        }
  -
  -        _state = STATE_VALIDATING;
  -    }
  -
  -    private boolean isSpecialAttribute(QName qn)
  -    {
  -        if (qn.getNamespaceURI().equals(URI_XSI))
  -            return qn.getLocalPart().equals(XSI_TYPE.getLocalPart()) ||
  -                qn.getLocalPart().equals(XSI_NIL.getLocalPart()) ||
  -                qn.getLocalPart().equals(XSI_SL.getLocalPart()) ||
  -                qn.getLocalPart().equals(XSI_NSL.getLocalPart());
  -
  -        return false;
  -    }
  -
  -    /**
  -     * Initializes the validator for the given schemaType
  -     * @param schemaType
  -     */
  -    private void initValidator(SchemaType schemaType)
  -    {
  -        assert schemaType!=null;
  -
  -        _validator = new Validator(schemaType, null, _stl, _options, _errorListener);
  -    }
  -
  -    private SchemaType typeForGlobalElement(QName qname)
  -    {
  -        assert qname!=null;
  -
  -        SchemaType docType = _stl.findDocumentType(qname);
  -
  -        if (docType==null)
  -        {
  -            _errorListener.add(XmlError.forMessage("Schema document type not found for element '" + qname + "'.",
  -                XmlError.SEVERITY_ERROR));
  -            _state = STATE_ERROR;
  -        }
  -        return docType;
  -    }
  -
  -    /**
  -     * @return Returns the validation state up to this point
  -     * NOTE: At least on START ELEMENT should have been consumed for a valid value to be returned.
  -     */
  -    public boolean isValid()
  -    {
  -        if ( _state==STATE_ERROR || _validator==null)
  -            return false;
  -
  -        return _validator.isValid();
  -    }
  -
  -//    /* for unit testing */
  -//    public static void main(String[] args) throws FileNotFoundException, XMLStreamException
  -//    {
  -//        ValidatingXMLStreamReader valXsr = new ValidatingXMLStreamReader();
  -//        for( int i = 0; i<args.length; i++)
  -//        {
  -//            validate(valXsr, args[i]);
  -//        }
  -//    }
  -//
  -//    private static void validate(ValidatingXMLStreamReader valXsr, String file)
  -//        throws XMLStreamException, FileNotFoundException
  -//    {
  -//        Collection errors = new ArrayList();
  -//        XMLStreamReader xsr = XMLInputFactory.newInstance().
  -//            createXMLStreamReader(new FileInputStream(new File(file)));
  -//        valXsr.init(xsr, null,
  -//            XmlBeans.typeLoaderForClassLoader(ValidatingXMLStreamReader.class.getClassLoader()),
  -//            null,
  -//            errors);
  -//
  -//        while( valXsr.hasNext() )
  -//        {
  -//            valXsr.next();
  -//        }
  -//
  -//        System.out.println("File '" + file + "' is: " + (valXsr.isValid() ? "Valid" : "INVALID") + "\t\t\t\t ----------");
  -//        for (Iterator i = errors.iterator(); i.hasNext(); )
  -//        {
  -//            XmlError err = (XmlError)i.next();
  -//            System.out.println("ERROR " + err.getSeverity() + " " + err.getLine() + ":" + err.getColumn() + " " +
  -//                err.getMessage() + " ");
  -//        }
  -//    }
  -//
  -//    private void debugEvent(int evType)
  -//    {
  -//        switch(evType)
  -//        {
  -//        case XMLEvent.START_ELEMENT:
  -//            System.out.println("SE     " + _elemEvent.getName());
  -//            break;
  -//        case XMLEvent.START_DOCUMENT:
  -//            System.out.println("SDoc");
  -//            break;
  -//        case XMLEvent.END_ELEMENT:
  -//            System.out.println("EE     " + _elemEvent.getName());
  -//            break;
  -//        case XMLEvent.END_DOCUMENT:
  -//            System.out.println("EDoc");
  -//            break;
  -//        case XMLEvent.SPACE:
  -//            System.out.println("SPACE");
  -//            break;
  -//        case XMLEvent.CDATA:
  -//            System.out.println("CDATA");
  -//            break;
  -//        case XMLEvent.CHARACTERS:
  -//            Chars c = new Chars();
  -//            _elemEvent.getText(c);
  -//            System.out.println("TEXT     " + c.asString());
  -//            break;
  -//
  -//        case XMLEvent.ATTRIBUTE:      // global attributes
  -//            System.out.println("ATT     count: " + _elemEvent._xmlStream.getAttributeCount());
  -//            for(int i=0; i<_elemEvent._xmlStream.getAttributeCount(); i++)
  -//            {
  -//                System.out.println("\t\t" + _elemEvent._xmlStream.getAttributeNamespace(i) + ":" +
  -//                    _elemEvent._xmlStream.getAttributeLocalName(i) + "  =  " +
  -//                    _elemEvent._xmlStream.getAttributeValue(i));
  -//            }
  -//            break;
  -//        case XMLEvent.COMMENT:
  -//            System.out.println("COMMENT");
  -//            break;
  -//        case XMLEvent.DTD:
  -//            System.out.println("DTD");
  -//            break;
  -//        case XMLEvent.ENTITY_DECLARATION:
  -//            System.out.println("ENTITY_DECL");
  -//            break;
  -//        case XMLEvent.ENTITY_REFERENCE:
  -//            System.out.println("ENTITY_REF");
  -//            break;
  -//        case XMLEvent.NAMESPACE:
  -//            System.out.println("NS");
  -//            break;
  -//        case XMLEvent.NOTATION_DECLARATION:
  -//            System.out.println("NOTATION_DECL");
  -//            break;
  -//        case XMLEvent.PROCESSING_INSTRUCTION:
  -//            System.out.println("PI");
  -//            break;
  -//        }
  -//    }
  -}
  +/*   Copyright 2004 The Apache Software Foundation
  + *
  + *   Licensed under the Apache License, Version 2.0 (the "License");
  + *   you may not use this file except in compliance with the License.
  + *   You may obtain a copy of the License at
  + *
  + *       http://www.apache.org/licenses/LICENSE-2.0
  + *
  + *   Unless required by applicable law or agreed to in writing, software
  + *   distributed under the License is distributed on an "AS IS" BASIS,
  + *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + *   See the License for the specific language governing permissions and
  + *  limitations under the License.
  + */
  +package org.apache.xmlbeans.impl.validator;
  +
  +import org.apache.xmlbeans.SchemaType;
  +import org.apache.xmlbeans.SchemaTypeLoader;
  +import org.apache.xmlbeans.XmlCursor;
  +import org.apache.xmlbeans.XmlError;
  +import org.apache.xmlbeans.XmlOptions;
  +import org.apache.xmlbeans.impl.common.Chars;
  +import org.apache.xmlbeans.impl.common.ValidatorListener;
  +import org.apache.xmlbeans.impl.common.XmlWhitespace;
  +import org.apache.xmlbeans.impl.common.QNameHelper;
  +
  +import javax.xml.namespace.QName;
  +import javax.xml.stream.XMLStreamException;
  +import javax.xml.stream.XMLStreamReader;
  +import javax.xml.stream.events.XMLEvent;
  +import javax.xml.stream.util.StreamReaderDelegate;
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.List;
  +
  +/**
  + * This class is a wrapper over a generic XMLInputStream that provides validation.
  + * There are 3 cases:
  + * <br/> 1) the XMLStreamReader represents a document, it contains only one element document
  + *          - in this case the user schema type should be null or it should be a document SchemaType
  + * <br/> 2) the XMLStreamReader represents an xml-fragment (content only) - must have at least one user type or xsi:type
  + * <br/>     a) it has an xsi:type - if user schema type is available it has to be a base type of xsi:type
  + * <br/>     b) it doesn't have xsi:type - user must provide a schema type
  + *         otherwise will error and will not do validation
  + * <br/> 3) the XMLStreamReader represents a global attribute - i.e. user schema type is null and only one attribute
  + *
  + * @author Cezar Andrei (cezar.andrei at bea.com)
  + * Date: Feb 13, 2004
  + */
  +public class ValidatingXMLStreamReader
  +    extends StreamReaderDelegate
  +    implements XMLStreamReader
  +{
  +    private static final String URI_XSI = "http://www.w3.org/2001/XMLSchema-instance";
  +    private static final QName XSI_TYPE = new QName(URI_XSI, "type");
  +    private static final QName XSI_NIL  = new QName(URI_XSI, "nil");
  +    private static final QName XSI_SL   = new QName(URI_XSI, "schemaLocation");
  +    private static final QName XSI_NSL  = new QName(URI_XSI, "noNamespaceSchemaLocation");
  +
  +    private SchemaType _contentType;
  +    private SchemaTypeLoader _stl;
  +    private XmlOptions _options;
  +    private Collection _errorListener;
  +    protected Validator _validator;
  +    private final ElementEventImpl _elemEvent;
  +    private final AttributeEventImpl _attEvent;
  +    private final SimpleEventImpl _simpleEvent;
  +
  +    private int _state;
  +    private final int STATE_FIRSTEVENT = 0;
  +    private final int STATE_VALIDATING = 1;
  +    private final int STATE_ATTBUFFERING = 2;
  +    private final int STATE_ERROR = 3;
  +
  +    private List _attNamesList;
  +    private List _attValuesList;
  +    private SchemaType _xsiType;
  +
  +    /**
  +     * Default constructor. Use init(...) to set the params.
  +     * See {@link #init}
  +     */
  +    public ValidatingXMLStreamReader()
  +    {
  +        super();
  +        _elemEvent = new ElementEventImpl();
  +        _attEvent = new AttributeEventImpl();
  +        _simpleEvent = new SimpleEventImpl();
  +    }
  +
  +    /**
  +     * Used in case of reusing the same ValidatinXMLStreamReader object
  +     * @param xsr The stream to be validated
  +     * @param contentType The schemaType of the content. This can be null for document and global Att validation
  +     * @param stl SchemaTypeLoader context of validation
  +     * @param options Validator options
  +     * @param errorListener Errors and warnings listener
  +     */
  +    public void init(XMLStreamReader xsr, SchemaType contentType, SchemaTypeLoader stl,
  +                     XmlOptions options, Collection errorListener)
  +    {
  +        setParent(xsr);
  +        _contentType = contentType;
  +        _stl = stl;
  +        _options = options;
  +        _errorListener = errorListener;
  +        _elemEvent.setXMLStreamReader(xsr);
  +        _attEvent.setXMLStreamReader(xsr);
  +        _simpleEvent.setXMLStreamReader(xsr);
  +        _validator = null;
  +        _state = STATE_FIRSTEVENT;
  +        if (_attNamesList!=null)
  +        {
  +            _attNamesList.clear();
  +            _attValuesList.clear();
  +        }
  +        _xsiType = null;
  +    }
  +
  +    private static class ElementEventImpl
  +        implements ValidatorListener.Event
  +    {
  +        private static final int BUF_LENGTH = 1024;
  +        private char[] _buf = new char[BUF_LENGTH];
  +        private int _length;
  +        private boolean _supportForGetTextCharacters = true;
  +
  +        private XMLStreamReader _xmlStream;
  +
  +        private void setXMLStreamReader(XMLStreamReader xsr)
  +        {
  +            _xmlStream = xsr;
  +        }
  +
  +        // can return null, used only to locate errors
  +        public XmlCursor getLocationAsCursor()
  +        {
  +            return null;
  +        }
  +
  +        public javax.xml.stream.Location getLocation()
  +        {
  +            return _xmlStream.getLocation();
  +        }
  +
  +        // fill up chars with the xsi:type attribute value if there is one othervise return false
  +        public boolean getXsiType(Chars chars) // BEGIN xsi:type
  +        {
  +            chars.string = _xmlStream.getAttributeValue(URI_XSI, "type");
  +            if (chars.string != null)
  +                return true;
  +            return false;
  +        }
  +
  +        // fill up chars with xsi:nill attribute value if any
  +        public boolean getXsiNil(Chars chars) // BEGIN xsi:nil
  +        {
  +            chars.string = _xmlStream.getAttributeValue(URI_XSI, "nil");
  +            if (chars.string != null)
  +                return true;
  +            return false;
  +        }
  +
  +        // not used curently
  +        public boolean getXsiLoc(Chars chars) // BEGIN xsi:schemaLocation
  +        {
  +            chars.string = _xmlStream.getAttributeValue(URI_XSI, "schemaLocation");
  +            if (chars.string != null)
  +                return true;
  +            return false;
  +        }
  +
  +        // not used curently
  +        public boolean getXsiNoLoc(Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
  +        {
  +            chars.string = _xmlStream.getAttributeValue(URI_XSI, "noNamespaceSchemaLocation");
  +            if (chars.string != null)
  +                return true;
  +            return false;
  +        }
  +
  +        // On START and ATTR
  +        public QName getName()
  +        {
  +            // avoid construction of a new QName object after the bug in getName() is fixed.
  +            QName qname = new QName(_xmlStream.getNamespaceURI(), _xmlStream.getLocalName());
  +            return qname;
  +        }
  +
  +        // On TEXT and ATTR
  +        public void getText(Chars chars)
  +        {
  +            chars.string = null;
  +            chars.buffer = _buf;
  +            chars.offset = 0;
  +            _length = 0;
  +            addTextToBuffer();
  +        }
  +
  +        public void getText(Chars chars, int wsr)
  +        {
  +            chars.string = XmlWhitespace.collapse(
  +                    _xmlStream.getText(), wsr );
  +        }
  +
  +        public boolean textIsWhitespace()
  +        {
  +            return _xmlStream.isWhiteSpace();
  +        }
  +
  +        public String getNamespaceForPrefix(String prefix)
  +        {
  +            return _xmlStream.getNamespaceURI(prefix);
  +        }
  +
  +        private void addTextToBuffer()
  +        {
  +            int textLength = _xmlStream.getTextLength();
  +            ensureBufferLength(textLength);
  +
  +            if (_supportForGetTextCharacters)
  +                try
  +                {
  +                    _length = _xmlStream.getTextCharacters(0, _buf, _length, textLength);
  +                }
  +                catch(Exception e)
  +                {
  +                    _supportForGetTextCharacters = false;
  +                }
  +
  +            if(!_supportForGetTextCharacters)
  +            {
  +                System.arraycopy(_xmlStream.getTextCharacters(), _xmlStream.getTextStart(), _buf, _length, textLength);
  +                _length = _length + textLength;
  +            }
  +        }
  +
  +        private void ensureBufferLength(int lengthToAdd)
  +        {
  +            if (_length + lengthToAdd>_buf.length)
  +            {
  +                char[] newBuf = new char[_length + lengthToAdd];
  +                if (_length>0)
  +                    System.arraycopy(_buf, 0, newBuf, 0, _length);
  +                _buf = newBuf;
  +            }
  +        }
  +    }
  +
  +    private static final class AttributeEventImpl
  +        implements ValidatorListener.Event
  +    {
  +        private int _attIndex;
  +        private XMLStreamReader _xmlStream;
  +
  +        private void setXMLStreamReader(XMLStreamReader xsr)
  +        {
  +            _xmlStream = xsr;
  +        }
  +
  +        // can return null, used only to locate errors
  +        public XmlCursor getLocationAsCursor()
  +        {
  +            return null;
  +        }
  +
  +        public javax.xml.stream.Location getLocation()
  +        {
  +            return _xmlStream.getLocation();
  +        }
  +
  +        // fill up chars with the xsi:type attribute value if there is one othervise return false
  +        public boolean getXsiType(Chars chars) // BEGIN xsi:type
  +        {
  +            throw new IllegalStateException();
  +        }
  +
  +        // fill up chars with xsi:nill attribute value if any
  +        public boolean getXsiNil(Chars chars) // BEGIN xsi:nil
  +        {
  +            throw new IllegalStateException();
  +        }
  +
  +        // not used curently
  +        public boolean getXsiLoc(final Chars chars) // BEGIN xsi:schemaLocation
  +        {
  +            throw new IllegalStateException();
  +        }
  +
  +        // not used curently
  +        public boolean getXsiNoLoc(final Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
  +        {
  +            throw new IllegalStateException();
  +        }
  +
  +        // On START and ATTR
  +        public QName getName()
  +        {
  +            assert _xmlStream.isStartElement() : "Not on Start Element.";
  +            String uri = _xmlStream.getAttributeNamespace(_attIndex);
  +            QName qn = new QName(uri==null ? "" : uri, _xmlStream.getAttributeLocalName(_attIndex));
  +            //System.out.println("    Att QName: " + qn);
  +            return qn;
  +        }
  +
  +        // On TEXT and ATTR
  +        public void getText(Chars chars)
  +        {
  +            assert _xmlStream.isStartElement() : "Not on Start Element.";
  +            chars.string = _xmlStream.getAttributeValue(_attIndex);
  +            chars.offset = 0;
  +            //System.out.println("    Att Text   : " + chars.asString() );
  +        }
  +
  +        public void getText(Chars chars, int wsr)
  +        {
  +            assert _xmlStream.isStartElement() : "Not on Start Element.";
  +            chars.string = XmlWhitespace.collapse(
  +                    _xmlStream.getAttributeValue(_attIndex), wsr );
  +            //System.out.println("    Att Text WS: " + chars.string );
  +        }
  +
  +        public boolean textIsWhitespace()
  +        {
  +            throw new IllegalStateException();
  +        }
  +
  +        public String getNamespaceForPrefix(String prefix)
  +        {
  +            assert _xmlStream.isStartElement() : "Not on Start Element.";
  +            return _xmlStream.getNamespaceURI(prefix);
  +        }
  +
  +        private void setAttributeIndex(int attIndex)
  +        {
  +            _attIndex = attIndex;
  +        }
  +    }
  +
  +    /**
  +     * This is used as implementation of Event for validating global attributes
  +     * and for pushing the buffered attributes
  +     */
  +    private static final class SimpleEventImpl
  +        implements ValidatorListener.Event
  +    {
  +        private String _text;
  +        private QName  _qname;
  +        private XMLStreamReader _xmlStream;
  +
  +        private void setXMLStreamReader(XMLStreamReader xsr)
  +        {
  +            _xmlStream = xsr;
  +        }
  +
  +        // should return null, getLocation will be used, used only to locate errors
  +        public XmlCursor getLocationAsCursor()
  +        { return null; }
  +
  +        public javax.xml.stream.Location getLocation()
  +        {
  +            return _xmlStream.getLocation();
  +        }
  +
  +        // fill up chars with the xsi:type attribute value if there is one othervise return false
  +        public boolean getXsiType(Chars chars) // BEGIN xsi:type
  +        { return false; }
  +
  +        // fill up chars with xsi:nill attribute value if any
  +        public boolean getXsiNil(Chars chars) // BEGIN xsi:nil
  +        { return false; }
  +
  +        // not used curently
  +        public boolean getXsiLoc(final Chars chars) // BEGIN xsi:schemaLocation
  +        { return false; }
  +
  +        // not used curently
  +        public boolean getXsiNoLoc(final Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
  +        { return false; }
  +
  +        // On START and ATTR
  +        public QName getName()
  +        { return _qname; }
  +
  +        // On TEXT and ATTR
  +        public void getText(Chars chars)
  +        {
  +            chars.string = _text;
  +            chars.offset = 0;
  +        }
  +
  +        public void getText(Chars chars, int wsr)
  +        {
  +            chars.string = XmlWhitespace.collapse( _text, wsr );
  +        }
  +
  +        public boolean textIsWhitespace()
  +        { return false; }
  +
  +        public String getNamespaceForPrefix(String prefix)
  +        {
  +            return _xmlStream.getNamespaceURI(prefix);
  +        }
  +    }
  +    /* public methods */
  +
  +    public Object getProperty(String s) throws IllegalArgumentException
  +    {
  +        return super.getProperty(s);
  +    }
  +
  +    public int next() throws XMLStreamException
  +    {
  +        int evType = super.next();
  +        //debugEvent(evType);
  +
  +        if (_state==STATE_ERROR)
  +            return evType;
  +
  +        switch(evType)
  +        {
  +        case XMLEvent.START_ELEMENT:
  +            if (_state == STATE_ATTBUFFERING)
  +                pushBufferedAttributes();
  +
  +            if (_validator==null)
  +            {
  +                // avoid construction of a new QName object after the bug in getName() is fixed.
  +                QName qname = new QName(getNamespaceURI(), getLocalName());
  +
  +                if (_contentType==null)
  +                    _contentType = typeForGlobalElement(qname);
  +
  +                if (_state==STATE_ERROR)
  +                    break;
  +
  +                initValidator(_contentType);
  +                _validator.nextEvent(Validator.BEGIN, _elemEvent);
  +            }
  +
  +            _validator.nextEvent(Validator.BEGIN, _elemEvent);
  +
  +            int attCount = getAttributeCount();
  +            for(int i=0; i<attCount; i++)
  +            {
  +                _attEvent.setAttributeIndex(i);
  +                QName qn = _attEvent.getName();
  +                if (isSpecialAttribute(qn))
  +                    continue;
  +
  +                _validator.nextEvent(Validator.ATTR, _attEvent);
  +            }
  +            break;
  +
  +        case XMLEvent.ATTRIBUTE:
  +            if (getAttributeCount()==0)
  +                break;
  +
  +            if (_state == STATE_FIRSTEVENT || _state == STATE_ATTBUFFERING)
  +            {
  +                // buffer all Attributes
  +                for (int i=0; i<getAttributeCount(); i++)
  +                {
  +                    // avoid construction of a new QName object after the bug in getName() is fixed.
  +                    QName qname = new QName(getAttributeNamespace(i), getAttributeLocalName(i));
  +
  +                    if (qname.equals(XSI_TYPE))
  +                    {
  +                        String xsiTypeValue = getAttributeValue(i);
  +                        String uri = super.getNamespaceURI(QNameHelper.getPrefixPart(xsiTypeValue));
  +                        QName xsiTypeQname = new QName(uri, QNameHelper.getLocalPart(xsiTypeValue));
  +                        _xsiType = _stl.findType(xsiTypeQname);
  +                    }
  +
  +                    if (_attNamesList==null)
  +                    {
  +                        _attNamesList = new ArrayList();
  +                        _attValuesList = new ArrayList();
  +                    }
  +                    // skip xsi:type xsi:nil xsi:schemaLocation xsi:noNamespaceSchemaLocation
  +                    if (isSpecialAttribute(qname))
  +                        continue;
  +
  +                    _attNamesList.add(qname);
  +                    _attValuesList.add(getAttributeValue(i));
  +                }
  +                _state = STATE_ATTBUFFERING;
  +            }
  +            else
  +                throw new IllegalStateException("ATT event must be only at the beggining of the stream.");
  +
  +            break;
  +
  +        case XMLEvent.END_ELEMENT:
  +        case XMLEvent.END_DOCUMENT:
  +            if (_state == STATE_ATTBUFFERING)
  +                pushBufferedAttributes();
  +
  +            _validator.nextEvent(Validator.END, _elemEvent);
  +            break;
  +
  +        case XMLEvent.CDATA:
  +        case XMLEvent.CHARACTERS:
  +            if (_state == STATE_ATTBUFFERING)
  +                pushBufferedAttributes();
  +
  +            if (_validator==null)
  +            {
  +                if (_contentType==null)
  +                {
  +                    _errorListener.add(XmlError.forMessage("No content type provided for validation of a content model.",
  +                        XmlError.SEVERITY_ERROR));
  +                    _state = STATE_ERROR;
  +                }
  +                initValidator(_contentType);
  +                _validator.nextEvent(Validator.BEGIN, _simpleEvent);
  +            }
  +
  +            _validator.nextEvent(Validator.TEXT, _elemEvent);
  +            break;
  +
  +        case XMLEvent.START_DOCUMENT:
  +        case XMLEvent.COMMENT:
  +        case XMLEvent.DTD:
  +        case XMLEvent.ENTITY_DECLARATION:
  +        case XMLEvent.ENTITY_REFERENCE:
  +        case XMLEvent.NAMESPACE:
  +        case XMLEvent.NOTATION_DECLARATION:
  +        case XMLEvent.PROCESSING_INSTRUCTION:
  +        case XMLEvent.SPACE:
  +            //ignore
  +            break;
  +
  +        default:
  +            throw new IllegalStateException("Unknown event type.");
  +        }
  +
  +        return evType;
  +    }
  +
  +    private void pushBufferedAttributes()
  +    {
  +        SchemaType validationType = null;
  +
  +        if (_xsiType!=null)
  +        {
  +            if (_contentType==null)
  +            {
  +                validationType = _xsiType;
  +            }
  +            else
  +            {
  +                // we have both _xsiType and _contentType
  +                if (_contentType.isAssignableFrom(_xsiType))
  +                {
  +                    validationType = _xsiType;
  +                }
  +                else
  +                {
  +                    _errorListener.add(XmlError.forMessage("Specified type '" + _contentType +
  +                        "' not compatible with found xsi:type '" + _xsiType + "'.", XmlError.SEVERITY_ERROR));
  +                    _state = STATE_ERROR;
  +                    return;
  +                }
  +            }
  +        }
  +        else
  +        {
  +            if (_contentType != null)
  +            {
  +                validationType = _contentType;
  +            }
  +            else if (_attNamesList!=null)
  +            {
  +                // no xsi:type, no _contentType
  +                // this is the global attribute case
  +                validationType = _stl.findAttributeType((QName)_attNamesList.get(0));
  +                if (validationType==null)
  +                {
  +                    _errorListener.add(XmlError.forMessage("A schema global element with name '" + _attNamesList.get(0) +
  +                        "' could not be found in the current schema type loader.", XmlError.SEVERITY_ERROR));
  +                    _state = STATE_ERROR;
  +                    return;
  +                }
  +                // if _attNamesList.size() > 1 than the validator will add an error
  +            }
  +            else
  +            {
  +                _errorListener.add(XmlError.forMessage("No content type provided for validation of a content model.",
  +                    XmlError.SEVERITY_ERROR));
  +                _state = STATE_ERROR;
  +                return;
  +            }
  +        }
  +
  +        // here validationType is the right type, start pushing all acumulated attributes
  +        _validator = new Validator(validationType, null, _stl, _options, _errorListener);
  +        _validator.nextEvent(Validator.BEGIN, _simpleEvent);
  +
  +        for (int i=0; i<_attNamesList.size(); i++)
  +        {
  +            _simpleEvent._qname = (QName)_attNamesList.get(i);
  +            _simpleEvent._text = (String)_attValuesList.get(i);
  +            _validator.nextEvent(Validator.ATTR, _simpleEvent);
  +        }
  +
  +        _state = STATE_VALIDATING;
  +    }
  +
  +    private boolean isSpecialAttribute(QName qn)
  +    {
  +        if (qn.getNamespaceURI().equals(URI_XSI))
  +            return qn.getLocalPart().equals(XSI_TYPE.getLocalPart()) ||
  +                qn.getLocalPart().equals(XSI_NIL.getLocalPart()) ||
  +                qn.getLocalPart().equals(XSI_SL.getLocalPart()) ||
  +                qn.getLocalPart().equals(XSI_NSL.getLocalPart());
  +
  +        return false;
  +    }
  +
  +    /**
  +     * Initializes the validator for the given schemaType
  +     * @param schemaType
  +     */
  +    private void initValidator(SchemaType schemaType)
  +    {
  +        assert schemaType!=null;
  +
  +        _validator = new Validator(schemaType, null, _stl, _options, _errorListener);
  +    }
  +
  +    private SchemaType typeForGlobalElement(QName qname)
  +    {
  +        assert qname!=null;
  +
  +        SchemaType docType = _stl.findDocumentType(qname);
  +
  +        if (docType==null)
  +        {
  +            _errorListener.add(XmlError.forMessage("Schema document type not found for element '" + qname + "'.",
  +                XmlError.SEVERITY_ERROR));
  +            _state = STATE_ERROR;
  +        }
  +        return docType;
  +    }
  +
  +    /**
  +     * @return Returns the validation state up to this point
  +     * NOTE: At least on START ELEMENT should have been consumed for a valid value to be returned.
  +     */
  +    public boolean isValid()
  +    {
  +        if ( _state==STATE_ERROR || _validator==null)
  +            return false;
  +
  +        return _validator.isValid();
  +    }
  +
  +//    /* for unit testing */
  +//    public static void main(String[] args) throws FileNotFoundException, XMLStreamException
  +//    {
  +//        ValidatingXMLStreamReader valXsr = new ValidatingXMLStreamReader();
  +//        for( int i = 0; i<args.length; i++)
  +//        {
  +//            validate(valXsr, args[i]);
  +//        }
  +//    }
  +//
  +//    private static void validate(ValidatingXMLStreamReader valXsr, String file)
  +//        throws XMLStreamException, FileNotFoundException
  +//    {
  +//        Collection errors = new ArrayList();
  +//        XMLStreamReader xsr = XMLInputFactory.newInstance().
  +//            createXMLStreamReader(new FileInputStream(new File(file)));
  +//        valXsr.init(xsr, null,
  +//            XmlBeans.typeLoaderForClassLoader(ValidatingXMLStreamReader.class.getClassLoader()),
  +//            null,
  +//            errors);
  +//
  +//        while( valXsr.hasNext() )
  +//        {
  +//            valXsr.next();
  +//        }
  +//
  +//        System.out.println("File '" + file + "' is: " + (valXsr.isValid() ? "Valid" : "INVALID") + "\t\t\t\t ----------");
  +//        for (Iterator i = errors.iterator(); i.hasNext(); )
  +//        {
  +//            XmlError err = (XmlError)i.next();
  +//            System.out.println("ERROR " + err.getSeverity() + " " + err.getLine() + ":" + err.getColumn() + " " +
  +//                err.getMessage() + " ");
  +//        }
  +//    }
  +//
  +//    private void debugEvent(int evType)
  +//    {
  +//        switch(evType)
  +//        {
  +//        case XMLEvent.START_ELEMENT:
  +//            System.out.println("SE     " + _elemEvent.getName());
  +//            break;
  +//        case XMLEvent.START_DOCUMENT:
  +//            System.out.println("SDoc");
  +//            break;
  +//        case XMLEvent.END_ELEMENT:
  +//            System.out.println("EE     " + _elemEvent.getName());
  +//            break;
  +//        case XMLEvent.END_DOCUMENT:
  +//            System.out.println("EDoc");
  +//            break;
  +//        case XMLEvent.SPACE:
  +//            System.out.println("SPACE");
  +//            break;
  +//        case XMLEvent.CDATA:
  +//            System.out.println("CDATA");
  +//            break;
  +//        case XMLEvent.CHARACTERS:
  +//            Chars c = new Chars();
  +//            _elemEvent.getText(c);
  +//            System.out.println("TEXT     " + c.asString());
  +//            break;
  +//
  +//        case XMLEvent.ATTRIBUTE:      // global attributes
  +//            System.out.println("ATT     count: " + _elemEvent._xmlStream.getAttributeCount());
  +//            for(int i=0; i<_elemEvent._xmlStream.getAttributeCount(); i++)
  +//            {
  +//                System.out.println("\t\t" + _elemEvent._xmlStream.getAttributeNamespace(i) + ":" +
  +//                    _elemEvent._xmlStream.getAttributeLocalName(i) + "  =  " +
  +//                    _elemEvent._xmlStream.getAttributeValue(i));
  +//            }
  +//            break;
  +//        case XMLEvent.COMMENT:
  +//            System.out.println("COMMENT");
  +//            break;
  +//        case XMLEvent.DTD:
  +//            System.out.println("DTD");
  +//            break;
  +//        case XMLEvent.ENTITY_DECLARATION:
  +//            System.out.println("ENTITY_DECL");
  +//            break;
  +//        case XMLEvent.ENTITY_REFERENCE:
  +//            System.out.println("ENTITY_REF");
  +//            break;
  +//        case XMLEvent.NAMESPACE:
  +//            System.out.println("NS");
  +//            break;
  +//        case XMLEvent.NOTATION_DECLARATION:
  +//            System.out.println("NOTATION_DECL");
  +//            break;
  +//        case XMLEvent.PROCESSING_INSTRUCTION:
  +//            System.out.println("PI");
  +//            break;
  +//        }
  +//    }
  +}
  
  
  
  1.9       +1 -2      xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/Validator.java
  
  Index: Validator.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/Validator.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Validator.java	12 Feb 2004 20:06:22 -0000	1.8
  +++ Validator.java	23 Mar 2004 02:42:45 -0000	1.9
  @@ -122,8 +122,7 @@
               {
                   assert event != null;
   
  -                _errorListener.add(
  -                    XmlError.forCursor( msg, severity, event.getLocationAsCursor() ) );
  +                _errorListener.add(IdentityConstraint.errorForEvent(msg, severity, event));
               }
           }
       }
  
  
  
  1.4       +5 -0      xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatorUtil.java
  
  Index: ValidatorUtil.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatorUtil.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ValidatorUtil.java	12 Feb 2004 20:06:22 -0000	1.3
  +++ ValidatorUtil.java	23 Mar 2004 02:42:45 -0000	1.4
  @@ -49,6 +49,11 @@
               return null;
           }
   
  +        public javax.xml.stream.Location getLocation()
  +        {
  +            return null;
  +        }
  +
           // fill up chars with the xsi:type attribute value if there is one othervise return false
           public boolean getXsiType(Chars chars) // BEGIN xsi:type
           {
  
  
  
  1.8       +171 -5    xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java
  
  Index: XmlObjectBase.java
  ===================================================================
  RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- XmlObjectBase.java	16 Mar 2004 21:57:42 -0000	1.7
  +++ XmlObjectBase.java	23 Mar 2004 02:42:45 -0000	1.8
  @@ -42,6 +42,7 @@
   import java.util.Set;
   import java.util.Calendar;
   import java.util.Collection;
  +import java.util.ArrayList;
   
   import org.apache.xmlbeans.impl.common.XmlWhitespace;
   import org.apache.xmlbeans.impl.common.ValidationContext;
  @@ -92,7 +93,7 @@
               return get_store().get_root_object();
           return this;
       }
  -    
  +
       private static XmlObjectBase underlying(XmlObject obj)
       {
           if (obj == null)
  @@ -118,12 +119,12 @@
   
               // copy the type
               XmlObject result = get_store().get_schematypeloader().newInstance(schemaType(), null);
  -            
  +
   
               // copy the data
               XmlObjectBase target = underlying(result);
               target.get_store().copy_contents_from(get_store());
  -            
  +
               return result;
           }
       }
  @@ -139,7 +140,7 @@
   
       public XMLStreamReader newXMLStreamReader()
           { return newXMLStreamReader(null); }
  -    
  +
       public XMLStreamReader newXMLStreamReader(XmlOptions options)
           { XmlCursor cur = newCursorForce(); try { return cur.newXMLStreamReader(makeInnerOptions(options)); } finally { cur.dispose(); } }
   
  @@ -407,7 +408,7 @@
               c.selectPath( path, options );
   
               if (!c.hasNextSelection())
  -                selections = new XmlObject[ 0 ];
  +                selections = EMPTY_RESULT;
               else
               {
                   selections = new XmlObject [ c.getSelectionCount() ];
  @@ -2229,6 +2230,171 @@
                   return 0;
   
               return value_hash_code();
  +        }
  +    }
  +
  +    private static final XmlObject[] EMPTY_RESULT = new XmlObject[0];
  +
  +    /**
  +     * Selects the contents of the children elements with the given name.
  +     */
  +    public XmlObject[] selectChildren(QName elementName)
  +    {
  +        XmlCursor xc = this.newCursor();
  +        try
  +        {
  +            if (!xc.isContainer())
  +                return EMPTY_RESULT;
  +
  +            List result = new ArrayList();
  +
  +            if (xc.toChild(elementName))
  +            {
  +                // look for elements
  +                do
  +                {
  +                    result.add(xc.getObject());
  +                }
  +                while (xc.toNextSibling(elementName));
  +            }
  +            if (result.size() == 0)
  +                return EMPTY_RESULT;
  +            else
  +                return (XmlObject[]) result.toArray(EMPTY_RESULT);
  +        }
  +        finally
  +        {
  +            xc.dispose();
  +        }
  +    }
  +
  +    /**
  +     * Selects the contents of the children elements with the given name.
  +     */
  +    public XmlObject[] selectChildren(String elementUri, String elementLocalName)
  +    {
  +        return selectChildren(new QName(elementUri, elementLocalName));
  +    }
  +
  +    /**
  +     * Selects the contents of the children elements that are contained in the elementNameSet.
  +     */
  +    public XmlObject[] selectChildren(QNameSet elementNameSet)
  +    {
  +        if (elementNameSet==null)
  +            throw new IllegalArgumentException();
  +
  +        XmlCursor xc = this.newCursor();
  +        try
  +        {
  +            if (!xc.isContainer())
  +                return EMPTY_RESULT;
  +
  +            List result = new ArrayList();
  +
  +            if (xc.toFirstChild())
  +            {
  +                // look for elements
  +                do
  +                {
  +                    assert xc.isContainer();
  +                    if (elementNameSet.contains(xc.getName()))
  +                    {
  +                        result.add(xc.getObject());
  +                    }
  +                }
  +                while (xc.toNextSibling());
  +            }
  +            if (result.size() == 0)
  +                return EMPTY_RESULT;
  +            else
  +                return (XmlObject[]) result.toArray(EMPTY_RESULT);
  +        }
  +        finally
  +        {
  +            xc.dispose();
  +        }
  +    }
  +
  +    /**
  +     * Selects the content of the attribute with the given name.
  +     */
  +    public XmlObject selectAttribute(QName attributeName)
  +    {
  +        XmlCursor xc = this.newCursor();
  +
  +        try
  +        {
  +            if (!xc.isContainer())
  +                return null;
  +
  +            List result = new ArrayList();
  +
  +            if (xc.toFirstAttribute())
  +            {
  +                //look for attributes
  +                do
  +                {
  +                    if (xc.getName().equals(attributeName))
  +                    {
  +                        return xc.getObject();
  +                    }
  +                }
  +                while (xc.toNextAttribute());
  +            }
  +            return null;
  +        }
  +        finally
  +        {
  +            xc.dispose();
  +        }
  +    }
  +
  +    /**
  +     * Selects the content of the attribute with the given name.
  +     */
  +    public XmlObject selectAttribute(String attributeUri, String attributeLocalName)
  +    {
  +        return selectAttribute(new QName(attributeUri, attributeLocalName));
  +    }
  +
  +    /**
  +     * Selects the contents of the attributes that are contained in the elementNameSet.
  +     */
  +    public XmlObject[] selectAttributes(QNameSet attributeNameSet)
  +    {
  +        if (attributeNameSet==null)
  +            throw new IllegalArgumentException();
  +
  +        XmlCursor xc = this.newCursor();
  +        try
  +        {
  +            if (!xc.isContainer())
  +                return EMPTY_RESULT;
  +
  +            List result = new ArrayList();
  +
  +            if (xc.toFirstAttribute())
  +            {
  +                //look for attributes
  +                do
  +                {
  +                    if (attributeNameSet.contains(xc.getName()))
  +                    {
  +                        result.add(xc.getObject());
  +                    }
  +                }
  +                while (xc.toNextAttribute());
  +            }
  +
  +            if (result.size() == 0)
  +                return EMPTY_RESULT;
  +            else
  +                return (XmlObject[]) result.toArray(EMPTY_RESULT);
  +        }
  +        finally
  +        {
  +            xc.dispose();
           }
       }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xmlbeans-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xmlbeans-cvs-help@xml.apache.org