You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ne...@apache.org on 2001/04/23 22:41:31 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/validators/schema/identity XPath.java Selector.java Field.java

neilg       01/04/23 13:41:30

  Modified:    java/docs releases.xml
               java/src/org/apache/xerces/validators/schema/identity
                        XPath.java Selector.java Field.java
  Log:
  updates to XPath support for Schema identity constraints to bring it closer to Schema PR.  More work needs to be done.  Patch from Achille Fokoue Nkoutche.
  
  Revision  Changes    Path
  1.52      +2 -0      xml-xerces/java/docs/releases.xml
  
  Index: releases.xml
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/docs/releases.xml,v
  retrieving revision 1.51
  retrieving revision 1.52
  diff -u -r1.51 -r1.52
  --- releases.xml	2001/04/19 21:35:48	1.51
  +++ releases.xml	2001/04/23 20:41:25	1.52
  @@ -4,6 +4,8 @@
   <s1 title="Releases">
     <s2 title='TBD'>
       <ul>
  +        <li>Update XPath support to bring it into compliance with Schema 
  +            PR [Achille Fokoue Nkoutche/Neil Graham]</li>
           <li>Implemented Schema PR changes to the syntax of &lt;attribute&gt; 
               declarations [Ted Han (than@ghx.com)/Neil Graham]</li>
           <li>Added French resource bundle for regex package [Jean-Claude Dufourd, Laurent Foret/Neil Graham]</li>
  
  
  
  1.6       +200 -20   xml-xerces/java/src/org/apache/xerces/validators/schema/identity/XPath.java
  
  Index: XPath.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/validators/schema/identity/XPath.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- XPath.java	2001/02/20 07:47:05	1.5
  +++ XPath.java	2001/04/23 20:41:27	1.6
  @@ -65,7 +65,7 @@
    * Bare minimum XPath parser.
    *
    * @author Andy Clark, IBM
  - * @version $Id: XPath.java,v 1.5 2001/02/20 07:47:05 andyc Exp $
  + * @version $Id: XPath.java,v 1.6 2001/04/23 20:41:27 neilg Exp $
    */
   public class XPath {
   
  @@ -86,9 +86,8 @@
       /** Expression. */
       protected String fExpression;
   
  -    /** Location path. */
  -    protected LocationPath fLocationPath;
   
  +
       // Xerces 1.x framework
   
       /** String pool. */
  @@ -111,9 +110,9 @@
       // Public methods
       //
   
  -    /** Returns a representation of the location path for this XPath. */
  +	/** Returns a representation of the first location path for this XPath. */
       public LocationPath getLocationPath() {
  -        return (LocationPath)fLocationPath.clone();
  +		return (LocationPath)fLocationPaths[0].clone();
       } // getLocationPath(LocationPath)
   
       //
  @@ -122,7 +121,14 @@
   
       /** Returns a string representation of this object. */
       public String toString() {
  -        return fLocationPath.toString();
  +		StringBuffer buf=new StringBuffer();
  +		for (int  i=0;i<fLocationPaths.length;i++){
  +			if (i>0){
  +				buf.append("|");
  +			}
  +			buf.append(fLocationPaths[i].toString());
  +		}
  +		return buf.toString();
       } // toString():String
   
       //
  @@ -153,7 +159,13 @@
                       token == XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME ||
                       //token == XPath.Tokens.EXPRTOKEN_NODETYPE_NODE ||
                       token == XPath.Tokens.EXPRTOKEN_OPERATOR_SLASH ||
  -                    token == XPath.Tokens.EXPRTOKEN_PERIOD
  +					token == XPath.Tokens.EXPRTOKEN_PERIOD ||
  +					//added to conform to PR Spec
  +					token == XPath.Tokens.EXPRTOKEN_NAMETEST_ANY ||
  +					token == XPath.Tokens.EXPRTOKEN_NAMETEST_NAMESPACE ||
  +					token == XPath.Tokens.EXPRTOKEN_OPERATOR_DOUBLE_SLASH ||
  +					token == XPath.Tokens.EXPRTOKEN_OPERATOR_UNION
  +					//
                       ) {
                       super.addToken(tokens, token);
                       return;
  @@ -182,10 +194,34 @@
                                              xtokens, fExpression, 0, length);
           //fTokens.dumpTokens();
           java.util.Vector stepsVector = new java.util.Vector();
  +		java.util.Vector locationPathsVector= new java.util.Vector();
           int tokenCount = xtokens.getTokenCount();
  +		boolean firstTokenOfLocationPath=true;
  +		
           for (int i = 0; i < tokenCount; i++) {
               int token = xtokens.getToken(i);
  +			boolean isNamespace=false;
  +			
               switch (token) {
  +				case  XPath.Tokens.EXPRTOKEN_OPERATOR_UNION :{
  +					if (i == 0) {
  +						throw new XPathException("not allowed to have '|' here");
  +					}
  +					
  +					int size = stepsVector.size();
  +					if (size == 0) {
  +						throw new XPathException("not allowed to have '||'");
  +					}
  +					Step[] steps = new Step[size];
  +					stepsVector.copyInto(steps);
  +					// add location path
  +					locationPathsVector.add(new LocationPath(steps));
  +					//reset stepsVector
  +					stepsVector.clear();
  +					firstTokenOfLocationPath=true;
  +					break;
  +				}
  +
                   case XPath.Tokens.EXPRTOKEN_AXISNAME_ATTRIBUTE: {
                       // consume "::" token and drop through
                       i++;
  @@ -196,10 +232,30 @@
                           throw new XPathException("missing attribute name");
                       }
                       token = xtokens.getToken(++i);
  -                    if (token != XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME) {
  -                        throw new XPathException("expected "+xtokens.getTokenName(XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME)+
  -                                                 ", found "+xtokens.getTokenName(token));
  +				
  +					if (token != XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME
  +						&& token!= XPath.Tokens.EXPRTOKEN_NAMETEST_ANY
  +						&& token!=  XPath.Tokens.EXPRTOKEN_NAMETEST_NAMESPACE) {
  +						throw new XPathException("expected \""+xtokens.getTokenName(XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME)+
  +												 "\" or \""+xtokens.getTokenName( XPath.Tokens.EXPRTOKEN_NAMETEST_ANY)+
  +												 "\" or \""+xtokens.getTokenName( XPath.Tokens.EXPRTOKEN_NAMETEST_NAMESPACE)+
  +												 "\", found "+xtokens.getTokenName(token));
  +					}
  +					boolean isNamespaceAtt=false;
  +					switch (token)
  +					{
  +					    case XPath.Tokens.EXPRTOKEN_NAMETEST_ANY:{
  +						    Axis axis = new Axis(Axis.ATTRIBUTE);
  +						    NodeTest nodeTest = new NodeTest(NodeTest.WILDCARD);
  +						    Step step = new Step(axis, nodeTest);
  +						    stepsVector.addElement(step);
  +						    break;
  +						}
  +						case XPath.Tokens.EXPRTOKEN_NAMETEST_NAMESPACE:{
  +							isNamespaceAtt=true;
                       }
  +					    case XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME:{
  +							
                       token = xtokens.getToken(++i);
                       int prefix = xtokens.getTokenString(token);
                       int uri = StringPool.EMPTY_STRING;
  @@ -209,6 +265,17 @@
                       if (prefix != -1 && context != null && uri == StringPool.EMPTY_STRING) {
                           throw new XPathException("prefix "+fStringPool.toString(prefix)+" not bound to namespace URI");
                       }
  +							
  +							if (isNamespaceAtt)
  +							{
  +								// build step
  +								Axis axis = new Axis(Axis.ATTRIBUTE);
  +								NodeTest nodeTest=new NodeTest(fStringPool,prefix,uri);
  +								Step step = new Step(axis, nodeTest);
  +								stepsVector.addElement(step);
  +								break;
  +							}
  +								
                       token = xtokens.getToken(++i);
                       int localpart = xtokens.getTokenString(token);
                       int rawname = prefix != -1
  @@ -222,6 +289,10 @@
                       stepsVector.addElement(step);
                       break;
                   }
  +					}
  +					firstTokenOfLocationPath=false;
  +					break;
  +				}
                   /***
                   case XPath.Tokens.EXPRTOKEN_AXISNAME_SELF: {
                       break;
  @@ -237,8 +308,27 @@
                   }
                   /***/
                   case XPath.Tokens.EXPRTOKEN_AXISNAME_CHILD: {
  +					
                       // consume "::" token and drop through
                       i++;
  +					if (i == tokenCount - 1) {
  +						throw new XPathException("expected step following '"
  +							+xtokens.getTokenName(XPath.Tokens.EXPRTOKEN_AXISNAME_CHILD)+"::'");
  +					}
  +					firstTokenOfLocationPath=false;
  +					break;
  +				}
  +				case XPath.Tokens.EXPRTOKEN_NAMETEST_ANY:{
  +					Axis axis = new Axis(Axis.CHILD);
  +					NodeTest nodeTest = new NodeTest(NodeTest.WILDCARD);
  +					Step step = new Step(axis, nodeTest);
  +					stepsVector.addElement(step);
  +					firstTokenOfLocationPath=false;
  +					break;
  +				}
  +				
  +				case XPath.Tokens.EXPRTOKEN_NAMETEST_NAMESPACE:{
  +					isNamespace=true;
                   }
                   case XPath.Tokens.EXPRTOKEN_NAMETEST_QNAME: {
                       // consume QName token
  @@ -252,6 +342,17 @@
                           uri == StringPool.EMPTY_STRING) {
                           throw new XPathException("prefix "+fStringPool.toString(prefix)+" not bound to namespace URI");
                       }
  +
  +					if (isNamespace)
  +					{
  +						// build step
  +						Axis axis = new Axis(Axis.CHILD);
  +						NodeTest nodeTest=new NodeTest(fStringPool,prefix,uri);
  +						Step step = new Step(axis, nodeTest);
  +						stepsVector.addElement(step);
  +						break;
  +					}
  +						
                       token = xtokens.getToken(++i);
                       int localpart = xtokens.getTokenString(token);
                       int rawname = prefix != -1
  @@ -263,6 +364,7 @@
                       NodeTest nodeTest = new NodeTest(fStringPool, new QName(prefix, localpart, rawname, uri));
                       Step step = new Step(axis, nodeTest);
                       stepsVector.addElement(step);
  +					firstTokenOfLocationPath=false;
                       break;
                   }
                   /***
  @@ -270,38 +372,75 @@
                       break;
                   }
                   /***/
  +				
  +				
                   case XPath.Tokens.EXPRTOKEN_PERIOD: {
                       // build step
                       Axis axis = new Axis(Axis.SELF);
                       NodeTest nodeTest = new NodeTest(NodeTest.NODE);
                       Step step = new Step(axis, nodeTest);
                       stepsVector.addElement(step);
  -                    break;
  -                }
  +					
  +					if (firstTokenOfLocationPath && i+1<tokenCount){
  +						token=xtokens.getToken(i+1);
  +						if (token == XPath.Tokens.EXPRTOKEN_OPERATOR_DOUBLE_SLASH){
  +							i++;
  +							if (i == tokenCount - 1) {
  +								throw new XPathException("expected step following '//'");
  +							}
  +							if (i+1<tokenCount)	{
  +								token=xtokens.getToken(i+1);
  +								if (token==XPath.Tokens.EXPRTOKEN_OPERATOR_SLASH)
  +									throw new XPathException("'/' not allowed after '//'");
  +							}	
  +							// build step
  +							axis = new Axis(Axis.DESCENDANT);
  +							nodeTest = new NodeTest(NodeTest.NODE);
  +							step = new Step(axis, nodeTest);
  +							stepsVector.addElement(step);
  +						}
  +					}
  +					firstTokenOfLocationPath=false;
  +                    break;
  +                }
  +				
  +				case XPath.Tokens.EXPRTOKEN_OPERATOR_DOUBLE_SLASH:{
  +					throw new XPathException("'//' only allowed after '.' at the beginning of an xpath");
  +				}
                   case XPath.Tokens.EXPRTOKEN_OPERATOR_SLASH: {
                       // keep on truckin'
  -                    if (i == 0) {
  +					if (firstTokenOfLocationPath) {
                           throw new XPathException("not allowed to select the root");
                       }
                       if (i == tokenCount - 1) {
                           throw new XPathException("expected step following '/'");
                       }
  +					firstTokenOfLocationPath=false;
                       break;
                   }
  +				default:
  +					firstTokenOfLocationPath=false;
               }
           }
   
           int size = stepsVector.size();
           if (size == 0) {
  +			if (locationPathsVector.size()==0)
               throw new XPathException("empty xpath expression");
  +			else
  +				throw new XPathException("xpath cannot end with '|'");
           }
           Step[] steps = new Step[size];
           stepsVector.copyInto(steps);
  +		locationPathsVector.add(new LocationPath(steps));
   
           // save location path
  -        fLocationPath = new LocationPath(steps);
  +		fLocationPaths=new LocationPath[locationPathsVector.size()];
  +		locationPathsVector.copyInto(fLocationPaths);
  +		
  +		
           if (DEBUG_XPATH_PARSE) {
  -            System.out.println(">>> "+fLocationPath);
  +			System.out.println(">>> "+fLocationPaths);
           }
   
       } // parseExpression(SymbolTable,NamespaceContext)
  @@ -352,7 +491,8 @@
           public String toString() {
               StringBuffer str = new StringBuffer();
               for (int i = 0; i < steps.length; i++) {
  -                if (i > 0) {
  +				if (i > 0	&& (steps[i-1].axis.type!=Axis.DESCENDANT
  +					&& steps[i].axis.type!=Axis.DESCENDANT) ){
                       str.append('/');
                   }
                   str.append(steps[i].toString());
  @@ -374,7 +514,7 @@
               return new LocationPath(this);
           } // clone():Object
   
  -    } // class LocationPath
  +	} // class locationPath 
   
       /**
        * A location path step comprised of an axis and node test.
  @@ -428,6 +568,9 @@
               if (axis.type == Axis.CHILD) {
                   return nodeTest.toString();
               }
  +			if (axis.type == Axis.DESCENDANT) {
  +				return "//";
  +			}
               return "??? ("+axis.type+')';
               /***/
           } // toString():String
  @@ -460,6 +603,9 @@
           /** Type: self. */
           public static final short SELF = 3;
   
  +		
  +		/** Type: descendant. */
  +		public static final short DESCENDANT = 4;
           //
           // Data
           //
  @@ -491,6 +637,7 @@
                   case CHILD: return "child";
                   case ATTRIBUTE: return "attribute";
                   case SELF: return "self";
  +				case DESCENDANT: return "descendant";
               }
               return "???";
           } // toString():String
  @@ -523,6 +670,9 @@
           /** Type: node. */
           public static final short NODE = 3;
   
  +		/** Type: namespace */
  +		public static final short NAMESPACE= 4;
  +		
           //
           // Data
           //
  @@ -551,6 +701,12 @@
               this.type = QNAME;
               this.name.setValues(name);
           } // <init>(QName)
  +		/** Constructs a node test of type Namespace. */
  +		public NodeTest(StringPool stringPool, int prefix, int uri) {
  +			fStringPool = stringPool;
  +			this.type = NAMESPACE;
  +			this.name.setValues(prefix,-1,-1,uri);
  +		}
   
           /** Copy constructor. */
           public NodeTest(NodeTest nodeTest) {
  @@ -576,6 +732,15 @@
                       }
                       return fStringPool.toString(name.localpart);
                   }
  +				case NAMESPACE: {
  +					if (name.prefix!=-1) {
  +						if (name.uri == StringPool.EMPTY_STRING) {
  +							return fStringPool.toString(name.prefix) + ":*";
  +						}
  +						return "{" + fStringPool.toString(name.uri) + '}' + fStringPool.toString(name.prefix) + ":*";
  +					} 
  +					return "???:*";
  +				}				
                   case WILDCARD: {
                       return "*";
                   }
  @@ -606,7 +771,7 @@
        * @author Glenn Marcy, IBM
        * @author Andy Clark, IBM
        *
  -     * @version $Id: XPath.java,v 1.5 2001/02/20 07:47:05 andyc Exp $
  +     * @version $Id: XPath.java,v 1.6 2001/04/23 20:41:27 neilg Exp $
        */
       private static final class Tokens {
       
  @@ -1064,7 +1229,7 @@
        * @author Glenn Marcy, IBM
        * @author Andy Clark, IBM
        *
  -     * @version $Id: XPath.java,v 1.5 2001/02/20 07:47:05 andyc Exp $
  +     * @version $Id: XPath.java,v 1.6 2001/04/23 20:41:27 neilg Exp $
        */
       private static class Scanner {
       
  @@ -2142,7 +2307,7 @@
        * @author Glenn Marcy, IBM
        * @author Andy Clark, IBM
        *
  -     * @version $Id: XPath.java,v 1.5 2001/02/20 07:47:05 andyc Exp $
  +     * @version $Id: XPath.java,v 1.6 2001/04/23 20:41:27 neilg Exp $
        */
       /***
       public static class XPathExprParser {
  @@ -3611,4 +3776,19 @@
   
       } // main(String[])
   
  +	/** Location paths. */
  +	protected LocationPath[] fLocationPaths;	//
  +	// Public methods
  +	//
  +	
  +	/** Returns a representation of all location paths for this XPath. 
  +		XPath = locationPath ( '|' locationPath)*
  +	*/
  +	public LocationPath[] getLocationPaths() {
  +		LocationPath[] ret=new LocationPath[fLocationPaths.length];
  +		for (int i=0;i<fLocationPaths.length;i++){
  +			ret[i]=(LocationPath)fLocationPaths[i].clone();
  +		}
  +		return ret; 
  +	} // getLocationPath(LocationPath)
   } // class XPath
  
  
  
  1.7       +10 -5     xml-xerces/java/src/org/apache/xerces/validators/schema/identity/Selector.java
  
  Index: Selector.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/validators/schema/identity/Selector.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Selector.java	2001/02/21 09:57:16	1.6
  +++ Selector.java	2001/04/23 20:41:27	1.7
  @@ -68,7 +68,7 @@
    * Schema identity constraint selector.
    *
    * @author Andy Clark, IBM
  - * @version $Id: Selector.java,v 1.6 2001/02/21 09:57:16 andyc Exp $
  + * @version $Id: Selector.java,v 1.7 2001/04/23 20:41:27 neilg Exp $
    */
   public class Selector {
   
  @@ -131,7 +131,7 @@
        * Schema identity constraint selector XPath expression.
        *
        * @author Andy Clark, IBM
  -     * @version $Id: Selector.java,v 1.6 2001/02/21 09:57:16 andyc Exp $
  +     * @version $Id: Selector.java,v 1.7 2001/04/23 20:41:27 neilg Exp $
        */
       public static class XPath
           extends org.apache.xerces.validators.schema.identity.XPath {
  @@ -147,17 +147,22 @@
               //       order to handle selectors such as "." that select
               //       the element container because the fields could be
               //       relative to that element. -Ac
  -            super("./"+xpath, stringPool, context);
  +			//       Unless xpath starts with a descendant node -Achille Fokoue
  +			super(((xpath.trim().startsWith("//") ||xpath.trim().startsWith(".//"))?
  +				xpath:"./"+xpath), stringPool, context);
       
               // verify that an attribute is not selected
  -            XPath.Axis axis = fLocationPath.steps[fLocationPath.steps.length-1].axis;
  +			for (int i=0;i<fLocationPaths.length;i++) {
  +				org.apache.xerces.validators.schema.identity.XPath.Axis axis =
  +					fLocationPaths[i].steps[fLocationPaths[i].steps.length-1].axis;
               if (axis.type == axis.ATTRIBUTE) {
                   throw new XPathException("selectors cannot select attributes");
               }
  +			}
       
           } // <init>(String,StringPool,NamespacesScope)
       
  -    } // class XPath
  +	} // class Selector.XPath
   
       /**
        * Selector matcher.
  
  
  
  1.6       +5 -2      xml-xerces/java/src/org/apache/xerces/validators/schema/identity/Field.java
  
  Index: Field.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/validators/schema/identity/Field.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Field.java	2001/02/20 07:47:05	1.5
  +++ Field.java	2001/04/23 20:41:28	1.6
  @@ -67,7 +67,7 @@
    * Schema identity constraint field.
    *
    * @author Andy Clark, IBM
  - * @version $Id: Field.java,v 1.5 2001/02/20 07:47:05 andyc Exp $
  + * @version $Id: Field.java,v 1.6 2001/04/23 20:41:28 neilg Exp $
    */
   public class Field {
   
  @@ -154,7 +154,10 @@
               //       order to handle selectors such as "@attr" that 
               //       select the attribute because the fields could be
               //       relative to the selector element. -Ac
  -            super("./"+xpath, stringPool, context);
  +			//       Unless xpath starts with a descendant node -Achille Fokoue
  +			super(((xpath.trim().startsWith("//") ||xpath.trim().startsWith(".//"))?
  +				xpath:"./"+xpath), stringPool, context);
  +			
           } // <init>(String,StringPool,NamespacesScope)
   
       } // class XPath
  
  
  

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