Author: ningjiang
Date: Thu May 31 02:09:55 2012
New Revision: 1344532

URL: http://svn.apache.org/viewvc?rev=1344532&view=rev
Log:
CAMEL-5321 Fixed the issue that Validator component fails on XSD with classpath 
relative imports

Added:
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/child.xsd
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/parent.xsd
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/child.xsd
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/parent.xsd
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/child/
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/child/child.xsd
    
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/parent.xsd
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?rev=1344532&r1=1344531&r2=1344532&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java 
(original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java 
Thu May 31 02:09:55 2012
@@ -833,12 +833,13 @@ public final class ObjectHelper {
     public static InputStream loadResourceAsStream(String name) {
         InputStream in = null;
 
+        String resolvedName = resolveUriPath(name);
         ClassLoader contextClassLoader = 
Thread.currentThread().getContextClassLoader();
         if (contextClassLoader != null) {
-            in = contextClassLoader.getResourceAsStream(name);
+            in = contextClassLoader.getResourceAsStream(resolvedName);
         }
         if (in == null) {
-            in = ObjectHelper.class.getClassLoader().getResourceAsStream(name);
+            in = 
ObjectHelper.class.getClassLoader().getResourceAsStream(resolvedName);
         }
 
         return in;
@@ -854,12 +855,13 @@ public final class ObjectHelper {
     public static URL loadResourceAsURL(String name) {
         URL url = null;
 
+        String resolvedName = resolveUriPath(name);
         ClassLoader contextClassLoader = 
Thread.currentThread().getContextClassLoader();
         if (contextClassLoader != null) {
-            url = contextClassLoader.getResource(name);
+            url = contextClassLoader.getResource(resolvedName);
         }
         if (url == null) {
-            url = ObjectHelper.class.getClassLoader().getResource(name);
+            url = 
ObjectHelper.class.getClassLoader().getResource(resolvedName);
         }
 
         return url;
@@ -893,6 +895,28 @@ public final class ObjectHelper {
 
         return url;
     }
+    
+    /**
+     * Helper operation used to remove relative path notation from 
+     * resources.  Most critical for resources on the Classpath
+     * as resource loaders will not resolve the relative paths correctly.
+     * 
+     * @param name the name of the resource to load
+     * @return the modified or unmodified string if there were no changes
+     */
+    private static String resolveUriPath(String name) {
+        String answer = name;
+        if(answer.indexOf("//") > -1) {
+            answer = answer.replaceAll("//", "/");
+        }
+        if(answer.indexOf("../") > -1) {
+            answer = answer.replaceAll("[A-Za-z0-9]*/\\.\\./", "");
+        }
+        if(answer.indexOf("./") > -1) {
+            answer = answer.replaceAll("\\./", "");
+        }
+        return answer;
+    }
 
     /**
      * A helper method to invoke a method via reflection and wrap any 
exceptions

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/child.xsd
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/child.xsd?rev=1344532&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/child.xsd
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/child.xsd
 Thu May 31 02:09:55 2012
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar";
+           xmlns:tns="http://foo.com/bar";
+           xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+           
+  <xs:include schemaLocation="./parent.xsd"/>
+  <xs:element name="childuser">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element ref="tns:user" />
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/parent.xsd
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/parent.xsd?rev=1344532&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/parent.xsd
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/dotslash/parent.xsd
 Thu May 31 02:09:55 2012
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar";
+           xmlns:tns="http://foo.com/bar";
+           xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+
+    
+  <xs:element name="user">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="id" type="xs:int"/>
+        <xs:element name="username" type="xs:string"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/child.xsd
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/child.xsd?rev=1344532&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/child.xsd
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/child.xsd
 Thu May 31 02:09:55 2012
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar";
+           xmlns:tns="http://foo.com/bar";
+           xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+           
+  <xs:include schemaLocation=".//parent.xsd"/>
+  <xs:element name="childuser">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element ref="tns:user" />
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/parent.xsd
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/parent.xsd?rev=1344532&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/parent.xsd
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/doubleslash/parent.xsd
 Thu May 31 02:09:55 2012
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar";
+           xmlns:tns="http://foo.com/bar";
+           xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+
+    
+  <xs:element name="user">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="id" type="xs:int"/>
+        <xs:element name="username" type="xs:string"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/child/child.xsd
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/child/child.xsd?rev=1344532&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/child/child.xsd
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/child/child.xsd
 Thu May 31 02:09:55 2012
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar";
+           xmlns:tns="http://foo.com/bar";
+           xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+           
+  <xs:include schemaLocation="../parent.xsd"/>
+  <xs:element name="childuser">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element ref="tns:user" />
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+

Added: 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/parent.xsd
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/parent.xsd?rev=1344532&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/parent.xsd
 (added)
+++ 
camel/trunk/camel-core/src/test/resources/org/apache/camel/component/validator/relativeparent/parent.xsd
 Thu May 31 02:09:55 2012
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<!--
+    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.
+-->
+<xs:schema elementFormDefault="qualified" version="1.0"
+           targetNamespace="http://foo.com/bar";
+           xmlns:tns="http://foo.com/bar";
+           xmlns:xs="http://www.w3.org/2001/XMLSchema";>
+
+    
+  <xs:element name="user">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="id" type="xs:int"/>
+        <xs:element name="username" type="xs:string"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+
+</xs:schema>
+


Reply via email to