1 package pl.caltha.forms.internal.util;
2
3 import java.io.File;
4 import java.net.MalformedURLException;
5 import java.net.URI;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9
10 import org.xml.sax.Attributes;
11
12 import pl.caltha.forms.ConstructionException;
13
14 /*** Utility class for form-tool.
15 *
16 * @author <a href="mailto:zwierzem@ngo.pl">Damian Gajda</a>
17 * @version $Id: Util.java,v 1.4 2006/04/28 10:02:23 pablo Exp $
18 */
19 public class Util
20 {
21
22
23
24 public static void insertMultipleIntoHash(Object key, Object value, HashMap hashMap)
25 {
26 if(key == null)
27 {
28 return;
29 }
30
31 ArrayList list = (ArrayList)(hashMap.get(key));
32 if(list == null)
33 {
34 list = new ArrayList(2);
35 }
36
37 if(!list.contains(value))
38 {
39 list.add(value);
40 }
41 hashMap.put(key, list);
42 }
43
44
45
46
47 public static boolean createBooleanAttribute(Attributes atts, String name, boolean defVal)
48 {
49 if(Util.getSAXAttributeVal(atts, name) != null)
50 {
51 return createBooleanAttribute(atts, name);
52 }
53 else
54 {
55 return defVal;
56 }
57 }
58
59 public static boolean createBooleanAttribute(Attributes atts, String name)
60 {
61 return Boolean.valueOf(Util.getSAXAttributeVal(atts, name)).booleanValue();
62 }
63
64 public static double createDoubleAttribute(Attributes atts, String name, double defVal)
65 throws ConstructionException
66 {
67 if(Util.getSAXAttributeVal(atts, name) != null)
68 {
69 return createDoubleAttribute(atts, name);
70 }
71 else
72 {
73 return defVal;
74 }
75 }
76
77 public static double createDoubleAttribute(Attributes atts, String name)
78 throws ConstructionException
79 {
80 try
81 {
82 return Double.parseDouble(Util.getSAXAttributeVal(atts, name));
83 }
84 catch (NumberFormatException nfe)
85 {
86 throw new ConstructionException("Invalid number attribute '"+name+"'", nfe);
87 }
88 }
89
90 public static int createIntAttribute(Attributes atts, String name, int defVal)
91 throws ConstructionException
92 {
93 if(Util.getSAXAttributeVal(atts, name) != null)
94 {
95 return createIntAttribute(atts, name);
96 }
97 else
98 {
99 return defVal;
100 }
101 }
102
103 public static String createAttribute(Attributes atts, String name, String defVal)
104 throws ConstructionException
105 {
106 String value = getSAXAttributeVal(atts, name);
107 if(value != null)
108 {
109 return value;
110 }
111 else
112 {
113 return defVal;
114 }
115 }
116
117 public static int createIntAttribute(Attributes atts, String name)
118 throws ConstructionException
119 {
120 try
121 {
122 return Integer.parseInt(Util.getSAXAttributeVal(atts, name));
123 }
124 catch (NumberFormatException nfe)
125 {
126 throw new ConstructionException("Invalid number attribute '"+name+"'", nfe);
127 }
128 }
129
130 /*** Retrieves a value from SAX Attributes object.
131 * @param atts Attributes from element parsing, provided by SAX parser.
132 * @param name Name of the attribute to be extracted.
133 * @return Value of the attribute or null if attribute was not found.
134 */
135 public static String getSAXAttributeVal(Attributes atts, String name)
136 {
137 return getSAXAttributeVal(atts, null, name);
138 }
139
140 /*** Retrieves a value from SAX Attributes object.
141 * @return Value of the attribute or null if attribute was not found.
142 * @param nameSpace XML namespace URI to which this attribute belongs.
143 * @param atts Attributes from element parsing, provided by SAX parser.
144 * @param name Name of the attribute to be extracted.
145 */
146 public static String getSAXAttributeVal(Attributes atts, String nameSpace, String name)
147 {
148 int i = -1;
149 if(nameSpace == null)
150 {
151 i = atts.getIndex(name);
152 }
153 else
154 {
155 i = atts.getIndex(nameSpace, name);
156 }
157 if (i >= 0)
158 {
159 return atts.getValue(i);
160 }
161 return null;
162 }
163
164 /*** Returns expanded string version of an URI.
165 * First checks if a <code>resourceURI</code> if its already expanded,
166 * if not treats it as an URI relative to a <code>baseURI</code>.
167 * @param baseURI a base URI
168 * @param resourceURI an URI of a resoruce.
169 * @return Expanded URI or an empty string if resoruceURI was empty.
170 */
171 public static String expandURI(String baseURI, String resourceURI)
172 {
173
174 if (resourceURI == null || resourceURI.length() == 0)
175 {
176 return "";
177 }
178
179
180 try
181 {
182 new URI(resourceURI);
183 return resourceURI;
184 }
185 catch (Exception e) {
186
187 }
188
189
190 resourceURI = fixURI(resourceURI);
191
192
193 URI base = null;
194 URI uri = null;
195 try
196 {
197 base = new URI(baseURI);
198
199
200
201 uri = base.resolve(resourceURI);
202 }
203 catch (Exception e)
204 {
205
206 }
207
208 if (uri == null)
209 {
210 return resourceURI;
211 }
212
213 return uri.toString();
214 }
215
216 private static String fixURI(String str)
217 {
218
219 str = str.replace(java.io.File.separatorChar, '/');
220
221
222 if (str.length() >= 2)
223 {
224 char ch1 = str.charAt(1);
225 if (ch1 == ':')
226 {
227 char ch0 = Character.toUpperCase(str.charAt(0));
228 if (ch0 >= 'A' && ch0 <= 'Z')
229 {
230 str = "/" + str;
231 }
232 }
233 }
234
235 return str;
236 }
237
238 public static String getWorkingPath(String parentPath, String resourcePath)
239 {
240 File resourceFile = new File(resourcePath);
241 if(resourceFile.exists())
242 {
243 return resourceFile.getAbsolutePath();
244 }
245
246 File parentFile = new File(parentPath);
247 if(!parentFile.isDirectory())
248 {
249 if(parentFile.getParentFile() != null)
250 {
251 parentFile = parentFile.getParentFile();
252 }
253 }
254
255 resourceFile = new File(parentFile.getPath(), resourcePath);
256 if(resourceFile.exists())
257 {
258 return resourceFile.getAbsolutePath();
259 }
260
261 return null;
262 }
263
264 public static URL getWorkingURL(URL parentURL, String resourceURL)
265 {
266 try
267 {
268 return new URL(resourceURL);
269 }
270 catch(MalformedURLException e)
271 {
272
273 }
274
275 try
276 {
277 return new URL(parentURL, resourceURL);
278 }
279 catch(MalformedURLException e)
280 {
281
282 }
283 return null;
284 }
285 }