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.activemq.maven;
018    
019    import java.io.File;
020    import java.net.MalformedURLException;
021    import java.net.URL;
022    
023    /**
024     * Helper to convert relative paths to XBean description files to URL-compliant absolute paths.
025     * 
026     * @author Marc CARRE <carre.marc@gmail.com>
027     */
028    public class XBeanFileResolver {
029        private static final String XBEAN_FILE = "xbean:file:";
030    
031        private XBeanFileResolver() {
032            // Utility class, should not be instantiated.
033        }
034    
035        /**
036         * Check if the provided path is an URL to a XBean file (xbean:file:<path/to/file>)
037         */
038        public static boolean isXBeanFile(final String configUri) {
039            return configUri.startsWith(XBEAN_FILE);
040        }
041    
042        /**
043         * Convert provided path into a URL-style absolute path. See also:
044         * http://maven.apache.org/plugin-developers/common-bugs.html# Converting_between_URLs_and_Filesystem_Paths
045         */
046        public static String toUrlCompliantAbsolutePath(final String configUri) {
047            if (!isXBeanFile(configUri))
048                return configUri;
049    
050            String filePath = extractFilePath(configUri);
051            return XBEAN_FILE + toAbsolutePath(filePath);
052        }
053    
054        private static String extractFilePath(final String configUri) {
055            return configUri.substring(getIndexFilePath(configUri), configUri.length());
056        }
057    
058        private static int getIndexFilePath(final String configUri) {
059            return configUri.indexOf(XBEAN_FILE) + XBEAN_FILE.length();
060        }
061    
062        private static String toAbsolutePath(final String path) {
063            try {
064                final URL url = new File(path).toURI().toURL();
065                return toFilePath(url);
066            } catch (MalformedURLException e) {
067                throw new RuntimeException("Failed to resolve relative path for: " + path, e);
068            }
069        }
070    
071        private static String toFilePath(final URL url) {
072            String filePath = url.getFile();
073            return underWindows() ? removePrependingSlash(filePath) : filePath;
074        }
075    
076        private static String removePrependingSlash(String filePath) {
077            // Remove prepending '/' because path would be /C:/temp/file.txt, as URL would be file:/C:/temp/file.txt
078            return filePath.substring(1, filePath.length());
079        }
080    
081        private static boolean underWindows() {
082            String os = System.getProperty("os.name").toLowerCase();
083            return (os.indexOf("win") >= 0);
084        }
085    }