You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/12/21 13:58:42 UTC

svn commit: r1424905 - in /camel/trunk/camel-core/src/main/java/org/apache/camel: component/validator/DefaultLSResourceResolver.java util/FileUtil.java

Author: ningjiang
Date: Fri Dec 21 12:58:41 2012
New Revision: 1424905

URL: http://svn.apache.org/viewvc?rev=1424905&view=rev
Log:
CAMEL-5837 replace the implementation of the getUserDir()

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java?rev=1424905&r1=1424904&r2=1424905&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/validator/DefaultLSResourceResolver.java Fri Dec 21 12:58:41 2012
@@ -50,7 +50,6 @@ public class DefaultLSResourceResolver i
             throw new IllegalArgumentException(String.format("Resource: %s refers an invalid resource without SystemId."
                     + " Invalid resource has type: %s, namespaceURI: %s, publicId: %s, systemId: %s, baseURI: %s", resourceUri, type, namespaceURI, publicId, systemId, baseURI));
         }
-        
         return new DefaultLSInput(publicId, systemId, baseURI);
     }
     
@@ -89,13 +88,18 @@ public class DefaultLSResourceResolver i
             String answer = "";
             if (ObjectHelper.isNotEmpty(base)) {
                 try {
-                    userDir = FileUtil.getUserDir().toString();
+                    userDir = FileUtil.getUserDir().toURI().toString();
                 } catch (Exception ex) {
                     // do nothing here
                 }
                 // get the relative path from the userdir
-                if (ObjectHelper.isNotEmpty(base) && base.startsWith("file") && base.startsWith(userDir)) {
-                    answer = FileUtil.onlyPath(base.substring(userDir.length())) + "/";
+                if (ObjectHelper.isNotEmpty(base) && base.startsWith("file://") && userDir.startsWith("file:")) {
+                    // skip the protocol part
+                    base = base.substring(7);
+                    userDir = userDir.substring(5);
+                    if (base.startsWith(userDir)) {
+                        answer = FileUtil.onlyPath(base.substring(userDir.length())) + "/";
+                    }
                 }
             }
             return answer;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java?rev=1424905&r1=1424904&r2=1424905&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/FileUtil.java Fri Dec 21 12:58:41 2012
@@ -41,150 +41,21 @@ public final class FileUtil {
 
     private static final transient Logger LOG = LoggerFactory.getLogger(FileUtil.class);
     private static final int RETRY_SLEEP_MILLIS = 10;
+    /**
+     * The System property key for the user directory.
+     */
+    private static final String USER_DIR_KEY = "user.dir";
+    private static final File USER_DIR = new File(System.getProperty(USER_DIR_KEY));
     private static File defaultTempDir;
     
-    // current value of the "user.dir" property
-    private static String gUserDir;
-    // cached URI object for the current value of the escaped "user.dir" property stored as a URI
-    private static URI gUserDirURI;
-    // which ASCII characters need to be escaped
-    private static boolean gNeedEscaping[] = new boolean[128];
-    // the first hex character if a character needs to be escaped
-    private static char gAfterEscaping1[] = new char[128];
-    // the second hex character if a character needs to be escaped
-    private static char gAfterEscaping2[] = new char[128];
-    private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7',
-                                     '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
-    // initialize the above 3 arrays
-    static {
-        for (int i = 0; i <= 0x1f; i++) {
-            gNeedEscaping[i] = true;
-            gAfterEscaping1[i] = gHexChs[i >> 4];
-            gAfterEscaping2[i] = gHexChs[i & 0xf];
-        }
-        gNeedEscaping[0x7f] = true;
-        gAfterEscaping1[0x7f] = '7';
-        gAfterEscaping2[0x7f] = 'F';
-        char[] escChs = {' ', '<', '>', '#', '%', '"', '{', '}',
-                         '|', '\\', '^', '~', '[', ']', '`'};
-        int len = escChs.length;
-        char ch;
-        for (int i = 0; i < len; i++) {
-            ch = escChs[i];
-            gNeedEscaping[ch] = true;
-            gAfterEscaping1[ch] = gHexChs[ch >> 4];
-            gAfterEscaping2[ch] = gHexChs[ch & 0xf];
-        }
-    }
     
     private FileUtil() {
         // Utils method
     }
     
 
-    // To escape the "user.dir" system property, by using %HH to represent
-    // special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', '#', '%'
-    // and '"'. It's a static method, so needs to be synchronized.
-    // this method looks heavy, but since the system property isn't expected
-    // to change often, so in most cases, we only need to return the URI
-    // that was escaped before.
-    // According to the URI spec, non-ASCII characters (whose value >= 128)
-    // need to be escaped too.
-    // REVISIT: don't know how to escape non-ASCII characters, especially
-    // which encoding to use. Leave them for now.
-    public static synchronized URI getUserDir() throws URISyntaxException {
-        // get the user.dir property
-        String userDir = "";
-        try {
-            userDir = System.getProperty("user.dir");
-        } catch (SecurityException se) {
-        }
-
-        // return empty string if property value is empty string.
-        if (userDir.length() == 0) {
-            return new URI("file", "", "", null, null);
-        }
-        // compute the new escaped value if the new property value doesn't
-        // match the previous one
-        if (gUserDirURI != null && userDir.equals(gUserDir)) {
-            return gUserDirURI;
-        }
-
-        // record the new value as the global property value
-        gUserDir = userDir;
-
-        char separator = java.io.File.separatorChar;
-        userDir = userDir.replace(separator, '/');
-
-        int len = userDir.length(); 
-        int ch;
-        StringBuffer buffer = new StringBuffer(len * 3);
-        // change C:/blah to /C:/blah
-        if (len >= 2 && userDir.charAt(1) == ':') {
-            ch = Character.toUpperCase(userDir.charAt(0));
-            if (ch >= 'A' && ch <= 'Z') {
-                buffer.append('/');
-            }
-        }
-
-        // for each character in the path
-        int i = 0;
-        for (; i < len; i++) {
-            ch = userDir.charAt(i);
-            // if it's not an ASCII character, break here, and use UTF-8 encoding
-            if (ch >= 128) {
-                break;
-            }
-            if (gNeedEscaping[ch]) {
-                buffer.append('%');
-                buffer.append(gAfterEscaping1[ch]);
-                buffer.append(gAfterEscaping2[ch]);
-                // record the fact that it's escaped
-            } else {
-                buffer.append((char)ch);
-            }
-        }
-
-        // we saw some non-ascii character
-        if (i < len) {
-            // get UTF-8 bytes for the remaining sub-string
-            byte[] bytes = null;
-            byte b;
-            try {
-                bytes = userDir.substring(i).getBytes("UTF-8");
-            } catch (java.io.UnsupportedEncodingException e) {
-                // should never happen
-                return new URI("file", "", userDir, null, null);
-            }
-            len = bytes.length;
-
-            // for each byte
-            for (i = 0; i < len; i++) {
-                b = bytes[i];
-                // for non-ascii character: make it positive, then escape
-                if (b < 0) {
-                    ch = b + 256;
-                    buffer.append('%');
-                    buffer.append(gHexChs[ch >> 4]);
-                    buffer.append(gHexChs[ch & 0xf]);
-                } else if (gNeedEscaping[b]) {
-                    buffer.append('%');
-                    buffer.append(gAfterEscaping1[b]);
-                    buffer.append(gAfterEscaping2[b]);
-                } else {
-                    buffer.append((char)b);
-                }
-            }
-        }
-
-        // change blah/blah to blah/blah/
-        if (!userDir.endsWith("/")) {
-            buffer.append('/');
-        }
-
-        gUserDirURI = new URI("file", "", buffer.toString(), null, null);
-
-        return gUserDirURI;
+    public static File getUserDir() {
+        return USER_DIR;
     }
 
     /**