Beispiel #1
0
  /**
   * Parses an option <code>args[i]</code> and return the number of tokens consumed.
   *
   * @return 0 if the argument is not understood. Returning 0 will let the caller report an error.
   * @exception BadCommandLineException If the callee wants to provide a custom message for an
   *     error.
   */
  public int parseArgument(String[] args, int i) throws BadCommandLineException {
    if (args[i].equals("-classpath") || args[i].equals("-cp")) {
      String a = requireArgument(args[i], args, ++i);
      for (String p : a.split(File.pathSeparator)) {
        File file = new File(p);
        try {
          classpaths.add(file.toURL());
        } catch (MalformedURLException e) {
          throw new BadCommandLineException(
              Messages.format(Messages.NOT_A_VALID_FILENAME, file), e);
        }
      }
      return 2;
    }
    if (args[i].equals("-d")) {
      targetDir = new File(requireArgument("-d", args, ++i));
      if (!targetDir.exists())
        throw new BadCommandLineException(Messages.format(Messages.NON_EXISTENT_DIR, targetDir));
      return 2;
    }
    if (args[i].equals("-readOnly")) {
      readOnly = true;
      return 1;
    }
    if (args[i].equals("-p")) {
      defaultPackage = requireArgument("-p", args, ++i);
      if (defaultPackage.length() == 0) { // user specified default package
        // there won't be any package to annotate, so disable them
        // automatically as a usability feature
        packageLevelAnnotations = false;
      }
      return 2;
    }
    if (args[i].equals("-debug")) {
      debugMode = true;
      verbose = true;
      return 1;
    }
    if (args[i].equals("-nv")) {
      strictCheck = false;
      return 1;
    }
    if (args[i].equals("-npa")) {
      packageLevelAnnotations = false;
      return 1;
    }
    if (args[i].equals("-no-header")) {
      noFileHeader = true;
      return 1;
    }
    if (args[i].equals("-verbose")) {
      verbose = true;
      return 1;
    }
    if (args[i].equals("-quiet")) {
      quiet = true;
      return 1;
    }
    if (args[i].equals("-XexplicitAnnotation")) {
      runtime14 = true;
      return 1;
    }
    if (args[i].equals("-enableIntrospection")) {
      enableIntrospection = true;
      return 1;
    }
    if (args[i].equals("-disableXmlSecurity")) {
      disableXmlSecurity = true;
      return 1;
    }
    if (args[i].equals("-contentForWildcard")) {
      contentForWildcard = true;
      return 1;
    }
    if (args[i].equals("-XautoNameResolution")) {
      automaticNameConflictResolution = true;
      return 1;
    }
    if (args[i].equals("-b")) {
      addFile(requireArgument("-b", args, ++i), bindFiles, ".xjb");
      return 2;
    }
    if (args[i].equals("-dtd")) {
      schemaLanguage = Language.DTD;
      return 1;
    }
    if (args[i].equals("-relaxng")) {
      schemaLanguage = Language.RELAXNG;
      return 1;
    }
    if (args[i].equals("-relaxng-compact")) {
      schemaLanguage = Language.RELAXNG_COMPACT;
      return 1;
    }
    if (args[i].equals("-xmlschema")) {
      schemaLanguage = Language.XMLSCHEMA;
      return 1;
    }
    if (args[i].equals("-wsdl")) {
      schemaLanguage = Language.WSDL;
      return 1;
    }
    if (args[i].equals("-extension")) {
      compatibilityMode = EXTENSION;
      return 1;
    }
    if (args[i].equals("-target")) {
      String token = requireArgument("-target", args, ++i);
      target = SpecVersion.parse(token);
      if (target == null)
        throw new BadCommandLineException(Messages.format(Messages.ILLEGAL_TARGET_VERSION, token));
      return 2;
    }
    if (args[i].equals("-httpproxyfile")) {
      if (i == args.length - 1 || args[i + 1].startsWith("-")) {
        throw new BadCommandLineException(Messages.format(Messages.MISSING_PROXYFILE));
      }

      File file = new File(args[++i]);
      if (!file.exists()) {
        throw new BadCommandLineException(Messages.format(Messages.NO_SUCH_FILE, file));
      }

      try {
        BufferedReader in =
            new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        parseProxy(in.readLine());
        in.close();
      } catch (IOException e) {
        throw new BadCommandLineException(
            Messages.format(Messages.FAILED_TO_PARSE, file, e.getMessage()), e);
      }

      return 2;
    }
    if (args[i].equals("-httpproxy")) {
      if (i == args.length - 1 || args[i + 1].startsWith("-")) {
        throw new BadCommandLineException(Messages.format(Messages.MISSING_PROXY));
      }

      parseProxy(args[++i]);
      return 2;
    }
    if (args[i].equals("-host")) {
      proxyHost = requireArgument("-host", args, ++i);
      return 2;
    }
    if (args[i].equals("-port")) {
      proxyPort = requireArgument("-port", args, ++i);
      return 2;
    }
    if (args[i].equals("-catalog")) {
      // use Sun's "XML Entity and URI Resolvers" by Norman Walsh
      // to resolve external entities.
      // http://www.sun.com/xml/developers/resolver/

      File catalogFile = new File(requireArgument("-catalog", args, ++i));
      try {
        addCatalog(catalogFile);
      } catch (IOException e) {
        throw new BadCommandLineException(
            Messages.format(Messages.FAILED_TO_PARSE, catalogFile, e.getMessage()), e);
      }
      return 2;
    }
    if (args[i].equals("-Xtest-class-name-allocator")) {
      classNameAllocator =
          new ClassNameAllocator() {
            public String assignClassName(String packageName, String className) {
              System.out.printf("assignClassName(%s,%s)\n", packageName, className);
              return className + "_Type";
            }
          };
      return 1;
    }

    if (args[i].equals("-encoding")) {
      encoding = requireArgument("-encoding", args, ++i);
      try {
        if (!Charset.isSupported(encoding)) {
          throw new BadCommandLineException(
              Messages.format(Messages.UNSUPPORTED_ENCODING, encoding));
        }
      } catch (IllegalCharsetNameException icne) {
        throw new BadCommandLineException(Messages.format(Messages.UNSUPPORTED_ENCODING, encoding));
      }
      return 2;
    }

    // see if this is one of the extensions
    for (Plugin plugin : getAllPlugins()) {
      try {
        if (('-' + plugin.getOptionName()).equals(args[i])) {
          activePlugins.add(plugin);
          plugin.onActivated(this);
          pluginURIs.addAll(plugin.getCustomizationURIs());

          // give the plugin a chance to parse arguments to this option.
          // this is new in 2.1, and due to the backward compatibility reason,
          // if plugin didn't understand it, we still return 1 to indicate
          // that this option is consumed.
          int r = plugin.parseArgument(this, args, i);
          if (r != 0) return r;
          else return 1;
        }

        int r = plugin.parseArgument(this, args, i);
        if (r != 0) return r;
      } catch (IOException e) {
        throw new BadCommandLineException(e.getMessage(), e);
      }
    }

    return 0; // unrecognized
  }