KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > util > StringUtils


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.util;
59
60 import java.io.*;
61 import java.util.*;
62 import java.net.URL JavaDoc;
63 import java.net.MalformedURLException JavaDoc;
64 import java.beans.Introspector JavaDoc;
65
66 /**
67  * Deals with strings (probably need to elaborate some more).
68  *
69  * @author Matthew J. Duftler
70  */

71 public class StringUtils
72 {
73   public static final String JavaDoc lineSeparator =
74     System.getProperty("line.separator", "\n");
75   public static String JavaDoc URI_SEPARATION_CHAR = "@";
76
77   /*
78     This method will return the correct name for a class object representing
79     a primitive, a single instance of a class, as well as n-dimensional arrays
80     of primitives or instances. This logic is needed to handle the string returned
81     from Class.getName(). If the class object represents a single instance (or
82     a primitive), Class.getName() returns the fully-qualified name of the class
83     and no further work is needed. However, if the class object represents an
84     array (of n dimensions), Class.getName() returns a Descriptor (the Descriptor
85     grammar is defined in section 4.3 of the Java VM Spec). This method will
86     parse the Descriptor if necessary.
87   */

88   public static String JavaDoc getClassName(Class JavaDoc targetClass)
89   {
90     String JavaDoc className = targetClass.getName();
91
92     return targetClass.isArray() ? parseDescriptor(className) : className;
93   }
94
95   /*
96     See the comment above for getClassName(targetClass)...
97   */

98   private static String JavaDoc parseDescriptor(String JavaDoc className)
99   {
100     char[] classNameChars = className.toCharArray();
101     int arrayDim = 0;
102     int i = 0;
103
104     while (classNameChars[i] == '[')
105     {
106       arrayDim++;
107       i++;
108     }
109
110     StringBuffer JavaDoc classNameBuf = new StringBuffer JavaDoc();
111
112     switch (classNameChars[i++])
113     {
114       case 'B' : classNameBuf.append("byte");
115                  break;
116       case 'C' : classNameBuf.append("char");
117                  break;
118       case 'D' : classNameBuf.append("double");
119                  break;
120       case 'F' : classNameBuf.append("float");
121                  break;
122       case 'I' : classNameBuf.append("int");
123                  break;
124       case 'J' : classNameBuf.append("long");
125                  break;
126       case 'S' : classNameBuf.append("short");
127                  break;
128       case 'Z' : classNameBuf.append("boolean");
129                  break;
130       case 'L' : classNameBuf.append(classNameChars,
131                                      i, classNameChars.length - i - 1);
132                  break;
133     }
134
135     for (i = 0; i < arrayDim; i++)
136       classNameBuf.append("[]");
137
138     return classNameBuf.toString();
139   }
140
141   /*
142     The recursiveDepth argument is used to insure that the algorithm gives up
143     after hunting 2 levels up in the contextURL's path.
144   */

145   private static URL JavaDoc getURL(URL JavaDoc contextURL, String JavaDoc spec, int recursiveDepth)
146                                                   throws MalformedURLException JavaDoc
147   {
148     URL JavaDoc url = null;
149
150     try
151     {
152       url = new URL JavaDoc(contextURL, spec);
153
154       try
155       {
156         url.openStream();
157       }
158       catch (IOException ioe1)
159       {
160         throw new MalformedURLException JavaDoc("This file was not found: " + url);
161       }
162     }
163     catch (MalformedURLException JavaDoc e1)
164     {
165       url = new URL JavaDoc("file", "", spec);
166
167       try
168       {
169         url.openStream();
170       }
171       catch (IOException ioe2)
172       {
173         if (contextURL != null)
174         {
175           String JavaDoc contextFileName = contextURL.getFile();
176           String JavaDoc parentName = new File(contextFileName).getParent();
177
178           if (parentName != null && recursiveDepth < 3)
179           {
180             return getURL(new URL JavaDoc("file", "", parentName + '/'),
181                           spec,
182                           recursiveDepth + 1);
183           }
184         }
185
186         throw new MalformedURLException JavaDoc("This file was not found: " + url);
187       }
188     }
189
190     return url;
191   }
192
193   /*
194   */

195   public static URL JavaDoc getURL(URL JavaDoc contextURL, String JavaDoc spec) throws MalformedURLException JavaDoc
196   {
197     return getURL(contextURL, spec, 1);
198   }
199
200   /*
201     Returns a Reader for reading from the specified resource, if the resource
202     points to a stream.
203   */

204   public static Reader getContentAsReader(URL JavaDoc url) throws SecurityException JavaDoc,
205                                                           IllegalArgumentException JavaDoc,
206                                                           IOException
207   {
208     if (url == null)
209     {
210       throw new IllegalArgumentException JavaDoc("URL cannot be null.");
211     }
212
213     try
214     {
215       Object JavaDoc content = url.getContent();
216
217       if (content == null)
218       {
219         throw new IllegalArgumentException JavaDoc("No content.");
220       }
221
222       if (content instanceof InputStream)
223       {
224         Reader in = new InputStreamReader((InputStream)content);
225
226         if (in.ready())
227         {
228           return in;
229         }
230         else
231         {
232           throw new FileNotFoundException();
233         }
234       }
235       else
236       {
237         throw new IllegalArgumentException JavaDoc((content instanceof String JavaDoc)
238                                            ? (String JavaDoc)content
239                                            : "This URL points to a: " +
240                                              StringUtils.getClassName(content.getClass()));
241       }
242     }
243     catch (SecurityException JavaDoc e)
244     {
245       throw new SecurityException JavaDoc("Your JVM's SecurityManager has disallowed this.");
246     }
247     catch (FileNotFoundException e)
248     {
249       throw new FileNotFoundException("This file was not found: " + url);
250     }
251   }
252
253   /*
254     Shorthand for: IOUtils.getStringFromReader(getContentAsReader(url)).
255   */

256   public static String JavaDoc getContentAsString(URL JavaDoc url) throws SecurityException JavaDoc,
257                                                           IllegalArgumentException JavaDoc,
258                                                           IOException
259   {
260     return IOUtils.getStringFromReader(getContentAsReader(url));
261   }
262
263   /**
264   * This method will perform the splicing of a full URI. It is currently
265   * the only place where the delimiting character in the URI that triggers the
266   * splicing operation is specified. (This character should later be specified
267   * as a constant...
268   *
269   * Creation date: (10/23/00 2:54:33 PM)
270   * @return java.lang.String
271   * @param fullTargetObjectURI java.lang.String
272   */

273   public static String JavaDoc parseFullTargetObjectURI(String JavaDoc fullTargetObjectURI) {
274          if ( fullTargetObjectURI == null ) return null ;
275      int delimIndex = fullTargetObjectURI.indexOf(URI_SEPARATION_CHAR);
276      if ( (fullTargetObjectURI != null) && (delimIndex != -1) )
277          return fullTargetObjectURI.substring(0,delimIndex);
278      else
279          return fullTargetObjectURI;
280     
281   }
282  
283
284 }
285
Popular Tags