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 2009/07/24 00:30:35 UTC

svn commit: r797249 - in /xmlbeans/trunk: src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java test/src/ValidatingXSRTests/checkin/TestUriValidation.java

Author: cezar
Date: Thu Jul 23 22:30:34 2009
New Revision: 797249

URL: http://svn.apache.org/viewvc?rev=797249&view=rev
Log:
Update list of chars to be replaced in URI validation.


Added:
    xmlbeans/trunk/test/src/ValidatingXSRTests/checkin/TestUriValidation.java
Modified:
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java?rev=797249&r1=797248&r2=797249&view=diff
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/XsTypeConverter.java Thu Jul 23 22:30:34 2009
@@ -42,6 +42,10 @@
     private static final String EMPTY_PREFIX = "";
     private static final BigDecimal DECIMAL__ZERO = new BigDecimal(0.0);
 
+    // See Section 2.4.3 of FRC2396  http://www.ietf.org/rfc/rfc2396.txt
+    private static final String[] URI_CHARS_TO_BE_REPLACED = {" "  , "{"  , "}"  , "|"  , "\\" , "^"  , "["  , "]"  , "`"  };
+    private static final String[] URI_CHARS_REPLACED_WITH  = {"%20", "%7b", "%7d", "%7c", "%5c", "%5e", "%5b", "%5d", "%60"};
+
     // ======================== float ========================
     public static float lexFloat(CharSequence cs)
         throws NumberFormatException
@@ -762,11 +766,14 @@
 
         // Per XMLSchema spec allow spaces inside URIs
         StringBuffer s = new StringBuffer(lexical_value.toString());
-        int i = 0;
-        while ((i = s.indexOf(" ", i)) >= 0)
+        for (int ic = 0; ic<URI_CHARS_TO_BE_REPLACED.length; ic++)
         {
-            s.replace(i, i + 1, "%20");
-            i += 3;
+            int i = 0;
+            while ((i = s.indexOf(URI_CHARS_TO_BE_REPLACED[ic], i)) >= 0)
+            {
+                s.replace(i, i + 1, URI_CHARS_REPLACED_WITH[ic]);
+                i += 3;
+            }
         }
 
         try

Added: xmlbeans/trunk/test/src/ValidatingXSRTests/checkin/TestUriValidation.java
URL: http://svn.apache.org/viewvc/xmlbeans/trunk/test/src/ValidatingXSRTests/checkin/TestUriValidation.java?rev=797249&view=auto
==============================================================================
--- xmlbeans/trunk/test/src/ValidatingXSRTests/checkin/TestUriValidation.java (added)
+++ xmlbeans/trunk/test/src/ValidatingXSRTests/checkin/TestUriValidation.java Thu Jul 23 22:30:34 2009
@@ -0,0 +1,110 @@
+package ValidatingXSRTests.checkin;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.framework.TestCase;
+import junit.framework.Assert;
+import org.apache.xmlbeans.impl.util.XsTypeConverter;
+import org.apache.xmlbeans.impl.common.InvalidLexicalValueException;
+
+/**
+ * Created by Cezar Andrei (cezar dot andrei at gmail dot com)
+ * Date: Jul 23, 2009
+ */
+public class TestUriValidation
+    extends TestCase
+{
+    public TestUriValidation(String name)
+    {
+        super(name);
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite(TestUriValidation.class);
+    }
+
+    public void testLexAnyUriValid()
+    {
+        String[] validURIs = {
+            "http://www.ics.uci.edu/pub/ietf/uri/#Related",
+            "http://www.ics.uci.edu/pub/ietf/uri/?query=abc#Related",
+            "http://a/b/c/d;p?q",
+            "g:h",
+            "./g",
+            "g/",
+            "/g",
+            "//g",
+            "?y",
+            "g?y",
+            "#s",
+            "g#s",
+            "g?y#s",
+            ";x",
+            "g;x",
+            "g;x?y#s",
+            ".",
+            "./",
+            "..",
+            "../",
+            "../g",
+            "../..",
+            "../../",
+            "../../g",
+            "http:// www   .ics.uci.edu   /pub/ietf/uri  /#Related",
+            "http:// www   .ics.uci.edu   /pub/iet%20%20f/uri  /#Related",
+            "http:\\example.com\\examples",
+            "http:\\\\example.com\\\\examples",
+        };
+
+        for (int i = 0; i < validURIs.length; i++)
+        {
+            try
+            {
+                XsTypeConverter.lexAnyURI(validURIs[i]);
+            }
+            catch (RuntimeException e)
+            {
+                System.out.println("URI should be valid: " + validURIs[i] + "  " + e.getCause().getCause().getMessage());
+                Assert.assertTrue("URI should be valid: " + validURIs[i], false);
+                throw new IllegalStateException("URI should be valid: " + validURIs[i]);
+            }
+        }
+    }
+
+    public void testLexAnyUriInvalid()
+    {
+        // From XQTS cvshead June 2009
+        String[] invalidURIs = {
+            "http:\\\\invalid>URI\\someURI",        // K2-SeqExprCast-207: Construct an xs:anyURI from an invalid string. However, in F&O 17.1.1, it is said that "For xs:anyURI, the extent to which an implementation validates the lexical form of xs:anyURI is implementation dependent.".
+            "http://www.example.com/file%GF.html",  // K2-SeqExprCast-210: '%' is not a disallowed character and therefore it's not encoded before being considered for RFC 2396 validness.
+            "foo://",                               // K2-SeqExprCast-421: Pass an invalid anyURI.
+            "foo:",                                 // K2-SeqExprCast-421-2: Pass an invalid anyURI.
+            "%gg",                                  // K2-SeqExprCast-422: Pass an invalid anyURI(#2).
+            ":/cut.jpg",                            // K2-SeqExprCast-423: no scheme
+            ":/images/cut.png",                     // K2-SeqExprCast-424: An URI without scheme, combined with a relative directory.
+            ":/",                                   // K2-SeqExprCast-505: ':/' is an invalid URI, no scheme.
+            "http:%%",                              // fn-resolve-uri-4: Evaluation of resolve-uri function with an invalid URI value for second argument.
+            ":",                                    // fn-resolve-uri-3: Evaluation of resolve-uri function with an invalid URI value for first argument.
+            "###Rel",
+            "##",
+            "????###",
+            "###????"
+        };
+
+        for (int i = 0; i < invalidURIs.length; i++)
+        {
+            try
+            {
+                XsTypeConverter.lexAnyURI(invalidURIs[i]);
+                System.out.println("URI should be invalid: " + invalidURIs[i] );
+                Assert.assertTrue("URI should be invalid: " + invalidURIs[i], false);
+                throw new IllegalStateException("URI should be invalid: " + invalidURIs[i]);
+            }
+            catch (InvalidLexicalValueException e)
+            {
+                Assert.assertTrue("URI should be invalid: " + invalidURIs[i] + "  " + e.getCause().getCause().getMessage(), true);
+            }
+        }
+    }
+}



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