1 /*
2 * Copyright (c) 2007, Ounce Labs, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the <organization> nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY OUNCE LABS, INC. ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL OUNCE LABS, INC. BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 package org.codehaus.mojo.ounce.utils;
28
29 import java.lang.reflect.Field;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35
36 import org.codehaus.plexus.util.StringUtils;
37
38 /**
39 * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
40 */
41 public class Utils
42 {
43
44 // static final String propertyFormat = "${**}";
45 static final String propertyFormat = "%**%";
46
47 /**
48 * Removes the pathToRemove from the path and optionally replaces it with a key
49 *
50 * @param path original path
51 * @param pathToRemove string to replace
52 * @param key string to replace with
53 * @return normalized string beginning with the key
54 */
55 public static String convertToRelativePath( String path, String pathToRemove, String key )
56 {
57 path = Utils.convertToUnixStylePath( path );
58 pathToRemove = Utils.convertToUnixStylePath( pathToRemove );
59
60 if ( StringUtils.isNotEmpty( key ) )
61 {
62 String formattedKey = StringUtils.replace( propertyFormat, "**", key );
63 path = StringUtils.replace( path, pathToRemove, formattedKey );
64 }
65 else
66 {
67 path = StringUtils.replace( path, pathToRemove + "/", "" );
68
69 // if any exact matches remain, replace with .
70 path = StringUtils.replace( path, pathToRemove, "." );
71 }
72
73 return path;
74 }
75
76 /**
77 * @param path
78 * @param pathVariableMap
79 * @return
80 */
81 static public String convertToVariablePath( String path, Map pathProperties )
82 {
83 if ( path != null && pathProperties != null && pathProperties.size() > 0 )
84 {
85 for ( Iterator iter = pathProperties.entrySet().iterator(); iter.hasNext(); )
86 {
87 Entry entry = (Entry) iter.next();
88 path = convertToRelativePath( path, (String) entry.getValue(), (String) entry.getKey() );
89 }
90 }
91 else
92 {
93 path = convertToUnixStylePath( path );
94 }
95 return path;
96 }
97
98 /**
99 * Performs the path/key substitution for all paths.
100 *
101 * @param paths
102 * @param pathVariableMap map of key/path pairs to replace
103 * @return processed list
104 */
105 static public List convertToPropertyPaths( List paths, Map pathProperties )
106 {
107 List result = paths;
108 if ( pathProperties != null && pathProperties.size() > 0 )
109 {
110 for ( Iterator iter = pathProperties.entrySet().iterator(); iter.hasNext(); )
111 {
112 Entry entry = (Entry) iter.next();
113 result = convertToRelativePaths( result, (String) entry.getValue(), (String) entry.getKey() );
114 }
115 }
116 return result;
117 }
118
119 /**
120 * Removes the pathToRemove from the paths and optionally replaces it with a key
121 *
122 * @param paths original paths in a list
123 * @param pathToRemove string to replace
124 * @param key string to replace with
125 * @return normalized string beginning with the key
126 */
127 static public List convertToRelativePaths( List paths, String pathToRemove, String key )
128 {
129 // go through the list, replace remove the
130 // strings
131 List newPaths = new ArrayList( paths.size() );
132 Iterator iter = paths.iterator();
133
134 while ( iter.hasNext() )
135 {
136 newPaths.add( convertToRelativePath( (String) iter.next(), pathToRemove, key ) );
137 }
138 return newPaths;
139 }
140
141 /**
142 * Converts the path to the correct format for ounce.
143 *
144 * @param pName
145 * @return
146 */
147 static public String convertToUnixStylePath( String pName )
148 {
149 if ( pName != null )
150 {
151 return pName.replace( '\\', '/' );
152 }
153 else
154 {
155 return null;
156 }
157 }
158
159 /**
160 * Use reflection to generate a toString with all parameters.
161 *
162 * @param obj
163 * @return
164 */
165 public static synchronized String getDynamicToString( Object obj )
166 {
167 StringBuffer buf = new StringBuffer();
168
169 try
170 {
171 Class clazz = obj.getClass();
172 buf.append( clazz.getName() + ": " );
173
174 Field[] fields = clazz.getDeclaredFields();
175 for ( int i = 0; i < fields.length; i++ )
176 {
177 buf.append( " " );
178 buf.append( fields[i].getName() );
179 buf.append( "= " );
180 try
181 {
182 fields[i].setAccessible( true );
183
184 buf.append( fields[i].get( obj ) );
185 buf.append( " " );
186 }
187 catch ( Exception e )
188 {
189 buf.append( "Error Retrieving Value " );
190 }
191 }
192 }
193 catch ( Exception e )
194 {
195 buf.append( "Exception: " + e.getMessage() );
196 }
197 return buf.toString();
198 }
199 }