You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by ce...@apache.org on 2008/04/17 21:55:54 UTC

svn commit: r649251 - in /xmlbeans/trunk: src/common/org/apache/xmlbeans/impl/common/ src/typeimpl/org/apache/xmlbeans/impl/values/ test/src/xmlcursor/checkin/ test/src/xmlobject/checkin/

Author: cezar
Date: Thu Apr 17 12:55:52 2008
New Revision: 649251

URL: http://svn.apache.org/viewvc?rev=649251&view=rev
Log:
Fix for XMLBEANS-369 selectPath with XMLBeans engine returns incorrect results
Set optimizes for unsync flag.
Adding tests for xpath fix, and tests for CData.


Added:
    xmlbeans/trunk/test/src/xmlobject/checkin/CDataTest.java
    xmlbeans/trunk/test/src/xmlobject/checkin/XPathTest.java
Modified:
    xmlbeans/trunk/src/common/org/apache/xmlbeans/impl/common/XPath.java
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java
    xmlbeans/trunk/test/src/xmlcursor/checkin/IsInSameDocumentTest.java

Modified: xmlbeans/trunk/src/common/org/apache/xmlbeans/impl/common/XPath.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/common/org/apache/xmlbeans/impl/common/XPath.java?rev=649251&r1=649250&r2=649251&view=diff
==============================================================================
--- xmlbeans/trunk/src/common/org/apache/xmlbeans/impl/common/XPath.java (original)
+++ xmlbeans/trunk/src/common/org/apache/xmlbeans/impl/common/XPath.java Thu Apr 17 12:55:52 2008
@@ -129,12 +129,14 @@
                 return (QName) ExecutionContext.this._stack.get( _stack.size() - 1 - i );
             }
 
+            // goes back to the begining of the sequence since last // wildcard
             private void backtrack ( )
             {
                 assert _curr != null;
                 
                 if (_curr._hasBacktrack)
-                {
+                {   // _backtrack seems to be a pointer to the step that follows a // wildcard
+                    // ex: for .//b/c/d steps c and d should backtrack to b in case there isn't a match 
                     _curr = _curr._backtrack;
                     return;
                 }
@@ -176,6 +178,7 @@
             
             int element ( QName name )
             {
+                //System.out.println("  Path.element: " + name);
                 _prev.add( _curr );
 
                 if (_curr == null)
@@ -190,6 +193,7 @@
                     
                     backtrack();
                     
+                    //System.out.println("    element - HIT " + _curr._flags);
                     return _curr == null ? HIT : HIT | _curr._flags;
                 }
 
@@ -220,6 +224,7 @@
 
             void end ( )
             {
+                //System.out.println("  Path.end ");
                 _curr = (Step) _prev.remove( _prev.size() - 1 );
             }
             
@@ -692,10 +697,16 @@
                 
                 if (tokenize( "." ))
                     deepDot = deepDot || deep;
-                else if (tokenize( "child", "::" ) && (name = tokenizeQName()) != null)
-                    steps = addStep( deep, false, name, steps );
-                else if ((name = tokenizeQName()) != null)
-                    steps = addStep( deep, false, name, steps );
+                else
+                {
+                    tokenize( "child", "::" );
+                    if ((name = tokenizeQName()) != null)
+                    {
+                        steps = addStep( deep, false, name, steps );
+                        deep = false; // only this step needs to be deep
+                        // other folowing steps will be deep only if they are preceded by // wildcard
+                    }
+                }
 
                 if (tokenize( "//" ))
                 {

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java?rev=649251&r1=649250&r2=649251&view=diff
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/values/XmlObjectBase.java Thu Apr 17 12:55:52 2008
@@ -122,7 +122,9 @@
 
     private boolean preCheck()
     {
-        if (has_store())
+//        if ( isImmutable() )
+//            return true;
+        if ( has_store() )
             return get_store().get_locale().noSync();
         return false;
     }
@@ -2000,42 +2002,75 @@
             set(obj.stringValue());
         else
         {
-            boolean acquired = false;
-            try
+            boolean noSyncThis = preCheck();
+            boolean noSyncObj  = obj.preCheck();
+
+            if (monitor() == obj.monitor())             // both are in the same locale
+            {
+                if (noSyncThis)                         // the locale is not sync
+                    newObj = setterHelper( obj );
+                else                                    // the locale is sync
+                {
+                    synchronized (monitor()) {
+                        newObj = setterHelper( obj );
+                    }
+                }
+            }
+            else                                        // on different locale's
             {
-                if (monitor() == obj.monitor())
+                if (noSyncThis)
                 {
-                    synchronized (monitor())
+                    if (noSyncObj)                      // both unsync
                     {
                         newObj = setterHelper( obj );
                     }
+                    else                                // only obj is sync
+                    {
+                        synchronized (obj.monitor()) {
+                            newObj = setterHelper( obj );
+                        }
+                    }
                 }
                 else
                 {
-                    // about to grab two locks: don't deadlock ourselves
-                    GlobalLock.acquire();
-                    acquired = true;
-
-                    synchronized (monitor())
+                    if (noSyncObj)                      // only this is sync
+                    {
+                        synchronized (monitor()) {
+                            newObj = setterHelper( obj );
+                        }
+                    }
+                    else                                // both are sync can't avoid the global lock
                     {
-                        synchronized (obj.monitor())
+                        boolean acquired = false;
+
+                        try
                         {
-                            GlobalLock.release();
-                            acquired = false;
+                            // about to grab two locks: don't deadlock ourselves
+                            GlobalLock.acquire();
+                            acquired = true;
 
-                            newObj = setterHelper( obj );
+                            synchronized (monitor())
+                            {
+                                synchronized (obj.monitor())
+                                {
+                                    GlobalLock.release();
+                                    acquired = false;
+
+                                    newObj = setterHelper( obj );
+                                }
+                            }
+                        }
+                        catch (InterruptedException e)
+                        {
+                            throw new XmlRuntimeException(e);
+                        }
+                        finally
+                        {
+                            if (acquired)
+                                GlobalLock.release();
                         }
                     }
                 }
-            }
-            catch (InterruptedException e)
-            {
-                throw new XmlRuntimeException(e);
-            }
-            finally
-            {
-                if (acquired)
-                    GlobalLock.release();
             }
         }
 

Modified: xmlbeans/trunk/test/src/xmlcursor/checkin/IsInSameDocumentTest.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/src/xmlcursor/checkin/IsInSameDocumentTest.java?rev=649251&r1=649250&r2=649251&view=diff
==============================================================================
--- xmlbeans/trunk/test/src/xmlcursor/checkin/IsInSameDocumentTest.java (original)
+++ xmlbeans/trunk/test/src/xmlcursor/checkin/IsInSameDocumentTest.java Thu Apr 17 12:55:52 2008
@@ -16,24 +16,13 @@
 
 package xmlcursor.checkin;
 
-import org.apache.xmlbeans.XmlOptions;
-import junit.framework.*;
-import junit.framework.Assert.*;
-
-import java.io.*;
-
-import org.apache.xmlbeans.XmlObject;
+import junit.framework.Test;
+import junit.framework.TestSuite;
 import org.apache.xmlbeans.XmlCursor;
-import org.apache.xmlbeans.XmlBeans;
 import org.apache.xmlbeans.XmlCursor.TokenType;
-import org.apache.xmlbeans.XmlDocumentProperties;
-import org.apache.xmlbeans.XmlCursor.XmlBookmark;
-
-import javax.xml.namespace.QName;
-
-import xmlcursor.common.*;
-
-import java.net.URL;
+import org.apache.xmlbeans.XmlObject;
+import xmlcursor.common.BasicCursorTestCase;
+import xmlcursor.common.Common;
 
 
 /**

Added: xmlbeans/trunk/test/src/xmlobject/checkin/CDataTest.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/src/xmlobject/checkin/CDataTest.java?rev=649251&view=auto
==============================================================================
--- xmlbeans/trunk/test/src/xmlobject/checkin/CDataTest.java (added)
+++ xmlbeans/trunk/test/src/xmlobject/checkin/CDataTest.java Thu Apr 17 12:55:52 2008
@@ -0,0 +1,111 @@
+/*   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 xmlobject.checkin;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlOptions;
+import org.apache.xmlbeans.SystemProperties;
+import org.apache.xmlbeans.XmlException;
+
+import dumbNS.RootDocument.Root;
+import dumbNS.RootDocument;
+
+import tools.util.Util;
+import tools.util.ResourceUtil;
+import tools.util.JarUtil;
+import xmlcursor.common.Common;
+
+
+/**
+ * Test for finner CData control feature.
+ */
+public class CDataTest
+    extends TestCase
+{
+    static final String NL = SystemProperties.getProperty("line.separator")!=null ?
+        SystemProperties.getProperty("line.separator") :
+        (System.getProperty("line.separator") != null ? System.getProperty("line.separator") : "\n");
+
+    public CDataTest(String name)
+    {
+        super(name);
+    }
+
+    public void testCData1()
+            throws Exception
+    {
+        String xmlText = "<a><![CDATA[cdata text]]></a>";
+
+        checkCData(xmlText, xmlText, xmlText);
+    }
+
+    public void testCData2()
+            throws Exception
+    {
+        String xmlText = "<a>" + NL +
+                "<b><![CDATA[cdata text]]> regular text</b>" + NL +
+                "</a>";
+        String expected1 = "<a>\n" +
+                           "<b><![CDATA[cdata text regular text]]></b>\n" +
+                           "</a>";
+        String expected2 = "<a>" + NL +
+                           "  <b><![CDATA[cdata text regular text]]></b>" + NL +
+                           "</a>";
+
+        checkCData(xmlText, expected1, expected2);
+    }
+
+    public void testCData3()
+            throws Exception
+    {
+        String xmlText = "<a>\n" +
+                "<c>text <![CDATA[cdata text]]></c>\n" +
+                "</a>";
+        String expected1 = "<a>\n" +
+                           "<c>text cdata text</c>\n" +
+                           "</a>";
+        String expected2 = "<a>" + NL +
+                           "  <c>text cdata text</c>" + NL +
+                           "</a>";
+
+        checkCData(xmlText, expected1, expected2);
+    }
+
+    private void checkCData(String xmlText, String expected1, String expected2)
+            throws XmlException
+    {
+        System.out.println("\ninput:\n" + xmlText);
+
+        XmlOptions opts = new XmlOptions();
+        opts.setUseCDataBookmarks();
+
+        XmlObject xo = XmlObject.Factory.parse( xmlText , opts);
+
+        String result1 = xo.xmlText(opts);
+        System.out.println("result xmlText:\n" + result1);
+        assertEquals("xmlText", expected1, result1);
+
+        opts.setSavePrettyPrint();
+        String result2 = xo.xmlText(opts);
+        System.out.println("result prettyPrint:\n" + result2);
+        assertEquals("prettyPrint", expected2, result2);
+    }
+}

Added: xmlbeans/trunk/test/src/xmlobject/checkin/XPathTest.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/src/xmlobject/checkin/XPathTest.java?rev=649251&view=auto
==============================================================================
--- xmlbeans/trunk/test/src/xmlobject/checkin/XPathTest.java (added)
+++ xmlbeans/trunk/test/src/xmlobject/checkin/XPathTest.java Thu Apr 17 12:55:52 2008
@@ -0,0 +1,124 @@
+package xmlobject.checkin;
+
+import junit.framework.TestCase;
+import org.apache.xmlbeans.XmlException;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlCursor;
+
+/**
+ * Created by Cezar Andrei (cezar dot andrei at gmail dot com)
+ * Date: Apr 10, 2008
+ */
+public class XPathTest
+        extends TestCase
+{
+    public XPathTest(String name)
+    {
+        super(name);
+    }
+
+    public void testPath()
+        throws XmlException
+    {
+        final XmlObject obj = XmlObject.Factory.parse(
+                "<a>" +
+                  "<b>" +
+                    "<c>val1</c>" +
+                    "<d><c>val2</c></d>" +
+                  "</b>" +
+                  "<c>val3</c>" +
+                "</a>");
+        final XmlCursor c = obj.newCursor();
+
+        c.selectPath(".//b/c");
+
+        int selCount = c.getSelectionCount();
+        assertEquals("SelectionCount", 1, selCount);
+
+        while ( c.hasNextSelection() )
+        {
+            c.toNextSelection();
+
+            assertEquals("OnStartElement", true, c.isStart());
+            assertEquals("TextValue", "val1", c.getTextValue());
+            System.out.println(" -> " + c.getObject() );
+        }
+        c.dispose();
+    }
+
+
+    public void testPath2()
+        throws XmlException
+    {
+        final XmlObject obj = XmlObject.Factory.parse(
+                "<a>" +
+                  "<b>" +
+                    "<c>val1</c>" +
+                    "<d>" +
+                      "<c>val2</c>" +
+                      "<b><c>val3</c></b>" +
+                    "</d>" +
+                  "</b>" +
+                  "<c>val4</c>" +
+                "</a>");
+        final XmlCursor c = obj.newCursor();
+
+        c.selectPath(".//b/c");
+
+        int selCount = c.getSelectionCount();
+        assertEquals("SelectionCount", 2, selCount);
+
+        assertEquals("hasNextSelection", true, c.hasNextSelection() );
+        c.toNextSelection();
+
+        System.out.println(" -> " + c.getObject() );
+        assertEquals("OnStartElement", true, c.isStart());
+        assertEquals("TextValue", "val1", c.getTextValue());
+
+
+        assertEquals("hasNextSelection2", true, c.hasNextSelection() );
+        c.toNextSelection();
+
+        System.out.println(" -> " + c.getObject() );
+        assertEquals("OnStartElement2", true, c.isStart());
+        assertEquals("TextValue2", "val3", c.getTextValue());
+
+        c.dispose();
+    }
+
+    public void testPath3()
+        throws XmlException
+    {
+        final XmlObject obj = XmlObject.Factory.parse(
+                "<a>" +
+                  "<b>" +
+                    "<c>val1</c>" +
+                    "<d>" +
+                      "<c>val2</c>" +
+                      "<b>" +
+                        "<c>val3" +
+                          "<c>val5</c>" +
+                        "</c>" +
+                      "</b>" +
+                    "</d>" +
+                   "</b>" +
+                   "<c>val4</c>" +
+                 "</a>");
+        final XmlCursor c = obj.newCursor();
+
+        c.selectPath(".//b/c//c");
+
+        int selCount = c.getSelectionCount();
+        assertEquals("SelectionCount", 1, selCount);
+
+        while ( c.hasNextSelection() )
+        {
+            c.toNextSelection();
+
+            System.out.println(" -> " + c.getObject() );
+            assertEquals("OnStartElement", true, c.isStart());
+            assertEquals("TextValue", "val5", c.getTextValue());
+        }
+        c.dispose();
+    }
+}



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