Exemplo n.º 1
0
  /**
   * 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
  }
Exemplo n.º 2
0
  /**
   * Converts the text based GRIN script to a binary format.
   *
   * @param assets The path to the assets in a jarfile, which is used as the first parameter to
   *     <code>AssetFinder.setSearchPath(String[], File[])</code>. Could be null.
   * @param assetsDir The path to the assets in the filesystem, which is used as the second
   *     parameter to <code>AssetFinder.setSearchPath(String[], File[])</code>. Could be null.
   * @param showFiles The GRIN text script files to read in.
   * @param extensionParser The ExtensionParser for handling extensions.
   * @param outputDir The directory to output generated files.
   * @param debug If true, include debug information to generated binary file.
   * @param optimize If true, apply optimization to the show object, such as creating image mosaics.
   */
  public static void convert(
      String[] assets,
      File[] assetsDir,
      String[] showFiles,
      ExtensionParser extensionParser,
      String outputDir,
      boolean debug,
      boolean optimize)
      throws IOException {
    if (outputDir == null) {
      outputDir = "."; // current dir
    }
    AssetFinder.setSearchPath(assets, assetsDir);
    List<File> files = new ArrayList<File>();
    SEShow[] shows = new SEShow[showFiles.length];
    try {
      for (int i = 0; i < showFiles.length; i++) {
        ShowBuilder builder = new ShowBuilder();
        builder.setExtensionParser(extensionParser);
        SEShow show = ShowParser.parseShow(showFiles[i], null, builder);
        shows[i] = show;
      }
      if (scaleX != 1000 || scaleY != 1000 || offsetX != 0 || offsetY != 0) {
        for (int i = 0; i < shows.length; i++) {
          shows[i].scaleBy(scaleX, scaleY, offsetX, offsetY);
        }
      }
      if (optimize) {
        GrinCompiler compiler = new GrinCompiler();
        compiler.setHeadless(headless);
        compiler.optimizeShows(shows, outputDir);
      }
      for (int i = 0; i < showFiles.length; i++) {
        if (!shows[i].getNoShowFile()) {
          String baseName = showFiles[i];
          if (baseName.indexOf('.') != -1) {
            baseName = baseName.substring(0, baseName.lastIndexOf('.'));
          }
          String fileName = shows[i].getBinaryGrinFileName();
          ;
          if (fileName == null) {
            fileName = baseName + ".grin";
          }
          File f = new File(outputDir, fileName);
          files.add(f);
          DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
          GrinBinaryWriter out = new GrinBinaryWriter(shows[i], debug);
          out.writeShow(dos);
          dos.close();

          f = new File(outputDir, baseName + ".xlet.java");
          files.add(f);
          out.writeCommandClass(shows[i], true, f);

          f = new File(outputDir, baseName + ".grinview.java");
          files.add(f);
          out.writeCommandClass(shows[i], false, f);
        }
      }
    } catch (IOException e) {
      // failed on writing, delete the output files
      for (File file : files) {
        if (file != null && file.exists()) {
          file.delete();
        }
      }
      throw e;
    }
  }