@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;
  }
示例#4
0
  @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();
    }
  }
示例#5
0
 @Test
 // <certification>在程序中对应一个字符串,故而没有列出。
 public void digest() {
   Digester digester = new Digester();
   // 注意,此处并没有象上例一样使用push,是因为此处从根元素创建了一个对//象实例
   digester.addObjectCreate("academy", Academy.class);
   // 将< academy >的属性与对象的属性关联
   digester.addSetProperties("academy");
   digester.addObjectCreate("academy/student", Student.class);
   digester.addSetProperties("academy/student");
   digester.addObjectCreate("academy/student/course", Course.class);
   digester.addBeanPropertySetter("academy/student/course/id");
   digester.addBeanPropertySetter("academy/student/course/name");
   digester.addSetNext("academy/student/course", "addCourse");
   digester.addSetNext("academy/student", "addStudent");
   digester.addObjectCreate("academy/teacher", Teacher.class);
   digester.addSetProperties("academy/teacher");
   // 当遇到academy/teacher/certification时,调用addCertification
   digester.addCallMethod("academy/teacher/certification", "addCertification", 1);
   // 设置addCertification的参数值,此处的0表示这个元素体的第一个值
   // 为参数值传入addCertification。在此处,即为<certification>的值。
   // (因为就只有一个)
   digester.addCallParam("academy/teacher/certification", 0);
   digester.addSetNext("academy/teacher", "addTeacher");
   try {
     Academy a = (Academy) digester.parse(xmlFile);
     System.out.print(a);
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   } catch (SAXException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
 /*
  * @see org.apache.commons.digester3.RuleSet#addRuleInstances(org.apache.commons.digester3.Digester)
  */
 @Override
 public void addRuleInstances(Digester digester) {
   digester.addRule(V_SYSTEM, createObjectCreateRule(SystemConfig.class));
   digester.addRule(V_SYSTEM, createSetNestedPropertiesRule());
 }
  /**
   * プラグイン情報をロードします。
   *
   * @param aStream プラグイン情報
   * @param aContext コンテキスト
   * @throws PluginServiceException プラグイン機能に起因する問題が発生した場合
   * @throws ConfigurationFormatException 設定ファイルに問題がある場合
   * @throws IOException 入出力操作に起因する問題が発生した場合
   */
  @SuppressWarnings("unchecked")
  private void doLoad(final InputStream aStream, final Context aContext)
      throws PluginServiceException, ConfigurationFormatException, IOException {
    synchronized (plugins) {
      // Load plugin xml file.
      List<PluginXmlEntity> pluginList;
      try {
        Digester digester = new Digester();
        digester.addObjectCreate("azuki/plugin-list", ArrayList.class);
        digester.addObjectCreate("azuki/plugin-list/plugin", PluginXmlEntity.class);
        digester.addSetProperties("azuki/plugin-list/plugin");
        digester.addSetNext("azuki/plugin-list/plugin", "add");
        pluginList = digester.parse(aStream);
      } catch (SAXException ex) {
        error(ex);
        throw new ConfigurationFormatException(ex);
      } catch (IOException ex) {
        error(ex);
        throw ex;
      }

      try {
        for (int i = 0; i < pluginList.size(); i++) {
          PluginXmlEntity p = pluginList.get(i);
          Class<Plugin> clazz = (Class<Plugin>) Class.forName(p.getPlugin());
          Plugin plugin = clazz.newInstance();

          PluginEntity pe = new PluginEntity();
          pe.name = p.getName();
          pe.config = p.getConfig();
          pe.plugin = plugin;
          plugins.add(pe);
        }
      } catch (ClassNotFoundException ex) {
        error(ex);
        throw new PluginServiceException(ex);
      } catch (IllegalAccessException ex) {
        error(ex);
        throw new PluginServiceException(ex);
      } catch (InstantiationException ex) {
        error(ex);
        throw new PluginServiceException(ex);
      }

      // Support
      for (int i = 0; i < plugins.size(); i++) {
        Plugin plugin = plugins.get(i).getPlugin();
        // Support context
        if (plugin instanceof ContextSupport) {
          ((ContextSupport) plugin).setContext(aContext);
        }
        // Support configuration
        if (plugin instanceof ConfigurationSupport) {
          String config = plugins.get(i).config;
          if (StringUtility.isNotEmpty(config)) {
            Configuration configuration =
                new InputStreamConfiguration(aContext.getResourceAsStream(config));
            ((ConfigurationSupport) plugin).setConfiguration(configuration);
          } else {
            warn("Not setting config file.[" + plugins.get(i).getName() + "]");
          }
        }
      }

      // initialize
      for (int i = 0; i < plugins.size(); i++) {
        Plugin plugin = plugins.get(i).getPlugin();
        plugin.initialize();
      }

      // load
      for (int i = 0; i < plugins.size(); i++) {
        Plugin plugin = plugins.get(i).getPlugin();
        plugin.load();
      }
    }
  }
示例#8
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;
    }
  }