xmlbeans-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ce...@apache.org
Subject svn commit: r161808 - in xmlbeans/trunk: src/store/org/apache/xmlbeans/impl/store/Path.java test/src/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java
Date Mon, 18 Apr 2005 22:24:06 GMT
Author: cezar
Date: Mon Apr 18 15:24:05 2005
New Revision: 161808

URL: http://svn.apache.org/viewcvs?view=rev&rev=161808
Log:
Yana Kadiyska. Fixing XQuery invocation when cursor is not at a container. Cleaning up code.Adding
relevant test

Added:
    xmlbeans/trunk/test/src/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java
Modified:
    xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Path.java

Modified: xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Path.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Path.java?view=diff&r1=161807&r2=161808
==============================================================================
--- xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Path.java (original)
+++ xmlbeans/trunk/src/store/org/apache/xmlbeans/impl/store/Path.java Mon Apr 18 15:24:05
2005
@@ -71,9 +71,9 @@
         return currentNodeVar;
     }
 
-    private static final int FORCE_XQRL = 0;
-    private static final int FORCE_XBEAN = 1;
-    private static final int FORCE_NEITHER = 2;
+    private static final int USE_XQRL = 0x02;
+    private static final int USE_XBEAN =0x01;
+    private static final int USE_SAXON =0x04;
 
     public static Path getCompiledPath(String pathExpr, XmlOptions options)
     {
@@ -81,82 +81,89 @@
 
         int force =
                 options.hasOption(_useXqrlForXpath)
-                ? FORCE_XQRL
+                ? USE_XQRL
                 : options.hasOption(_useXbeanForXpath)
-                ? FORCE_XBEAN
-                : FORCE_NEITHER;
+                ? USE_XBEAN
+                : USE_XBEAN|USE_XQRL|USE_SAXON; //set all bits
 
         return getCompiledPath(pathExpr, force, getCurrentNodeVar(options));
     }
 
-    static synchronized Path getCompiledPath(String pathExpr, int force, String currentVar)
+    static synchronized Path getCompiledPath(String pathExpr, int force,
+        String currentVar)
     {
-        if (force == FORCE_XQRL) {
-            Path path = (Path) _xqrlPathCache.get(pathExpr);
+        Path path = null;
+        Map namespaces = (force & USE_SAXON) != 0 ? new HashMap() : null;
 
-            if (path == null) {
-                path = createXqrlCompiledPath(pathExpr, currentVar);
-
-                if (path == null)
-                    throw new RuntimeException("Can't force XQRL: Can't find xqrl");
-
-                _xqrlPathCache.put(path._pathKey, path);
-            }
+        if ((force & USE_XBEAN) != 0)
+            path = (Path) _xbeanPathCache.get(pathExpr);
+        if (path == null && (force & USE_XBEAN) != 0)
+            path = (Path) _xqrlPathCache.get(pathExpr);
 
+        if (path != null)
             return path;
-        }
-        Map namespaces = new HashMap();
-        if (force == FORCE_XBEAN) {
-            Path path = (Path) _xbeanPathCache.get(pathExpr);
-
-            if (path == null) {
-                path = XbeanPath.create(pathExpr, currentVar, namespaces);
-
-                if (path == null)
-                    throw new XmlRuntimeException("Path too complex for XBean path engine:
" + pathExpr);
-
-                _xbeanPathCache.put(path._pathKey, path);
-            }
 
-            return path;
+        if ((force & USE_XBEAN) != 0)
+            path = getCompiledPathXbean(pathExpr, currentVar, namespaces);
+        if (path == null && (force & USE_XQRL) != 0 &&
+            !SaxonXBeansDelegate.bInstantiated)
+            path = getCompiledPathXqrl(pathExpr, currentVar);
+        if (path == null && (force & USE_SAXON) != 0)
+            path = getCompiledPathSaxon(pathExpr, currentVar,
+                namespaces);
+        if (path == null)
+        {
+            StringBuffer errMessage = new StringBuffer();
+            if ((force & USE_XBEAN) != 0)
+                errMessage.append(" Trying XBeans path engine...");
+            if ((force & USE_XQRL) != 0 && !SaxonXBeansDelegate.bInstantiated)
+                errMessage.append(" Trying XQRL...");
+            if ((force & USE_SAXON) != 0)
+                errMessage.append(" Trying Saxon...");
+            throw new RuntimeException(errMessage.toString()+" FAILED on "+pathExpr);
         }
+        return path;
+    }
 
-        assert force == FORCE_NEITHER;
-
-        Path path = (Path) _xbeanPathCache.get(pathExpr);
-
-        if (path == null)
-            path = (Path) _xqrlPathCache.get(pathExpr);
+    static private synchronized Path getCompiledPathXqrl(String pathExpr,
+        String currentVar)
+    {
+        Path path = createXqrlCompiledPath(pathExpr, currentVar);
+        if (path != null)
+            _xqrlPathCache.put(path._pathKey, path);
+        return path;
+    }
 
-        if (path == null) {
-            path = XbeanPath.create(pathExpr, currentVar, namespaces);
+    static private synchronized Path getCompiledPathXbean(String pathExpr,
+        String currentVar, Map namespaces)
+    {
+        Path path = XbeanPath.create(pathExpr, currentVar, namespaces);
+        if (path != null)
+            _xbeanPathCache.put(path._pathKey, path);
+        return path;
+    }
 
-            if (path != null)
-                _xbeanPathCache.put(path._pathKey, path);
-            else if (! SaxonXBeansDelegate.bInstantiated ){
-                path = createXqrlCompiledPath(pathExpr, currentVar);
-                if (path != null)
-                    _xqrlPathCache.put(path._pathKey, path);
-            }
-            //XQRL is not on the path either; this has to be Saxon
-            if (path == null) {
-                int offset =
-                        namespaces.get(XPath._NS_BOUNDARY) == null ?
-                        0 :
-                        ((Integer) namespaces.get(XPath._NS_BOUNDARY)).intValue();
-                namespaces.remove(XPath._NS_BOUNDARY);
-                path = SaxonPathImpl.create(pathExpr.substring(offset),
-                        currentVar,
-                        namespaces);
-                if (path != null)
-                    _xbeanPathCache.put(path._pathKey, path);
-            }
+    static private synchronized Path getCompiledPathSaxon(String pathExpr, String currentVar,
Map namespaces)
+    {
+        Path path = null;
+        if ( namespaces == null )  namespaces = new HashMap();
+        try{
+            XPath.compileXPath(pathExpr, currentVar, namespaces);
+        }catch (XPath.XPathCompileException e){
+            //do nothing, this function is only called to populate the namespaces map
         }
-        if (path == null)
-            throw new RuntimeException("Path too complex for xmlbeans: " + pathExpr);
+        int offset =
+            namespaces.get(XPath._NS_BOUNDARY) == null ?
+            0 :
+            ((Integer) namespaces.get(XPath._NS_BOUNDARY)).intValue();
+        namespaces.remove(XPath._NS_BOUNDARY);
+        path = SaxonPathImpl.create(pathExpr.substring(offset),
+            currentVar,
+            namespaces);
         return path;
     }
 
+
     public static synchronized String compilePath(String pathExpr, XmlOptions options)
     {
         return getCompiledPath(pathExpr, options)._pathKey;
@@ -196,8 +203,10 @@
             // attrs and elements.
 
             if (!c.isContainer() || _compiledPath.sawDeepDot())
-                return getCompiledPath(_pathKey, FORCE_XQRL, _currentVar).execute(c);
-
+            {
+                int force = USE_SAXON | USE_XQRL;
+                return getCompiledPath(_pathKey, force, _currentVar).execute(c);
+            }
             return new XbeanPathEngine(_compiledPath, c);
         }
 

Added: xmlbeans/trunk/test/src/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/test/src/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java?view=auto&rev=161808
==============================================================================
--- xmlbeans/trunk/test/src/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java (added)
+++ xmlbeans/trunk/test/src/xmlcursor/xpath/complex/checkin/ContainerCommentTest.java Mon
Apr 18 15:24:05 2005
@@ -0,0 +1,83 @@
+/*   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package xmlcursor.xpath.complex.checkin;
+
+import org.apache.xmlbeans.XmlCursor;
+import org.apache.xmlbeans.XmlObject;
+import junit.framework.TestCase;
+
+/**
+ * Date: Apr 12, 2005
+ * Time: 1:08:39 PM
+ * This class tests the execution of an XQuery where the
+ * starting point of the query is a comment
+ */
+public class ContainerCommentTest extends TestCase
+{
+    public static void testFunctionPathWithCursor()  throws Exception
+           {
+
+
+               XmlObject employees = XmlObject.Factory.parse(sXml);
+               String m_namespaceDeclaration =
+                   "declare namespace xq='http://xmlbeans.apache.org/samples/xquery/employees';";
+
+               boolean hasResults = false;
+               String[] names;
+
+               XmlCursor cursor = employees.newCursor();
+               cursor.toNextToken();
+
+               cursor.selectPath(m_namespaceDeclaration + "$this//xq:employee");
+               if (cursor.getSelectionCount() > 0)
+               {
+                   hasResults = true;
+                   cursor.toNextSelection();
+
+                   names = new String[cursor.getSelectionCount()];
+
+                   for (int i = 0; i < cursor.getSelectionCount(); i++)
+                   {
+                       XmlCursor nameCursor = cursor.newCursor();
+                       nameCursor.selectPath(m_namespaceDeclaration +
+                               "$this/xq:name/text()");
+                       nameCursor.toNextSelection();
+                       names[i] = nameCursor.getTextValue();
+                       cursor.toNextSelection();
+                       System.out.println(names[i]);
+                   }
+               }
+           }
+
+      static String prolog="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
+        "<!-- My Comment -->";
+    public static String sXml=prolog+
+        "<employees xmlns=\"http://xmlbeans.apache.org/samples/xquery/employees\">\n"
+
+                "  <employee>\n" +
+                "    <name>Fred Jones</name>\n" +
+                "    <address location=\"home\">\n" +
+                "      <street>900 Aurora Ave.</street>\n" +
+                "      <city>Seattle</city>\n" +
+                "      <state>WA</state>\n" +
+                "      <zip>98115</zip>\n" +
+                "    </address>\n" +
+                "    <phone location=\"work\">(425)555-5665</phone>\n" +
+                "    <phone location=\"home\">(206)555-5555</phone>\n" +
+                "    <phone location=\"mobile\">(206)555-4321</phone>\n" +
+                "  </employee>\n" +
+                "  </employees>";
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org


Mime
View raw message