[Bug 340936] Add adapter for old API to support old clients within reason
https://bugs.eclipse.org/bugs/show_bug.cgi?id=340936
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xml b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xml
index f4320b7..ec15721 100644
--- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xml
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xml
@@ -1,4 +1,4 @@
-<person id="100" dob="2006-12-10">

-  <fname>Mukul</fname>

-  <lname>Gandhi</lname>

+<person id="100" dob="2006-12-10">
+  <fname>Mukul</fname>
+  <lname>Gandhi</lname>
 </person>
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xsd b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xsd
index 3d4413d..58f6f8d 100644
--- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xsd
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134.xsd
@@ -1,14 +1,14 @@
-<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

- 

-  <xs:element name="person">

-   <xs:complexType>

-    <xs:sequence>

-     <xs:element name="fname" type = "xs:string" />

-     <xs:element name="lname" type = "xs:string" />

-    </xs:sequence>

-    <xs:attribute name="id" type="xs:integer" />

-    <xs:attribute name="dob" type="xs:date" />

-   </xs:complexType>

-  </xs:element>

- 

+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+ 
+  <xs:element name="person">
+   <xs:complexType>
+    <xs:sequence>
+     <xs:element name="fname" type = "xs:string" />
+     <xs:element name="lname" type = "xs:string" />
+    </xs:sequence>
+    <xs:attribute name="id" type="xs:integer" />
+    <xs:attribute name="dob" type="xs:date" />
+   </xs:complexType>
+  </xs:element>
+ 
 </xs:schema>
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xml b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xml
index cda672d..f945230 100644
--- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xml
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xml
@@ -1,6 +1,6 @@
-<person>

-  <id>100</id>

-  <fname>Mukul</fname>

-  <lname>Gandhi</lname>

-  <dob>2006-12-10</dob>

+<person>
+  <id>100</id>
+  <fname>Mukul</fname>
+  <lname>Gandhi</lname>
+  <dob>2006-12-10</dob>
 </person>
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xsd b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xsd
index b422e8b..8f91793 100644
--- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xsd
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bug276134_2.xsd
@@ -1,14 +1,14 @@
-<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

- 

-  <xs:element name="person">

-   <xs:complexType>

-    <xs:sequence>

-     <xs:element name="id" type = "xs:integer" />

-     <xs:element name="fname" type = "xs:string" />

-     <xs:element name="lname" type = "xs:string" />

-     <xs:element name="dob" type = "xs:date" />

-    </xs:sequence>

-   </xs:complexType>

-  </xs:element>

- 

+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+ 
+  <xs:element name="person">
+   <xs:complexType>
+    <xs:sequence>
+     <xs:element name="id" type = "xs:integer" />
+     <xs:element name="fname" type = "xs:string" />
+     <xs:element name="lname" type = "xs:string" />
+     <xs:element name="dob" type = "xs:date" />
+    </xs:sequence>
+   </xs:complexType>
+  </xs:element>
+ 
 </xs:schema>
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bugNilled.xsd b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bugNilled.xsd
index a7e2b6d..d1270cb 100644
--- a/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bugNilled.xsd
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/bugTestFiles/bugNilled.xsd
@@ -1,14 +1,14 @@
-<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

- 

-  <xs:element name="root">

-   <xs:complexType>

-    <xs:sequence>

-     <xs:element name="element1" type = "xs:integer" nillable="true"/>

-     <xs:element name="element2" type = "xs:integer" nillable="true"/>

-     <xs:element name="element3" type = "xs:integer" nillable="false"/>

-     <xs:any namespace="##any" processContents="skip"/>

-    </xs:sequence>

-   </xs:complexType>

-  </xs:element>

- 

+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+ 
+  <xs:element name="root">
+   <xs:complexType>
+    <xs:sequence>
+     <xs:element name="element1" type = "xs:integer" nillable="true"/>
+     <xs:element name="element2" type = "xs:integer" nillable="true"/>
+     <xs:element name="element3" type = "xs:integer" nillable="false"/>
+     <xs:any namespace="##any" processContents="skip"/>
+    </xs:sequence>
+   </xs:complexType>
+  </xs:element>
+ 
 </xs:schema>
\ No newline at end of file
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/newapi/CompleteNewApiTest.java b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/newapi/CompleteNewApiTest.java
new file mode 100644
index 0000000..b5b5622
--- /dev/null
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/newapi/CompleteNewApiTest.java
@@ -0,0 +1,155 @@
+package org.eclipse.wst.xml.xpath2.processor.test.newapi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Iterator;
+
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+
+import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer;
+import org.apache.xerces.xni.grammars.Grammar;
+import org.apache.xerces.xni.grammars.XMLGrammarDescription;
+import org.apache.xerces.xni.grammars.XMLGrammarPool;
+import org.apache.xerces.xni.grammars.XSGrammar;
+import org.apache.xerces.xs.ElementPSVI;
+import org.apache.xerces.xs.XSModel;
+import org.custommonkey.xmlunit.XMLConstants;
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.wst.xml.xpath2.api.DynamicContext;
+import org.eclipse.wst.xml.xpath2.api.StaticContext;
+import org.eclipse.wst.xml.xpath2.api.XPath2Expression;
+import org.eclipse.wst.xml.xpath2.processor.DOMLoader;
+import org.eclipse.wst.xml.xpath2.processor.Engine;
+import org.eclipse.wst.xml.xpath2.processor.Evaluator;
+import org.eclipse.wst.xml.xpath2.processor.ResultSequence;
+import org.eclipse.wst.xml.xpath2.processor.StaticError;
+import org.eclipse.wst.xml.xpath2.processor.XPathParserException;
+import org.eclipse.wst.xml.xpath2.processor.XercesLoader;
+import org.eclipse.wst.xml.xpath2.processor.ast.XPath;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.AnyType;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.XSBoolean;
+import org.eclipse.wst.xml.xpath2.processor.internal.types.xerces.XercesTypeModel;
+import org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder;
+import org.osgi.framework.Bundle;
+import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
+
+public class CompleteNewApiTest extends XMLTestCase {
+
+	protected Document domDoc = null;
+	protected Bundle bundle = null;
+
+	private DynamicContext dynamicContext = null;
+	
+	protected void setUp() throws Exception {
+		super.setUp();
+		bundle = Platform
+				.getBundle("org.w3c.xqts.testsuite");
+
+		if (bundle == null) {
+			System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
+		}
+		System.setProperty("javax.xml.validation.SchemaFactory:http://www.w3.org/2001/XMLSchema","org.apache.xerces.jaxp.validation.XMLSchemaFactory");
+	}
+
+	protected void loadDOMDocument(URL fileURL) throws IOException {
+		InputStream is = fileURL.openStream();
+		DOMLoader domloader = new XercesLoader();
+		domloader.set_validating(false);
+		domDoc = domloader.load(is);
+		domDoc.setDocumentURI(fileURL.toString());
+	}
+
+	protected XSModel getGrammar() {
+		ElementPSVI rootPSVI = (ElementPSVI) domDoc.getDocumentElement();
+		XSModel schema = rootPSVI.getSchemaInformation();
+		return schema;
+	}
+
+	private Schema getSchema(InputStream schemaIs) throws SAXException {
+		SchemaFactory sf = SchemaFactory
+				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+		Schema schema = sf.newSchema(new StreamSource(schemaIs));
+		return schema;
+	}
+
+	protected XSModel getGrammar(URL schemaURL) throws IOException,
+			SAXException {
+		InputStream schemaIs = schemaURL.openStream();
+		SchemaFactory sf = SchemaFactory
+				.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+		Schema schema = sf.newSchema(new StreamSource(schemaIs));
+		XSGrammarPoolContainer poolContainer = (XSGrammarPoolContainer) schema;
+		XMLGrammarPool pool = poolContainer.getGrammarPool();
+		Grammar[] grammars = pool
+				.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
+
+		XSGrammar[] xsGrammars = new XSGrammar[grammars.length];
+		System.arraycopy(grammars, 0, xsGrammars, 0, grammars.length);
+
+		return xsGrammars[0].toXSModel(xsGrammars);
+	}
+
+	protected String buildResultString(ResultSequence rs) {
+		String actual = new String();
+		Iterator iterator = rs.iterator();
+		while (iterator.hasNext()) {
+			AnyType anyType = (AnyType)iterator.next();
+			
+			actual = actual + anyType.string_value() + " ";
+		}
+
+		return actual.trim();
+	}
+	
+		
+	protected Object evaluateSimpleXPath(String xpath, StaticContext sc, Document doc, Class resultClass) {
+		XPath2Expression path;
+		try {
+			path = new Engine().parseExpression(xpath, sc);
+		}
+		catch (XPathParserException e) {
+			throw new RuntimeException("XPath parse: " + e.getMessage(), e);
+		}
+		catch (StaticError e) {
+			throw new RuntimeException("Static error: " + e.getMessage(), e);
+		}
+	
+		org.eclipse.wst.xml.xpath2.api.ResultSequence rs = path.evaluate(dynamicContext, doc != null ? new Object[] { doc } : new Object[0]);
+		assertEquals("Expected single result from \'" + xpath + "\'", 1, rs.size());
+		Object result = rs.value(0);
+		assertTrue("Exected XPath result instanceof class " + resultClass.getSimpleName() + " from \'" + xpath + "\', got " + result.getClass(), resultClass.isInstance(result));
+		return result;
+	}
+
+	public void testSimpleMath() throws Exception {
+//		String xpath = "($input-context/atomic:root/atomic:integer) union ($input-context/atomic:root/atomic:integer)";
+		String xpath = "2+2 = 4";
+		
+		Boolean b = (Boolean)evaluateSimpleXPath(xpath, new StaticContextBuilder(), null, Boolean.class);
+
+		assertEquals(Boolean.TRUE, b);
+	}
+
+	public void xtestNamesWhichAreKeywords() throws Exception {
+		// Bug 273719
+		URL fileURL = bundle.getEntry("/bugTestFiles/bug311480.xml");
+		loadDOMDocument(fileURL);
+
+		// Get XML Schema Information for the Document
+		XSModel schema = getGrammar();
+
+//		String xpath = "($input-context/atomic:root/atomic:integer) union ($input-context/atomic:root/atomic:integer)";
+		String xpath = "(/element/eq eq 'eq') or //child::xs:*";
+		
+		Boolean b = (Boolean)evaluateSimpleXPath(xpath, new StaticContextBuilder().withTypeModel(new XercesTypeModel(schema)), domDoc, Boolean.class);
+
+		assertEquals(Boolean.TRUE, b);
+	}
+
+
+}
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/newapi/ContextBuilderTest.java b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/newapi/ContextBuilderTest.java
new file mode 100644
index 0000000..f989bc6
--- /dev/null
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/newapi/ContextBuilderTest.java
@@ -0,0 +1,18 @@
+package org.eclipse.wst.xml.xpath2.processor.test.newapi;
+
+import junit.framework.TestCase;
+
+import org.eclipse.wst.xml.xpath2.processor.util.StaticContextBuilder;
+
+public class ContextBuilderTest extends TestCase {
+
+	
+	public void testReasonableDefaults() {
+		StaticContextBuilder scb = new StaticContextBuilder();
+		
+		assertFalse(scb.isXPath1Compatible());
+		assertEquals("", scb.getDefaultNamespace());
+		assertEquals(null, scb.getBaseUri());
+	}
+	
+}
diff --git a/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/types/BuiltinTypeTest.java b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/types/BuiltinTypeTest.java
new file mode 100644
index 0000000..7dc7526
--- /dev/null
+++ b/tests/org.eclipse.wst.xml.xpath2.processor.tests/src/org/eclipse/wst/xml/xpath2/processor/test/types/BuiltinTypeTest.java
@@ -0,0 +1,12 @@
+package org.eclipse.wst.xml.xpath2.processor.test.types;
+
+import org.eclipse.wst.xml.xpath2.processor.internal.types.builtin.BuiltinTypeLibrary;
+
+import junit.framework.TestCase;
+
+public class BuiltinTypeTest extends TestCase {
+
+	public void testCreateAtomic() {
+		BuiltinTypeLibrary.XS_BOOLEAN.constructNative(Boolean.TRUE);
+	}
+}