001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.xbean.spring.context.impl;
018    
019    /**
020     * A helper class for turning namespaces into META-INF/services URIs
021     * 
022     * @version $Revision: 1.1 $
023     */
024    public class NamespaceHelper {
025        public static final String META_INF_PREFIX = "META-INF/services/org/apache/xbean/spring/";
026        public static final String OLD_META_INF_PREFIX = "META-INF/services/org/xbean/spring/";
027    
028        /**
029         * Converts the namespace and localName into a valid path name we can use on
030         * the classpath to discover a text file
031         */
032        public static String createDiscoveryPathName(String uri, String localName) {
033            if (isEmpty(uri)) {
034                return localName;
035            }
036            return createDiscoveryPathName(uri) + "/" + localName;
037        }
038    
039        /**
040         * Converts the namespace and localName into a valid path name we can use on
041         * the classpath to discover a text file
042         */
043        public static String createDiscoveryPathName(String uri) {
044            // TODO proper encoding required
045            // lets replace any dodgy characters
046            return META_INF_PREFIX + uri.replaceAll("://", "/").replace(':', '/').replace(' ', '_');
047        }
048    
049        /**
050         * Creates the old URI for backwards compatibility
051         */
052        public static String createDiscoveryOldPathName(String uri) {
053            // TODO proper encoding required
054            // lets replace any dodgy characters
055            return OLD_META_INF_PREFIX + uri.replaceAll("://", "/").replace(':', '/').replace(' ', '_');
056        }
057    
058        public static boolean isEmpty(String uri) {
059            return uri == null || uri.length() == 0;
060        }
061    
062    }