Author: mrglavas
Date: Mon Feb 27 09:09:34 2006
New Revision: 381385

URL: http://svn.apache.org/viewcvs?rev=381385&view=rev
Log:
Re-implementing the XPath fixup routine so that it doesn't create temporary 
objects.
Since we no longer call trim(), this also fixes a bug in the white space 
processing.
trim() trims off characters which are not XML spaces.

Modified:
    xerces/java/trunk/src/org/apache/xerces/impl/xs/identity/Field.java

Modified: xerces/java/trunk/src/org/apache/xerces/impl/xs/identity/Field.java
URL: 
http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/impl/xs/identity/Field.java?rev=381385&r1=381384&r2=381385&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/xs/identity/Field.java 
(original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/xs/identity/Field.java Mon Feb 
27 09:09:34 2006
@@ -16,11 +16,10 @@
 
 package org.apache.xerces.impl.xs.identity;
 
-import java.util.StringTokenizer;
-
 import org.apache.xerces.impl.xpath.XPathException;
 import org.apache.xerces.impl.xs.util.ShortListImpl;
 import org.apache.xerces.util.SymbolTable;
+import org.apache.xerces.util.XMLChar;
 import org.apache.xerces.xni.NamespaceContext;
 import org.apache.xerces.xs.ShortList;
 import org.apache.xerces.xs.XSComplexTypeDefinition;
@@ -124,34 +123,72 @@
             }
         } // <init>(String,SymbolTable,NamespacesContext)
         
+        /** Fixup XPath expression. Avoid creating a new String if possible. */
         private static String fixupXPath(String xpath) {
+            
+            final int end = xpath.length();
+            int offset = 0;
+            boolean whitespace = true;
+            char c;
+            
             // NOTE: We have to prefix the field XPath with "./" in
             //       order to handle selectors such as "@attr" that 
             //       select the attribute because the fields could be
             //       relative to the selector element. -Ac
             //       Unless xpath starts with a descendant node -Achille Fokoue
             //      ... or a / or a . - NG
-            int union = xpath.indexOf('|');
-            if (union == -1) {
-                return ((xpath.trim().startsWith("/") || 
xpath.trim().startsWith(".")) ?
-                        xpath : "./"+xpath);
+            for (; offset < end; ++offset) {
+                c = xpath.charAt(offset);
+                if (whitespace) {
+                    if (!XMLChar.isSpace(c)) {
+                        if (c == '.' || c == '/') {
+                            whitespace = false;
+                        }
+                        else if (c != '|') {
+                            return fixupXPath2(xpath, offset, end);
+                        }
+                    }
+                }
+                else if (c == '|') {
+                    whitespace = true;
+                }
             }
-            else {
-                // REVISIT: Replace with a more efficient solution.
-                StringBuffer buffer = new StringBuffer(xpath.length());
-                StringTokenizer tokenizer = new StringTokenizer(xpath, "|");
-                int length = tokenizer.countTokens();
-                for (int i = length - 1; i >= 0; --i) {
-                    String token = tokenizer.nextToken();
-                    buffer.append(((token.trim().startsWith("/") || 
token.trim().startsWith(".")) ?
-                            token : "./"+token));
-                    if (i > 0) {
-                        buffer.append('|');
+            return xpath;
+            
+        } // fixupXPath(String):String
+        
+        private static String fixupXPath2(String xpath, int offset, final int 
end) {
+       
+            StringBuffer buffer = new StringBuffer(end + 2);
+            for (int i = 0; i < offset; ++i) {
+                buffer.append(xpath.charAt(i));
+            }
+            buffer.append("./");
+            
+            boolean whitespace = false;
+            char c;
+            
+            for (; offset < end; ++offset) {
+                c = xpath.charAt(offset);
+                if (whitespace) {
+                    if (!XMLChar.isSpace(c)) {
+                        if (c == '.' || c == '/') {
+                            whitespace = false;
+                        }
+                        else if (c != '|') {
+                            buffer.append("./");
+                            whitespace = false;
+                        }
                     }
                 }
-                return buffer.toString();
+                else if (c == '|') {
+                    whitespace = true;
+                }
+                buffer.append(c);
             }
-        }
+            return buffer.toString();
+            
+        } // fixupXPath2(String, int, int):String
 
     } // class XPath
 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to