1 /* 2 * Copyright 2006-2012 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.maven.util; 17 18 import java.io.File; 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import org.apache.maven.artifact.Artifact; 23 import org.codehaus.plexus.util.StringUtils; 24 25 /** 26 * Utilities for handling paths Maven. 27 * 28 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a> 29 * @version $Revision:591 $ 30 */ 31 public final class PathUtils 32 { 33 // ********************************* Fields ********************************* 34 35 // --- constants ------------------------------------------------------------ 36 37 // --- members -------------------------------------------------------------- 38 39 // ****************************** Initializer ******************************* 40 41 // ****************************** Constructors ****************************** 42 43 /** 44 * Utility class pattern. 45 */ 46 private PathUtils() 47 { 48 } 49 50 // ****************************** Inner Classes ***************************** 51 52 // ********************************* Methods ******************************** 53 54 // --- init ----------------------------------------------------------------- 55 56 // --- get&set -------------------------------------------------------------- 57 58 // --- business ------------------------------------------------------------- 59 60 /** 61 * Builds a class path string from the list of artifacts. 62 * 63 * @param artifacts the artifacts to include into the class path. 64 * @param pathSeparator the separator to put between the path elements. 65 * @return the generated class path string. 66 */ 67 public static String toClassPathString(final List<Artifact> artifacts, 68 final char pathSeparator) 69 { 70 final StringBuffer buffer = new StringBuffer(1024); 71 72 for (Artifact artifact : artifacts) 73 { 74 final File file = artifact.getFile(); 75 final String path = file.getPath(); 76 buffer.append(path).append(pathSeparator); 77 } 78 79 return StringUtils.chop(buffer.toString()); 80 } 81 82 /** 83 * Builds a class path from the list of artifacts. 84 * 85 * @param artifacts the artifacts to include into the class path. 86 * @return the generated class path elements ({@link String}s). 87 */ 88 public static List<String> toClassPath(final List<Artifact> artifacts) 89 { 90 final List<String> classPathElements = 91 new ArrayList<String>(artifacts.size()); 92 93 for (Artifact artifact : artifacts) 94 { 95 final File file = artifact.getFile(); 96 final String path = file.getPath(); 97 classPathElements.add(path); 98 } 99 100 return classPathElements; 101 } 102 103 /** 104 * Checks if any elements of the list of directory roots exists. 105 * 106 * @param roots the directory roots check for existence. 107 * @return <code>true</code> if at least one of the roots exists, 108 * <code>false</code> otherwise. 109 */ 110 public static boolean exists(final List<String> roots) 111 { 112 for (String root : roots) 113 { 114 final File rootDir = new File(root); 115 if (rootDir.isDirectory()) 116 { 117 return true; 118 } 119 } 120 return false; 121 } 122 123 public static String getUpPath(final String relativePath) 124 { 125 if (StringUtils.isBlank(relativePath)) 126 { 127 return ("."); 128 } 129 final String normalized = relativePath.replace('\\', '/'); 130 final int count = StringUtils.countMatches(normalized, "/"); 131 if (count > 0) 132 { 133 final String path = StringUtils.repeat("../", count + 1); 134 return StringUtils.chop(path); 135 } 136 else 137 { 138 return (".."); 139 } 140 } 141 142 // --- object basics -------------------------------------------------------- 143 144 }