Author: fadushin
Date: Thu Apr 10 10:21:59 2008
New Revision: 646883

URL: http://svn.apache.org/viewvc?rev=646883&view=rev
Log:
[WSS-107] Copyright in X509NameTokenizer

 * Applied George's patch
 * Added a unit test to cover code

Thanks, George!


Added:
    webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java   (with 
props)
Modified:
    
webservices/wss4j/trunk/src/org/apache/ws/security/components/crypto/X509NameTokenizer.java
    webservices/wss4j/trunk/test/components/PackageTests.java

Modified: 
webservices/wss4j/trunk/src/org/apache/ws/security/components/crypto/X509NameTokenizer.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/org/apache/ws/security/components/crypto/X509NameTokenizer.java?rev=646883&r1=646882&r2=646883&view=diff
==============================================================================
--- 
webservices/wss4j/trunk/src/org/apache/ws/security/components/crypto/X509NameTokenizer.java
 (original)
+++ 
webservices/wss4j/trunk/src/org/apache/ws/security/components/crypto/X509NameTokenizer.java
 Thu Apr 10 10:21:59 2008
@@ -1,10 +1,25 @@
 /*
- * This source is a plain copy from bouncycastle software.
- * Thus:
- * Copyright (c) 2000 The Legion Of The Bouncy Castle 
(http://www.bouncycastle.org)
+ * Copyright The Apache Software Foundation.
+ *
+ *  Licensed 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.ws.security.components.crypto;
 
+import java.util.ArrayList;
+
+import org.apache.xml.security.utils.RFC2253Parser;
+
 /**
  * class for breaking up an X500 Name into it's component tokens, ala
  * java.util.StringTokenizer. We need this class as some of the
@@ -12,56 +27,75 @@
  * StringTokenizer.
  */
 public class X509NameTokenizer {
-    private String oid;
-    private int index;
-    private StringBuffer buf = new StringBuffer();
-
-    public X509NameTokenizer(String oid) {
-        this.oid = oid;
-        this.index = -1;
+
+    private final java.util.List tokens = new ArrayList();
+    private int index = 0;
+
+    public X509NameTokenizer(String dn) {
+       final String _DN = RFC2253Parser.normalize(dn);
+       int i = 0;
+       int l = 0;
+       int k;
+       for (int j = 0; (k = _DN.indexOf(",", j)) >= 0; j = k + 1) {
+          l += countQuotes(_DN, j, k);
+          if ((k > 0) && (_DN.charAt(k - 1) != '\\') && (l % 2) == 0) {
+             tokens.add(_DN.substring(i, k).trim());
+             i = k + 1;
+             l = 0;
+          }
+       }
+       if (_DN.trim().length() != 0) {
+           tokens.add(trim(_DN.substring(i)));
+       }
     }
 
     public boolean hasMoreTokens() {
-        return (index != oid.length());
+        return (index < tokens.size());
     }
 
     public String nextToken() {
-        if (index == oid.length()) {
-            return null;
+        if (hasMoreTokens()) {
+            return (String) tokens.get(index++);
+        } else {
+            return "";
         }
+    }
 
-        int end = index + 1;
-        boolean quoted = false;
-        boolean escaped = false;
-
-        buf.setLength(0);
-
-        while (end != oid.length()) {
-            char c = oid.charAt(end);
-
-            if (c == '"') {
-                if (!escaped) {
-                    quoted = !quoted;
-                } else {
-                    buf.append(c);
-                }
-                escaped = false;
-            } else {
-                if (escaped || quoted) {
-                    buf.append(c);
-                    escaped = false;
-                } else if (c == '\\') {
-                    escaped = true;
-                } else if (c == ',') {
-                    break;
-                } else {
-                    buf.append(c);
-                }
-            }
-            end++;
-        }
 
-        index = end;
-        return buf.toString().trim();
+    /**
+     * Returns the number of Quotation from i to j
+     *
+     * @param s
+     * @param i
+     * @param j
+     * @return number of quotes
+     */
+    private static int countQuotes(String s, int i, int j) {
+       int k = 0;
+       for (int l = i; l < j; l++) {
+          if (s.charAt(l) == '"') {
+             k++;
+          }
+       }
+       return k;
+    }
+
+    /**
+     * Method trim
+     *
+     * @param str
+     * @return the string
+     */
+    private static String trim(String str) {
+       String trimed = str.trim();
+       int i = str.indexOf(trimed) + trimed.length();
+       if ((str.length() > i) && trimed.endsWith("\\")
+               &&!trimed.endsWith("\\\\")) {
+          if (str.charAt(i) == ' ') {
+             trimed = trimed + " ";
+          }
+       }
+       return trimed;
     }
+
 }

Modified: webservices/wss4j/trunk/test/components/PackageTests.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/components/PackageTests.java?rev=646883&r1=646882&r2=646883&view=diff
==============================================================================
--- webservices/wss4j/trunk/test/components/PackageTests.java (original)
+++ webservices/wss4j/trunk/test/components/PackageTests.java Thu Apr 10 
10:21:59 2008
@@ -50,6 +50,7 @@
     public static Test suite() {
         TestSuite suite = new TestSuite();
         suite.addTestSuite(TestMerlin.class);
+        suite.addTestSuite(TestX509NameTokenizer.class);
         return suite;
     }
 

Added: webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java?rev=646883&view=auto
==============================================================================
--- webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java (added)
+++ webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java Thu Apr 
10 10:21:59 2008
@@ -0,0 +1,109 @@
+/*
+ * Copyright The Apache Software Foundation.
+ *
+ *  Licensed 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 components;
+
+import junit.framework.TestCase;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.ws.security.components.crypto.X509NameTokenizer;
+
+/**
+ *
+ */
+public class TestX509NameTokenizer extends TestCase {
+
+    public TestX509NameTokenizer(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestX509NameTokenizer.class);
+    }
+
+    public void 
+    testEmptyX509NameTokenizer() {
+        checkEmpty(new X509NameTokenizer(""));
+        checkEmpty(new X509NameTokenizer("  "));
+        checkEmpty(new X509NameTokenizer(" \t \n  \r\n"));
+    }
+    
+    public void 
+    testWellFormedX509NameTokenizer() {
+        checkTokenizer(
+            new X509NameTokenizer("foo"), 
+            new String[] { "foo" }
+        );
+        checkTokenizer(
+            new X509NameTokenizer(" foo   "), 
+            new String[] { "foo" }
+        );
+        checkTokenizer(
+            new X509NameTokenizer(" foo,   "), 
+            new String[] { "foo", "" }
+        );
+        checkTokenizer(
+            new X509NameTokenizer(" foo\\,   "), 
+            new String[] { "foo\\,"}
+        );
+        checkTokenizer(
+            new X509NameTokenizer(" foo\\,   bar  "), 
+            new String[] { "foo\\,   bar"}
+        );
+        checkTokenizer(
+            new X509NameTokenizer(" \"foo,\"   "), 
+            new String[] { "\"foo,\""}
+        );
+        checkTokenizer(
+            new X509NameTokenizer("foo, bar"), 
+            new String[] { "foo", "bar"}
+        );
+        checkTokenizer(
+            new X509NameTokenizer("\"foo bar\", gnu gnat"), 
+            new String[] { "\"foo bar\"", "gnu gnat"}
+        );
+        checkTokenizer(
+            new X509NameTokenizer("foo\\ "), 
+            new String[] { "foo\\ "}
+        );
+        checkTokenizer(
+            new X509NameTokenizer("foo\\\\ "), 
+            new String[] { "foo\\\\"}
+        );
+    }
+    
+    private void
+    checkEmpty(
+        final X509NameTokenizer tokenizer
+    ) {
+        checkTokenizer(
+            tokenizer, new String[0]
+        );
+    }
+    
+    private void
+    checkTokenizer(
+        final X509NameTokenizer tokenizer,
+        final String[] expected
+    ) {
+        for (int i = 0;  i < expected.length;  ++i) {
+            assertTrue(tokenizer.hasMoreTokens());
+            assertEquals(tokenizer.nextToken(), expected[i]);
+        }
+        assertTrue(!tokenizer.hasMoreTokens());
+        assertEquals(tokenizer.nextToken(), "");
+    }
+}

Propchange: webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: webservices/wss4j/trunk/test/components/TestX509NameTokenizer.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



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

Reply via email to