You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by sa...@apache.org on 2011/08/26 20:09:03 UTC
svn commit: r1162191 - in
/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath:
XPath20.java XPath20Parser.java
Author: sandygao
Date: Fri Aug 26 18:09:02 2011
New Revision: 1162191
URL: http://svn.apache.org/viewvc?rev=1162191&view=rev
Log:
Proper handling of QNames in CTA XPath, to resolve them when the schema is built, and to check if the referenced types are schema built-ins.
Modified:
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20.java
xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20Parser.java
Modified: xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20.java?rev=1162191&r1=1162190&r2=1162191&view=diff
==============================================================================
--- xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20.java (original)
+++ xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20.java Fri Aug 26 18:09:02 2011
@@ -19,6 +19,8 @@ package org.apache.xerces.impl.xpath;
import java.io.StringReader;
+import javax.xml.XMLConstants;
+
import org.apache.xerces.impl.dv.SchemaDVFactory;
import org.apache.xerces.impl.dv.XSSimpleType;
import org.apache.xerces.impl.dv.xs.TypeValidator;
@@ -49,7 +51,7 @@ public class XPath20 {
//The parser expects '\n' at the end of the expression. So insert it.
StringReader reader = new StringReader(fExpression + "\n");
- XPath20Parser parser = new XPath20Parser(reader);
+ XPath20Parser parser = new XPath20Parser(reader, nsContext);
fRootNode = parser.parseExpression();
}
@@ -103,7 +105,7 @@ abstract class XPathSyntaxTreeNode {
return TYPE_UNDEFINED;
}
- public String getTypeName() {
+ public XSSimpleType getSimpleType() {
return null;
}
}
@@ -172,14 +174,14 @@ class ConjunctionNode extends XPathSynta
}
class AttrNode extends XPathSyntaxTreeNode {
- private String name;
+ private QName name;
- public AttrNode(String name) {
+ public AttrNode(QName name) {
this.name = name;
}
public boolean evaluate(QName element, XMLAttributes attributes, NamespaceContext nsContext) throws Exception {
- String attrValue = attributes.getValue(name);
+ String attrValue = attributes.getValue(name.uri, name.localpart);
if (attrValue == null || attrValue.length() == 0) {
return false;
}
@@ -187,10 +189,7 @@ class AttrNode extends XPathSyntaxTreeNo
}
public Object getValue(XMLAttributes attributes, NamespaceContext nsContext) throws Exception {
- int qNamePrfxIdx = name.indexOf(':');
- String attrPrefix = (qNamePrfxIdx != -1) ? name.substring(0, qNamePrfxIdx) : "";
- String attrLocalName = (qNamePrfxIdx != -1) ? name.substring(qNamePrfxIdx + 1) : name;
- String attrValue = attributes.getValue(nsContext.getURI(attrPrefix.intern()), attrLocalName);
+ String attrValue = attributes.getValue(name.uri, name.localpart);
if (attrValue == null) {
throw new XPathException("Attribute value is null");
}
@@ -275,10 +274,9 @@ class CompNode extends XPathSyntaxTreeNo
} else if (type1 == TYPE_UNTYPED && type2 == TYPE_OTHER) {
// attr and cast expr
- String type = child2.getTypeName();
String attrVal = child1.getValue(attributes, nsContext).toString();
- simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(type);
+ simpleType = (XSSimpleTypeDecl)child2.getSimpleType();
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
@@ -289,10 +287,9 @@ class CompNode extends XPathSyntaxTreeNo
} else if (type1 == TYPE_OTHER && type2 == TYPE_UNTYPED) {
// cast expr and attr
- String type = child1.getTypeName();
String attrVal = child2.getValue(attributes, nsContext).toString();
- simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(type);
+ simpleType = (XSSimpleTypeDecl)child1.getSimpleType();
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
@@ -303,16 +300,13 @@ class CompNode extends XPathSyntaxTreeNo
} else if (type1 == TYPE_OTHER && type2 == TYPE_OTHER) {
//cast expr and cast expr
- String typeName1 = child1.getTypeName();
- String typeName2 = child2.getTypeName();
-
- simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(typeName1);
+ simpleType = (XSSimpleTypeDecl)child1.getSimpleType();
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
short dt1 = simpleType.getBuiltInKind();
- simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(typeName2);
+ simpleType = (XSSimpleTypeDecl)child2.getSimpleType();
if (simpleType == null) {
throw new XPathException("Casted type is not a built-in type");
}
@@ -349,12 +343,18 @@ class CompNode extends XPathSyntaxTreeNo
}
class CastNode extends XPathSyntaxTreeNode {
- private String castedType;
+ private XSSimpleType castedType;
private XPathSyntaxTreeNode child;
- public CastNode(XPathSyntaxTreeNode child, String castedType) {
+ public CastNode(XPathSyntaxTreeNode child, QName type) throws XPathException {
this.child = child;
- this.castedType = castedType;
+ if (!XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(type.uri)) {
+ throw new XPathException("Casted type is not a built-in type");
+ }
+ castedType = dvFactory.getBuiltInType(type.localpart);
+ if (castedType == null) {
+ throw new XPathException("Casted type is not a built-in type");
+ }
}
public boolean evaluate(QName element, XMLAttributes attributes, NamespaceContext nsContext) throws Exception {
@@ -386,10 +386,7 @@ class CastNode extends XPathSyntaxTreeNo
}
public Object getValue(XMLAttributes attributes, NamespaceContext nsContext) throws Exception {
- XSSimpleType type = dvFactory.getBuiltInType(getTypeName());
- if (type == null) {
- throw new XPathException("Casted type is not a built-in type");
- }
+ XSSimpleType type = getSimpleType();
Object obj;
if (child.getType() == TYPE_UNTYPED) {
@@ -408,21 +405,14 @@ class CastNode extends XPathSyntaxTreeNo
return obj;
}
- public String getTypeName() {
- String localname = castedType;
- int index = localname.indexOf(':');
- if (index != -1) {
- localname = localname.substring(index + 1);
- }
- return localname;
+ public XSSimpleType getSimpleType() {
+ return castedType;
}
public int getType() {
- String type = getTypeName();
- XSSimpleTypeDecl simpleType = (XSSimpleTypeDecl) dvFactory.getBuiltInType(type);
- if (simpleType.getNumeric()) {
+ if (castedType.getNumeric()) {
return TYPE_DOUBLE;
- } else if (type.equals("string")) {
+ } else if (castedType.getName().equals("string")) {
return TYPE_STRING;
} else {
return TYPE_OTHER;
@@ -431,10 +421,10 @@ class CastNode extends XPathSyntaxTreeNo
}
class FunctionNode extends XPathSyntaxTreeNode {
- private String name;
+ private QName name;
private XPathSyntaxTreeNode child;
- public FunctionNode(String name, XPathSyntaxTreeNode child) {
+ public FunctionNode(QName name, XPathSyntaxTreeNode child) {
this.name = name;
this.child = child;
}
Modified: xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20Parser.java
URL: http://svn.apache.org/viewvc/xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20Parser.java?rev=1162191&r1=1162190&r2=1162191&view=diff
==============================================================================
--- xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20Parser.java (original)
+++ xerces/java/branches/xml-schema-1.1-dev/src/org/apache/xerces/impl/xpath/XPath20Parser.java Fri Aug 26 18:09:02 2011
@@ -17,6 +17,9 @@
package org.apache.xerces.impl.xpath;
+import org.apache.xerces.xni.NamespaceContext;
+import org.apache.xerces.xni.QName;
+
/**
* Lexical analyzer and parser for test XPath expressions. This parser
* implementation constructs a syntax tree for valid test XPath expressions
@@ -89,6 +92,8 @@ public class XPath20Parser {
"\"\\n\"",
};
+ protected final NamespaceContext fNsContext;
+
public XPathSyntaxTreeNode parseExpression() throws XPathException {
return Test();
}
@@ -141,7 +146,7 @@ public class XPath20Parser {
private XPathSyntaxTreeNode BooleanExpr() throws XPathException {
XPathSyntaxTreeNode n1, n2;
- String s;
+ QName name;
int comp;
switch ((nextTokenIndex == -1) ? nextToken() : nextTokenIndex) {
case OPEN_PARAN:
@@ -151,11 +156,11 @@ public class XPath20Parser {
return n1;
case NCNAME:
- s = QName();
+ name = QName();
consumeToken(OPEN_PARAN);
n1 = OrExpr();
consumeToken(CLOSE_PARAN);
- return new FunctionNode(s, n1);
+ return new FunctionNode(name, n1);
case SYMBOL_AT:
case NUMERIC_LITERAL:
@@ -184,21 +189,26 @@ public class XPath20Parser {
}
}
- private String QName() throws XPathException {
+ private QName QName() throws XPathException {
Token t1, t2;
- String s;
+ QName name;
t1 = consumeToken(NCNAME);
- s = t1.image;
switch ((nextTokenIndex == -1) ? nextToken() : nextTokenIndex) {
case SYMBOL_COLON:
consumeToken(SYMBOL_COLON);
t2 = consumeToken(NCNAME);
- s += ":" + t2.image;
+ // TODO: better way to intern the strings
+ String prefix = t1.image.intern();
+ String local = t2.image.intern();
+ name = new QName(prefix, local, prefix + ':' + local, fNsContext.getURI(prefix));
break;
default:
+ // TODO: better way to intern the strings
+ local = t1.image.intern();
+ name = new QName("", local, local, null);
array1[4] = gen;
}
- return s;
+ return name;
}
private int Comparator() throws XPathException {
@@ -236,13 +246,13 @@ public class XPath20Parser {
private XPathSyntaxTreeNode CastExpr() throws XPathException {
XPathSyntaxTreeNode n;
- String s;
+ QName name;
n = SimpleValue();
switch ((nextTokenIndex == -1) ? nextToken() : nextTokenIndex) {
case KEYWORD_CAST:
consumeToken(KEYWORD_CAST);
consumeToken(KEYWORD_AS);
- s = QName();
+ name = QName();
switch ((nextTokenIndex == -1) ? nextToken() : nextTokenIndex) {
case SYMBOL_QUESTION:
consumeToken(SYMBOL_QUESTION);
@@ -250,7 +260,7 @@ public class XPath20Parser {
default:
array1[6] = gen;
}
- return new CastNode(n, s);
+ return new CastNode(n, name);
default:
array1[7] = gen;
@@ -278,10 +288,8 @@ public class XPath20Parser {
}
private XPathSyntaxTreeNode AttrName() throws XPathException {
- String t;
consumeToken(SYMBOL_AT);
- t = NameTest();
- return new AttrNode(t);
+ return new AttrNode(NameTest());
}
private XPathSyntaxTreeNode Literal() throws XPathException {
@@ -303,8 +311,8 @@ public class XPath20Parser {
}
}
- private String NameTest() throws XPathException {
- String t;
+ private QName NameTest() throws XPathException {
+ QName t;
t = QName();
return t;
}
@@ -323,7 +331,8 @@ public class XPath20Parser {
final private int[] array1 = new int[10];
/** Constructor. */
- public XPath20Parser(java.io.Reader stream) {
+ public XPath20Parser(java.io.Reader stream, NamespaceContext nsContext) {
+ fNsContext = nsContext;
inputStream = new SimpleCharStream(stream, 1, 1);
tokenSource = new XPath20ParserTokenManager(inputStream);
token = new Token();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org