[340933] - Migrate to new XPath2 API
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/DynamicContext.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/DynamicContext.java
index 7ac992e..68448e9 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/DynamicContext.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/DynamicContext.java
@@ -1,126 +1,128 @@
-/*******************************************************************************

- * Copyright (c) 2011, Jesper Steen Moller, and others

- * All rights reserved. This program and the accompanying materials

- * are made available under the terms of the Eclipse Public License v1.0

- * which accompanies this distribution, and is available at

- * http://www.eclipse.org/legal/epl-v10.html

- *

- * Contributors:

- *     Jesper Steen Moller - initial API and implementation

- *******************************************************************************/

-

-package org.eclipse.wst.xml.xpath2.api;

-

-import java.util.GregorianCalendar;

-import java.util.Map;

-

-import javax.xml.datatype.Duration;

-import javax.xml.namespace.QName;

-

-import org.w3c.dom.Node;

-

-/**

- * Interface for dynamic context. This covers the Dynamic Context as defined by the XPath2 specification, EXCEPT for 

- * context item and size (handled in EvaluationContext) 

- * @since 2.0

- */

-public interface DynamicContext {

-

-	public Node getLimitNode();

-

-	/**

-	 * Get variable.

-	 * 

-	 * @param name

-	 *            is the name of the variable.

-	 * @return variable.

-	 */

-	public ResultSequence getVariable(QName name);

-

-	/**

-	 * Resolve an URI

-	 * 

-	 * @param uri

-	 *            is the possibly relative URI to resolve

-	 * @return the absolutized, resolved URI.

-	 */

-	public String resolveUri(String uri);

-

-	/**

-	 * [Definition: Current dateTime. This

-	 * information represents an implementation-dependent point in time during

-	 * the processing of an expression, and includes an explicit timezone. It

-	 * can be retrieved by the fn:current-dateTime function. If invoked

-	 * multiple times during the execution of an expression, this function

-	 * always returns the same result.]

-     *

-	 * Returns the current date time using the GregorianCalendar.

-	 * 

-	 * @return The current date and time, which will always be same for the dynamic context.

-	 */

-	public GregorianCalendar getCurrentDateTime();

-	/**

-	 * [Definition: Implicit timezone. This

-	 * is the timezone to be used when a date, time, or dateTime value that

-	 * does not have a timezone is used in a comparison or arithmetic

-	 * operation. The implicit timezone is an implementation-defined value of

-	 * type xs:dayTimeDuration. See [XML Schema] for the range of legal values

-	 * of a timezone.]

-	 * 

-	 * @return current date time and implicit timezone.

-	 */

-	public Duration getTimezoneOffset();

-

-	/**

-	 * [Definition: Available documents. This is a mapping of

-	 * strings onto document nodes. The string represents the absolute URI of

-	 * a resource. The document node is the root of a tree that represents

-	 * that resource using the data model. The document node is returned by

-	 * the fn:doc function when applied to that URI.] The set of available

-	 * documents is not limited to the set of statically known documents, and

-	 * it may be empty. If there are one or more URIs in available documents

-	 * that map to a document node D, then the document-uri property of D must

-	 * either be absent, or must be one of these URIs. Note: This means that

-	 * given a document node $N, the result of fn:doc(fn:document-uri($N)) is

-	 * $N will always be True, unless fn:document-uri($N) is an empty

-	 * sequence.

-	 * 

-	 * @param uri

-	 *            is the URI of the document.

-	 * @return document.

-	 */

-	public org.w3c.dom.Document getDocument(String uri);

-	

-	/**

-	 * [Definition: Available collections. This is a mapping of

-	 * strings onto sequences of nodes. The string represents the absolute URI

-	 * of a resource. The sequence of nodes represents the result of the

-	 * fn:collection function when that URI is supplied as the argument. ] The

-	 * set of available collections is not limited to the set of statically

-	 * known collections, and it may be empty. For every document node D that

-	 * is in the target of a mapping in available collections, or that is the

-	 * root of a tree containing such a node, the document-uri property of D

-	 * must either be absent, or must be a URI U such that available documents

-	 * contains a mapping from U to D." Note: This means that for any document

-	 * node $N retrieved using the fn:collection function, either directly or

-	 * by navigating to the root of a node that was returned, the result of

-	 * fn:doc(fn:document-uri($N)) is $N will always be True, unless

-	 * fn:document-uri($N) is an empty sequence. This implies a requirement

-	 * for the fn:doc and fn:collection functions to be consistent in their

-	 * effect. If the implementation uses catalogs or user-supplied URI

-	 * resolvers to dereference URIs supplied to the fn:doc function, the

-	 * implementation of the fn:collection function must take these mechanisms

-	 * into account. For example, an implementation might achieve this by

-	 * mapping the collection URI to a set of document URIs, which are then

-	 * resolved using the same catalog or URI resolver that is used by the

-	 * fn:doc function.*/

-	public Map/*<String, List<Document>>*/ getCollections();

-	/**

-	 * [Definition: Default collection. This is the sequence

-	 * of nodes that would result from calling the fn:collection function with

-	 * no arguments.] The value of default collection may be initialized by

-	 * the implementation.

-	 */	

-	public ResultSequence/*<String, List<Document>>*/ getDefaultCollection();

-

-}

+/*******************************************************************************
+ * Copyright (c) 2011, Jesper Steen Moller, and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Jesper Steen Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
+ *******************************************************************************/
+
+package org.eclipse.wst.xml.xpath2.api;
+
+import java.net.URI;
+import java.util.GregorianCalendar;
+import java.util.Map;
+
+import javax.xml.datatype.Duration;
+import javax.xml.namespace.QName;
+
+import org.w3c.dom.Node;
+
+/**
+ * Interface for dynamic context. This covers the Dynamic Context as defined by the XPath2 specification, EXCEPT for 
+ * context item and size (handled in EvaluationContext) 
+ * @since 2.0
+ */
+public interface DynamicContext {
+
+	public Node getLimitNode();
+
+	/**
+	 * Get variable.
+	 * 
+	 * @param name
+	 *            is the name of the variable.
+	 * @return variable.
+	 */
+	public ResultSequence getVariable(QName name);
+
+	/**
+	 * Resolve an URI
+	 * 
+	 * @param uri
+	 *            is the possibly relative URI to resolve
+	 * @return the absolutized, resolved URI.
+	 */
+	public URI resolveUri(String uri);
+
+	/**
+	 * [Definition: Current dateTime. This
+	 * information represents an implementation-dependent point in time during
+	 * the processing of an expression, and includes an explicit timezone. It
+	 * can be retrieved by the fn:current-dateTime function. If invoked
+	 * multiple times during the execution of an expression, this function
+	 * always returns the same result.]
+     *
+	 * Returns the current date time using the GregorianCalendar.
+	 * 
+	 * @return The current date and time, which will always be same for the dynamic context.
+	 */
+	public GregorianCalendar getCurrentDateTime();
+	/**
+	 * [Definition: Implicit timezone. This
+	 * is the timezone to be used when a date, time, or dateTime value that
+	 * does not have a timezone is used in a comparison or arithmetic
+	 * operation. The implicit timezone is an implementation-defined value of
+	 * type xs:dayTimeDuration. See [XML Schema] for the range of legal values
+	 * of a timezone.]
+	 * 
+	 * @return current date time and implicit timezone.
+	 */
+	public Duration getTimezoneOffset();
+
+	/**
+	 * [Definition: Available documents. This is a mapping of
+	 * strings onto document nodes. The string represents the absolute URI of
+	 * a resource. The document node is the root of a tree that represents
+	 * that resource using the data model. The document node is returned by
+	 * the fn:doc function when applied to that URI.] The set of available
+	 * documents is not limited to the set of statically known documents, and
+	 * it may be empty. If there are one or more URIs in available documents
+	 * that map to a document node D, then the document-uri property of D must
+	 * either be absent, or must be one of these URIs. Note: This means that
+	 * given a document node $N, the result of fn:doc(fn:document-uri($N)) is
+	 * $N will always be True, unless fn:document-uri($N) is an empty
+	 * sequence.
+	 * 
+	 * @param uri
+	 *            is the URI of the document.
+	 * @return document.
+	 */
+	public org.w3c.dom.Document getDocument(URI uri);
+	
+	/**
+	 * [Definition: Available collections. This is a mapping of
+	 * strings onto sequences of nodes. The string represents the absolute URI
+	 * of a resource. The sequence of nodes represents the result of the
+	 * fn:collection function when that URI is supplied as the argument. ] The
+	 * set of available collections is not limited to the set of statically
+	 * known collections, and it may be empty. For every document node D that
+	 * is in the target of a mapping in available collections, or that is the
+	 * root of a tree containing such a node, the document-uri property of D
+	 * must either be absent, or must be a URI U such that available documents
+	 * contains a mapping from U to D." Note: This means that for any document
+	 * node $N retrieved using the fn:collection function, either directly or
+	 * by navigating to the root of a node that was returned, the result of
+	 * fn:doc(fn:document-uri($N)) is $N will always be True, unless
+	 * fn:document-uri($N) is an empty sequence. This implies a requirement
+	 * for the fn:doc and fn:collection functions to be consistent in their
+	 * effect. If the implementation uses catalogs or user-supplied URI
+	 * resolvers to dereference URIs supplied to the fn:doc function, the
+	 * implementation of the fn:collection function must take these mechanisms
+	 * into account. For example, an implementation might achieve this by
+	 * mapping the collection URI to a set of document URIs, which are then
+	 * resolved using the same catalog or URI resolver that is used by the
+	 * fn:doc function.*/
+	public Map/*<String, List<Document>>*/ getCollections();
+	/**
+	 * [Definition: Default collection. This is the sequence
+	 * of nodes that would result from calling the fn:collection function with
+	 * no arguments.] The value of default collection may be initialized by
+	 * the implementation.
+	 */	
+	public ResultSequence/*<String, List<Document>>*/ getDefaultCollection();
+
+}
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultBuffer.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultBuffer.java
index a67cb35..e78dc6d 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultBuffer.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultBuffer.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Jesper Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.api;
@@ -36,21 +37,19 @@
 		values.clear();
 	}
 	
-	public ResultBuffer with(Item at) {
+	public ResultBuffer add(Item at) {
 		values.add(at);
 		return this;
 	}
 
-	public void add(Item at) {
+	public ResultBuffer append(Item at) {
 		values.add(at);
+		return this;
 	}
 
-	public void append(Item at) {
-		values.add(at);
-	}
-
-	public void concat(ResultSequence rs) {
+	public ResultBuffer concat(ResultSequence rs) {
 		values.addAll(collectionWrapper(rs));
+		return this;
 	}
 
 	public static final class SingleResultSequence implements ResultSequence {
@@ -94,8 +93,8 @@
 		/* (non-Javadoc)
 		 * @see org.eclipse.wst.xml.xpath2.api.ResultSequence#first()
 		 */
-		public Item first() {
-			return value;
+		public Object firstValue() {
+			return value.getNativeValue();
 		}
 		
 		/* (non-Javadoc)
@@ -112,7 +111,7 @@
 				public final Object next() {
 					if (! seenIt) {
 						seenIt = true;
-						return SingleResultSequence.this;
+						return value;
 					}
 					throw new IllegalStateException("This iterator is at its end");
 				}
@@ -167,8 +166,8 @@
 		/* (non-Javadoc)
 		 * @see org.eclipse.wst.xml.xpath2.api.ResultSequence#first()
 		 */
-		public Item first() {
-			return item(0);
+		public Object firstValue() {
+			return item(0).getNativeValue();
 		}
 		
 		/* (non-Javadoc)
@@ -223,7 +222,7 @@
 
 	private Collection collectionWrapper(final ResultSequence rs) {
 		// This is a dummy collections, solely exists for faster inserts into our array
-		return new Collection() {
+		return new Collection/*<Item>*/() {
 
 			public boolean add(Object arg0) {
 				return false;
@@ -248,7 +247,7 @@
 				return rs.empty();
 			}
 
-			public Iterator iterator() {
+			public Iterator/*<Item>*/ iterator() {
 				return rs.iterator();
 			}
 
@@ -269,16 +268,21 @@
 			}
 
 			public Object[] toArray() {
-				return null;
+				return toArray(new Item[size()]);
 			}
 
 			public Object[] toArray(Object[] arg0) {
-				return null;
+				if (arg0.length < size())
+					arg0 = new Item[size()];
+				for (int i = 0; i< size(); ++i) {
+					arg0[i] = rs.item(i);
+				}
+				return arg0;
 			}
 		};
 	}
 	
-	final static ResultSequence EMPTY = new ResultSequence() {
+	public final static ResultSequence EMPTY = new ResultSequence() {
 
 		public int size() {
 			return 0;
@@ -304,7 +308,7 @@
 			throw new IndexOutOfBoundsException("Sequence is empty!");
 		}
 		
-		public Item first() {
+		public Object firstValue() {
 			throw new IndexOutOfBoundsException("Sequence is empty!");
 		}
 		
@@ -326,4 +330,13 @@
 		}
 	};
 
+	public Collection/*<Item>*/ getCollection() {
+		return this.values;
+	}
+
+	public ResultBuffer concat(Collection/*<Item>*/ others) {
+		this.values.addAll(others);
+		return this;
+	}
+
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultSequence.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultSequence.java
index 2f6fa36..a3bf822 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultSequence.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/api/ResultSequence.java
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Jesper Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.api;
@@ -51,7 +52,7 @@
 	 * 
 	 * @return Native object representing the first item.
 	 */
-	Object first();
+	Object firstValue();
 
 	/**
 	 * XPath2 type definition description of the item at location '0'
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator.java
index deadfbb..00988cd 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator.java
@@ -141,6 +141,7 @@
 import org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger;
+import org.eclipse.wst.xml.xpath2.processor.util.ResultSequenceUtil;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -543,9 +544,13 @@
 	 * @return result sequence.
 	 */
 	public ResultSequence evaluate(XPathNode node) {
-		return (ResultSequence) node.accept(this);
+		return ResultSequenceUtil.newToOld((ResultSequence) node.accept(this));
 	}
 
+	public org.eclipse.wst.xml.xpath2.api.ResultSequence evaluate2(XPath node) {
+		return (org.eclipse.wst.xml.xpath2.api.ResultSequence) node.accept(this);
+	}
+	
 	// basically the comma operator...
 	private ResultSequence do_expr(Iterator i) {
 
@@ -580,17 +585,12 @@
 		return rs;
 	}
 
-	// XXX ugly
-	// type: 0 = for [return == "correct"]
-	// 1 = for all [return false, return empty on true]
-	// 2 = there exists [return true, return empty on false]
-	private ResultSequence do_for_quantified_expr(ListIterator iter,
-			Expr finalexpr, int type) {
+	private void do_for_each(ListIterator iter,
+			Expr finalexpr, ResultSequence destination) {
 
 		// we have more vars to bind...
 		if (iter.hasNext()) {
 			boolean allocated_var = false;
-			ResultSequence result = ResultSequenceFactory.create_new();
 			VarExprPair ve = (VarExprPair) iter.next();
 
 			// evaluate binding sequence
@@ -599,7 +599,7 @@
 			// XXX
 			if (rs.empty()) {
 				iter.previous();
-				return result;
+				return;
 			}
 
 			QName varname = ve.varname();
@@ -615,47 +615,8 @@
 				allocated_var = true;
 
 				_dc.new_scope();
-				ResultSequence res = do_for_quantified_expr(iter, finalexpr,
-						type);
+				do_for_each(iter, finalexpr, destination);
 				_dc.destroy_scope();
-				assert res != null;
-
-				// ok here we got a "real" result, now figure
-				// out what to do with it
-				XSBoolean effbool = null;
-				switch (type) {
-				// for expression
-				case 0:
-					result.concat(res);
-					break;
-
-				// we need the effective boolean value
-				// of the expression
-				case 1:
-				case 2:
-					effbool = effective_boolean_value(res);
-					break;
-
-				default:
-					assert false;
-				}
-
-				// we got a quantified expression
-				if (effbool != null) {
-					// for all
-					if (type == 1) {
-						result = ResultSequenceFactory.create_new(effbool);
-						if (!effbool.value())
-							break;
-					}
-					// there exists
-					else if (type == 2) {
-						result = ResultSequenceFactory.create_new(effbool);
-						if (effbool.value())
-							break;
-					} else
-						assert false;
-				}
 			}
 
 			if (allocated_var) {
@@ -664,11 +625,113 @@
 			}
 
 			iter.previous();
-			return result;
 		}
 		// we finally got to do the "last expression"
 		else {
-			return (ResultSequence) finalexpr.accept(this);
+			destination.concat((ResultSequence) finalexpr.accept(this));
+		}
+	}
+
+
+	// XXX ugly
+	// type: 0 = for [return == "correct"]
+	// 1 = for all [return false, return empty on true]
+	// 2 = there exists [return true, return empty on false]
+	private XSBoolean do_for_all(ListIterator iter,
+			Expr finalexpr) {
+
+		// we have more vars to bind...
+		if (iter.hasNext()) {
+			boolean allocated_var = false;
+			VarExprPair ve = (VarExprPair) iter.next();
+
+			// evaluate binding sequence
+			ResultSequence rs = (ResultSequence) ve.expr().accept(this);
+
+			QName varname = ve.varname();
+
+			// for each item of binding sequence, bind the range
+			// variable and check the predicate
+
+			try {
+				for (Iterator i = rs.iterator(); i.hasNext();) {
+					AnyType item = (AnyType) i.next();
+	
+					_dc.set_variable(varname, item);
+					allocated_var = true;
+	
+					_dc.new_scope();
+					XSBoolean effbool = do_for_all(iter, finalexpr);
+					_dc.destroy_scope();
+	
+					// ok here we got a "real" result, now figure
+					// out what to do with it
+					if (!effbool.value())
+						return XSBoolean.FALSE;
+				}
+			} finally {
+				if (allocated_var) {
+					boolean del = _dc.del_variable(varname);
+					assert del == true;
+				}
+				iter.previous();
+			}
+			return XSBoolean.TRUE;
+		}
+		// we finally got to do the "last expression"
+		else {
+			return effective_boolean_value((ResultSequence) finalexpr.accept(this));
+		}
+
+	}
+
+	private XSBoolean do_exists(ListIterator iter,
+			Expr finalexpr) {
+
+		// we have more vars to bind...
+		if (iter.hasNext()) {
+			boolean allocated_var = false;
+			VarExprPair ve = (VarExprPair) iter.next();
+
+			// evaluate binding sequence
+			ResultSequence rs = (ResultSequence) ve.expr().accept(this);
+
+			QName varname = ve.varname();
+
+			// for each item of binding sequence, bind the range
+			// variable and check the expression
+
+			try {
+				for (Iterator i = rs.iterator(); i.hasNext();) {
+					AnyType item = (AnyType) i.next();
+	
+					_dc.set_variable(varname, item);
+					allocated_var = true;
+	
+					_dc.new_scope();
+					XSBoolean effbool = do_exists(iter, finalexpr);
+					_dc.destroy_scope();
+					assert effbool != null;
+	
+					// ok here we got a "real" result, now figure
+					// out what to do with it
+					if (effbool.value())
+						return XSBoolean.TRUE;
+				}
+			} finally {
+				if (allocated_var) {
+					boolean del = _dc.del_variable(varname);
+					assert del == true;
+				}
+				iter.previous();
+			}
+			
+			// since none in this sequence evaluated to true, return false
+			return XSBoolean.FALSE;
+		}
+		// we finally got to do the "last expression"
+		else {
+			return effective_boolean_value((ResultSequence) finalexpr.accept(this));
 		}
 
 	}
@@ -683,7 +746,9 @@
 	public Object visit(ForExpr fex) {
 		// XXX
 		List pairs = new ArrayList(fex.ve_pairs());
-		return do_for_quantified_expr(pairs.listIterator(), fex.expr(), 0);
+		ResultSequence dest = ResultSequenceFactory.create_new();
+		do_for_each(pairs.listIterator(), fex.expr(), dest);
+		return dest;
 	}
 
 	/**
@@ -697,33 +762,15 @@
 		// XXX
 		List pairs = new ArrayList(qex.ve_pairs());
 
-		int hack = 0;
-
 		switch (qex.type()) {
 		case QuantifiedExpr.SOME:
-			hack = 2;
-			break;
-		case QuantifiedExpr.ALL:
-			hack = 1;
-			break;
-
-		default:
-			assert false;
-			return null; // unreach
+		{
+			return ResultSequenceFactory.create_new(do_exists(pairs.listIterator(), qex.expr()));
 		}
-		ResultSequence rs = do_for_quantified_expr(pairs.listIterator(), qex
-				.expr(), hack);
-
-		// make sure we found answer
-		if (!rs.empty())
-			return rs;
-
-		// ok because all of this is a hack... here we go
-		switch (qex.type()) {
-		case QuantifiedExpr.SOME:
-			return ResultSequenceFactory.create_new(new XSBoolean(false));
 		case QuantifiedExpr.ALL:
-			return ResultSequenceFactory.create_new(new XSBoolean(true));
+		{
+			return ResultSequenceFactory.create_new(do_for_all(pairs.listIterator(), qex.expr()));
+		}
 		default:
 			assert false;
 			return null; // unreach
@@ -2347,4 +2394,5 @@
 		_dc.set_focus(original_focus);
 		return rs;
 	}
+
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator2.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator2.java
new file mode 100644
index 0000000..3d48d98
--- /dev/null
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/DefaultEvaluator2.java
@@ -0,0 +1,2324 @@
+/*******************************************************************************
+ * Copyright (c) 2011, Jesper Steen Moller, and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Jesper Steen Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
+ *******************************************************************************/
+
+package org.eclipse.wst.xml.xpath2.processor;
+
+import static org.eclipse.wst.xml.xpath2.processor.util.ResultSequenceUtil.newToOld;
+
+import java.math.BigInteger;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.GregorianCalendar;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+
+import javax.xml.XMLConstants;
+import javax.xml.datatype.Duration;
+
+import org.eclipse.wst.xml.xpath2.api.EvaluationContext;
+import org.eclipse.wst.xml.xpath2.api.Function;
+import org.eclipse.wst.xml.xpath2.api.Item;
+import org.eclipse.wst.xml.xpath2.api.ResultBuffer;
+import org.eclipse.wst.xml.xpath2.api.ResultSequence;
+import org.eclipse.wst.xml.xpath2.api.StaticContext;
+import org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition;
+import org.eclipse.wst.xml.xpath2.api.typesystem.TypeModel;
+import org.eclipse.wst.xml.xpath2.processor.ast.XPath;
+import org.eclipse.wst.xml.xpath2.processor.internal.Axis;
+import org.eclipse.wst.xml.xpath2.processor.internal.DescendantOrSelfAxis;
+import org.eclipse.wst.xml.xpath2.processor.internal.Focus;
+import org.eclipse.wst.xml.xpath2.processor.internal.ForwardAxis;
+import org.eclipse.wst.xml.xpath2.processor.internal.ParentAxis;
+import org.eclipse.wst.xml.xpath2.processor.internal.ReverseAxis;
+import org.eclipse.wst.xml.xpath2.processor.internal.SelfAxis;
+import org.eclipse.wst.xml.xpath2.processor.internal.SeqType;
+import org.eclipse.wst.xml.xpath2.processor.internal.StaticNameError;
+import org.eclipse.wst.xml.xpath2.processor.internal.StaticTypeNameError;
+import org.eclipse.wst.xml.xpath2.processor.internal.TypeError;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.AddExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.AndExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.AnyKindTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.AttributeTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.AxisStep;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.BinExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.CastExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.CastableExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.CmpExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.CntxItemExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.CommentTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.DecimalLiteral;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.DivExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.DocumentTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.DoubleLiteral;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ElementTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ExceptExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.Expr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.FilterExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ForExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ForwardStep;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.FunctionCall;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.IDivExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.IfExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.InstOfExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.IntegerLiteral;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.IntersectExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ItemType;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.MinusExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ModExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.MulExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.NameTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.OrExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.PITest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ParExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.PipeExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.PlusExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.QuantifiedExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.RangeExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.ReverseStep;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.SchemaAttrTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.SchemaElemTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.SequenceType;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.SingleType;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.StepExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.StringLiteral;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.SubExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.TextTest;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.TreatAsExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.UnionExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.VarExprPair;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.VarRef;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathExpr;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathNode;
+import org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathVisitor;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.ConstructorFL;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FnBoolean;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FnData;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FnRoot;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsDiv;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsEq;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsGe;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsGt;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsIDiv;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsLe;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsLt;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsMinus;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsMod;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsNe;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsPlus;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FsTimes;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FunctionLibrary;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.OpExcept;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.OpIntersect;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.OpTo;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.OpUnion;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyAtomicType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.AttrType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.CommentType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.DocType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.ElementType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.NodeType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.NumericType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.PIType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.QName;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.TextType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSAnyURI;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDayTimeDuration;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSDuration;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSInteger;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.builtin.BuiltinTypeLibrary;
+import org.eclipse.wst.xml.xpath2.processor.util.ResultSequenceUtil;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * Default evaluator interface
+ */
+public class DefaultEvaluator2 implements XPathVisitor, Evaluator {
+
+	private static final String XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema";
+
+	private static final QName ANY_ATOMIC_TYPE = new QName("xs",
+			"anyAtomicType", XML_SCHEMA_NS);
+
+	private DynamicContext _legacyDc;
+	private org.eclipse.wst.xml.xpath2.api.DynamicContext _dc;
+	private XPathException _err;
+
+	// this is a parameter that may be set on a call...
+	// the parameter may become invalid on the next call... i.e. the
+	// previous parameter is not saved... so use with care! [remember...
+	// this thing is highly recursive]
+	private Object _param;
+	private EvaluationContext _ec;
+
+	private StaticContext _sc;
+
+	private Focus _focus = new Focus(ResultBuffer.EMPTY);
+
+	Focus focus() { return _focus ; }
+	
+	void set_focus(Focus f) { _focus = f; }
+
+	static class Pair {
+		public Object _one;
+		public Object _two;
+
+		public Pair(Object o, Object t) {
+			_one = o;
+			_two = t;
+		}
+	}
+
+	private void popScope() {
+		if (_innerScope == null) throw new IllegalStateException("Unmatched scope pop");
+		_innerScope = _innerScope.nextScope;
+	}
+
+	private QName resolve(QName var) {
+		String ns = _sc.getNamespaceContext().getNamespaceURI(var.prefix());
+		QName qName = new QName(null, var.local(), ns);
+
+		return qName;
+	}
+
+	private void pushScope(QName var, org.eclipse.wst.xml.xpath2.api.ResultSequence value) {
+		_innerScope = new VariableScope(resolve(var), value, _innerScope);		
+	}
+
+	/**
+	 * @since 2.0
+	 */
+	public DefaultEvaluator2(org.eclipse.wst.xml.xpath2.api.StaticContext staticContext, org.eclipse.wst.xml.xpath2.api.DynamicContext dynamicContext, Object[] contextItems) {
+		_sc = staticContext;
+		_dc = dynamicContext;
+		_legacyDc = new DynamicContext() {
+
+			public boolean xpath1_compatible() {
+				return false;
+			}
+
+			public boolean prefix_exists(String prefix) {
+				return ! XMLConstants.NULL_NS_URI.equals(_sc.getNamespaceContext().getNamespaceURI(prefix));
+			}
+
+			public String resolve_prefix(String prefix) {
+				return _sc.getNamespaceContext().getNamespaceURI(prefix);
+			}
+
+			public String default_namespace() {
+				return _sc.getDefaultNamespace();
+			}
+
+			public String default_function_namespace() {
+				return _sc.getDefaultFunctionNamespace();
+			}
+
+			public TypeDefinition attribute_type_definition(QName attr) {
+				return _sc.getTypeModel().lookupAttributeDeclaration(attr.namespace(), attr.local());
+			}
+
+			public TypeDefinition element_type_definition(QName elem) {
+				return _sc.getTypeModel().lookupElementDeclaration(elem.namespace(), elem.local());
+			}
+
+			public boolean attribute_declared(QName attr) {
+				return attribute_type_definition(attr) != null;
+			}
+
+			public boolean element_declared(QName elem) {
+				return element_type_definition(elem) != null;
+			}
+
+			public boolean function_exists(QName name, int arity) {
+				return _sc.resolveFunction(name.asQName(), arity) != null;
+			}
+
+			public XSAnyURI base_uri() {
+				return new XSAnyURI(_sc.getBaseUri().toString());
+			}
+
+			public void new_scope() {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public void destroy_scope() {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public void add_variable(QName name) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public boolean del_variable(QName name) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public boolean variable_exists(QName name) {
+				return _sc.getInScopeVariables().isVariablePresent(name.asQName());
+			}
+
+			public boolean variable_in_scope(QName var) {
+				return _sc.getInScopeVariables().isVariablePresent(var.asQName());
+			}
+
+			public boolean type_defined(QName name) {
+				// TODO Auto-generated method stub
+				return false;
+			}
+
+			public boolean derives_from(NodeType at, QName et) {
+				// TODO Auto-generated method stub
+				return false;
+			}
+
+			public boolean derives_from(NodeType at, TypeDefinition et) {
+				// TODO Auto-generated method stub
+				return false;
+			}
+
+			public void add_namespace(String prefix, String ns) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public boolean expand_function_qname(QName name) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public boolean expand_elem_type_qname(QName name) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public boolean expand_qname(QName name) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public void add_function_library(FunctionLibrary fl) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public AnyAtomicType make_atomic(QName name) {
+				String ns = name.namespace();
+				for (Object fl : _sc.getFunctionLibraries()) {
+					if (fl instanceof ConstructorFL) {
+						ConstructorFL cfl = (ConstructorFL)fl;
+						return cfl.atomic_type(name);
+					}
+				}
+				return null;
+			}
+
+			public void set_base_uri(String baseuri) {
+				throw new UnsupportedOperationException("Don't");
+			}
+
+			public Map get_collections() {
+				return _dc.getCollections();
+			}
+
+			public void set_collections(Map collections) {
+			}
+
+			public TypeModel getTypeModel(Node element) {
+				return _sc.getTypeModel();
+			}
+
+			public AnyType context_item() {
+				return _focus.context_item();
+			}
+
+			public int context_position() {
+				return _focus.position();
+			}
+
+			public int last() {
+				return _focus.last();
+			}
+
+			public Object get_variable(QName name) {
+				return getVariable(name);
+			}
+
+			public void set_variable(QName var, AnyType val) {
+				pushScope(var, (org.eclipse.wst.xml.xpath2.api.ResultSequence)val);
+			}
+
+			public org.eclipse.wst.xml.xpath2.processor.ResultSequence evaluate_function(QName name, Collection args) throws DynamicError {
+				throw new UnsupportedOperationException("Use new DynamicContext instead");
+			}
+
+			public XSDuration tz() {
+				Duration d = _dc.getTimezoneOffset();
+				return new XSDayTimeDuration(0, d.getHours(), d.getMinutes(), 0.0, d.getSign() == -1);
+			}
+
+			public org.eclipse.wst.xml.xpath2.processor.ResultSequence get_doc(URI uri) {
+				Document document = _dc.getDocument(uri);
+				if (document == null) return ResultSequenceFactory.create_new();
+				return newToOld(new DocType(document, _sc.getTypeModel()));
+			}
+
+			public URI resolve_uri(String uri) {
+				return _dc.resolveUri(uri);
+			}
+
+			public GregorianCalendar current_date_time() {
+				return _dc.getCurrentDateTime();
+			}
+
+			public void set_focus(Focus focus) {
+				_focus = focus;
+			}
+
+			public Focus focus() {
+				return _focus;
+			}
+
+			public Comparator get_collation(String uri) {
+				return _sc.getCollationProvider().getCollation(uri);
+			}
+
+			public String default_collation_name() {
+				return _sc.getCollationProvider().getDefaultCollation();
+			}
+
+			public int node_position(Node node) {
+				return 0;
+			}
+
+			public void set_variable(QName var, org.eclipse.wst.xml.xpath2.processor.ResultSequence val) {
+				// TODO Auto-generated method stub
+				
+			}
+			
+		};
+		_ec = new EvaluationContext() {
+			
+			public org.eclipse.wst.xml.xpath2.api.DynamicContext getDynamicContext() {
+				return _dc;
+			}
+			
+			public AnyType getContextItem() {
+				return _focus.context_item();
+			}
+			
+			public int getContextPosition() {
+				return _focus.position();
+			}
+
+			public int getLastPosition() {
+				return _focus.last();
+			}
+			
+			public StaticContext getStaticContext() {
+				return _sc;
+			}
+		};
+		
+
+		_err = null;
+
+		// initialize context item with root of document
+		ResultBuffer rs = new ResultBuffer();
+		for (Object obj : contextItems) {
+			if (obj instanceof Node) rs.add(NodeType.dom_to_xpath((Node)obj, _sc.getTypeModel()));
+		}
+
+		set_focus(new Focus(rs.getSequence()));
+		_param = null;
+	}
+
+
+	class VariableScope {
+		public VariableScope(QName name, org.eclipse.wst.xml.xpath2.api.ResultSequence value, VariableScope nextScope) {
+			this.name = name;
+			this.value = value;
+			this.nextScope = nextScope;
+		}
+		final public QName name;
+		final public org.eclipse.wst.xml.xpath2.api.ResultSequence value;
+		final public VariableScope nextScope; 
+	}	
+	
+	private VariableScope _innerScope = null;
+
+	private org.eclipse.wst.xml.xpath2.api.ResultSequence getVariable(QName name) {
+		// First, try local scopes
+		VariableScope scope = _innerScope;
+		while (scope != null) {
+			if (name.equals(scope.name)) return scope.value;
+			scope = scope.nextScope;
+		}
+		return (org.eclipse.wst.xml.xpath2.api.ResultSequence) _dc.getVariable(name.asQName());
+	}
+
+	// XXX this kinda sux
+	// the problem is that visistor interface does not throw exceptions...
+	// so we get around it ;D
+	private void report_error(DynamicError err) {
+		throw err;
+	}
+
+	private void report_error(TypeError err) {
+		throw new DynamicError(err);
+	}
+
+	private void report_error(StaticNameError err) {
+		throw err;
+	}
+
+	/**
+	 * evaluate the xpath node
+	 * 
+	 * @param node
+	 *            is the xpath node.
+	 * @throws dynamic
+	 *             error.
+	 * @return result sequence.
+	 */
+	public org.eclipse.wst.xml.xpath2.processor.ResultSequence evaluate(XPathNode node) {
+		return ResultSequenceUtil.newToOld(evaluate2(node));
+	}
+
+	public ResultSequence evaluate2(XPathNode node) {
+		fixFunctions();
+		
+		return (org.eclipse.wst.xml.xpath2.api.ResultSequence) node.accept(this);
+	}
+	
+	private void fixFunctions() {
+		for (Object obj : _sc.getFunctionLibraries()) {
+			if (obj instanceof FunctionLibrary) {
+				FunctionLibrary fl = (FunctionLibrary)obj;
+				fl.set_dynamic_context(_legacyDc);
+				fl.set_static_context(_legacyDc);
+			}
+		}
+	}
+
+	// basically the comma operator...
+	private ResultSequence do_expr(Iterator i) {
+
+		ResultSequence rs = null;
+		ResultBuffer buffer = null;
+
+		while (i.hasNext()) {
+			Expr e = (Expr) i.next();
+
+			ResultSequence result = (ResultSequence) e.accept(this);
+
+			if (rs == null && buffer == null)
+				rs = result;
+			else {
+				if (buffer == null) {
+					buffer = new ResultBuffer();
+					buffer.concat(rs);
+					rs = null;
+				}
+				buffer.concat(result);
+			}
+		}
+
+		if (buffer != null) {
+			return buffer.getSequence();
+		} else if (rs != null) {
+			return rs;
+		} else return ResultBuffer.EMPTY;
+	}
+
+	/**
+	 * iterate through xpath expression
+	 * 
+	 * @param xp
+	 *            is the xpath.
+	 * @return result sequence.
+	 */
+	public Object visit(XPath xp) {
+		ResultSequence rs = do_expr(xp.iterator());
+
+		return rs;
+	}
+
+	private void do_for_each(ListIterator iter,
+			Expr finalexpr, ResultBuffer destination) {
+
+		// we have more vars to bind...
+		if (iter.hasNext()) {
+			VarExprPair ve = (VarExprPair) iter.next();
+
+			// evaluate binding sequence
+			ResultSequence rs = (ResultSequence) ve.expr().accept(this);
+
+			// XXX
+			if (rs.empty()) {
+				iter.previous();
+				return;
+			}
+
+			QName varname = ve.varname();
+
+			// for each item of binding sequence, bind the range
+			// variable and do the expression, concatenating the
+			// result
+
+			for (Iterator i = rs.iterator(); i.hasNext();) {
+				AnyType item = (AnyType) i.next();
+
+				pushScope(varname, item);
+				do_for_each(iter, finalexpr, destination);
+				popScope();
+			}
+			iter.previous();
+		}
+		// we finally got to do the "last expression"
+		else {
+			destination.concat((ResultSequence) finalexpr.accept(this));
+		}
+	}
+
+
+	// XXX ugly
+	// type: 0 = for [return == "correct"]
+	// 1 = for all [return false, return empty on true]
+	// 2 = there exists [return true, return empty on false]
+	private XSBoolean do_for_all(ListIterator iter,
+			Expr finalexpr) {
+
+		// we have more vars to bind...
+		if (iter.hasNext()) {
+			VarExprPair ve = (VarExprPair) iter.next();
+
+			// evaluate binding sequence
+			ResultSequence rs = (ResultSequence) ve.expr().accept(this);
+
+			QName varname = ve.varname();
+
+			// for each item of binding sequence, bind the range
+			// variable and check the predicate
+
+			try {
+				for (Iterator i = rs.iterator(); i.hasNext();) {
+					AnyType item = (AnyType) i.next();
+	
+					pushScope(varname, item);
+					XSBoolean effbool = do_for_all(iter, finalexpr);
+					popScope();
+					
+					// ok here we got a "real" result, now figure
+					// out what to do with it
+					if (!effbool.value())
+						return XSBoolean.FALSE;
+				}
+			} finally {
+				iter.previous();
+			}
+			return XSBoolean.TRUE;
+		}
+		// we finally got to do the "last expression"
+		else {
+			return effective_boolean_value((ResultSequence) finalexpr.accept(this));
+		}
+
+	}
+
+	private XSBoolean do_exists(ListIterator iter,
+			Expr finalexpr) {
+
+		// we have more vars to bind...
+		if (iter.hasNext()) {
+			VarExprPair ve = (VarExprPair) iter.next();
+
+			// evaluate binding sequence
+			ResultSequence rs = (ResultSequence) ve.expr().accept(this);
+
+			QName varname = ve.varname();
+
+			// for each item of binding sequence, bind the range
+			// variable and check the expression
+
+			try {
+				for (Iterator i = rs.iterator(); i.hasNext();) {
+					AnyType item = (AnyType) i.next();
+	
+					pushScope(varname, item);
+					XSBoolean effbool = do_exists(iter, finalexpr);
+					popScope();
+	
+					// ok here we got a "real" result, now figure
+					// out what to do with it
+					if (effbool.value())
+						return XSBoolean.TRUE;
+				}
+			} finally {
+				iter.previous();
+			}
+			
+			// since none in this sequence evaluated to true, return false
+			return XSBoolean.FALSE;
+		}
+		// we finally got to do the "last expression"
+		else {
+			return effective_boolean_value((ResultSequence) finalexpr.accept(this));
+		}
+
+	}
+
+	/**
+	 * visit for expression
+	 * 
+	 * @param fex
+	 *            is the for expression.
+	 * @return a new function.
+	 */
+	public Object visit(ForExpr fex) {
+		// XXX
+		List pairs = new ArrayList(fex.ve_pairs());
+		ResultBuffer rb = new ResultBuffer(); 
+		do_for_each(pairs.listIterator(), fex.expr(), rb);
+		return rb.getSequence();
+	}
+
+	/**
+	 * visit quantified expression
+	 * 
+	 * @param qex
+	 *            is the quantified expression.
+	 * @return a new function or null.
+	 */
+	public Object visit(QuantifiedExpr qex) {
+		// XXX
+		List pairs = new ArrayList(qex.ve_pairs());
+
+		int hack = 0;
+
+		switch (qex.type()) {
+		case QuantifiedExpr.SOME:
+			return do_exists(pairs.listIterator(), qex.expr());
+		case QuantifiedExpr.ALL:
+			return do_for_all(pairs.listIterator(), qex.expr());
+
+		default:
+			assert false;
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit if expression
+	 * 
+	 * @param ifex
+	 *            is the if expression.
+	 * @return a ifex.then_clause().accept(this).
+	 */
+	public Object visit(IfExpr ifex) {
+		ResultSequence test_res = do_expr(ifex.iterator());
+
+		XSBoolean res = effective_boolean_value(test_res);
+
+		if (res.value())
+			return ifex.then_clause().accept(this);
+		else
+			return ifex.else_clause().accept(this);
+	}
+
+	private boolean[] do_logic_exp(BinExpr e) {
+		Collection args = do_bin_args(e);
+
+		Iterator argiter = args.iterator();
+
+		ResultSequence one = (ResultSequence) argiter.next();
+		ResultSequence two = (ResultSequence) argiter.next();
+
+		boolean oneb = effective_boolean_value(one).value();
+		boolean twob = effective_boolean_value(two).value();
+
+		boolean res[] = { oneb, twob };
+		return res;
+	}
+
+	/**
+	 * visit or expression
+	 * 
+	 * @param orex
+	 *            is the or expression.
+	 * @return a new function
+	 */
+	public Object visit(OrExpr orex) {
+		boolean res[] = do_logic_exp(orex);
+
+		return ResultSequenceFactory
+				.create_new(new XSBoolean(res[0] || res[1]));
+	}
+
+	/**
+	 * visit and expression
+	 * 
+	 * @param andex
+	 *            is the and expression.
+	 * @return a new function
+	 */
+	public Object visit(AndExpr andex) {
+		boolean res[] = do_logic_exp(andex);
+
+		return ResultSequenceFactory
+				.create_new(new XSBoolean(res[0] && res[1]));
+	}
+
+	private ResultSequence node_cmp(int type, Collection args) {
+		assert args.size() == 2;
+
+		Iterator argsiter = args.iterator();
+
+		ResultSequence one = (ResultSequence) argsiter.next();
+		ResultSequence two = (ResultSequence) argsiter.next();
+
+		int size_one = one.size();
+		int size_two = two.size();
+
+		if (size_one > 1 || size_two > 1)
+			report_error(TypeError.invalid_type(null));
+
+		if (size_one == 0 || size_two == 0)
+			return ResultBuffer.EMPTY;
+
+		Item at_one = one.item(0);
+		Item at_two = two.item(0);
+
+		if (!(at_one instanceof NodeType) || !(at_two instanceof NodeType))
+			report_error(TypeError.invalid_type(null));
+
+		// ok we got the args finally
+		NodeType nt_one = (NodeType) at_one;
+		NodeType nt_two = (NodeType) at_two;
+
+		boolean answer = false; // we are pessimistic as usual
+
+		// do comparison
+		switch (type) {
+		case CmpExpr.IS:
+			answer = nt_one.node_value() == nt_two.node_value();
+			break;
+
+		case CmpExpr.LESS_LESS:
+			answer = nt_one.before(nt_two);
+			break;
+
+		case CmpExpr.GREATER_GREATER:
+			answer = nt_one.after(nt_two);
+			break;
+
+		default:
+			assert false;
+		}
+
+		return XSBoolean.valueOf(answer);
+	}
+
+	/**
+	 * visit compare expression
+	 * 
+	 * @param cmpex
+	 *            is the compare expression.
+	 * @return a new function or null
+	 */
+	public Object visit(CmpExpr cmpex) {
+		try {
+			Collection args = do_bin_args(cmpex);
+
+			switch (cmpex.type()) {
+			case CmpExpr.EQ:
+				return FsEq.fs_eq_value(args, _legacyDc);
+
+			case CmpExpr.NE:
+				return FsNe.fs_ne_value(args, _legacyDc);
+
+			case CmpExpr.GT:
+				return FsGt.fs_gt_value(args, _legacyDc);
+
+			case CmpExpr.LT:
+				return FsLt.fs_lt_value(args, _legacyDc);
+
+			case CmpExpr.GE:
+				return FsGe.fs_ge_value(args, _legacyDc);
+
+			case CmpExpr.LE:
+				return FsLe.fs_le_value(args, _legacyDc);
+
+			case CmpExpr.EQUALS:
+				return FsEq.fs_eq_general(args, _legacyDc);
+
+			case CmpExpr.NOTEQUALS:
+				return FsNe.fs_ne_general(args, _legacyDc);
+
+			case CmpExpr.GREATER:
+				return FsGt.fs_gt_general(args, _legacyDc);
+
+			case CmpExpr.LESSTHAN:
+				return FsLt.fs_lt_general(args, _legacyDc);
+
+			case CmpExpr.GREATEREQUAL:
+				return FsGe.fs_ge_general(args, _legacyDc);
+
+			case CmpExpr.LESSEQUAL:
+				return FsLe.fs_le_general(args, _legacyDc);
+
+			case CmpExpr.IS:
+			case CmpExpr.LESS_LESS:
+			case CmpExpr.GREATER_GREATER:
+				return node_cmp(cmpex.type(), args);
+
+			default:
+				assert false;
+			}
+		} catch (DynamicError err) {
+			report_error(err);
+		}
+		return null; // unreach
+	}
+
+	/**
+	 * visit range expression
+	 * 
+	 * @param rex
+	 *            is the range expression.
+	 * @return a new function
+	 */
+	public Object visit(RangeExpr rex) {
+		ResultSequence one = (ResultSequence) rex.left().accept(this);
+		ResultSequence two = (ResultSequence) rex.right().accept(this);
+		if (one.empty() || two.empty()) return ResultSequenceFactory.create_new(); 
+		Collection args = new ArrayList();
+		args.add(one);
+		args.add(two);
+
+		try {
+			return OpTo.op_to(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	private XSBoolean effective_boolean_value(ResultSequence rs) {
+		try {
+			return FnBoolean.fn_boolean(ResultSequenceUtil.newToOld(rs));
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	
+	/**
+	 * visit and expression
+	 * 
+	 * @param addex
+	 *            is the and expression.
+	 * @return a new function
+	 */
+	public Object visit(AddExpr addex) {
+		try {
+			Collection args = do_bin_args(addex);
+			return FsPlus.fs_plus(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit sub expression
+	 * 
+	 * @param subex
+	 *            is the sub expression.
+	 * @return a new function
+	 */
+	public Object visit(SubExpr subex) {
+		try {
+			Collection args = do_bin_args(subex);
+			return FsMinus.fs_minus(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit multiply expression
+	 * 
+	 * @param mulex
+	 *            is the mul expression.
+	 * @return a new function
+	 */
+	public Object visit(MulExpr mulex) {
+		try {
+			Collection args = do_bin_args(mulex);
+			return FsTimes.fs_times(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit division expression
+	 * 
+	 * @param mulex
+	 *            is the division expression.
+	 * @return a new function
+	 */
+	public Object visit(DivExpr mulex) {
+		try {
+			Collection args = do_bin_args(mulex);
+			return FsDiv.fs_div(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit integer division expression
+	 * 
+	 * @param mulex
+	 *            is the integer division expression.
+	 * @return a new function
+	 */
+	public Object visit(IDivExpr mulex) {
+		try {
+			Collection args = do_bin_args(mulex);
+			return FsIDiv.fs_idiv(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit modular expression
+	 * 
+	 * @param mulex
+	 *            is the modular expression.
+	 * @return a new function
+	 */
+	public Object visit(ModExpr mulex) {
+		try {
+			Collection args = do_bin_args(mulex);
+			return FsMod.fs_mod(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	private Collection do_bin_args(BinExpr e) {
+		ResultSequence one = (ResultSequence) e.left().accept(this);
+		ResultSequence two = (ResultSequence) e.right().accept(this);
+
+		Collection args = new ArrayList();
+		args.add(newToOld(one));
+		args.add(newToOld(two));
+
+		return args;
+	}
+
+	/**
+	 * visit union expression
+	 * 
+	 * @param unex
+	 *            is the union expression.
+	 * @return a new function
+	 */
+	public Object visit(UnionExpr unex) {
+		try {
+			Collection args = do_bin_args(unex);
+			return OpUnion.op_union(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit pipe expression
+	 * 
+	 * @param pipex
+	 *            is the pipe expression.
+	 * @return a new function
+	 */
+	// XXX same as above
+	public Object visit(PipeExpr pipex) {
+		try {
+			Collection args = do_bin_args(pipex);
+			return OpUnion.op_union(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit intersect expression
+	 * 
+	 * @param iexpr
+	 *            is the intersect expression.
+	 * @return a new function
+	 */
+	public Object visit(IntersectExpr iexpr) {
+		try {
+			Collection args = do_bin_args(iexpr);
+			return OpIntersect.op_intersect(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit except expression
+	 * 
+	 * @param eexpr
+	 *            is the except expression.
+	 * @return a new function
+	 */
+	public Object visit(ExceptExpr eexpr) {
+		try {
+			Collection args = do_bin_args(eexpr);
+			return OpExcept.op_except(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit instance of expression
+	 * 
+	 * @param ioexp
+	 *            is the instance of expression.
+	 * @return a new function
+	 */
+	public Object visit(InstOfExpr ioexp) {
+		// get the value
+		ResultSequence rs = (ResultSequence) ioexp.left().accept(this);
+
+		// get the sequence type
+		SequenceType seqt = (SequenceType) ioexp.right();
+		return ResultSequenceFactory.create_new(new XSBoolean(isInstanceOf(rs, seqt)));
+	}
+		
+	private boolean isInstanceOf(ResultSequence rs, SequenceType seqt) {
+		Object oldParam = this._param;
+		try {
+			this._param = new Pair(null, rs);
+			int sequenceLength = rs.size();
+			// Run the matcher
+			seqt.accept(this);
+			rs = (ResultSequence) ((Pair)_param)._two;
+			int lengthAfter = rs.size();
+		
+			if (sequenceLength != lengthAfter)
+				return false; // Something didn't match, so it's not an instance of it
+			
+			return seqt.isLengthValid(sequenceLength);
+		} finally {
+			this._param = oldParam;
+		}
+	}
+
+	/**
+	 * visit treat-as expression
+	 * 
+	 * @param taexp
+	 *            is the treat-as expression.
+	 * @return a new function
+	 */
+	public Object visit(TreatAsExpr taexp) {
+
+		ResultSequence rs = (ResultSequence) taexp.left().accept(this);
+
+		SequenceType seqt = (SequenceType) taexp.right();
+		SeqType st = new SeqType(seqt, _legacyDc, ResultSequenceUtil.newToOld(rs));
+
+		try {
+			st.match(ResultSequenceUtil.newToOld(rs));
+		} catch (DynamicError err) {
+			report_error(err);
+		}
+
+		return rs;
+	}
+
+	/**
+	 * visit castable expression
+	 * 
+	 * @param cexp
+	 *            is the castable expression.
+	 * @return a new function
+	 */
+	public Object visit(CastableExpr cexp) {
+		boolean castable = false;
+		try {
+			CastExpr ce = new CastExpr((Expr) cexp.left(), (SingleType) cexp
+					.right());
+
+			visit(ce);
+			castable = true;
+		} catch (Throwable t) {
+			castable = false;
+		}
+
+		return ResultSequenceFactory.create_new(new XSBoolean(castable));
+	}
+
+	/**
+	 * visit cast expression
+	 * 
+	 * @param cexp
+	 *            is the cast expression.
+	 * @return a new function
+	 */
+	public Object visit(CastExpr cexp) {
+
+		ResultSequence rs = (ResultSequence) cexp.left().accept(this);
+		SingleType st = (SingleType) cexp.right();
+
+		rs = FnData.atomize(newToOld(rs));
+
+		if (rs.size() > 1)
+			report_error(TypeError.invalid_type(null));
+
+		if (rs.empty()) {
+			if (st.qmark())
+				return rs;
+			else
+				report_error(TypeError.invalid_type(null));
+		}
+
+		AnyType at = (AnyType) rs.item(0);
+
+		if (!(at instanceof AnyAtomicType))
+			report_error(TypeError.invalid_type(null));
+
+		AnyAtomicType aat = (AnyAtomicType) at;
+
+		QName type = st.type();
+
+		// check if constructor exists
+		// try {
+		if (!_legacyDc.function_exists(type, 1))
+			report_error(TypeError.invalid_type(null));
+		/*
+		 * } catch(StaticNsNameError err) {
+		 * report_error(TypeError.invalid_type(null)); }
+		 */
+		// prepare args from function
+		Collection args = new ArrayList();
+		args.add(ResultSequenceFactory.create_new(aat));
+
+		try {
+			Function function = _sc.resolveFunction(type.asQName(), args.size());
+			return function.evaluate(args, _ec);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit minus expression
+	 * 
+	 * @param e
+	 *            is the minus expression.
+	 * @return a new function
+	 */
+	public Object visit(MinusExpr e) {
+		ResultSequence rs = (ResultSequence) e.arg().accept(this);
+
+		Collection args = new ArrayList();
+		args.add(newToOld(rs));
+
+		try {
+			return FsMinus.fs_minus_unary(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit plus expression
+	 * 
+	 * @param e
+	 *            is the plus expression.
+	 * @return a new function
+	 */
+	public Object visit(PlusExpr e) {
+		ResultSequence rs = (ResultSequence) e.arg().accept(this);
+
+		Collection args = new ArrayList();
+		args.add(newToOld(rs));
+
+		try {
+			return FsPlus.fs_plus_unary(args);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	// this will evaluate the step expression for the whole focus and return
+	// the result.
+	//
+	// i.e. It will execute the step expression for each item in the focus
+	// [each time changing the context item].
+	private ResultSequence do_step(StepExpr se) {
+
+		ResultBuffer rs = new ResultBuffer();
+		ArrayList results = new ArrayList();
+		int type = 0; // 0: don't know yet
+		// 1: atomic
+		// 2: node
+
+		Focus focus = focus();
+		int original_pos = focus.position();
+
+		// execute step for all items in focus
+		while (true) {
+			results.add(se.accept(this));
+
+			// go to next
+			if (!focus.advance_cp())
+				break;
+		}
+
+		// make sure we didn't change focus
+		focus.set_position(original_pos);
+
+		boolean node_types = false;
+
+		// check the results
+		for (Iterator i = results.iterator(); i.hasNext();) {
+			ResultSequence result = (ResultSequence) i.next();
+
+			// make sure results are of same type, and add them in
+			for (Iterator j = result.iterator(); j.hasNext();) {
+				AnyType item = (AnyType) j.next();
+
+				// first item
+				if (type == 0) {
+					if (item instanceof AnyAtomicType)
+						type = 1;
+					else if (item instanceof NodeType)
+						type = 2;
+					else
+						assert false;
+
+				}
+
+				// make sure we got coherent types
+				switch (type) {
+				// atomic... just concat
+				case 1:
+					if (!(item instanceof AnyAtomicType))
+						report_error(TypeError.mixed_vals(null));
+					rs.add(item);
+					break;
+
+				case 2:
+					node_types = true;
+					if (!(item instanceof NodeType))
+						report_error(TypeError.mixed_vals(null));
+					rs.add(item);
+					break;
+
+				default:
+					assert false;
+				}
+			}
+		}
+
+		// XXX lame
+		if (node_types) {
+			rs = NodeType.linarize(rs);
+		}
+
+		return rs.getSequence();
+	}
+
+	private ResultSequence root_self_node() {
+		Axis axis = new SelfAxis();
+		ResultSequence rs;
+
+		// XXX the cast!!!
+		rs = axis.iterate((NodeType) focus().context_item(), _legacyDc);
+
+		rs = kind_test(rs, NodeType.class);
+
+		try {
+			List records = new ArrayList();
+			records.add(newToOld(rs));
+			rs = FnRoot.fn_root(records, _legacyDc);
+		} catch (DynamicError err) {
+			report_error(err);
+		}
+		return rs;
+	}
+
+	private ResultSequence descendant_or_self_node(ResultSequence rs) {
+		ResultBuffer res = new ResultBuffer();
+		Axis axis = new DescendantOrSelfAxis();
+
+		// for all nodes, get descendant or self nodes
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			NodeType item = (NodeType) i.next();
+
+			ResultSequence nodes = axis.iterate(item, _legacyDc);
+			nodes = kind_test(nodes, NodeType.class);
+
+			res.concat(nodes);
+		}
+
+		return res.getSequence();
+	}
+
+	/**
+	 * visit XPath expression
+	 * 
+	 * @param e
+	 *            is the XPath expression.
+	 * @return a new function
+	 */
+	public Object visit(XPathExpr e) {
+		XPathExpr xp = e;
+
+		ResultSequence rs = null;
+		Focus original_focus = _legacyDc.focus();
+
+		// do all the steps
+		while (xp != null) {
+			StepExpr se = xp.expr();
+
+			if (se != null) {
+				// this is not the first step
+				if (rs != null) {
+					// XXX ?
+					// the expression didn't return any
+					// results...
+					if (rs.size() == 0)
+						break;
+
+					// make sure result of previous step are
+					// nodes!
+					for (Iterator i = rs.iterator(); i.hasNext();) {
+						AnyType item = (AnyType) i.next();
+
+						if (!(item instanceof NodeType)) {
+							report_error(TypeError.step_conatins_atoms(null));
+							return null; // unreach
+						}
+					}
+
+					// check if we got a //
+					if (xp.slashes() == 2) {
+						rs = descendant_or_self_node(rs);
+
+						if (rs.size() == 0)
+							break;
+					}
+
+					// make result of previous step the new
+					// focus
+					set_focus(new Focus(rs));
+
+					// do the step for all item in context
+					rs = do_step(se);
+				}
+				// this is first step...
+				// note... we may be called from upstream...
+				// like in the expression sorbo/*[2] ... we may
+				// be called to evaluate the 2... the caller
+				// will iterate through the whole outer focus
+				// for us
+				else {
+					// XXX ???
+					if (xp.slashes() == 1) {
+						rs = root_self_node();
+						set_focus(new Focus(rs));
+
+						rs = do_step(se);
+					} else if (xp.slashes() == 2) {
+						rs = root_self_node();
+
+						rs = descendant_or_self_node(rs);
+
+						set_focus(new Focus(rs));
+
+						rs = do_step(se);
+					} else
+						rs = (ResultSequence) se.accept(this);
+				}
+			}
+			// the expression is "/"
+			else {
+				assert xp.slashes() == 1;
+
+				rs = root_self_node();
+			}
+
+			xp = xp.next();
+		}
+
+		// restore focus
+		set_focus(original_focus);
+
+		return rs;
+	}
+
+	/**
+	 * visit a forward step expression
+	 * 
+	 * @param e
+	 *            is the forward step.
+	 * @return a new function
+	 */
+	public Object visit(ForwardStep e) {
+
+		// get context node
+		AnyType ci = focus().context_item();
+		
+		if (ci == null) 
+			report_error(DynamicError.contextUndefined());
+
+		if (!(ci instanceof NodeType))
+			report_error(TypeError.ci_not_node(ci.string_type()));
+
+		NodeType cn = (NodeType) ci;
+
+		// get the nodes on the axis
+		ForwardAxis axis = e.iterator();
+		ResultSequence nodes = axis.iterate(cn, _legacyDc);
+		// get all nodes in the axis, and principal node
+		Pair arg = new Pair(axis.principal_node_kind().string_type(), nodes);
+
+		// do the name test
+		_param = arg;
+		ResultSequence rs = (ResultSequence) e.node_test().accept(this);
+
+		return rs;
+	}
+
+	/**
+	 * visit a reverse step expression
+	 * 
+	 * @param e
+	 *            is the reverse step.
+	 * @return a new function
+	 */
+	// XXX unify with top
+	public Object visit(ReverseStep e) {
+		// get context node
+		AnyType ci = focus().context_item();
+
+		if (!(ci instanceof NodeType))
+			report_error(TypeError.ci_not_node(ci.string_type()));
+
+		NodeType cn = (NodeType) ci;
+
+		// get the nodes on the axis
+		ReverseAxis axis = e.iterator();
+
+		// short for "gimme da parent"
+		if (e.axis() == ReverseStep.DOTDOT) {
+			axis = new ParentAxis();
+
+			return kind_test(axis.iterate(cn, _legacyDc), NodeType.class);
+		}
+
+		assert axis != null;
+
+		ResultSequence nodes = axis.iterate(cn, _legacyDc);
+		// get all nodes in the axis, and principal node
+		Pair arg = new Pair(axis.principal_node_kind().string_type(), nodes);
+
+		// do the name test
+		_param = arg;
+		ResultSequence rs = (ResultSequence) e.node_test().accept(this);
+
+		return rs;
+	}
+
+	// XXX this routine sux
+	private boolean name_test(NodeType node, QName name, String type) {
+		// make sure principal node kind is the same
+		if (node == null) {
+			return false;
+		}
+		if (!type.equals(node.string_type())) {
+			return false;
+		}
+
+		String test_prefix = name.prefix();
+
+		// if unprefixed and principal node kind is element, set default
+		// element namespace
+		if (test_prefix == null && type.equals("element")) {
+			// XXX make a new copy
+			name = new QName(null, name.local());
+			name.set_namespace(_sc.getDefaultNamespace());
+
+			// if we actually have a namespace, pretend we do =D
+			if (name.namespace() != null && name.namespace().length() > 0)
+				test_prefix = "";
+		}
+
+		QName node_name = node.node_name();
+
+		assert node_name != null;
+
+		// make sure namespace matches
+		String node_namespace = node_name.namespace();
+
+		String test_namespace = null;
+		if (name.expanded())
+			test_namespace = name.namespace();
+
+		// name test has no prefix
+		if (test_prefix == null) {
+			// ok no namespace... match
+			if (node_namespace == null) {
+			} else {
+				return false;
+			}
+		}
+		// name test has a prefix and is not wildcard
+		// XXX AT THIS POINT ALL PREFIXES NEED TO BE RESOLVED!
+		else if (!test_namespace.equals("*")) {
+			// the node doesn't have a namespace... no match
+			if (node_namespace == null) {
+				return false;
+			}
+			// check namespaces
+			else {
+				if (node_namespace.equals(test_namespace)) {
+					// namespace matches
+				} else {
+					return false;
+				}
+			}
+		}
+
+		// make sure local part matches
+		// check for wildcard in localpart
+		if (name.local().equals("*"))
+			return true;
+
+		// check if local part matches
+		if (!name.local().equals(node_name.local())) {
+			return false;
+		}
+
+		return true;
+	}
+
+	/**
+	 * visit a name test expression
+	 * 
+	 * @param e
+	 *            is thename test.
+	 * @return a result sequence
+	 */
+	public Object visit(NameTest e) {
+		QName name = e.name();
+
+		// get the arguments
+		Pair arg = (Pair) _param;
+		String type = (String) arg._one;
+		ResultSequence rs = (ResultSequence) arg._two;
+
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			NodeType nt = (NodeType) i.next();
+
+			// check if node passes name test
+			if (!name_test(nt, name, type))
+				i.remove();
+
+		}
+
+		return rs;
+	}
+
+	/**
+	 * visit variable reference
+	 * 
+	 * @param e
+	 *            is the variable reference.
+	 * @return a result sequence
+	 */
+	public Object visit(VarRef e) {
+		ResultBuffer rs = new ResultBuffer();
+
+		Object var = getVariable(e.name());
+
+		assert var != null;
+
+		if (var instanceof AnyType) {
+		   rs.add((AnyType) var);
+		}
+		else if (var instanceof ResultSequence) {
+		   rs.concat((ResultSequence) var);	
+		}
+
+		return rs.getSequence();
+	}
+
+	/**
+	 * visit string literal.
+	 * 
+	 * @param e
+	 *            is the string literal.
+	 * @return a result sequence
+	 */
+	public Object visit(StringLiteral e) {
+		return e.value();
+	}
+
+	/**
+	 * visit integer literal.
+	 * 
+	 * @param e
+	 *            is the integer literal.
+	 * @return a result sequence
+	 */
+	public Object visit(IntegerLiteral e) {
+		return e.value();
+	}
+
+	/**
+	 * visit double literal.
+	 * 
+	 * @param e
+	 *            is the double literal.
+	 * @return a result sequence
+	 */
+	public Object visit(DoubleLiteral e) {
+		return e.value();
+	}
+
+	/**
+	 * visit decimal literal.
+	 * 
+	 * @param e
+	 *            is the decimal literal.
+	 * @return a result sequence
+	 */
+	public Object visit(DecimalLiteral e) {
+		ResultBuffer rs = new ResultBuffer();
+
+		rs.add(e.value());
+		return rs.getSequence();
+	}
+
+	/**
+	 * visit parent expression.
+	 * 
+	 * @param e
+	 *            is the parent expression.
+	 * @return a new function
+	 */
+	public Object visit(ParExpr e) {
+		return do_expr(e.iterator());
+	}
+
+	/**
+	 * visit context item expression.
+	 * 
+	 * @param e
+	 *            is the context item expression.
+	 * @return a result sequence
+	 */
+	public Object visit(CntxItemExpr e) {
+		ResultBuffer rs = new ResultBuffer();
+
+		AnyType contextItem = focus().context_item();
+		if (contextItem == null) {
+			report_error(DynamicError.contextUndefined());
+		}
+		rs.add(contextItem);
+		return rs.getSequence();
+	}
+
+	/**
+	 * visit function call.
+	 * 
+	 * @param e
+	 *            is the function call.
+	 * @return a new function or null
+	 */
+	public Object visit(FunctionCall e) {
+		ArrayList args = new ArrayList();
+
+		for (Iterator i = e.iterator(); i.hasNext();) {
+			Expr arg = (Expr) i.next();
+			// each argument will produce a result sequence
+			args.add(newToOld((ResultSequence)arg.accept(this)));
+		}
+
+		try {
+			Function function = _sc.resolveFunction(e.name().asQName(), args.size());
+			
+			return function.evaluate(args, _ec);
+		} catch (DynamicError err) {
+			report_error(err);
+			return null; // unreach
+		}
+	}
+
+	/**
+	 * visit single type.
+	 * 
+	 * @param e
+	 *            is the single type.
+	 * @return null
+	 */
+	public Object visit(SingleType e) {
+		return null;
+	}
+
+	/**
+	 * visit sequence type.
+	 * 
+	 * @param e
+	 *            is the sequence type.
+	 * @return null
+	 */
+	public Object visit(SequenceType e) {
+		ItemType it = e.item_type();
+
+		if (it != null)
+			it.accept(this);
+
+		return null;
+	}
+
+	/**
+	 * visit item type.
+	 * 
+	 * @param e
+	 *            is the item type.
+	 * @return null
+	 */
+	public Object visit(ItemType e) {
+
+		switch (e.type()) {
+		case ItemType.ITEM:
+			break;
+		case ItemType.QNAME:
+
+			// try {
+			if (_sc.getTypeModel().lookupType(e.qname().namespace(), e.qname().local()) == null) {
+				if (BuiltinTypeLibrary.BUILTIN_TYPES.lookupType(e.qname().namespace(), e.qname().local()) == null)
+				{
+					report_error(new StaticTypeNameError("Type not defined: "
+							+ e.qname().string()));
+				}
+			}
+			
+			ResultSequence arg = (ResultSequence) ((Pair) _param)._two;
+			((Pair) _param)._two = item_test(arg, e.qname());
+			break;
+
+		case ItemType.KINDTEST:
+			((Pair) _param)._two = e.kind_test().accept(this);
+			break;
+		}
+
+		return null;
+	}
+
+	private ResultSequence item_test(ResultSequence rs, QName qname) {
+		ResultBuffer rb = new ResultBuffer();
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			AnyType item = (AnyType) i.next();
+			
+			if (item instanceof NodeType) {
+				NodeType node = ((NodeType)item);
+				if (_legacyDc.derives_from(node , qname)) rb.add(node);
+			} else {
+				// atomic of some sort
+				if (qname.equals(ANY_ATOMIC_TYPE)) {
+					rb.add(item);
+					continue; // match !
+				}
+				
+				final AnyAtomicType aat = _legacyDc.make_atomic(qname);
+				if (aat.getClass().isInstance(item)) rb.add(item);
+				
+				// fall through => non-match
+			}
+		}
+		return rb.getSequence();
+	}
+
+    private ResultSequence kind_test(ResultSequence rs, Class kind) {
+    	ResultBuffer rb = new ResultBuffer();
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			Item item = (Item) i.next();
+			if (kind.isInstance(item))
+				rb.add(item);
+		}
+		return rb.getSequence();
+	}
+
+	/**
+	 * visit any kind test.
+	 * 
+	 * @param e
+	 *            is the any kind test.
+	 * @return a new function
+	 */
+	public Object visit(AnyKindTest e) {
+		ResultSequence arg = (ResultSequence) ((Pair) _param)._two;
+
+		return kind_test(arg, NodeType.class);
+	}
+
+	/**
+	 * visit document test.
+	 * 
+	 * @param e
+	 *            is the document test.
+	 * @return result sequence
+	 */
+	public Object visit(DocumentTest e) {
+		ResultSequence arg = (ResultSequence) ((Pair) _param)._two;
+		int type = e.type();
+
+		// filter doc nodes
+		ResultSequence rs = kind_test(arg, DocType.class);
+
+		if (type == DocumentTest.NONE)
+			return rs;
+
+		// for all docs, find the ones with exactly one element, and do
+		// the element test
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			DocType doc = (DocType) i.next();
+			int elem_count = 0;
+			ElementType elem = null;
+
+			// make sure doc has only 1 element
+			NodeList children = doc.node_value().getChildNodes();
+			for (int j = 0; j < children.getLength(); j++) {
+				Node child = children.item(j);
+
+				// bingo
+				if (child.getNodeType() == Node.ELEMENT_NODE) {
+					elem_count++;
+
+					if (elem_count > 1)
+						break;
+
+					elem = new ElementType((Element) child, this._legacyDc.getTypeModel(child));
+				}
+			}
+
+			// this doc is no good... send him to hell
+			if (elem_count != 1) {
+				i.remove();
+				continue;
+			}
+
+			assert elem != null;
+
+			// setup parameter for element test
+			ResultSequence res = new ResultBuffer.SingleResultSequence(elem);
+			_param = new Pair("element", res);
+
+			// do name test
+			res = null;
+			if (type == DocumentTest.ELEMENT)
+				res = (ResultSequence) e.elem_test().accept(this);
+			else if (type == DocumentTest.SCHEMA_ELEMENT)
+				res = (ResultSequence) e.schema_elem_test().accept(this);
+			else
+				assert false;
+
+			// check if element survived nametest
+			if (res.size() != 1)
+				i.remove();
+		}
+
+		return rs;
+	}
+
+	/**
+	 * visit text test.
+	 * 
+	 * @param e
+	 *            is the text test.
+	 * @return a new function
+	 */
+	public Object visit(TextTest e) {
+		ResultSequence arg = (ResultSequence) ((Pair) _param)._two;
+
+		((Pair) _param)._two = kind_test(arg, TextType.class);
+		return ((Pair) _param)._two;
+	}
+
+	/**
+	 * visit comment test.
+	 * 
+	 * @param e
+	 *            is the text test.
+	 * @return a new function
+	 */
+	public Object visit(CommentTest e) {
+		ResultSequence arg = (ResultSequence) ((Pair) _param)._two;
+
+		return kind_test(arg, CommentType.class);
+	}
+
+	/**
+	 * visit PI test.
+	 * 
+	 * @param e
+	 *            is the PI test.
+	 * @return a argument
+	 */
+	public Object visit(PITest e) {
+		ResultSequence arg = (ResultSequence) ((Pair) _param)._two;
+
+		String pit_arg = e.arg();
+
+		// match any pi
+		if (pit_arg == null)
+			return kind_test(arg, PIType.class);
+
+		for (Iterator i = arg.iterator(); i.hasNext();) {
+			AnyType item = (AnyType) i.next();
+
+			// match PI
+			if (item instanceof PIType) {
+				PIType pi = (PIType) item;
+
+				// match target
+				if (!pit_arg.equals(pi.value().getTarget()))
+					i.remove();
+			} else
+				i.remove();
+		}
+
+		return arg;
+	}
+
+	/**
+	 * visit attribute test.
+	 * 
+	 * @param e
+	 *            is the attribute test.
+	 * @return a result sequence
+	 */
+	public Object visit(AttributeTest e) {
+		// filter out all attrs
+		ResultSequence rs = kind_test((ResultSequence) ((Pair) _param)._two,
+				AttrType.class);
+
+		ResultBuffer rb = new ResultBuffer();
+		
+		QName name = e.name();
+		QName type = e.type();
+
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			NodeType node = (NodeType) i.next();
+			// match the name if it's not a wild card
+			if (name != null && !e.wild()) {
+				if (!name_test(node, name, "attribute"))
+					continue;
+			}
+			// match the type
+			if (type != null) {
+				// check if element derives from
+				if (!_legacyDc.derives_from(node, type))
+					continue;
+			}
+			rb.add(node);
+		}
+		((Pair) _param)._two = rb.getSequence();
+		return ((Pair) _param)._two;
+	}
+
+	/**
+	 * visit schema attribute test.
+	 * 
+	 * @param e
+	 *            is the schema attribute test.
+	 * @return a result sequence
+	 */
+	public Object visit(SchemaAttrTest e) {
+		// filter out all attrs
+		ResultSequence rs = kind_test((ResultSequence) ((Pair) _param)._two,
+				AttrType.class);
+
+		// match the name
+		QName name = e.arg();
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			if (!name_test((NodeType) i.next(), name, "attribute"))
+
+				i.remove();
+		}
+
+		// check the type
+		TypeDefinition et = _legacyDc.attribute_type_definition(name);
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			NodeType node = (NodeType) i.next();
+
+			if (!_legacyDc.derives_from(node, et))
+				i.remove();
+
+		}
+
+		return rs;
+	}
+
+	/**
+	 * visit element test.
+	 * 
+	 * @param e
+	 *            is the element test.
+	 * @return a result sequence
+	 */
+	public Object visit(ElementTest e) {
+		// filter out all elements
+		ResultSequence rs = kind_test((ResultSequence) ((Pair) _param)._two,
+				ElementType.class);
+
+		// match the name if it's not a wild card
+		ResultBuffer rb = new ResultBuffer();
+		QName nameTest = e.name();
+		QName typeTest = e.type();
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			NodeType node = (NodeType) i.next();
+
+			if (nameTest != null && !e.wild()) {
+				// skip if there's a name test and the name does not match
+				if (!name_test((ElementType) node, nameTest, "element")) continue;
+			}
+			if (typeTest != null) {
+				// check if element derives from
+				if (! _legacyDc.derives_from(node, typeTest)) continue;
+				
+				// nilled may be true or false
+				if (! e.qmark()) {
+					XSBoolean nilled = (XSBoolean) node.nilled().first();
+					if (nilled.value()) continue;
+				}
+				rb.add(node);
+			}
+		}
+		((Pair) _param)._two = rb.getSequence();
+		return ((Pair) _param)._two;
+	}
+
+	/**
+	 * visit schema element test.
+	 * 
+	 * @param e
+	 *            is the schema element test.
+	 * @return a result sequence
+	 */
+	public Object visit(SchemaElemTest e) {
+		// filter out all elements
+		ResultSequence rs = kind_test((ResultSequence) ((Pair) _param)._two,
+				ElementType.class);
+
+		// match the name
+		// XXX substitution groups
+		QName name = e.name();
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			if (!name_test((ElementType) i.next(), name, "element"))
+
+				i.remove();
+		}
+
+		// check the type
+		TypeDefinition et = _legacyDc.element_type_definition(name);
+		for (Iterator i = rs.iterator(); i.hasNext();) {
+			NodeType node = (NodeType) i.next();
+
+			if (!_legacyDc.derives_from(node, et)) {
+				i.remove();
+				continue;
+			}
+
+			XSBoolean nilled = (XSBoolean) node.nilled().first();
+			// XXX or, in schema it is nillable
+			if (nilled.value())
+				i.remove();
+		}
+
+		return rs;
+	}
+
+	private boolean predicate_truth(ResultSequence rs) {
+		// rule 1 of spec... if numeric type:
+		// if num eq position then true else false
+		if (rs.size() == 1) {
+			AnyType at = (AnyType) rs.item(0);
+
+			if (at instanceof NumericType) {
+				try {
+					return FsEq.fs_eq_fast(at, new XSInteger(BigInteger.valueOf(_legacyDc.context_position())), _legacyDc);
+				} catch (DynamicError err) {
+					report_error(err);
+
+					// unreach
+					assert false;
+					return false;
+				}
+			}
+		}
+
+		// rule 2
+		XSBoolean ret = effective_boolean_value(rs);
+
+		return ret.value();
+	}
+
+	// do the predicate for all items in focus
+	private ResultSequence do_predicate(Collection exprs) {
+		ResultBuffer rs = new ResultBuffer();
+
+		Focus focus = _legacyDc.focus();
+		int original_cp = focus.position();
+
+		// optimization
+		// check if predicate is single numeric constant
+		if (exprs.size() == 1) {
+			Expr expr = (Expr) exprs.iterator().next();
+
+			if (expr instanceof XPathExpr) {
+				XPathExpr xpe = (XPathExpr) expr;
+				if (xpe.next() == null && xpe.slashes() == 0
+						&& xpe.expr() instanceof FilterExpr) {
+					FilterExpr fex = (FilterExpr) xpe.expr();
+					if (fex.primary() instanceof IntegerLiteral) {
+						int pos = (((IntegerLiteral) fex.primary()).value()
+								.int_value()).intValue();
+
+						if (pos <= focus.last() && pos > 0) {
+							focus.set_position(pos);
+							rs.add(focus.context_item());
+						}
+						focus.set_position(original_cp);
+						return rs.getSequence();
+					}
+				}
+			}
+		}
+
+		// go through all elements
+		while (true) {
+			// do the predicate
+			// XXX saxon doesn't allow for predicates to have
+			// commas... but XPath 2.0 spec seems to do
+			ResultSequence res = do_expr(exprs.iterator());
+
+			// if predicate is true, the context item is definitely
+			// in the sequence
+			if (predicate_truth(res))
+				rs.add(_legacyDc.context_item());
+
+			if (!focus.advance_cp())
+				break;
+
+		}
+
+		// restore
+		focus.set_position(original_cp);
+
+		return rs.getSequence();
+	}
+
+	/**
+	 * visit axis step.
+	 * 
+	 * @param e
+	 *            is the axis step.
+	 * @return a result sequence
+	 */
+	public Object visit(AxisStep e) {
+		ResultSequence rs = (ResultSequence) e.step().accept(this);
+
+		if (e.predicate_count() == 0)
+			return rs;
+
+		// I take it predicates are logical ANDS...
+		Focus original_focus = _legacyDc.focus();
+
+		// go through all predicates
+		for (Iterator i = e.iterator(); i.hasNext();) {
+			// empty results... get out of here ? XXX
+			if (rs.size() == 0)
+				break;
+
+			_legacyDc.set_focus(new Focus(rs));
+			rs = do_predicate((Collection) i.next());
+
+		}
+
+		// restore focus [context switching ;D ]
+		_legacyDc.set_focus(original_focus);
+		return rs;
+	}
+
+	/**
+	 * visit filter expression
+	 * 
+	 * @param e
+	 *            is the filter expression.
+	 * @return a result sequence
+	 */
+	// XXX unify with top ?
+	public Object visit(FilterExpr e) {
+		ResultSequence rs = (ResultSequence) e.primary().accept(this);
+
+		// if no predicates are present, then the result is the same as
+		// the primary expression
+		if (e.predicate_count() == 0)
+			return rs;
+
+		Focus original_focus = _legacyDc.focus();
+
+		// go through all predicates
+		for (Iterator i = e.iterator(); i.hasNext();) {
+			if (rs.size() == 0)
+				break;
+
+			_legacyDc.set_focus(new Focus(rs));
+			rs = do_predicate((Collection) i.next());
+
+		}
+
+		// restore focus [context switching ;D ]
+		_legacyDc.set_focus(original_focus);
+		return rs;
+	}
+
+}
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/StaticNameResolver.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/StaticNameResolver.java
index 7b473a5..085ac79 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/StaticNameResolver.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/StaticNameResolver.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2009 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor;
@@ -260,6 +261,11 @@
 	private boolean expand_qname(QName name, String def) {
 		String prefix = name.prefix();
 
+		if ("*".equals(prefix)) {
+			name.set_namespace("*");
+			return true;
+		}
+
 		if (prefix == null) {
 			name.set_namespace(def);
 			return true;
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/ast/XPath.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/ast/XPath.java
index f45bb02..a22a7cb 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/ast/XPath.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/ast/XPath.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -8,6 +8,7 @@
  * Contributors:
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
  *     Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.ast;
@@ -19,6 +20,7 @@
 import org.eclipse.wst.xml.xpath2.api.StaticContext;
 import org.eclipse.wst.xml.xpath2.api.XPath2Expression;
 import org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator;
+import org.eclipse.wst.xml.xpath2.processor.DefaultEvaluator2;
 import org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathNode;
 import org.eclipse.wst.xml.xpath2.processor.internal.ast.XPathVisitor;
 
@@ -88,7 +90,7 @@
 	 */
 	public ResultSequence evaluate(DynamicContext dynamicContext, Object[] contextItems) {
 		if (_staticContext == null) throw new IllegalStateException("Static Context not set yet!");
-		return new DefaultEvaluator(_staticContext, dynamicContext, contextItems).evaluate(this);
+		return new DefaultEvaluator2(_staticContext, dynamicContext, contextItems).evaluate2(this);
 	}
 	
 	/**
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/DefaultResultSequence.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/DefaultResultSequence.java
index 6ae29a1..8946ce5 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/DefaultResultSequence.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/DefaultResultSequence.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -8,6 +8,7 @@
  * Contributors:
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
  *     Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal;
@@ -20,7 +21,7 @@
 
 /**
  * Default implementation of a result sequence.
- * 
+ * @deprecated use ResultBuffer instead
  */
 public class DefaultResultSequence extends ResultSequence {
 
@@ -96,6 +97,13 @@
 	}
 
 	/**
+	 * @return first item from array _seq
+	 */
+	public Object firstValue() {
+		return get(0).getNativeValue();
+	}
+
+	/**
 	 * Whether or not array _seq is empty
 	 * 
 	 * @return a boolean
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/RangeResultSequence.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/RangeResultSequence.java
index c2bed15..3b8f373 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/RangeResultSequence.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/RangeResultSequence.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0
  *     Mukul Gandhi - bug 274805 - improvements to xs:integer data type 
  *     Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal;
@@ -139,6 +140,13 @@
 	}
 
 	/**
+	 * @return first item in range
+	 */
+	public Object firstValue() {
+		return get(0).getNativeValue();
+	}
+
+	/**
 	 * asks if the range is empty?
 	 * 
 	 * @return boolean
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FnAbs.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FnAbs.java
index 5a5361c..0a944eb 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FnAbs.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FnAbs.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,12 +9,14 @@
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
  *     Jesper Steen Moeller - bug 285145 - implement full arity checking
  *     Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal.function;
 
+import org.eclipse.wst.xml.xpath2.api.EvaluationContext;
+import org.eclipse.wst.xml.xpath2.api.ResultSequence;
 import org.eclipse.wst.xml.xpath2.processor.DynamicError;
-import org.eclipse.wst.xml.xpath2.processor.ResultSequence;
 import org.eclipse.wst.xml.xpath2.processor.ResultSequenceFactory;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.*;
 
@@ -47,7 +49,7 @@
 	 *             Dynamic error.
 	 * @return Result of evaluation.
 	 */
-	public ResultSequence evaluate(Collection args) throws DynamicError {
+	public ResultSequence evaluate(Collection args, EvaluationContext ec) {
 		// 1 argument only!
 		assert args.size() >= min_arity() && args.size() <= max_arity();
 
@@ -78,30 +80,25 @@
 		if (nt instanceof XSDouble) {
 			XSDouble dat = (XSDouble) nt;
 			if (dat.zero() || dat.negativeZero()) {
-				rs.add(new XSDouble("0"));
-				return rs;
+				return new XSDouble("0");
 			}
 			if (dat.infinite()) {
-				rs.add(new XSDouble(Double.POSITIVE_INFINITY));
-				return rs;
+				return new XSDouble(Double.POSITIVE_INFINITY);
 			}
 		}
 
 		if (nt instanceof XSFloat) {
 			XSFloat dat = (XSFloat) nt;
 			if (dat.zero() || dat.negativeZero()) {
-				rs.add(new XSFloat((new Float(0)).floatValue()));
-				return rs;
+				return new XSFloat((new Float(0)).floatValue());
 			}
 			if (dat.infinite()) {
-				rs.add(new XSFloat(Float.POSITIVE_INFINITY));
-				return rs;
+				return new XSFloat(Float.POSITIVE_INFINITY);
 			}
 		}
 
 
-		rs.add(nt.abs());
-		return rs;
+		return nt.abs();
 	}
 
 	/**
@@ -122,7 +119,7 @@
 		if (size == 0)
 			return null;
 
-		AnyType at = arg.first();
+		AnyType at = (AnyType) arg.item(0);
 
 		if (!(at instanceof NumericType))
 			throw DynamicError.invalidType();
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FsMinus.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FsMinus.java
index 8b6e2d6..c290f8c 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FsMinus.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FsMinus.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
  *     Jesper Steen Moeller - bug 285145 - implement full arity checking
  *     Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal.function;
@@ -16,6 +17,7 @@
 import org.eclipse.wst.xml.xpath2.processor.DynamicError;
 import org.eclipse.wst.xml.xpath2.processor.ResultSequence;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.*;
+import org.eclipse.wst.xml.xpath2.processor.util.ResultSequenceUtil;
 
 import java.util.*;
 
@@ -72,7 +74,7 @@
 		// make sure we got only one arg
 		if (args.size() != 1)
 			DynamicError.throw_type_error();
-		ResultSequence arg = (ResultSequence) args.iterator().next();
+		ResultSequence arg = ResultSequenceUtil.newToOld((org.eclipse.wst.xml.xpath2.api.ResultSequence) args.iterator().next());
 
 		// make sure we got only one numeric atom
 		if (arg.size() != 1)
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/Function.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/Function.java
index 32fb141..ef9fede 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/Function.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/Function.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2009 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -13,10 +13,13 @@
  *     Jesper Steen Moeller - bug 285145 - implement full arity checking
  *     Jesper Steen Moeller - bug 281159 - implement xs:anyUri -> xs:string promotion
  *     Jesper Steen Moller  - bug 281938 - undefined context should raise error
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal.function;
 
+import org.eclipse.wst.xml.xpath2.api.EvaluationContext;
+import org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition;
 import org.eclipse.wst.xml.xpath2.processor.DynamicContext;
 import org.eclipse.wst.xml.xpath2.processor.DynamicError;
 import org.eclipse.wst.xml.xpath2.processor.ResultSequence;
@@ -24,13 +27,15 @@
 import org.eclipse.wst.xml.xpath2.processor.StaticContext;
 import org.eclipse.wst.xml.xpath2.processor.internal.*;
 import org.eclipse.wst.xml.xpath2.processor.internal.types.*;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.builtin.BuiltinTypeLibrary;
+import org.eclipse.wst.xml.xpath2.processor.util.ResultSequenceUtil;
 
 import java.util.*;
 
 /**
  * Support for functions.
  */
-public abstract class Function {
+public abstract class Function implements org.eclipse.wst.xml.xpath2.api.Function {
 
 	protected QName _name;
 	/**
@@ -173,8 +178,32 @@
 	 *             Dynamic error.
 	 * @return Result of evaluation.
 	 */
-	public abstract ResultSequence evaluate(Collection args)
-			throws DynamicError;
+	public ResultSequence evaluate(Collection args)
+			throws DynamicError {
+		
+		return ResultSequenceUtil.newToOld(evaluate(args, new EvaluationContext() {
+			
+			public org.eclipse.wst.xml.xpath2.api.StaticContext getStaticContext() {
+				throw new UnsupportedOperationException("ikke længere");
+			}
+			
+			public int getLastPosition() {
+				throw new UnsupportedOperationException("ikke længere");
+			}
+			
+			public org.eclipse.wst.xml.xpath2.api.DynamicContext getDynamicContext() {
+				throw new UnsupportedOperationException("ikke længere");
+			}
+			
+			public int getContextPosition() {
+				throw new UnsupportedOperationException("ikke længere");
+			}
+			
+			public Object getContextItem() {
+				throw new UnsupportedOperationException("ikke længere");
+			}
+		}));
+	}
 
 	// convert argument according to section 3.1.5 of xpath 2.0 spec
 	/**
@@ -269,7 +298,7 @@
 
 		// convert all arguments
 		while (argi.hasNext()) {
-			result.add(convert_argument((ResultSequence) argi.next(),
+			result.add(convert_argument(ResultSequenceUtil.newToOld((org.eclipse.wst.xml.xpath2.api.ResultSequence) argi.next()),
 					(SeqType) expi.next()));
 		}
 
@@ -319,4 +348,48 @@
 		return _min_arity != _max_arity;
 	}
 
+	public String getName() {
+		return name().local();
+	}
+
+	public int getMinArity() {
+		return min_arity();
+	}
+
+	public int getMaxArity() {
+		return max_arity();
+	}
+
+	public boolean isVariableArgument() {
+		return this.is_vararg();
+	}
+
+	public boolean canMatchArity(int actualArity) {
+		return matches_arity(actualArity);
+	}
+
+	public TypeDefinition getResultType() {
+		return BuiltinTypeLibrary.XS_UNTYPED;
+	}
+
+	public TypeDefinition getArgumentType(int index) {
+		return BuiltinTypeLibrary.XS_UNTYPED;
+	}
+
+	public String getArgumentNameHint(int index) {
+		return "argument_"  + index;
+	}
+
+	public TypeDefinition computeReturnType(Collection args,
+			org.eclipse.wst.xml.xpath2.api.StaticContext sc) {
+		return BuiltinTypeLibrary.XS_UNTYPED;
+	}
+
+	public org.eclipse.wst.xml.xpath2.api.ResultSequence evaluate(Collection args,
+			EvaluationContext evaluationContext) {
+		
+		ResultSequence result = evaluate(args);
+		return ResultSequenceUtil.oldToNew(result);
+	}
+
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FunctionLibrary.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FunctionLibrary.java
index 625e123..fdb2438 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FunctionLibrary.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/function/FunctionLibrary.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2009 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,6 +9,7 @@
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
  *     Mukul Gandhi - bug 274471 - improvements to fn:string function (support for arity 0)
  *     Jesper Steen Moeller - bug 285145 - implement full arity checking
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal.function;
@@ -23,7 +24,7 @@
 /**
  * Class for Function Library support.
  */
-public class FunctionLibrary {
+public class FunctionLibrary implements org.eclipse.wst.xml.xpath2.api.FunctionLibrary {
 	private String _namespace;
 	private Map _functions;
 	private StaticContext _sc;
@@ -135,4 +136,18 @@
 	public DynamicContext dynamic_context() {
 		return _dc;
 	}
+
+	public boolean functionExists(String name, int arity) {
+		return function_exists(new QName(null,name, namespace()), arity);
+	}
+
+	public org.eclipse.wst.xml.xpath2.api.Function resolveFunction(
+			String localName, int arity) {
+
+		return function(new QName("f", localName, namespace()), arity);
+	}
+
+	public String getNamespace() {
+		return namespace();
+	}
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/AnyType.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/AnyType.java
index f4eceb8..e61e75f 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/AnyType.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/AnyType.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2009 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 package org.eclipse.wst.xml.xpath2.processor.internal.types;
 
@@ -59,6 +60,7 @@
 	public Object getNativeValue() {
 		return Boolean.TRUE;
 	}
+	
 	public int size() {
 		return 1;
 	}
@@ -69,7 +71,7 @@
 	}
 
 	private void checkIOOB(int index) {
-		throw new IndexOutOfBoundsException("Index out of bounds, index = " + index + ", length = 1");
+		if (index != 0) throw new IndexOutOfBoundsException("Index out of bounds, index = " + index + ", length = 1");
 	}
 
 	public Object value(int index) {
@@ -81,10 +83,14 @@
 		return getItemType();
 	}
 	
-	public Object first() {
+	public AnyType first() {
 		return this;
 	}
-	
+
+	public Object firstValue() {
+		return this.getNativeValue();
+	}
+
 	public TypeDefinition sequenceType() {
 		return getTypeDefinition();
 	}
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/NodeType.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/NodeType.java
index 89e76ac..e23c022 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/NodeType.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/NodeType.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -18,15 +18,19 @@
  *     Mukul Gandhi - bug 323900 - improving computing the typed value of element &
  *                                 attribute nodes, where the schema type of nodes
  *                                 are simple, with varieties 'list' and 'union'.                                 
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal.types;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
+import java.util.TreeSet;
 
+import org.eclipse.wst.xml.xpath2.api.ResultBuffer;
 import org.eclipse.wst.xml.xpath2.api.typesystem.ComplexTypeDefinition;
 import org.eclipse.wst.xml.xpath2.api.typesystem.PrimitiveType;
 import org.eclipse.wst.xml.xpath2.api.typesystem.SimpleTypeDefinition;
@@ -35,6 +39,7 @@
 import org.eclipse.wst.xml.xpath2.processor.PsychoPathTypeHelper;
 import org.eclipse.wst.xml.xpath2.processor.ResultSequence;
 import org.eclipse.wst.xml.xpath2.processor.ResultSequenceFactory;
+import org.eclipse.wst.xml.xpath2.processor.util.ResultSequenceUtil;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Comment;
 import org.w3c.dom.Document;
@@ -53,7 +58,13 @@
 	protected static final String SCHEMA_TYPE_ID = "ID";
 	private Node _node;
 	protected TypeModel _typeModel;
-		
+
+	public static final Comparator NODE_COMPARATOR = new Comparator() {
+		public int compare(Object o1, Object o2) {
+			return compare_node((NodeType)o1, (NodeType)o2);
+		}
+	};
+	
 	/**
 	 * Initialises according to the supplied parameters
 	 * 
@@ -408,4 +419,10 @@
 	public Object getNativeValue() {
 		return node_value();
 	}
+
+	public static ResultBuffer linarize(ResultBuffer rs) {
+		TreeSet all = new TreeSet(NODE_COMPARATOR);
+		all.addAll(rs.getCollection());
+		return new ResultBuffer().concat(all);
+	}
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/QName.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/QName.java
index 3d1edad..2fa5e4f 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/QName.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/QName.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2005, 2010 Andrea Bittau, University College London, and others
+ * Copyright (c) 2005, 2011 Andrea Bittau, University College London, and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -10,6 +10,7 @@
  *     Jesper Moller- bug 281159 - debugging convenience toString method 
  *     David Carver (STAR) - bug 288886 - add unit tests and fix fn:resolve-qname function
  *     Mukul Gandhi - bug 280798 - PsychoPath support for JDK 1.4
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor.internal.types;
@@ -231,7 +232,7 @@
 	 *            Namespace this node belongs in
 	 */
 	public void set_namespace(String n) {
-		_namespace = n;
+		_namespace = n != null ? (n.length() == 0 ? null : n) : null;
 		_expanded = true;
 	}
 
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/XSBoolean.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/XSBoolean.java
index b10e241..a29f26c 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/XSBoolean.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/types/XSBoolean.java
@@ -29,6 +29,8 @@
  */
 public class XSBoolean extends CtrType implements CmpEq, CmpGt, CmpLt {
 	private static final String XS_BOOLEAN = "xs:boolean";
+	public static final XSBoolean TRUE = new XSBoolean(true);
+	public static final XSBoolean FALSE = new XSBoolean(false);
 	private boolean _value;
 
 	/**
@@ -57,6 +59,11 @@
 		return XS_BOOLEAN;
 	}
 
+	@Override
+	public Object getNativeValue() {
+		return Boolean.valueOf(_value);
+	}
+	
 	/**
 	 * Retrieve the datatype name
 	 * 
@@ -209,4 +216,9 @@
 		return BuiltinTypeLibrary.XS_BOOLEAN;
 	}
 
+	public static org.eclipse.wst.xml.xpath2.api.ResultSequence valueOf(
+			boolean answer) {
+		return answer ? TRUE : FALSE;
+	}
+
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/ContextBuilder.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/ContextBuilder.java
deleted file mode 100644
index 2b5c06b..0000000
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/ContextBuilder.java
+++ /dev/null
@@ -1,414 +0,0 @@
-package org.eclipse.wst.xml.xpath2.processor.util;
-//package org.eclipse.wst.xml.xpath2;
-//
-//import java.io.FileNotFoundException;
-//import java.io.IOException;
-//import java.net.MalformedURLException;
-//import java.net.URI;
-//import java.net.URL;
-//import java.util.Collection;
-//import java.util.Comparator;
-//import java.util.GregorianCalendar;
-//import java.util.HashMap;
-//import java.util.Map;
-//import java.util.TimeZone;
-//
-//import javax.xml.namespace.NamespaceContext;
-//import javax.xml.namespace.QName;
-//
-//import org.apache.xerces.xs.XSModel;
-//import org.eclipse.wst.xml.xpath2.api.CollationProvider;
-//import org.eclipse.wst.xml.xpath2.api.DynamicContext;
-//import org.eclipse.wst.xml.xpath2.api.Function;
-//import org.eclipse.wst.xml.xpath2.api.ResultSequence;
-//import org.eclipse.wst.xml.xpath2.api.StaticContext;
-//import org.eclipse.wst.xml.xpath2.api.types.DocType;
-//import org.eclipse.wst.xml.xpath2.api.types.NodeType;
-//import org.eclipse.wst.xml.xpath2.api.types.XSDuration;
-//import org.eclipse.wst.xml.xpath2.api.types.XSQName;
-//import org.eclipse.wst.xml.xpath2.api.typesystem.DOMTypeProvider;
-//import org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition;
-//import org.eclipse.wst.xml.xpath2.api.typesystem.TypeModel;
-//import org.eclipse.wst.xml.xpath2.processor.DOMLoader;
-//import org.eclipse.wst.xml.xpath2.processor.DOMLoaderException;
-//import org.eclipse.wst.xml.xpath2.processor.DynamicError;
-//import org.eclipse.wst.xml.xpath2.processor.ResultSequenceFactory;
-//import org.eclipse.wst.xml.xpath2.processor.XercesLoader;
-//import org.eclipse.wst.xml.xpath2.processor.internal.Focus;
-//import org.eclipse.wst.xml.xpath2.processor.internal.function.AbstractFunction;
-//import org.eclipse.wst.xml.xpath2.processor.internal.function.FunctionLibrary;
-//import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType;
-//import org.eclipse.wst.xml.xpath2.processor.internal.types.xerces.XercesTypeModel;
-//import org.w3c.dom.Document;
-//import org.w3c.dom.Node;
-//
-///**
-// * The default implementation of a Dynamic Context.
-// * 
-// * Initializes and provides functionality of a dynamic context according to the
-// * XPath 2.0 specification.
-// */
-//public class ContextBuilder implements StaticContext, DynamicContext {
-//
-//	private ResultSequence _focus;
-//	private org.eclipse.wst.xml.xpath2.api.types.XSDuration _tz;
-//	private Map _loaded_documents;
-//	private GregorianCalendar _current_date_time;
-//	private String _default_collation_name = CODEPOINT_COLLATION;
-//	private CollationProvider _collation_provider;
-//
-//	/**
-//	 * Constructor.
-//	 * 
-//	 * @param schema
-//	 *            Schema information of document. May be null
-//	 * @param doc
-//	 *            Document [root] node of XML source.
-//	 */
-//	public ContextBuilder(XSModel schema, Document doc) {
-//		this(new XercesTypeModel(schema));
-//	}
-//
-//	/**
-//	 * Constructor.
-//	 * 
-//	 * @param schema
-//	 *            Schema information of document. May be null
-//	 * @param doc
-//	 *            Document [root] node of XML source.
-//	 * @since 1.2
-//	 */
-//	public ContextBuilder(TypeModel schema) {
-//
-//		_focus = null;
-//		_tz = new XSDayTimeDurationImpl(0, 5, 0, 0, true);
-//		_loaded_documents = new HashMap();
-//	}
-//	/**
-//	 * Reads the day from a TimeDuration type
-//	 * 
-//	 * @return an xs:integer _tz
-//	 * @since 1.1
-//	 */
-//	public XSDuration getTimezoneOffset() {
-//		return _tz;
-//	}
-//
-//	/**
-//	 * Gets the Current stable date time from the dynamic context.
-//	 * @since 1.1
-//	 * @see org.eclipse.wst.xml.xpath2.api.DynamicContext#get_current_time()
-//	 */
-//	public GregorianCalendar getCurrentDateTime() {
-//		if (_current_date_time == null) {
-//			_current_date_time = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
-//		}
-//		return _current_date_time;
-//	}
-//	
-//	/**
-//	 * Changes the current focus.
-//	 * 
-//	 * @param f
-//	 *            focus to set
-//	 */
-//	public void set_focus(Focus f) {
-//		_focus = f;
-//	}
-//
-//	/**
-//	 * Return the focus
-//	 * 
-//	 * @return _focus
-//	 */
-//	public Focus focus() {
-//		return _focus;
-//	}
-//
-//	/**
-//	 * Retrieve context item that is in focus
-//	 * 
-//	 * @return an AnyType result from _focus.context_item()
-//	 */
-//	public AnyType getContextItem() {
-//		return _focus.context_item();
-//	}
-//
-//	/**
-//	 * Retrieve the position of the focus
-//	 * 
-//	 * @return an integer result from _focus.position()
-//	 */
-//	public int context_position() {
-//		return _focus.position();
-//	}
-//
-//	/**
-//	 * Retrieve the position of the last focus
-//	 * 
-//	 * @return an integer result from _focus.last()
-//	 */
-//	public int last() {
-//		return _focus.last();
-//	}
-//
-//	public Object getVariable(XSQName name) {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//	
-//	/**
-//	 * Retrieve the variable name
-//	 * 
-//	 * @return an AnyType result from get_var(name) or return NULL
-//	 * @since 1.2
-//	 */
-//	public Object get_variable2(XSQName name) {
-//		// XXX: built-in variables
-//		if ("fs".equals(name.prefix())) {
-//			if (name.local().equals("dot"))
-//				return getContextItem();
-//
-//			return null;
-//		}
-//		return get_var(name);
-//	}
-//
-//	/**
-//	 * 
-//	 * @return a ResultSequence from funct.evaluate(args)
-//	 */
-//	public ResultSequence evaluateFunction(XSQName name, Collection args)
-//			throws DynamicError {
-//		AbstractFunction funct = function(name, args.size());
-//
-//		assert funct != null;
-//
-//		return funct.evaluate(args);
-//	}
-//
-//	/**
-//	 * Adds function definitions.
-//	 * 
-//	 * @param fl
-//	 *            Function library to add.
-//	 * 
-//	 */
-//	@Override
-//	public void add_function_library(FunctionLibrary fl) {
-//		super.add_function_library(fl);
-//		fl.set_dynamic_context(this);
-//	}
-//
-//	/**
-//	 * get document
-//	 * 
-//	 * @return a ResultSequence from ResultSequenceFactory.create_new()
-//	 * @since 1.1
-//	 */
-//	public ResultSequence getDocument(URI resolved) {
-//		Document doc = null;
-//		if (_loaded_documents.containsKey(resolved)) {
-//			 //tried before
-//			doc = (Document)_loaded_documents.get(resolved);
-//		} else {
-//			doc = retrieve_doc(resolved);
-//			_loaded_documents.put(resolved, doc);
-//		}
-//
-//		if (doc == null)
-//			return null;
-//
-//		return ResultSequenceFactory.create_new(new DocType(doc, getTypeModel(doc)));
-//	}
-//	/**
-//	 * @since 1.1
-//	 */
-//	public URI resolveUri(String uri) {
-//		try {
-//			URI realURI = URI.create(uri);
-//			if (realURI.isAbsolute()) {
-//				return realURI;
-//			} else {
-//				URI baseURI = URI.create(base_uri().getStringValue());
-//				return baseURI.resolve(uri);
-//			}
-//		} catch (IllegalArgumentException iae) {
-//			return null;
-//		}
-//	}
-//
-//	// XXX make it nice, and move it out as a utility function
-//	private Document retrieve_doc(URI uri) {
-//		try {
-//			DOMLoader loader = new XercesLoader();
-//			loader.set_validating(false);
-//
-//			Document doc = loader.load(new URL(uri.toString()).openStream());
-//			doc.setDocumentURI(uri.toString());
-//			return doc;
-//		} catch (DOMLoaderException e) {
-//			return null;
-//		} catch (FileNotFoundException e) {
-//			return null;
-//		} catch (MalformedURLException e) {
-//			return null;
-//		} catch (IOException e) {
-//			return null;
-//		}
-//	}
-//
-//	/**
-//	 * Sets the value of a variable.
-//	 * 
-//	 * @param var
-//	 *            Variable name.
-//	 * @param val
-//	 *            Variable value.
-//	 */
-//	@Override
-//	public void set_variable(XSQName var, AnyType val) {
-//		super.set_variable(var, val);
-//	}
-//	
-//	
-//	/*
-//	 * Set a XPath2 sequence into a variable.
-//	 */
-//	/**
-//	 * @since 1.2
-//	 */
-//	public void set_variable(XSQName var, ResultSequence val) {
-//		super.set_variable(var, val);
-//	}
-//
-//	/**
-//	 * @since 1.1
-//	 */
-//	public void set_default_collation(String _default_collation) {
-//		this._default_collation_name = _default_collation;
-//	}
-//
-//	/**
-//	 * @since 1.1
-//	 */
-//	public String getDefaultCollationName() {
-//		return _default_collation_name;
-//	}
-//
-//	// We are explicitly NOT using generics here, in anticipation of JDK1.4 compatibility
-//	private static Comparator CODEPOINT_COMPARATOR = new Comparator() {
-//		
-//		public int compare(Object o1, Object o2) {
-//			return ((String)o1).compareTo((String)o2);
-//		}
-//	};
-//	
-//	/**
-//	 * @since 1.1
-//	 * 
-//	 */
-//	public Comparator<Object> get_collation(String uri) {
-//		if (CODEPOINT_COLLATION.equals(uri)) return CODEPOINT_COMPARATOR;
-//		
-//		return _collation_provider != null ? _collation_provider.get_collation(uri) : null;
-//	}
-//	
-//	/**
-//	 * 
-//	 * 
-//	 * @param provider
-//	 * @since 1.1
-//	 */
-//	public void set_collation_provider(CollationProvider provider) {
-//		this._collation_provider = provider;
-//	}
-//	
-//	/**
-//	 * Use focus().position() to retrieve the value.
-//	 * @deprecated  This will be removed in a future version use focus().position().
-//	 */
-//	@Deprecated
-//	public int node_position(Node node) {
-//	  // unused parameter!
-//	  return _focus.position();	
-//	}
-//	
-//	/**
-//	 * @since 1.2
-//	 */
-//	@Override
-//	public TypeModel getTypeModel(Node node) {
-//		return super.getTypeModel(node);
-//	}
-//
-//	public NodeType getLimitNode() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public ResultSequence getVariable(QName name) {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public ResultSequence evaluateFunction(String name, Collection args) {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public DocType getDocument(String uri) {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public boolean isXPath1Compatible() {
-//		// TODO Auto-generated method stub
-//		return false;
-//	}
-//
-//	public TypeDefinition getInitialContextType() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public NamespaceContext getNamespaceContext() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public String getDefaultNamespace() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public String getDefaultFunctionNamespace() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public DOMTypeProvider getTypeProvider() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public Function resolveFunction(String name, int arity) {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public URI getBaseUri() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public Collection getFunctionLibraries() {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//
-//	public TypeDefinition getCollectionType(String collectionName) {
-//		// TODO Auto-generated method stub
-//		return null;
-//	}
-//}
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/DynamicContextBuilder.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/DynamicContextBuilder.java
index 0c664fd..d6cf80d 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/DynamicContextBuilder.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/DynamicContextBuilder.java
@@ -1,6 +1,22 @@
+/*******************************************************************************
+ * Copyright (c) 2011, Jesper Steen Moller, and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Jesper Steen Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
+ *******************************************************************************/
+
 package org.eclipse.wst.xml.xpath2.processor.util;
 
-import java.util.Collection;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Map;
@@ -14,6 +30,9 @@
 import org.eclipse.wst.xml.xpath2.api.DynamicContext;
 import org.eclipse.wst.xml.xpath2.api.ResultSequence;
 import org.eclipse.wst.xml.xpath2.api.StaticContext;
+import org.eclipse.wst.xml.xpath2.processor.DOMLoader;
+import org.eclipse.wst.xml.xpath2.processor.XercesLoader;
+import org.eclipse.wst.xml.xpath2.processor.internal.function.FnCollection;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
@@ -35,12 +54,21 @@
 			throw new RuntimeException("Cannot initialize XML datatypes", e);
 		}
 	}
-	private Duration _tz = _datatypeFactory.newDuration(0);
-	private Map/*<String, DocType>*/ documents;
+	private TimeZone _systemTimezone = TimeZone.getDefault();
+	
+	private Duration _tz = _datatypeFactory.newDuration(_systemTimezone.getRawOffset());
 	private GregorianCalendar _currentDateTime;
 	
-	private Map/*<QName,ResultSequence>*/ _variables = new HashMap/*<QName,ResultSequence>*/();
-	private StaticContext _staticContext;
+	private Map<QName,ResultSequence> _variables = new HashMap<QName,ResultSequence>();
+	private final StaticContext _staticContext;
+
+	private Map<String, ResultSequence> _collections;
+
+	private Map<URI, Document> _loaded_documents = new HashMap<URI, Document>();
+
+	public DynamicContextBuilder(StaticContext sc) {
+		_staticContext = sc;
+	}
 	
 	/**
 	 * Reads the day from a TimeDuration type
@@ -69,34 +97,69 @@
 		return (ResultSequence) _variables.get(name);
 	}
 
-	public ResultSequence evaluateFunction(QName name, Collection args) {
-		
-		// TODO Auto-generated method stub
-		return null;
+	public Document getDocument(URI resolved) {
+		Document doc = null;
+		if (_loaded_documents.containsKey(resolved)) {
+			 //tried before
+			doc = (Document)_loaded_documents.get(resolved);
+		} else {
+			doc = retrieve_doc(resolved);
+			_loaded_documents.put(resolved, doc);
+		}
+
+		return doc;
 	}
 
-	public Document getDocument(String uri) {
-		// TODO Auto-generated method stub
-		return null;
+	// XXX make it nice, and move it out as a utility function
+	private Document retrieve_doc(URI uri) {
+		try {
+			DOMLoader loader = new XercesLoader();
+			loader.set_validating(false);
+
+			Document doc = loader.load(new URL(uri.toString()).openStream());
+			doc.setDocumentURI(uri.toString());
+			return doc;
+		} catch (FileNotFoundException e) {
+			return null;
+		} catch (MalformedURLException e) {
+			return null;
+		} catch (IOException e) {
+			return null;
+		}
 	}
 
-	public String resolveUri(String uri) {
-		return uri;
+	public URI resolveUri(String uri) {
+		try {
+			URI realURI = URI.create(uri);
+			if (realURI.isAbsolute()) {
+				return realURI;
+			} else {
+				return _staticContext.getBaseUri().resolve(uri);
+			}
+		} catch (IllegalArgumentException iae) {
+			return null;
+		}
 	}
 
-	public DynamicContextBuilder withStaticContext(StaticContext sc) {
-		_staticContext = sc;
-		return this;
-	}
-
-	public Map getCollections() {
-		// TODO Auto-generated method stub
-		return null;
+	public Map<String, ResultSequence> getCollections() {
+		return _collections;
 	}
 
 	public ResultSequence getDefaultCollection() {
-		// TODO Auto-generated method stub
-		return null;
+		return getCollections().get(FnCollection.DEFAULT_COLLECTION_URI);
+	}
+
+	public DynamicContextBuilder withVariable(javax.xml.namespace.QName qName, ResultSequence values) {
+		this._variables.put(qName, values);
+		return this;
 	}
 	
+	public DynamicContextBuilder withTimezoneOffset(Duration d) {
+		this._tz = d;
+		return this;
+	}
+
+	public void withCollections(Map<String, ResultSequence> map) {
+		this._collections = map;
+	}
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/ResultSequenceUtil.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/ResultSequenceUtil.java
new file mode 100644
index 0000000..bf03598
--- /dev/null
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/ResultSequenceUtil.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * Copyright (c) 2011, Jesper Steen Moller, and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Jesper Steen Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
+ *******************************************************************************/
+
+package org.eclipse.wst.xml.xpath2.processor.util;
+
+import org.eclipse.wst.xml.xpath2.api.ResultBuffer;
+import org.eclipse.wst.xml.xpath2.processor.ResultSequence;
+import org.eclipse.wst.xml.xpath2.processor.ResultSequenceFactory;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType;
+
+public class ResultSequenceUtil {
+
+	public static org.eclipse.wst.xml.xpath2.api.ResultSequence oldToNew(
+			ResultSequence result) {
+		if (result instanceof org.eclipse.wst.xml.xpath2.api.ResultSequence) 
+			return (org.eclipse.wst.xml.xpath2.api.ResultSequence)result;
+		
+		ResultBuffer buff = new ResultBuffer();
+		for (int i = 0; i < result.size(); ++i) {
+			buff.add(result.get(i));
+		}
+		return buff.getSequence();
+	}
+
+	public static ResultSequence newToOld(
+			org.eclipse.wst.xml.xpath2.api.ResultSequence result) {
+		if (result instanceof ResultSequence) 
+			return (ResultSequence)result;
+		
+		ResultSequence rs = ResultSequenceFactory.create_new();
+		for (int i = 0; i < result.size(); ++i) {
+			rs.add((AnyType) result.item(i));
+		}
+		return rs;
+	}
+
+}
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/StaticContextBuilder.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/StaticContextBuilder.java
index e8731ae..b91ba4e 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/StaticContextBuilder.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/util/StaticContextBuilder.java
@@ -1,6 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2011, Jesper Steen Moller, and others
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Jesper Steen Moller - initial API and implementation
+ *     Jesper Steen Moller  - bug 340933 - Migrate to new XPath2 API
+ *******************************************************************************/
+
 package org.eclipse.wst.xml.xpath2.processor.util;
 
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Collection;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -11,6 +24,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import javax.xml.XMLConstants;
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 
@@ -21,6 +35,10 @@
 import org.eclipse.wst.xml.xpath2.api.StaticVariableResolver;
 import org.eclipse.wst.xml.xpath2.api.typesystem.TypeDefinition;
 import org.eclipse.wst.xml.xpath2.api.typesystem.TypeModel;
+import org.eclipse.wst.xml.xpath2.processor.function.FnFunctionLibrary;
+import org.eclipse.wst.xml.xpath2.processor.function.XSCtrLibrary;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.builtin.BuiltinTypeDefinition;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.builtin.BuiltinTypeLibrary;
 import org.w3c.dom.Node;
 
 public class StaticContextBuilder implements StaticContext {
@@ -31,15 +49,20 @@
 	private String _default_namespace = "";
 	private String _default_function_namespace = XPATH_FUNCTIONS_NS;
 	private TypeDefinition _initialContextType = null; 
+	private String _defaultCollation = CollationProvider.CODEPOINT_COLLATION;
 
 	// key: String prefix, contents: String namespace
 	private Map/*<String, String>*/ _namespaces = new HashMap/*<String, String>*/();
-	private Map/*<String, FunctionLibrary>*/ _functionLibraries;
-	private boolean _useDefaultLibraries = true;
+	private Map<String, FunctionLibrary> _functionLibraries = new HashMap<String, FunctionLibrary>();
+	{
+		_functionLibraries.put(XPATH_FUNCTIONS_NS, new FnFunctionLibrary());
+		_functionLibraries.put(XSCtrLibrary.XML_SCHEMA_NS, new XSCtrLibrary());
+	}
 
 	private URI _base_uri;
 	private Map/*<String, TypeDefinition>*/ _documents = new HashMap/*<String, TypeDefinition>*/();
 	private Map/*<String, TypeDefinition>*/ _variableTypes = new HashMap/*<String, TypeDefinition>*/();
+	private Map/*<String, Short>*/ _variableCardinality = new HashMap/*<String, Short>*/();
 	private Map/*<String, TypeDefinition>*/ _collectionTypes = new HashMap/*<String, TypeDefinition>*/();
 
 	private Set/*<QName>*/ _hiddenFunctions = new HashSet/*<QName>*/();
@@ -74,7 +97,9 @@
 			}
 			
 			public String getNamespaceURI(String prefix) {
-				return (String)_namespaces.get(prefix);
+				String ns = (String)_namespaces.get(prefix);
+				if (ns == null) ns = XMLConstants.NULL_NS_URI;
+				return ns;
 			}
 		};
 	}
@@ -158,15 +183,32 @@
 		_hiddenFunctions.add(functionToSuppress);
 		return this;
 	}
+	public StaticContextBuilder withoutFunction(QName ... functionsToSuppress) {
+		for (QName name : functionsToSuppress)
+			_hiddenFunctions.add(name);
+		return this;
+	}
 	
 	public TypeDefinition getDefaultCollectionType() {
-		// TODO Auto-generated method stub
-		return null;
+		return BuiltinTypeLibrary.XS_UNTYPED;
 	}
 
 	public StaticVariableResolver getInScopeVariables() {
-		// TODO Auto-generated method stub
-		return null;
+		return new StaticVariableResolver() {
+
+			public boolean isVariablePresent(QName name) {
+				return _variableTypes.containsKey(name);
+			}
+
+			public TypeDefinition getVariableType(QName name) {
+				return (TypeDefinition) _variableTypes.get(name);
+			}
+
+			public short getVariableOccurrence(QName name) {
+				return (Short) _variableCardinality.get(name);
+			}
+			
+		};
 	}
 
 	// We are explicitly NOT using generics here, in anticipation of JDK1.4 compatibility
@@ -180,7 +222,7 @@
 	private CollationProvider _collationProvider = new CollationProvider() {
 		
 		public String getDefaultCollation() {
-			return CollationProvider.CODEPOINT_COLLATION;
+			return _defaultCollation;
 		}
 		
 		public Comparator getCollation(String uri) {
@@ -188,7 +230,7 @@
 			return null;
 		}
 	};
-	
+
 	public CollationProvider getCollationProvider() {
 		return _collationProvider;
 	}
@@ -197,4 +239,28 @@
 		_collationProvider = cp;
 		return this;
 	}
+
+	public StaticContextBuilder withVariable(javax.xml.namespace.QName qName,
+			TypeDefinition type, short cardinality) {
+		_variableTypes.put(qName, type);
+		_variableCardinality.put(qName, cardinality);
+		return this;
+	}
+
+	public StaticContextBuilder withBaseUri(String string) throws URISyntaxException {
+		_base_uri = new URI(string);		
+		return this;
+	}
+
+	public StaticContextBuilder withFunctionLibrary(
+			String namespace,
+			FunctionLibrary fl) {
+		_functionLibraries.put(namespace, fl);
+		return this;
+	}
+
+	public StaticContextBuilder withDefaultCollation(String uri) {
+		this._defaultCollation = uri;
+		return this;
+	}
 }