@Override
 public UrlRewriteRulesDescriptor load(Reader reader) throws IOException {
   Digester digester = loader.newDigester(new ExtendedBaseRules());
   digester.setValidating(false);
   try {
     UrlRewriteRulesDescriptor rules = digester.parse(reader);
     return rules;
   } catch (SAXException e) {
     throw new IOException(e);
   }
 }
  private void parseDataSource(String file) throws Exception {
    Digester digester = new Digester();
    digester.setValidating(false);
    digester.push(this);
    digester.addCallMethod("datasources/datasource", "addDataSource", 6);

    digester.addCallParam("datasources/datasource/name", 0);
    digester.addCallParam("datasources/datasource/driver", 1);
    digester.addCallParam("datasources/datasource/url", 2);
    digester.addCallParam("datasources/datasource/username", 3);
    digester.addCallParam("datasources/datasource/password", 4);
    digester.addCallParam("datasources/datasource/internallogon", 5);
    digester.parse(new File(file));
  }
  /**
   * Pre-parses a file for some information not available from the FindBugs parser. Creates a
   * mapping of FindBugs warnings to messages. A bug is represented by its unique hash code. Also
   * obtains original categories for bug types.
   *
   * @param file the FindBugs XML file
   * @return the map of warning messages
   * @throws SAXException if the file contains no valid XML
   * @throws IOException signals that an I/O exception has occurred.
   */
  List<XmlBugInstance> preParse(final InputStream file) throws SAXException, IOException {
    Digester digester = new Digester();
    digester.setValidating(false);
    digester.setClassLoader(FindBugsParser.class.getClassLoader());

    String rootXPath = "BugCollection/BugInstance";
    digester.addObjectCreate(rootXPath, XmlBugInstance.class);
    digester.addSetProperties(rootXPath);

    String fileXPath = rootXPath + "/LongMessage";
    digester.addCallMethod(fileXPath, "setMessage", 0);

    digester.addSetNext(rootXPath, "add", Object.class.getName());
    ArrayList<XmlBugInstance> bugs = new ArrayList<XmlBugInstance>();
    digester.push(bugs);
    digester.parse(file);

    return bugs;
  }
  @Test
  public void digestXmlRules() {
    FileReader characterStream = null;
    try {
      characterStream = new FileReader(file);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    InputSource rulesSource = new InputSource(characterStream);
    Digester digester = new Digester();
    digester.setValidating(false);
    // digester.

    // URL url= this.getClass().getClassLoader().getResource("xml.academy.xml");
    try {
      Academy aca = (Academy) digester.parse(xmlFile);
      System.out.println(aca);
    } catch (IOException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    }
  }
Exemple #5
0
  /**
   * 解析base.mapred.xml配置信息到java对象
   *
   * @return 配置信息pojo对象
   */
  public static MapreduceConfigInfo parseConfig(String extraPartitions) {
    Digester digester = new Digester();
    digester.setValidating(false);

    digester.addObjectCreate("mapred", MapreduceConfigInfo.class);
    digester.addBeanPropertySetter("mapred/baseId");
    digester.addBeanPropertySetter("mapred/projectId");
    digester.addBeanPropertySetter("mapred/resourceName");
    digester.addBeanPropertySetter("mapred/idePath");

    digester.addBeanPropertySetter("mapred/mapOutputKey");
    digester.addBeanPropertySetter("mapred/mapOutputValue");
    digester.addBeanPropertySetter("mapred/partitionColumns");
    digester.addBeanPropertySetter("mapred/outputKeySortColumns");
    digester.addBeanPropertySetter("mapred/outputKeySortOrders");
    digester.addBeanPropertySetter("mapred/outputGroupingColumns");
    digester.addBeanPropertySetter("mapred/numReduceTask");
    digester.addBeanPropertySetter("mapred/memoryForMapTask");
    digester.addBeanPropertySetter("mapred/memoryForReduceTask");

    digester.addBeanPropertySetter("mapred/jobLauncher");
    digester.addBeanPropertySetter("mapred/mapper");
    digester.addBeanPropertySetter("mapred/reducer");
    digester.addBeanPropertySetter("mapred/combiner");

    digester.addObjectCreate("mapred/inputTables/table", OdpsTableInfo.class);
    digester.addBeanPropertySetter("mapred/inputTables/table/name");
    digester.addCallMethod("mapred/inputTables/table/partitions/partition", "addPartition", 1);
    digester.addCallParam("mapred/inputTables/table/partitions/partition", 0);
    digester.addSetNext("mapred/inputTables/table", "addInputTable");

    digester.addObjectCreate("mapred/outputTable", OdpsTableInfo.class);
    digester.addBeanPropertySetter("mapred/outputTable/name");
    digester.addCallMethod("mapred/outputTable/partition", "addPartition", 1);
    digester.addCallParam("mapred/outputTable/partition", 0);
    digester.addSetNext("mapred/outputTable", "setOutputTable");

    InputStream is = ClassLoader.getSystemResourceAsStream("META-INF/base.mapred.xml");
    try {
      MapreduceConfigInfo conf = digester.parse(is);

      // 将额外分区合并入输入表和输出表
      if (!extraPartitions.isEmpty()) {
        String[] eps = extraPartitions.split(":");
        for (String ep : eps) {
          int pos = ep.indexOf("/");
          String tableName = ep.substring(0, pos);
          String partition = ep.substring(pos + 1);

          for (OdpsTableInfo t : conf.getInputTables()) {
            if (t.getName().equals(tableName)) {
              t.addPartition(partition);
            }
          }

          if (conf.getOutputTable().getName().equals(tableName)) {
            conf.getOutputTable().addPartition(partition);
          }
        }
      }

      return conf;
    } catch (Exception e) {
      return null;
    }
  }