geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From xuhaih...@apache.org
Subject svn commit: r1160977 - in /geronimo/xbean/trunk/xbean-bundleutils/src: main/java/org/apache/xbean/osgi/bundle/util/ test/java/org/apache/xbean/osgi/bundle/util/
Date Wed, 24 Aug 2011 05:02:21 GMT
Author: xuhaihong
Date: Wed Aug 24 05:02:21 2011
New Revision: 1160977

URL: http://svn.apache.org/viewvc?rev=1160977&view=rev
Log:
XBEAN-182 Provide a builder class to convert the header elements to header string

Added:
    geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
  (with props)
Modified:
    geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderParser.java
    geronimo/xbean/trunk/xbean-bundleutils/src/test/java/org/apache/xbean/osgi/bundle/util/HeaderParserTest.java

Added: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java?rev=1160977&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
(added)
+++ geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
Wed Aug 24 05:02:21 2011
@@ -0,0 +1,66 @@
+/**
+ *  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.xbean.osgi.bundle.util;
+
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import org.apache.xbean.osgi.bundle.util.HeaderParser.HeaderElement;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class HeaderBuilder {
+
+    private static final Pattern EXTENDED_PATTERN = Pattern.compile("[\\w_\\-\\.]+");
+
+    public static String build(List<HeaderElement> headerElements) {
+        if (headerElements == null || headerElements.size() == 0) {
+            return "";
+        }
+        StringBuilder header = new StringBuilder();
+        for (HeaderElement headerElement : headerElements) {
+            String name = headerElement.getName();
+            if (name == null || name.length() == 0) {
+                throw new IllegalArgumentException("Invalid header name for the header elment
" + headerElement);
+            }
+            if (header.length() > 0) {
+                header.append(",");
+            }
+            header.append(name);
+            for (Map.Entry<String, String> attribute : headerElement.getAttributes().entrySet())
{
+                header.append(";").append(attribute.getKey()).append("=");
+                if (EXTENDED_PATTERN.matcher(attribute.getValue()).matches()) {
+                    header.append(attribute.getValue());
+                } else {
+                    header.append("\"").append(attribute.getValue()).append("\"");
+                }
+            }
+            for (Map.Entry<String, String> directive : headerElement.getDirectives().entrySet())
{
+                header.append(";").append(directive.getKey()).append(":=");
+                if (EXTENDED_PATTERN.matcher(directive.getValue()).matches()) {
+                    header.append(directive.getValue());
+                } else {
+                    header.append("\"").append(directive.getValue()).append("\"");
+                }
+            }
+        }
+        return header.toString();
+    }
+}

Propchange: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderParser.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderParser.java?rev=1160977&r1=1160976&r2=1160977&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderParser.java
(original)
+++ geronimo/xbean/trunk/xbean-bundleutils/src/main/java/org/apache/xbean/osgi/bundle/util/HeaderParser.java
Wed Aug 24 05:02:21 2011
@@ -158,7 +158,48 @@ public class HeaderParser  {
         
         public void addDirective(String name, String value) {
             directives.put(name, value);
-        }        
-        
+        }
+
+        @Override
+        public String toString() {
+            return "HeaderElement [path=" + path + ", attributes=" + attributes + ", directives="
+ directives + "]";
+        }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((attributes == null) ? 0 : attributes.hashCode());
+            result = prime * result + ((directives == null) ? 0 : directives.hashCode());
+            result = prime * result + ((path == null) ? 0 : path.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            HeaderElement other = (HeaderElement) obj;
+            if (attributes == null) {
+                if (other.attributes != null)
+                    return false;
+            } else if (!attributes.equals(other.attributes))
+                return false;
+            if (directives == null) {
+                if (other.directives != null)
+                    return false;
+            } else if (!directives.equals(other.directives))
+                return false;
+            if (path == null) {
+                if (other.path != null)
+                    return false;
+            } else if (!path.equals(other.path))
+                return false;
+            return true;
+        }                
     }
 }

Modified: geronimo/xbean/trunk/xbean-bundleutils/src/test/java/org/apache/xbean/osgi/bundle/util/HeaderParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-bundleutils/src/test/java/org/apache/xbean/osgi/bundle/util/HeaderParserTest.java?rev=1160977&r1=1160976&r2=1160977&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-bundleutils/src/test/java/org/apache/xbean/osgi/bundle/util/HeaderParserTest.java
(original)
+++ geronimo/xbean/trunk/xbean-bundleutils/src/test/java/org/apache/xbean/osgi/bundle/util/HeaderParserTest.java
Wed Aug 24 05:02:21 2011
@@ -36,10 +36,13 @@ public class HeaderParserTest extends Te
         assertEquals("/foo/bar.xml", paths.get(1).getName());
         assertEquals(0, paths.get(1).getAttributes().size());
         assertEquals(0, paths.get(1).getDirectives().size());
+
+        assertEquals(paths, HeaderParser.parseHeader(HeaderBuilder.build(paths)));
     }
     
     public void testComplex() throws Exception {
-        List<HeaderElement> paths = HeaderParser.parseHeader("OSGI-INF/blueprint/comp1_named.xml;ignored-directive:=true,OSGI-INF/blueprint/comp2_named.xml;some-other-attribute=1");
+        String header = "OSGI-INF/blueprint/comp1_named.xml;ignored-directive:=true,OSGI-INF/blueprint/comp2_named.xml;some-other-attribute=1";
+        List<HeaderElement> paths = HeaderParser.parseHeader(header);
         assertEquals(2, paths.size());
         assertEquals("OSGI-INF/blueprint/comp1_named.xml", paths.get(0).getName());
         assertEquals(0, paths.get(0).getAttributes().size());
@@ -49,10 +52,12 @@ public class HeaderParserTest extends Te
         assertEquals(1, paths.get(1).getAttributes().size());
         assertEquals("1", paths.get(1).getAttribute("some-other-attribute"));
         assertEquals(0, paths.get(1).getDirectives().size());
+        assertEquals(paths, HeaderParser.parseHeader(HeaderBuilder.build(paths)));
     }
 
     public void testPaths() throws Exception {
-        List<HeaderElement> paths = HeaderParser.parseHeader("OSGI-INF/blueprint/comp1_named.xml;ignored-directive:=true,OSGI-INF/blueprint/comp2_named.xml;foo.xml;a=b;1:=2;c:=d;4=5");
+        String header = "OSGI-INF/blueprint/comp1_named.xml;ignored-directive:=true,OSGI-INF/blueprint/comp2_named.xml;foo.xml;a=b;1:=2;c:=d;4=5";
+        List<HeaderElement> paths = HeaderParser.parseHeader(header);
         assertEquals(3, paths.size());
         assertEquals("OSGI-INF/blueprint/comp1_named.xml", paths.get(0).getName());
         assertEquals(0, paths.get(0).getAttributes().size());
@@ -68,10 +73,13 @@ public class HeaderParserTest extends Te
         assertEquals(2, paths.get(2).getDirectives().size());
         assertEquals("d", paths.get(2).getDirective("c"));
         assertEquals("2", paths.get(2).getDirective("1"));
+        System.out.println(HeaderBuilder.build(paths));
+        assertEquals(paths, HeaderParser.parseHeader(HeaderBuilder.build(paths)));
     }
     
     public void testExportPackages() throws Exception {
-        List<HeaderElement> paths = HeaderParser.parseHeader("org.apache.geronimo.kernel.rmi;uses:=\"javax.rmi.ssl,org.apache.geronimo.gbean,org.slf4j\",org.apache.geronimo.kernel.proxy");
+        String header = "org.apache.geronimo.kernel.rmi;uses:=\"javax.rmi.ssl,org.apache.geronimo.gbean,org.slf4j\",org.apache.geronimo.kernel.proxy";
+        List<HeaderElement> paths = HeaderParser.parseHeader(header);
         assertEquals(2, paths.size());
         
         assertEquals("org.apache.geronimo.kernel.rmi", paths.get(0).getName());
@@ -82,10 +90,12 @@ public class HeaderParserTest extends Te
         assertEquals("org.apache.geronimo.kernel.proxy", paths.get(1).getName());
         assertEquals(0, paths.get(1).getAttributes().size());
         assertEquals(0, paths.get(1).getDirectives().size());
+        assertEquals(paths, HeaderParser.parseHeader(HeaderBuilder.build(paths)));
     }
     
     public void testImportPackages() throws Exception {
-        List<HeaderElement> paths = HeaderParser.parseHeader("com.thoughtworks.xstream;version=\"1.3\",com.thoughtworks.xstream.converters");
+        String header = "com.thoughtworks.xstream;version=\"1.3\",com.thoughtworks.xstream.converters";
+        List<HeaderElement> paths = HeaderParser.parseHeader(header);
         assertEquals(2, paths.size());
         
         assertEquals("com.thoughtworks.xstream", paths.get(0).getName());
@@ -96,5 +106,7 @@ public class HeaderParserTest extends Te
         assertEquals("com.thoughtworks.xstream.converters", paths.get(1).getName());
         assertEquals(0, paths.get(1).getAttributes().size());
         assertEquals(0, paths.get(1).getDirectives().size());
+
+        assertEquals(paths, HeaderParser.parseHeader(HeaderBuilder.build(paths)));
     }
 }



Mime
View raw message