/**
   * Generates code for tag provider class from specified configuration XML file. In order to create
   * custom tag info provider, make config file and call this main method with the specified file.
   * Output will be generated on the standard output. This way default tag provider (class
   * DefaultTagProvider) is generated from default.xml which which is packaged in the source
   * distribution.
   *
   * @param args
   * @throws IOException
   * @throws SAXException
   * @throws ParserConfigurationException
   */
  public static void main(String[] args)
      throws IOException, SAXException, ParserConfigurationException {
    final ConfigFileTagProvider provider = new ConfigFileTagProvider();
    provider.generateCode = true;

    File configFile = new File("default.xml");
    String packagePath = "org.htmlcleaner";
    String className = "DefaultTagProvider";

    final ConfigParser parser = provider.new ConfigParser(provider);
    System.out.println("package " + packagePath + ";");
    System.out.println("import java.util.HashMap;");
    System.out.println(
        "public class " + className + " extends HashMap implements ITagInfoProvider {");
    System.out.println("public " + className + "() {");
    System.out.println("TagInfo tagInfo;");
    parser.parse(new InputSource(new FileReader(configFile)));
    System.out.println("}");
    System.out.println("}");
  }
예제 #2
0
  /**
   * 获取配置
   *
   * @return 配置
   */
  public Config getConfig() {
    ConfigParser configParser = new PropConfigParser();

    return configParser.parse();
  }
  private SchemaCrawlerCommandLine(
      final Config config2, final ConnectionOptions connectionOptions, final String... args)
      throws SchemaCrawlerException {
    if (args == null || args.length == 0) {
      throw new SchemaCrawlerException("No command line arguments provided");
    }

    String[] remainingArgs = args;

    final CommandParser commandParser = new CommandParser();
    remainingArgs = commandParser.parse(remainingArgs);
    if (!commandParser.hasOptions()) {
      throw new SchemaCrawlerException("No command specified");
    }
    command = commandParser.getOptions().toString();

    final OutputOptionsParser outputOptionsParser = new OutputOptionsParser();
    remainingArgs = outputOptionsParser.parse(remainingArgs);
    outputOptions = outputOptionsParser.getOptions();

    final boolean isBundledWithDriver = config2 != null;
    if (isBundledWithDriver) {
      this.config = config2;
    } else {
      this.config = new Config();
    }
    if (remainingArgs.length > 0) {
      final ConfigParser configParser = new ConfigParser();
      remainingArgs = configParser.parse(remainingArgs);
      config.putAll(configParser.getOptions());
    }

    if (connectionOptions != null) {
      this.connectionOptions = connectionOptions;
    } else if (isBundledWithDriver) {
      final BaseDatabaseConnectionOptionsParser bundledDriverConnectionOptionsParser =
          new BundledDriverConnectionOptionsParser(config);
      remainingArgs = bundledDriverConnectionOptionsParser.parse(remainingArgs);
      this.connectionOptions = bundledDriverConnectionOptionsParser.getOptions();
    } else {
      final CommandLineConnectionOptionsParser commandLineConnectionOptionsParser =
          new CommandLineConnectionOptionsParser(config);
      remainingArgs = commandLineConnectionOptionsParser.parse(remainingArgs);
      ConnectionOptions parsedConnectionOptions = commandLineConnectionOptionsParser.getOptions();
      if (parsedConnectionOptions == null) {
        final ConfigConnectionOptionsParser configConnectionOptionsParser =
            new ConfigConnectionOptionsParser(config);
        remainingArgs = configConnectionOptionsParser.parse(remainingArgs);
        parsedConnectionOptions = configConnectionOptionsParser.getOptions();
      }
      this.connectionOptions = parsedConnectionOptions;
    }

    final SchemaCrawlerOptionsParser schemaCrawlerOptionsParser =
        new SchemaCrawlerOptionsParser(config);
    remainingArgs = schemaCrawlerOptionsParser.parse(remainingArgs);
    schemaCrawlerOptions = schemaCrawlerOptionsParser.getOptions();

    if (remainingArgs.length > 0) {
      LOGGER.log(
          Level.INFO,
          "Too many command line arguments provided: " + ObjectToString.toString(remainingArgs));
    }
  }
  public void parseFile(File file) throws IOException {
    if (!file.exists()) throw new IOException("File not found!");

    Field[] fields = getClass().getDeclaredFields();

    FileInputStream fin = new FileInputStream(file);
    try {
      DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
      Document dom = db.parse(fin);
      Element elm = dom.getDocumentElement();

      NodeList list = elm.getChildNodes();
      if (list != null && list.getLength() > 0) {
        for (int i = 0; i < list.getLength(); i++) {
          if (!(list.item(i) instanceof Element)) continue;
          Element item = (Element) list.item(i);
          String item_name = item.getNodeName();

          for (Field f : fields) {
            if (f.getName().equals(item_name)) {
              if (isConfigItem(f)) {
                if (ConfigParser.class.isAssignableFrom(f.getType())) {
                  ConfigParser parser = (ConfigParser) f.getType().getConstructor().newInstance();
                  parser.parse(item.getChildNodes());
                  f.setAccessible(true);
                  f.set(this, parser);
                } else {
                  if (String.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, item.getFirstChild().getNodeValue());
                  } else if (Integer.class.isAssignableFrom(f.getType())
                      || int.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, Integer.parseInt(item.getFirstChild().getNodeValue()));
                  } else if (Boolean.class.isAssignableFrom(f.getType())
                      || boolean.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, item.getFirstChild().getNodeValue().toLowerCase().contains("true"));
                  } else if (Float.class.isAssignableFrom(f.getType())
                      || float.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, Float.parseFloat(item.getFirstChild().getNodeValue()));
                  } else if (Double.class.isAssignableFrom(f.getType())
                      || double.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, Double.parseDouble(item.getFirstChild().getNodeValue()));
                  } else if (Long.class.isAssignableFrom(f.getType())
                      || long.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, Long.parseLong(item.getFirstChild().getNodeValue()));
                  } else if (Float.class.isAssignableFrom(f.getType())
                      || float.class.isAssignableFrom(f.getType())) {
                    f.setAccessible(true);
                    f.set(this, Float.parseFloat(item.getFirstChild().getNodeValue()));
                  } else {
                    System.err.println("Cannot assign value for item \"" + item_name + "\"");
                  }
                }
              } else {
                System.err.println("Unknown mapConfig item \"" + item_name + "\"");
              }
              break;
            }
          }
        }
      }
    } catch (ParserConfigurationException e) {
      throw new IOException("Error reading config file!", e);
    } catch (SAXException e) {
      throw new IOException("Error parsing file!", e);
    } catch (IllegalAccessException e) {
      throw new IOException("Error setting value for config file!", e);
    } catch (NoSuchMethodException e) {
      throw new IOException("Field did not have a default constructor!", e);
    } catch (InstantiationException e) {
      throw new IOException("Error creating new field!", e);
    } catch (InvocationTargetException e) {
      throw new IOException("Error invoking method to parse item!", e);
    }
  }