1 36 package org.ungoverned.oscar.util; 37 38 import java.util.ArrayList ; 39 import java.util.List ; 40 import java.util.StringTokenizer ; 41 42 import org.osgi.framework.BundleException; 43 import org.ungoverned.oscar.LibraryInfo; 44 45 public class TextUtil 46 { 47 52 public static String [] parseCommaDelimitedString(String value) 53 { 54 if (value == null) 55 { 56 value = ""; 57 } 58 59 StringTokenizer tok = 60 new StringTokenizer (value, OscarConstants.CLASS_PATH_SEPARATOR); 61 62 int count = tok.countTokens(); 63 64 if (count == 0) 65 { 66 return null; 67 } 68 69 String [] tokens = new String [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 83 public static Object [][] parsePackageStrings(String [] packages) 84 throws IllegalArgumentException 85 { 86 if (packages == null) 87 { 88 return null; 89 } 90 91 Object [][] pkgs = new Object [packages.length][3]; 92 93 for (int pkgIdx = 0; pkgIdx < packages.length; pkgIdx++) 94 { 95 StringTokenizer tokPkg = 96 new StringTokenizer (packages[pkgIdx], OscarConstants.PACKAGE_SEPARATOR); 97 98 String pkg = tokPkg.nextToken().trim(); 99 100 if (pkg.length() == 0) 101 { 102 throw new IllegalArgumentException ("Package name cannot have length of zero."); 103 } 104 105 String 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 ("Missing '" 115 + OscarConstants.PACKAGE_VERSION_TOKEN + "' parameter."); 116 } 117 118 int idx = versionString.indexOf('='); 119 120 if (idx < 0) 121 { 122 throw new IllegalArgumentException ( 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 tokVersion = new StringTokenizer (versionString, 134 OscarConstants.VERSION_SEGMENT_SEPARATOR); 135 136 if ((tokVersion.countTokens() < 1) 137 || (tokVersion.countTokens() > OscarConstants.VERSION_SEGMENT_COUNT)) 138 { 139 throw new IllegalArgumentException ("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 ex) 156 { 157 throw new IllegalArgumentException ("Improper version number."); 158 } 159 } 160 161 pkgs[pkgIdx][0] = pkg; 162 pkgs[pkgIdx][1] = version; 163 } 164 165 return pkgs; 166 } 167 168 179 public static Object [][] parseImportExportHeader(String s) 180 throws BundleException 181 { 182 Object [][] 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 [0][3] : pkgs; 194 } 195 196 199 public static LibraryInfo[] parseLibraryStrings(String [] libStrs) 200 throws IllegalArgumentException 201 { 202 if (libStrs == null) 203 { 204 return null; 205 } 206 207 List libList = new ArrayList (); 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 |