From 11345c495de00726ec60e30c5a96fdd52b77c386 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 20 Feb 2008 15:29:52 +0000 Subject: [PATCH] use Xalan extension to invoke custom XpathFunction 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 | 5 ++ .../common/spring/CamelCaseXpathFunction.java | 90 +++++++++++++++++++ .../PlexusBeanDefinitionDocumentReader.java | 2 + .../archiva/common/spring/plexus2spring.xsl | 28 +++++- .../spring/CamelCaseXpathFunctionTest.java | 28 ++++++ 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunction.java create mode 100644 springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunctionTest.java diff --git a/springy/archiva-base/archiva-common/pom.xml b/springy/archiva-base/archiva-common/pom.xml index 883ded3af..1be3c960f 100644 --- a/springy/archiva-base/archiva-common/pom.xml +++ b/springy/archiva-base/archiva-common/pom.xml @@ -56,6 +56,11 @@ spring-beans 2.5.1 + + xalan + xalan + 2.7.0 + dom4j dom4j 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 index 000000000..858e8736d --- /dev/null +++ b/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunction.java @@ -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 Nicolas De Loof + * @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(); + } +} diff --git a/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java b/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java index ec4a1eeb6..6290af08d 100644 --- a/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java +++ b/springy/archiva-base/archiva-common/src/main/java/org/apache/maven/archiva/common/spring/PlexusBeanDefinitionDocumentReader.java @@ -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 ); diff --git a/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl b/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl index 9d0791e51..eb22435fb 100644 --- a/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl +++ b/springy/archiva-base/archiva-common/src/main/resources/org/apache/maven/archiva/common/spring/plexus2spring.xsl @@ -1,6 +1,30 @@ + + + version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" + xmlns:plexus="org.apache.maven.archiva.common.spring.CamelCaseXpathFunction"> + - + 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 index 000000000..5a5940eac --- /dev/null +++ b/springy/archiva-base/archiva-common/src/test/java/org/apache/maven/archiva/common/spring/CamelCaseXpathFunctionTest.java @@ -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" } ) ) ); + } + +} -- 2.39.5