1 11 12 package org.eclipse.pde.internal.build; 13 14 import java.io.*; 15 import java.util.*; 16 import javax.xml.parsers.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 import org.xml.sax.*; 20 import org.xml.sax.helpers.DefaultHandler ; 21 22 26 public class ProductFile extends DefaultHandler implements IPDEBuildConstants { 27 private final static SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 28 29 private static final String PROGRAM_ARGS = "programArgs"; private static final String PROGRAM_ARGS_LINUX = "programArgsLin"; private static final String PROGRAM_ARGS_MAC = "programArgsMac"; private static final String PROGRAM_ARGS_SOLARIS = "programArgsSol"; private static final String PROGRAM_ARGS_WIN = "programArgsWin"; private static final String VM_ARGS = "vmArgs"; private static final String VM_ARGS_LINUX = "vmArgsLin"; private static final String VM_ARGS_MAC = "vmArgsMac"; private static final String VM_ARGS_SOLARIS = "vmArgsSol"; private static final String VM_ARGS_WIN = "vmArgsWin"; 40 private static final String SOLARIS_LARGE = "solarisLarge"; private static final String SOLARIS_MEDIUM = "solarisMedium"; private static final String SOLARIS_SMALL = "solarisSmall"; private static final String SOLARIS_TINY = "solarisTiny"; private static final String WIN32_16_LOW = "winSmallLow"; private static final String WIN32_16_HIGH = "winSmallHigh"; private static final String WIN32_24_LOW = "win24Low"; private static final String WIN32_32_LOW = "winMediumLow"; private static final String WIN32_32_HIGH = "winMediumHigh"; private static final String WIN32_48_LOW = "winLargeLow"; private static final String WIN32_48_HIGH = "winLargeHigh"; 52 private static final String PRODUCT = "product"; private static final String CONFIG_INI = "configIni"; private static final String LAUNCHER = "launcher"; private static final String LAUNCHER_ARGS = "launcherArgs"; private static final String PLUGINS = "plugins"; private static final String FEATURES = "features"; private static final String SPLASH = "splash"; private static final String P_USE_ICO = "useIco"; 61 private static final int STATE_START = 0; 63 private static final int STATE_PRODUCT = 1; 64 private static final int STATE_LAUNCHER = 2; 65 private static final int STATE_LAUNCHER_ARGS = 3; 66 private static final int STATE_PLUGINS = 4; 67 private static final int STATE_FEATURES = 5; 68 private static final int STATE_PROGRAM_ARGS = 6; 69 private static final int STATE_PROGRAM_ARGS_LINUX = 7; 70 private static final int STATE_PROGRAM_ARGS_MAC = 8; 71 private static final int STATE_PROGRAM_ARGS_SOLARIS = 9; 72 private static final int STATE_PROGRAM_ARGS_WIN = 10; 73 private static final int STATE_VM_ARGS = 11; 74 private static final int STATE_VM_ARGS_LINUX = 12; 75 private static final int STATE_VM_ARGS_MAC = 13; 76 private static final int STATE_VM_ARGS_SOLARIS = 14; 77 private static final int STATE_VM_ARGS_WIN = 15; 78 79 private int state = STATE_START; 80 81 private SAXParser parser; 82 private String currentOS = null; 83 private boolean useIco = false; 84 private ArrayList result = new ArrayList(6); 85 private String launcherName = null; 86 private String icons[] = null; 87 private String configPath = null; 88 private String id = null; 89 private boolean useFeatures = false; 90 private List plugins = null; 91 private List fragments = null; 92 private List features = null; 93 private String splashLocation = null; 94 private String productName = null; 95 private String application = null; 96 97 private Properties launcherArgs = new Properties(); 98 99 private static String normalize(String text) { 100 if (text == null || text.trim().length() == 0) 101 return ""; 103 text = text.replaceAll("\\r|\\n|\\f|\\t", " "); return text.replaceAll("\\s+", " "); } 106 107 110 public ProductFile(String location, String os) throws CoreException { 111 super(); 112 this.currentOS = os; 113 try { 114 parserFactory.setNamespaceAware(true); 115 parser = parserFactory.newSAXParser(); 116 InputStream in = new BufferedInputStream(new FileInputStream(location)); 117 parser.parse(new InputSource(in), this); 118 } catch (ParserConfigurationException e) { 119 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.exception_productParse, location), e)); 120 } catch (SAXException e) { 121 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.exception_productParse, location), e)); 122 } catch (FileNotFoundException e) { 123 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FILE, NLS.bind(Messages.exception_missingElement, location), null)); 124 } catch (IOException e) { 125 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_PRODUCT_FORMAT, NLS.bind(Messages.exception_productParse, location), e)); 126 } 127 } 128 129 public String getLauncherName() { 130 return launcherName; 131 } 132 133 public List getPlugins() { 134 return getPlugins(true); 135 } 136 137 public List getPlugins(boolean includeFragments) { 138 List p = plugins != null ? plugins : Collections.EMPTY_LIST; 139 if(!includeFragments) 140 return p; 141 142 List f = fragments != null ? fragments : Collections.EMPTY_LIST; 143 int size = p.size() + f.size(); 144 if (size == 0) 145 return Collections.EMPTY_LIST; 146 147 List both = new ArrayList(size); 148 both.addAll(p); 149 both.addAll(f); 150 return both; 151 } 152 153 public List getFragments() { 154 if(fragments == null) 155 return Collections.EMPTY_LIST; 156 return fragments; 157 } 158 159 public List getFeatures() { 160 if(features == null) 161 return Collections.EMPTY_LIST; 162 return features; 163 } 164 165 public boolean containsPlugin(String plugin) { 166 return (plugins != null && plugins.contains(plugin)) || (fragments != null && fragments.contains(plugin)); 167 } 168 169 172 public String [] getIcons() { 173 if (icons != null) 174 return icons; 175 String [] temp = new String [result.size()]; 176 int i = 0; 177 for (Iterator iter = result.iterator(); iter.hasNext();) { 178 String element = (String ) iter.next(); 179 if (element != null) 180 temp[i++] = element; 181 } 182 icons = new String [i]; 183 System.arraycopy(temp, 0, icons, 0, i); 184 return icons; 185 } 186 187 public String getConfigIniPath() { 188 return configPath; 189 } 190 191 public String getId() { 192 return id; 193 } 194 195 public String getSplashLocation() { 196 return splashLocation; 197 } 198 199 public String getProductName() { 200 return productName; 201 } 202 203 public String getApplication() { 204 return application; 205 } 206 207 public boolean useFeatures() { 208 return useFeatures; 209 } 210 211 public String getVMArguments(String os) { 212 String key = null; 213 if( os.equals(Platform.OS_WIN32)){ 214 key = VM_ARGS_WIN; 215 } else if( os.equals(Platform.OS_LINUX)) { 216 key = VM_ARGS_LINUX; 217 } else if( os.equals(Platform.OS_MACOSX)) { 218 key = VM_ARGS_MAC; 219 } else if(os.equals(Platform.OS_SOLARIS)) { 220 key = VM_ARGS_SOLARIS; 221 } 222 223 String prefix = launcherArgs.getProperty(VM_ARGS); 224 String platform = null, args = null; 225 if (key != null) 226 platform = launcherArgs.getProperty(key); 227 if (prefix != null) 228 args = platform != null ? prefix + " " + platform : prefix; else 230 args = platform != null ? platform : ""; return normalize(args); 232 } 233 234 public String getProgramArguments(String os) { 235 String key = null; 236 if( os.equals(Platform.OS_WIN32)){ 237 key = PROGRAM_ARGS_WIN; 238 } else if( os.equals(Platform.OS_LINUX)) { 239 key = PROGRAM_ARGS_LINUX; 240 } else if( os.equals(Platform.OS_MACOSX)) { 241 key = PROGRAM_ARGS_MAC; 242 } else if(os.equals(Platform.OS_SOLARIS)) { 243 key = PROGRAM_ARGS_SOLARIS; 244 } 245 246 String prefix = launcherArgs.getProperty(PROGRAM_ARGS); 247 String platform = null, args = null; 248 if (key != null) 249 platform = launcherArgs.getProperty(key); 250 if (prefix != null) 251 args = platform != null ? prefix + " " + platform : prefix; else 253 args = platform != null ? platform : ""; return normalize(args); 255 } 256 257 public void startElement(String uri, String localName, String qName, Attributes attributes) { 258 switch (state) { 259 case STATE_START : 260 if (PRODUCT.equals(localName)) { 261 processProduct(attributes); 262 state = STATE_PRODUCT; 263 } 264 break; 265 266 case STATE_PRODUCT : 267 if (CONFIG_INI.equals(localName)) { 268 processConfigIni(attributes); 269 } else if (LAUNCHER.equals(localName)) { 270 processLauncher(attributes); 271 state = STATE_LAUNCHER; 272 } else if (PLUGINS.equals(localName)) { 273 state = STATE_PLUGINS; 274 } else if (FEATURES.equals(localName)) { 275 state = STATE_FEATURES; 276 } else if (LAUNCHER_ARGS.equals(localName)) { 277 state = STATE_LAUNCHER_ARGS; 278 } else if (SPLASH.equals(localName)) { 279 splashLocation = attributes.getValue("location"); } 281 break; 282 283 case STATE_LAUNCHER : 284 if (Platform.OS_SOLARIS.equals(localName)) { 285 processSolaris(attributes); 286 } else if ("win".equals(localName)) { processWin(attributes); 288 } else if (Platform.OS_LINUX.equals(localName)) { 289 processLinux(attributes); 290 } else if (Platform.OS_MACOSX.equals(localName)) { 291 processMac(attributes); 292 } 293 if ("ico".equals(localName)) { processIco(attributes); 295 } else if ("bmp".equals(localName)) { processBmp(attributes); 297 } 298 break; 299 300 case STATE_LAUNCHER_ARGS : 301 if (PROGRAM_ARGS.equals(localName)) { 302 state = STATE_PROGRAM_ARGS; 303 } else if (PROGRAM_ARGS_LINUX.equals(localName)) { 304 state = STATE_PROGRAM_ARGS_LINUX; 305 } else if (PROGRAM_ARGS_MAC.equals(localName)) { 306 state = STATE_PROGRAM_ARGS_MAC; 307 } else if (PROGRAM_ARGS_SOLARIS.equals(localName)) { 308 state = STATE_PROGRAM_ARGS_SOLARIS; 309 } else if (PROGRAM_ARGS_WIN.equals(localName)) { 310 state = STATE_PROGRAM_ARGS_WIN; 311 } else if (VM_ARGS.equals(localName)) { 312 state = STATE_VM_ARGS; 313 } else if (VM_ARGS_LINUX.equals(localName)) { 314 state = STATE_VM_ARGS_LINUX; 315 } else if (VM_ARGS_MAC.equals(localName)) { 316 state = STATE_VM_ARGS_MAC; 317 } else if (VM_ARGS_SOLARIS.equals(localName)) { 318 state = STATE_VM_ARGS_SOLARIS; 319 } else if (VM_ARGS_WIN.equals(localName)) { 320 state = STATE_VM_ARGS_WIN; 321 } 322 break; 323 324 case STATE_PLUGINS : 325 if ("plugin".equals(localName)) { processPlugin(attributes); 327 } 328 break; 329 330 case STATE_FEATURES : 331 if ("feature".equals(localName)) { processFeature(attributes); 333 } 334 break; 335 } 336 } 337 338 public void endElement(String uri, String localName, String qName) { 339 switch (state) { 340 case STATE_PLUGINS : 341 if (PLUGINS.equals(localName)) 342 state = STATE_PRODUCT; 343 break; 344 case STATE_FEATURES : 345 if (FEATURES.equals(localName)) 346 state = STATE_PRODUCT; 347 break; 348 case STATE_LAUNCHER_ARGS : 349 if (LAUNCHER_ARGS.equals(localName)) 350 state = STATE_PRODUCT; 351 break; 352 case STATE_LAUNCHER : 353 if (LAUNCHER.equals(localName)) 354 state = STATE_PRODUCT; 355 break; 356 357 case STATE_PROGRAM_ARGS : 358 case STATE_PROGRAM_ARGS_LINUX : 359 case STATE_PROGRAM_ARGS_MAC : 360 case STATE_PROGRAM_ARGS_SOLARIS : 361 case STATE_PROGRAM_ARGS_WIN : 362 case STATE_VM_ARGS : 363 case STATE_VM_ARGS_LINUX : 364 case STATE_VM_ARGS_MAC : 365 case STATE_VM_ARGS_SOLARIS : 366 case STATE_VM_ARGS_WIN : 367 state = STATE_LAUNCHER_ARGS; 368 break; 369 } 370 } 371 372 public void characters(char[] ch, int start, int length) { 373 switch (state) { 374 case STATE_PROGRAM_ARGS : 375 addLaunchArgumentToMap(PROGRAM_ARGS, String.valueOf(ch, start, length)); 376 break; 377 case STATE_PROGRAM_ARGS_LINUX : 378 addLaunchArgumentToMap(PROGRAM_ARGS_LINUX, String.valueOf(ch, start, length)); 379 break; 380 case STATE_PROGRAM_ARGS_MAC : 381 addLaunchArgumentToMap(PROGRAM_ARGS_MAC, String.valueOf(ch, start, length)); 382 break; 383 case STATE_PROGRAM_ARGS_SOLARIS : 384 addLaunchArgumentToMap(PROGRAM_ARGS_SOLARIS, String.valueOf(ch, start, length)); 385 break; 386 case STATE_PROGRAM_ARGS_WIN : 387 addLaunchArgumentToMap(PROGRAM_ARGS_WIN, String.valueOf(ch, start, length)); 388 break; 389 case STATE_VM_ARGS : 390 addLaunchArgumentToMap(VM_ARGS, String.valueOf(ch, start, length)); 391 break; 392 case STATE_VM_ARGS_LINUX : 393 addLaunchArgumentToMap(VM_ARGS_LINUX, String.valueOf(ch, start, length)); 394 break; 395 case STATE_VM_ARGS_MAC : 396 addLaunchArgumentToMap(VM_ARGS_MAC, String.valueOf(ch, start, length)); 397 break; 398 case STATE_VM_ARGS_SOLARIS : 399 addLaunchArgumentToMap(VM_ARGS_SOLARIS, String.valueOf(ch, start, length)); 400 break; 401 case STATE_VM_ARGS_WIN : 402 addLaunchArgumentToMap(VM_ARGS_WIN, String.valueOf(ch, start, length)); 403 break; 404 } 405 } 406 407 private void addLaunchArgumentToMap(String key, String value) { 408 if (launcherArgs == null) 409 launcherArgs = new Properties(); 410 411 String oldValue = launcherArgs.getProperty(key); 412 if (oldValue != null) 413 launcherArgs.setProperty(key, oldValue + value); 414 else 415 launcherArgs.setProperty(key, value); 416 } 417 private void processPlugin(Attributes attributes) { 418 String fragment = attributes.getValue("fragment"); if (fragment != null && new Boolean (fragment).booleanValue()) { 420 if (fragments == null) 421 fragments = new ArrayList(); 422 fragments.add(attributes.getValue("id")); } else { 424 if (plugins == null) 425 plugins = new ArrayList(); 426 plugins.add(attributes.getValue("id")); } 428 } 429 430 private void processFeature(Attributes attributes) { 431 if (features == null) 432 features = new ArrayList(); 433 features.add(attributes.getValue("id")); } 435 436 private void processProduct(Attributes attributes) { 437 id = attributes.getValue("id"); productName = attributes.getValue("name"); application = attributes.getValue("application"); String use = attributes.getValue("useFeatures"); if (use != null) 442 useFeatures = IBuildPropertiesConstants.TRUE.equalsIgnoreCase(use); 443 } 444 445 private void processConfigIni(Attributes attributes) { 446 if (attributes.getValue("use").equals("custom")) { configPath = attributes.getValue("path"); } 449 } 450 451 private void processLauncher(Attributes attributes) { 452 launcherName = attributes.getValue("name"); } 454 455 private boolean osMatch(String os) { 456 if (os == currentOS) 457 return true; 458 if (os == null) 459 return false; 460 return os.equals(currentOS); 461 } 462 463 private void processSolaris(Attributes attributes) { 464 if (!osMatch(Platform.OS_SOLARIS)) 465 return; 466 result.add(attributes.getValue(SOLARIS_LARGE)); 467 result.add(attributes.getValue(SOLARIS_MEDIUM)); 468 result.add(attributes.getValue(SOLARIS_SMALL)); 469 result.add(attributes.getValue(SOLARIS_TINY)); 470 } 471 472 private void processWin(Attributes attributes) { 473 if (!osMatch(Platform.OS_WIN32)) 474 return; 475 useIco = IBuildPropertiesConstants.TRUE.equalsIgnoreCase(attributes.getValue(P_USE_ICO)); 476 } 477 478 private void processIco(Attributes attributes) { 479 if (!osMatch(Platform.OS_WIN32) || !useIco) 480 return; 481 result.add(attributes.getValue("path")); } 483 484 private void processBmp(Attributes attributes) { 485 if (!osMatch(Platform.OS_WIN32) || useIco) 486 return; 487 result.add(attributes.getValue(WIN32_16_HIGH)); 488 result.add(attributes.getValue(WIN32_16_LOW)); 489 result.add(attributes.getValue(WIN32_24_LOW)); 490 result.add(attributes.getValue(WIN32_32_HIGH)); 491 result.add(attributes.getValue(WIN32_32_LOW)); 492 result.add(attributes.getValue(WIN32_48_HIGH)); 493 result.add(attributes.getValue(WIN32_48_LOW)); 494 } 495 496 private void processLinux(Attributes attributes) { 497 if (!osMatch(Platform.OS_LINUX)) 498 return; 499 result.add(attributes.getValue("icon")); } 501 502 private void processMac(Attributes attributes) { 503 if (!osMatch(Platform.OS_MACOSX)) 504 return; 505 result.add(attributes.getValue("icon")); } 507 } 508 | Popular Tags |