xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ce...@apache.org
Subject cvs commit: xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator ValidatorUtil.java Validator.java
Date Thu, 05 Feb 2004 23:24:26 GMT
cezar       2004/02/05 15:24:26

  Modified:    v2/src/typeimpl/org/apache/xmlbeans/impl/validator
                        Validator.java
  Added:       v2/src/typeimpl/org/apache/xmlbeans/impl/validator
                        ValidatorUtil.java
  Log:
  Return null for skiped elements in Validator.getCurrentElement()
  Adding an utility for validation of simple schema types.
  
  CR: Eric
  DRT: passes
  
  Revision  Changes    Path
  1.6       +6 -4      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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Validator.java	3 Feb 2004 02:05:56 -0000	1.5
  +++ Validator.java	5 Feb 2004 23:24:26 -0000	1.6
  @@ -1001,7 +1001,7 @@
       // Simple Type Validation
       //
       // emptyContent means that you can't use the event to get text: there is
  -    // not text, but you can use the event to do prefix resolution (in the case
  +    // no text, but you can use the event to do prefix resolution (in the case
       // where the default is a qname)
       //
   
  @@ -1027,8 +1027,6 @@
               return null;
           }
   
  -
  -
           // Get the value as a string (as normalized by the white space rule
           // TODO - will want to optimize this later
   
  @@ -1624,7 +1622,11 @@
           if (_localElement != null)
               return _localElement;
   
  -        //try getting it from the stack (this should happen after END)  
  +        // it means the element is to be skiped and it doesn't have a known SchemaLocalElement
  +        if (_eatContent>0)
  +            return null;
  +
  +        //try getting it from the stack (this should happen after END)
           if (_stateStack!=null && _stateStack._field instanceof SchemaLocalElement
)
               return (SchemaLocalElement)_stateStack._field;
   
  
  
  
  1.1                  xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/validator/ValidatorUtil.java
  
  Index: ValidatorUtil.java
  ===================================================================
  package org.apache.xmlbeans.impl.validator;
  
  import org.apache.xmlbeans.SchemaType;
  import org.apache.xmlbeans.XmlCursor;
  import org.apache.xmlbeans.impl.common.Chars;
  import org.apache.xmlbeans.impl.common.PrefixResolver;
  import org.apache.xmlbeans.impl.common.ValidatorListener;
  import org.apache.xmlbeans.impl.common.XmlWhitespace;
  
  import javax.xml.namespace.QName;
  import java.util.Collection;
  
  /**
   * Author: Cezar Andrei (cezar.andrei at bea.com)
   * Date: Feb 5, 2004
   */
  public class ValidatorUtil
  {
      private static class EventImpl
          implements ValidatorListener.Event
      {
          PrefixResolver _prefixResolver;
          Chars _text;
  
          EventImpl(PrefixResolver prefixResolver, Chars chars)
          {
              _prefixResolver = prefixResolver;
              _text = chars;
          }
  
          // 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;
          }
  
          public boolean getXsiLoc(Chars chars) // BEGIN xsi:schemaLocation
          {
              return false;
          }
  
          public boolean getXsiNoLoc(Chars chars) // BEGIN xsi:noNamespaceSchemaLocation
          {
              return false;
          }
  
          // On START and ATTR
          public QName getName()
          {
              return null;
          }
  
          // On TEXT and ATTR
          public void getText(Chars chars)
          {
              chars.string = _text.asString();
          }
  
          public void getText(Chars chars, int wsr)
          {
              chars.string = XmlWhitespace.collapse(
                      _text.asString(), wsr );
          }
  
          public boolean textIsWhitespace()
          {
              return false;
          }
  
          public String getNamespaceForPrefix(String prefix)
          {
              return _prefixResolver.getNamespaceForPrefix(prefix);
          }
      }
  
      public static boolean validateSimpleType ( SchemaType type, String value,
          Collection errors, PrefixResolver prefixResolver)
      {
          if (!type.isSimpleType() &&
                  type.getContentType() != SchemaType.SIMPLE_CONTENT)
          {
              assert false;
              throw new RuntimeException( "Not a simple type" );
          }
  
          Validator validator =
                  new Validator(
                          type, null, type.getTypeSystem(), null, errors);
  
          //make only one event at the beginning and than reuse it
          Chars text = new Chars();
          EventImpl ev = new EventImpl(prefixResolver, text);
  
          validator.nextEvent(ValidatorListener.BEGIN, ev);
  
          text.string = value;
          validator.nextEvent(ValidatorListener.TEXT, ev);
  
          validator.nextEvent(ValidatorListener.END, ev);
  
          return validator.isValid();
      }
  
  //    public static void main(String[] args)
  //    {
  //        String value;
  //        value = "  +1.2323 ";
  //        System.out.println("float " + validateSimpleType(XmlFloat.type, value, null ,
null));
  //        value = " +234  ";
  //        System.out.println("posInt " + validateSimpleType(XmlPositiveInteger.type, value,
null , null));
  //        value = "2001-01-01";
  //        System.out.println("IntOrDateUnion " + validateSimpleType(DocDocument.Doc.IntOrDateUnion.type,
value, null , null));
  //        value = "232321";
  //        System.out.println("IntOrDateUnion " + validateSimpleType(DocDocument.Doc.IntOrDateUnion.type,
value, null , null));
  //    }
  }
  
  
  

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


Mime
View raw message