You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2008/03/27 19:56:07 UTC

svn commit: r641940 - in /xerces/java/trunk/src/org/apache/xerces/impl/xs: FilePathToURI.java XMLSchemaLoader.java

Author: mrglavas
Date: Thu Mar 27 11:56:04 2008
New Revision: 641940

URL: http://svn.apache.org/viewvc?rev=641940&view=rev
Log:
Fixing a bug in the processing of the JAXP 1.2 "http://java.sun.com/xml/jaxp/properties/schemaSource"
property. When the value specified is a java.io.File we need to make sure we set the system ID on the
XMLInputSource otherwise relative URIs in the schema may not resolve.

Note that the FilePathToURI code was borrowed from XML Commons.

Added:
    xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java   (with props)
Modified:
    xerces/java/trunk/src/org/apache/xerces/impl/xs/XMLSchemaLoader.java

Added: xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java?rev=641940&view=auto
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java (added)
+++ xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java Thu Mar 27 11:56:04 2008
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.xerces.impl.xs;
+
+class FilePathToURI {
+
+    // 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];
+        }
+    }
+
+    // To escape a file path to a URI, by using %HH to represent
+    // special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', '#', '%'
+    // and '"' and non-ASCII characters (whose value >= 128).
+    public static String filepath2URI(String path){
+        // return null if path is null.
+        if (path == null)
+            return null;
+
+        char separator = java.io.File.separatorChar;
+        path = path.replace(separator, '/');
+
+        int len = path.length(), ch;
+        StringBuffer buffer = new StringBuffer(len*3);
+        buffer.append("file://");
+        // change C:/blah to /C:/blah
+        if (len >= 2 && path.charAt(1) == ':') {
+            ch = Character.toUpperCase(path.charAt(0));
+            if (ch >= 'A' && ch <= 'Z') {
+                buffer.append('/');
+            }
+        }
+
+        // for each character in the path
+        int i = 0;
+        for (; i < len; i++) {
+            ch = path.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 = path.substring(i).getBytes("UTF-8");
+            } catch (java.io.UnsupportedEncodingException e) {
+                // should never happen
+                return path;
+            }
+            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);
+                }
+            }
+        }
+
+        return buffer.toString();
+    }
+
+} //FilePathToURI

Propchange: xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/java/trunk/src/org/apache/xerces/impl/xs/FilePathToURI.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: xerces/java/trunk/src/org/apache/xerces/impl/xs/XMLSchemaLoader.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/impl/xs/XMLSchemaLoader.java?rev=641940&r1=641939&r2=641940&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/xs/XMLSchemaLoader.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/xs/XMLSchemaLoader.java Thu Mar 27 11:56:04 2008
@@ -830,15 +830,17 @@
         } 
         else if (val instanceof File) {
             File file = (File) val;
+            String escapedURI = FilePathToURI.filepath2URI(file.getAbsolutePath());
             InputStream is = null;
             try {
                 is = new BufferedInputStream(new FileInputStream(file));
-            } catch (FileNotFoundException ex) {
+            } 
+            catch (FileNotFoundException ex) {
                 fErrorReporter.reportError(XSMessageFormatter.SCHEMA_DOMAIN,
                         "schema_reference.4", new Object[] { file.toString() },
                         XMLErrorReporter.SEVERITY_ERROR);
             }
-            return new XMLInputSource(null, null, null, is, null);
+            return new XMLInputSource(null, escapedURI, null, is, null);
         }
         MessageFormatter mf = fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN);
         throw new XMLConfigurationException(



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