Mercurial > repos > ganjoo > webservice_toolsuite
comparison WebServiceToolWorkflow/lib/SAWADLParser/src/edu/uga/cs/lsdis/meteors/wadls/util/StringUtils.java @ 0:d5cd409b8a18 default tip
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author | ganjoo |
---|---|
date | Tue, 07 Jun 2011 18:00:50 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d5cd409b8a18 |
---|---|
1 /* | |
2 * (c) Copyright IBM Corp 2001, 2005 | |
3 */ | |
4 | |
5 package edu.uga.cs.lsdis.meteors.wadls.util; | |
6 | |
7 import java.io.*; | |
8 import java.util.*; | |
9 import java.net.URL; | |
10 import java.net.MalformedURLException; | |
11 | |
12 /** | |
13 * Deals with strings (probably need to elaborate some more). | |
14 * | |
15 * @author Matthew J. Duftler | |
16 */ | |
17 public class StringUtils | |
18 { | |
19 public static final String lineSeparator = | |
20 System.getProperty("line.separator", "\n"); | |
21 public static final String lineSeparatorStr = cleanString(lineSeparator); | |
22 | |
23 // Ensure that escape sequences are passed through properly. | |
24 public static String cleanString(String str) | |
25 { | |
26 if (str == null) | |
27 return null; | |
28 else | |
29 { | |
30 char[] charArray = str.toCharArray(); | |
31 StringBuffer sBuf = new StringBuffer(); | |
32 | |
33 for (int i = 0; i < charArray.length; i++) | |
34 switch (charArray[i]) | |
35 { | |
36 case '\"' : sBuf.append("\\\""); | |
37 break; | |
38 case '\\' : sBuf.append("\\\\"); | |
39 break; | |
40 case '\n' : sBuf.append("\\n"); | |
41 break; | |
42 case '\r' : sBuf.append("\\r"); | |
43 break; | |
44 default : sBuf.append(charArray[i]); | |
45 break; | |
46 } | |
47 | |
48 return sBuf.toString(); | |
49 } | |
50 } | |
51 | |
52 /* | |
53 This method will return the correct name for a class object representing | |
54 a primitive, a single instance of a class, as well as n-dimensional arrays | |
55 of primitives or instances. This logic is needed to handle the string returned | |
56 from Class.getName(). If the class object represents a single instance (or | |
57 a primitive), Class.getName() returns the fully-qualified name of the class | |
58 and no further work is needed. However, if the class object represents an | |
59 array (of n dimensions), Class.getName() returns a Descriptor (the Descriptor | |
60 grammar is defined in section 4.3 of the Java VM Spec). This method will | |
61 parse the Descriptor if necessary. | |
62 */ | |
63 public static String getClassName(Class targetClass) | |
64 { | |
65 String className = targetClass.getName(); | |
66 | |
67 return targetClass.isArray() ? parseDescriptor(className) : className; | |
68 } | |
69 | |
70 /* | |
71 See the comment above for getClassName(targetClass)... | |
72 */ | |
73 private static String parseDescriptor(String className) | |
74 { | |
75 char[] classNameChars = className.toCharArray(); | |
76 int arrayDim = 0; | |
77 int i = 0; | |
78 | |
79 while (classNameChars[i] == '[') | |
80 { | |
81 arrayDim++; | |
82 i++; | |
83 } | |
84 | |
85 StringBuffer classNameBuf = new StringBuffer(); | |
86 | |
87 switch (classNameChars[i++]) | |
88 { | |
89 case 'B' : classNameBuf.append("byte"); | |
90 break; | |
91 case 'C' : classNameBuf.append("char"); | |
92 break; | |
93 case 'D' : classNameBuf.append("double"); | |
94 break; | |
95 case 'F' : classNameBuf.append("float"); | |
96 break; | |
97 case 'I' : classNameBuf.append("int"); | |
98 break; | |
99 case 'J' : classNameBuf.append("long"); | |
100 break; | |
101 case 'S' : classNameBuf.append("short"); | |
102 break; | |
103 case 'Z' : classNameBuf.append("boolean"); | |
104 break; | |
105 case 'L' : classNameBuf.append(classNameChars, | |
106 i, classNameChars.length - i - 1); | |
107 break; | |
108 } | |
109 | |
110 for (i = 0; i < arrayDim; i++) | |
111 classNameBuf.append("[]"); | |
112 | |
113 return classNameBuf.toString(); | |
114 } | |
115 | |
116 /* | |
117 @param contextURL the context in which to attempt to resolve the spec. | |
118 Effectively a document base. | |
119 */ | |
120 public static URL getURL(URL contextURL, String spec) | |
121 throws MalformedURLException | |
122 { | |
123 if (contextURL != null) | |
124 { | |
125 File tempFile = new File(spec); | |
126 | |
127 if (tempFile.isAbsolute()) | |
128 { | |
129 return tempFile.toURL(); | |
130 } | |
131 } | |
132 | |
133 try | |
134 { | |
135 return new URL(contextURL, spec); | |
136 } | |
137 catch (MalformedURLException e) | |
138 { | |
139 if (contextURL == null) | |
140 { | |
141 return new File(spec).toURL(); | |
142 } | |
143 else | |
144 { | |
145 throw e; | |
146 } | |
147 } | |
148 } | |
149 | |
150 /* | |
151 Returns an InputStream for reading from the specified resource, if the | |
152 resource points to a stream. | |
153 */ | |
154 public static InputStream getContentAsInputStream(URL url) | |
155 throws SecurityException, | |
156 IllegalArgumentException, | |
157 IOException | |
158 { | |
159 if (url == null) | |
160 { | |
161 throw new IllegalArgumentException("URL cannot be null."); | |
162 } | |
163 | |
164 try | |
165 { | |
166 Object content = url.getContent(); | |
167 | |
168 if (content == null) | |
169 { | |
170 throw new IllegalArgumentException("No content."); | |
171 } | |
172 | |
173 if (content instanceof InputStream) | |
174 { | |
175 return (InputStream)content; | |
176 } | |
177 else | |
178 { | |
179 throw new IllegalArgumentException((content instanceof String) | |
180 ? (String)content | |
181 : "This URL points to a: " + | |
182 StringUtils.getClassName(content.getClass())); | |
183 } | |
184 } | |
185 catch (SecurityException e) | |
186 { | |
187 throw new SecurityException("Your JVM's SecurityManager has " + | |
188 "disallowed this."); | |
189 } | |
190 catch (FileNotFoundException e) | |
191 { | |
192 throw new FileNotFoundException("This file was not found: " + url); | |
193 } | |
194 } | |
195 | |
196 public static List parseNMTokens(String nmTokens) | |
197 { | |
198 StringTokenizer strTok = new StringTokenizer(nmTokens, " "); | |
199 List tokens = new Vector(); | |
200 | |
201 while (strTok.hasMoreTokens()) | |
202 { | |
203 tokens.add(strTok.nextToken()); | |
204 } | |
205 | |
206 return tokens; | |
207 } | |
208 | |
209 public static String getNMTokens(List list) | |
210 { | |
211 if (list != null) | |
212 { | |
213 StringBuffer strBuf = new StringBuffer(); | |
214 int size = list.size(); | |
215 | |
216 for (int i = 0; i < size; i++) | |
217 { | |
218 String token = (String)list.get(i); | |
219 | |
220 strBuf.append((i > 0 ? " " : "") + token); | |
221 } | |
222 | |
223 return strBuf.toString(); | |
224 } | |
225 else | |
226 { | |
227 return null; | |
228 } | |
229 } | |
230 } |