[338494] [xpath2] prohibiting xpath expressions starting with / or // to be parsed
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/JFlexCupParser.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/JFlexCupParser.java
index 76c554c..7a1bfff 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/JFlexCupParser.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/JFlexCupParser.java
@@ -6,19 +6,22 @@
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
- *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
+ *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0
+ *     Bug 338494    - prohibiting xpath expressions starting with / or // to be parsed. 
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor;
 
-import java_cup.runtime.*;
-import java.io.*;
-
 import org.eclipse.wst.xml.xpath2.processor.ast.XPath;
 import org.eclipse.wst.xml.xpath2.processor.internal.CupError;
 import org.eclipse.wst.xml.xpath2.processor.internal.JFlexError;
-import org.eclipse.wst.xml.xpath2.processor.internal.XPathFlex;
 import org.eclipse.wst.xml.xpath2.processor.internal.XPathCup;
+import org.eclipse.wst.xml.xpath2.processor.internal.XPathCupRestricted;
+import org.eclipse.wst.xml.xpath2.processor.internal.XPathFlex;
+
+import java.io.StringReader;
+
+import java_cup.runtime.Symbol;
 
 /**
  * JFlexCupParser parses the xpath expression
@@ -41,7 +44,6 @@
 		try {
 			Symbol res = p.parse();
 			return (XPath) res.value;
-
 		} catch (JFlexError e) {
 			throw new XPathParserException("JFlex lexer error: " + e.reason());
 		} catch (CupError e) {
@@ -55,4 +57,37 @@
 			throw new XPathParserException(err);
 		}
 	}
+	
+	/**
+	 * Tries to parse the xpath expression
+	 * 
+	 * @param xpath
+	 *            is the xpath string.
+	 * @param isRootlessAccess
+	 *            if 'true' then PsychoPath engine can't parse xpath expressions starting with / or //.
+	 * @throws XPathParserException.
+	 * @return the xpath value.
+	 */
+	public XPath parse(String xpath, boolean isRootlessAccess) throws XPathParserException {
+
+		XPathFlex lexer = new XPathFlex(new StringReader(xpath));
+
+		XPathCup p = null;
+		if (isRootlessAccess) {
+			p = new XPathCupRestricted(lexer); 
+		}
+		else {
+			p = new XPathCup(lexer); 
+		}
+		try {
+			Symbol res = p.parse();
+			return (XPath) res.value;
+		} catch (JFlexError e) {
+			throw new XPathParserException("JFlex lexer error: " + e.reason());
+		} catch (CupError e) {
+			throw new XPathParserException("CUP parser error: " + e.reason());
+		} catch (Exception e) {
+			throw new XPathParserException(e.getMessage());
+		}
+	}
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/XPathParser.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/XPathParser.java
index 7ffa22e..3cbce4c 100644
--- a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/XPathParser.java
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/XPathParser.java
@@ -6,7 +6,8 @@
  * http://www.eclipse.org/legal/epl-v10.html
  *
  * Contributors:
- *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0 
+ *     Andrea Bittau - initial API and implementation from the PsychoPath XPath 2.0
+ *     Bug 338494    - prohibiting xpath expressions starting with / or // to be parsed.
  *******************************************************************************/
 
 package org.eclipse.wst.xml.xpath2.processor;
@@ -28,4 +29,17 @@
 	 * @return The parsed XPath.
 	 */
 	public XPath parse(String xpath) throws XPathParserException;
+	
+	/**
+	 * Constructor for the XPath parser interface.
+	 * 
+	 * @param xpath
+	 *            is the input XPath to be parsed.
+	 * @param isRootlessAccess
+	 *            if 'true' then PsychoPath engine can't parse xpath expressions starting with / or //.
+	 * @throws XPathParserException
+	 *             XPath parser exception.
+	 * @return The parsed XPath.
+	 */
+	public XPath parse(String xpath, boolean isRootlessAccess) throws XPathParserException;
 }
diff --git a/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/XPathCupRestricted.java b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/XPathCupRestricted.java
new file mode 100644
index 0000000..b267d3f
--- /dev/null
+++ b/bundles/org.eclipse.wst.xml.xpath2.processor/src/org/eclipse/wst/xml/xpath2/processor/internal/XPathCupRestricted.java
@@ -0,0 +1,44 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Mukul Gandhi, 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:
+ *     Mukul Gandhi - initial API and implementation
+ *     Bug 338494   - prohibiting xpath expressions starting with / or // to be parsed.
+ *******************************************************************************/
+
+package org.eclipse.wst.xml.xpath2.processor.internal;
+
+public class XPathCupRestricted extends XPathCup {
+
+  /** Default constructor. */
+  public XPathCupRestricted() {super();}
+  
+  /** Constructor which sets the default scanner. */
+  public XPathCupRestricted(java_cup.runtime.Scanner s) {super(s);}
+
+  /** Invoke a user supplied parse action. */
+  public java_cup.runtime.Symbol do_action(
+    int                        act_num,
+    java_cup.runtime.lr_parser parser,
+    java.util.Stack            stack,
+    int                        top)
+    throws java.lang.Exception
+  {
+    if (act_num == 67 || act_num == 68 || act_num == 69) {
+    	// these action numbers correspond to following grammar productions,
+    	// a) FORWARD_SLASH
+    	// b) FORWARD_SLASH RelativePathExpr
+    	// c) FORWARD_SLASHSLASH RelativePathExpr
+    	throw new Exception("Expression starts with / or //");
+    }
+    else {
+       return action_obj.CUP$XPathCup$do_action(act_num, parser, stack, top);
+    }
+  }
+
+}
+