]> source.dussan.org Git - archiva.git/commitdiff
use Xalan extension to invoke custom XpathFunction
authorNicolas De Loof <nicolas@apache.org>
Wed, 20 Feb 2008 15:29:52 +0000 (15:29 +0000)
committerNicolas De Loof <nicolas@apache.org>
Wed, 20 Feb 2008 15:29:52 +0000 (15:29 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@629505 13f79535-47bb-0310-9956-ffa450edef68

springy/archiva-base/archiva-common/pom.xml
springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunction.java [new file with mode: 0644]
springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java
springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl
springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunctionTest.java [new file with mode: 0644]

index 883ded3afb4faa5097c74f874fa8116ad37f4235..1be3c960fd215004412bf45612f916e623533b7f 100644 (file)
       <artifactId>spring-beans</artifactId>
       <version>2.5.1</version>
     </dependency>
+    <dependency>
+      <groupId>xalan</groupId>
+      <artifactId>xalan</artifactId>
+      <version>2.7.0</version>
+    </dependency>
     <dependency>
       <groupId>dom4j</groupId>
       <artifactId>dom4j</artifactId>
diff --git a/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunction.java b/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunction.java
new file mode 100644 (file)
index 0000000..858e873
--- /dev/null
@@ -0,0 +1,90 @@
+package org.apache.maven.archiva.common.spring;
+
+/*
+ * 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.
+ */
+
+import java.util.List;
+import java.util.StringTokenizer;
+
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPathFunction;
+import javax.xml.xpath.XPathFunctionException;
+import javax.xml.xpath.XPathFunctionResolver;
+
+/**
+ * XPathFunction to convert plexus property-name to Spring propertyName.
+ *
+ * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
+ * @since 1.1
+ */
+public class CamelCaseXpathFunction
+    implements XPathFunction, XPathFunctionResolver
+{
+
+    private static final QName name = new QName( "http://plexus.codehaus.org/", "camelCase" );
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see javax.xml.xpath.XPathFunctionResolver#resolveFunction(javax.xml.namespace.QName,
+     * int)
+     */
+    public XPathFunction resolveFunction( QName functionName, int arity )
+    {
+        if ( name.equals( functionName.getLocalPart() ) && arity >= 1 )
+        {
+            return new CamelCaseXpathFunction();
+        }
+        return null;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @see javax.xml.xpath.XPathFunction#evaluate(java.util.List)
+     */
+    public Object evaluate( List args )
+        throws XPathFunctionException
+    {
+        return toCamelCase( (String) args.get( 0 ) );
+    }
+
+    public static String toCamelCase( String string )
+    {
+        StringBuilder camelCase = new StringBuilder();
+        boolean first = true;
+
+        StringTokenizer tokenizer = new StringTokenizer( string.toLowerCase(), "-" );
+        while ( tokenizer.hasMoreTokens() )
+        {
+            String token = tokenizer.nextToken();
+            if ( first )
+            {
+                camelCase.append( token.charAt( 0 ) );
+                first = false;
+            }
+            else
+            {
+                camelCase.append( Character.toUpperCase( token.charAt( 0 ) ) );
+            }
+            camelCase.append( token.substring( 1, token.length() ) );
+        }
+        return camelCase.toString();
+    }
+}
index ec4a1eeb6a9d09058d8d4ced2d8273c76eb2b76b..6290af08d77954afe2132d7ef90ce6b1a2cdf5d7 100644 (file)
@@ -60,6 +60,8 @@ public class PlexusBeanDefinitionDocumentReader
 
             DOMResult transResult = new DOMResult();
 
+            // FIXME : uses Xalan extension. need either to force Xalan as Transformer or
+            // register a XpathFunctionResolver (how ?)
             TransformerFactory tf = TransformerFactory.newInstance();
             Transformer t = tf.newTransformer( xsltSource );
             t.transform( xmlSource, transResult );
index 9d0791e51c9234e19657cfe9759355d982c87bd8..eb22435fb31104226652fe69f2d068694265d3c3 100644 (file)
@@ -1,6 +1,30 @@
 <?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
 <xsl:stylesheet
-    version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
+    version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+    xmlns:plexus="org.apache.maven.archiva.common.spring.CamelCaseXpathFunction">
+<!--
+    use xalan extension mecanism to call static methods
+    @see http://www.ibm.com/developerworks/library/x-xalanextensions.html
+ -->
 
 <xsl:output method="xml" indent="yes"
     doctype-public="-//SPRING//DTD BEAN 2.0//EN"
@@ -50,7 +74,7 @@
       <xsl:for-each select="configuration/*">
         <property>
           <xsl:attribute name="name">
-            <xsl:value-of select="name(.)" />
+            <xsl:value-of select="plexus:toCamelCase( name(.) )" />
           </xsl:attribute>
           <xsl:attribute name="value">
             <xsl:value-of select="." />
diff --git a/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunctionTest.java b/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunctionTest.java
new file mode 100644 (file)
index 0000000..5a5940e
--- /dev/null
@@ -0,0 +1,28 @@
+package org.apache.maven.archiva.common.spring;
+
+import java.util.Arrays;
+
+import javax.xml.xpath.XPathFunction;
+
+import junit.framework.TestCase;
+
+/**
+ * @author ndeloof
+ *
+ */
+public class CamelCaseXpathFunctionTest
+    extends TestCase
+{
+
+    private XPathFunction function = new CamelCaseXpathFunction();
+
+    /**
+     * Test method for {@link org.apache.maven.archiva.common.spring.CamelCaseXpathFunction#toCamelCase(java.lang.String)}.
+     */
+    public void testToCamelCase()
+    throws Exception
+    {
+        assertEquals( "aCamelCaseProperty", function.evaluate( Arrays.asList( new String[] { "a-camel-case-property" } ) ) );
+    }
+
+}