private static int argToInt(String s) { try { return Integer.parseInt(s); } catch (NumberFormatException ex) { ex.printStackTrace(); System.out.println(); System.out.println(s + " is not an integer"); System.out.println(); usage(); return -1; // not reached } }
private static int argToMills(String s) { try { Double d = Double.parseDouble(s); return (int) Math.round(d * 1000); } catch (NumberFormatException ex) { ex.printStackTrace(); System.out.println(); System.out.println(s + " is not a double"); System.out.println(); usage(); return -1; // not reached } }
/** * A driver method for the Main.convert(String, String). * * @param args Arguments. Requires the name of the text-based GRIN script to read. * @see #convert(String[], File[], String[], ExtensionParser, String, boolean, boolean) */ public static void main(String[] args) { if (args == null || args.length == 0) { usage(); } int index = 0; LinkedList<String> assetPathLL = new LinkedList<String>(); LinkedList<String> assetDirsLL = new LinkedList<String>(); LinkedList<String> showFilesLL = new LinkedList<String>(); String extensionParserName = "com.hdcookbook.grin.io.text.NullExtensionParser"; String outputDir = null; boolean debug = false; boolean optimize = true; while (index < args.length) { if ("-assets".equals(args[index])) { index++; String path = args[index]; if (index == args.length) { usage(); } if (!path.endsWith("/")) { path = path + "/"; } assetPathLL.add(path); } else if ("-asset_dir".equals(args[index])) { index++; if (index == args.length) { usage(); } String path = args[index]; assetDirsLL.add(path); } else if ("-debug".equals(args[index])) { debug = true; } else if ("-show_mosaic".equals(args[index])) { headless = false; } else if ("-extension_parser".equals(args[index])) { index++; if (index == args.length) { usage(); } extensionParserName = args[index]; } else if ("-out".equals(args[index])) { index++; if (index == args.length) { usage(); } outputDir = args[index]; } else if ("-scaleX".equals(args[index])) { index++; if (index == args.length) { usage(); } scaleX = argToMills(args[index]); } else if ("-scaleY".equals(args[index])) { index++; if (index == args.length) { usage(); } scaleY = argToMills(args[index]); } else if ("-offsetX".equals(args[index])) { index++; if (index == args.length) { usage(); } offsetX = argToInt(args[index]); } else if ("-offsetY".equals(args[index])) { index++; if (index == args.length) { usage(); } offsetY = argToInt(args[index]); } else if ("-avoid_optimization".equals(args[index])) { optimize = false; } else if ("-optimize".equals(args[index])) { optimize = true; } else if ("".equals(args[index])) { // Skip it. In ant, it's much easier to make a property blank // than it is to pass one argument fewer to a program. } else { showFilesLL.add(args[index]); } index++; } if (showFilesLL.isEmpty()) { usage(); } if (headless) { System.setProperty("java.awt.headless", "true"); } ExtensionParser extensionParser = null; if (extensionParserName != null && !"".equals(extensionParserName)) { try { extensionParser = (ExtensionParser) Class.forName(extensionParserName).newInstance(); } catch (IllegalAccessException ex) { ex.printStackTrace(); } catch (InstantiationException ex) { ex.printStackTrace(); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } if (extensionParser == null) { System.err.println(); System.err.println("Error: extension parser \"" + extensionParserName + "\" not found."); System.err.println(); System.exit(1); } } String[] assetPath = null; File[] assetDirs = null; if (assetDirsLL.size() > 0) { assetDirs = new File[assetDirsLL.size()]; int i = 0; for (Iterator it = assetDirsLL.iterator(); it.hasNext(); ) { File f = new File((String) it.next()); assetDirs[i++] = f; } } else { assetDirs = new File[0]; } if (assetPathLL.isEmpty() && assetDirs.length == 0) { assetPath = new String[] {"."}; // current dir } else { assetPath = assetPathLL.toArray(new String[assetPathLL.size()]); } String[] showFiles = showFilesLL.toArray(new String[showFilesLL.size()]); AssetFinder.setHelper( new AssetFinder() { protected void abortHelper() { System.exit(1); } protected Font getFontHelper(String fontName, int style, int size) { // On JavaSE, one cannot easily load a custom font. // The font created here will have a different glyph from what's // expected for the xlet runtime, but it will hold the correct // fontName, style, and size. return new Font(fontName, style, size); } }); AssetFinder.setSearchPath(assetPath, assetDirs); try { convert(assetPath, assetDirs, showFiles, extensionParser, outputDir, debug, optimize); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } // System.exit(0); /// HDCOOKBOOK-220: This causes the negative value exit problem }