@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(); } }
/** * 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; }
/** * プラグイン情報をロードします。 * * @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(); } } }