KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > util > TextUtil


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.util;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41
42 import org.osgi.framework.BundleException;
43 import org.ungoverned.oscar.LibraryInfo;
44
45 public class TextUtil
46 {
47     /**
48      * Parses comma delimited string and returns an array containing the tokens.
49      * @param value the comma delimited string to parse.
50      * @return an array of string tokens or null if there were no tokens.
51     **/

52     public static String JavaDoc[] parseCommaDelimitedString(String JavaDoc value)
53     {
54         if (value == null)
55         {
56            value = "";
57         }
58
59         StringTokenizer JavaDoc tok =
60             new StringTokenizer JavaDoc(value, OscarConstants.CLASS_PATH_SEPARATOR);
61
62         int count = tok.countTokens();
63
64         if (count == 0)
65         {
66             return null;
67         }
68
69         // Create an array containing the tokens.
70
String JavaDoc[] tokens = new String JavaDoc[count];
71
72         for (int i = 0; tok.hasMoreElements(); i++)
73         {
74             tokens[i] = tok.nextToken().trim();
75         }
76
77         return tokens;
78     }
79
80     /**
81      * Parse package strings from manifest.
82     **/

83     public static Object JavaDoc[][] parsePackageStrings(String JavaDoc[] packages)
84         throws IllegalArgumentException JavaDoc
85     {
86         if (packages == null)
87         {
88             return null;
89         }
90
91         Object JavaDoc[][] pkgs = new Object JavaDoc[packages.length][3];
92
93         for (int pkgIdx = 0; pkgIdx < packages.length; pkgIdx++)
94         {
95             StringTokenizer JavaDoc tokPkg =
96                 new StringTokenizer JavaDoc(packages[pkgIdx], OscarConstants.PACKAGE_SEPARATOR);
97
98             String JavaDoc pkg = tokPkg.nextToken().trim();
99
100             if (pkg.length() == 0)
101             {
102                 throw new IllegalArgumentException JavaDoc("Package name cannot have length of zero.");
103             }
104
105             String JavaDoc versionString = tokPkg.hasMoreTokens()
106                 ? tokPkg.nextToken().trim() : null;
107             int[] version = new int[OscarConstants.VERSION_SEGMENT_COUNT];
108
109             if (versionString != null)
110             {
111                 if (!versionString.toLowerCase().startsWith(
112                     OscarConstants.PACKAGE_VERSION_TOKEN))
113                 {
114                     throw new IllegalArgumentException JavaDoc("Missing '"
115                         + OscarConstants.PACKAGE_VERSION_TOKEN + "' parameter.");
116                 }
117
118                 int idx = versionString.indexOf('=');
119
120                 if (idx < 0)
121                 {
122                     throw new IllegalArgumentException JavaDoc(
123                         "Version specification missing assignment.");
124                 }
125
126                 versionString = versionString.substring(idx + 1).trim();
127
128                 if (versionString.startsWith("\"") && versionString.endsWith("\""))
129                 {
130                     versionString = versionString.substring(1, versionString.length() - 1);
131                 }
132
133                 StringTokenizer JavaDoc tokVersion = new StringTokenizer JavaDoc(versionString,
134                     OscarConstants.VERSION_SEGMENT_SEPARATOR);
135
136                 if ((tokVersion.countTokens() < 1)
137                     || (tokVersion.countTokens() > OscarConstants.VERSION_SEGMENT_COUNT))
138                 {
139                     throw new IllegalArgumentException JavaDoc("Improper version number: "
140                         + versionString);
141                 }
142
143                 try
144                 {
145                     version[0] = Integer.parseInt(tokVersion.nextToken());
146                     if (tokVersion.hasMoreTokens())
147                     {
148                         version[1] = Integer.parseInt(tokVersion.nextToken());
149                         if (tokVersion.hasMoreTokens())
150                         {
151                             version[2] = Integer.parseInt(tokVersion.nextToken());
152                         }
153                     }
154                 }
155                 catch (NumberFormatException JavaDoc ex)
156                 {
157                     throw new IllegalArgumentException JavaDoc("Improper version number.");
158                 }
159             }
160
161             pkgs[pkgIdx][0] = pkg;
162             pkgs[pkgIdx][1] = version;
163         }
164
165         return pkgs;
166     }
167
168     /**
169      * Parses the <tt>Import-Package</tt> or <tt>Export-Package</tt>
170      * manifest header. This routine will throw an exception if the
171      * passed in string value is an empty string, but a <tt>null</tt>
172      * value is acceptable.
173      * @param s the value of the import or export manifest header.
174      * @return an array of <tt>Object</tt> arrays, one for each parsed
175      * package, or an empty array if there are no packages.
176      * throws org.osgi.framework.BundleException if there is an error
177      * parsing the string.
178     **/

179     public static Object JavaDoc[][] parseImportExportHeader(String JavaDoc s)
180         throws BundleException
181     {
182         Object JavaDoc[][] pkgs = null;
183         if (s != null)
184         {
185             if (s.length() == 0)
186             {
187                 throw new BundleException(
188                     "The import and export headers cannot be an empty string.");
189             }
190             pkgs = TextUtil.parsePackageStrings(
191                 TextUtil.parseCommaDelimitedString(s));
192         }
193         return (pkgs == null) ? new Object JavaDoc[0][3] : pkgs;
194     }
195
196     /**
197      * Parse native code from manifest.
198     **/

199     public static LibraryInfo[] parseLibraryStrings(String JavaDoc[] libStrs)
200         throws IllegalArgumentException JavaDoc
201     {
202         if (libStrs == null)
203         {
204             return null;
205         }
206
207         List JavaDoc libList = new ArrayList JavaDoc();
208
209         for (int i = 0; i < libStrs.length; i++)
210         {
211             LibraryInfo[] libs = LibraryInfo.parse(libStrs[i]);
212             for (int libIdx = 0;
213                 (libs != null) && (libIdx < libs.length);
214                 libIdx++)
215             {
216                 libList.add(libs[libIdx]);
217             }
218         }
219
220         if (libList.size() == 0)
221         {
222             return null;
223         }
224
225         return (LibraryInfo[]) libList.toArray(new LibraryInfo[libList.size()]);
226     }
227 }
Popular Tags