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