List of usage examples for org.apache.commons.digester3 Digester addSetNext
public void addSetNext(String pattern, String methodName)
From source file:com.smartapps.avro.PhoneticXmlLoader.java
public Data getData() throws IOException, SAXException { Digester digester = new Digester(); digester.setValidating(false);/*from ww w .j a v a 2 s . co m*/ digester.addObjectCreate("data", Data.class); digester.addBeanPropertySetter("data/classes/vowel", "vowel"); digester.addBeanPropertySetter("data/classes/consonant", "consonant"); digester.addBeanPropertySetter("data/classes/punctuation", "punctuation"); digester.addBeanPropertySetter("data/classes/casesensitive", "casesensitive"); digester.addObjectCreate("data/patterns/pattern", Pattern.class); digester.addBeanPropertySetter("data/patterns/pattern/find", "find"); digester.addBeanPropertySetter("data/patterns/pattern/replace", "replace"); digester.addObjectCreate("data/patterns/pattern/rules/rule", Rule.class); digester.addBeanPropertySetter("data/patterns/pattern/rules/rule/replace", "replace"); digester.addObjectCreate("data/patterns/pattern/rules/rule/find/match", Match.class); digester.addBeanPropertySetter("data/patterns/pattern/rules/rule/find/match", "value"); digester.addSetProperties("data/patterns/pattern/rules/rule/find/match", "type", "type"); digester.addSetProperties("data/patterns/pattern/rules/rule/find/match", "scope", "scope"); digester.addSetNext("data/patterns/pattern/rules/rule/find/match", "addMatch"); digester.addSetNext("data/patterns/pattern/rules/rule", "addRule"); digester.addSetNext("data/patterns/pattern", "addPattern"); // Data data = (Data) digester.parse(this.url); // InputStreamReader isr = new InputStreamReader(is, "UTF-8"); Data data = (Data) digester.parse(is); return data; }
From source file:eu.scape_project.planning.xml.PlanParser.java
/** * Imports the XML representation of templates. * /*from w ww. ja v a2 s . c o m*/ * @param in * the input stream to read from * @return a list of read templates. * @throws PlatoException * if the template cannot be parsed */ public List<TemplateTree> importTemplates(final InputStream in) throws PlatoException { try { Digester digester = new Digester(); // digester.setValidating(true); StrictErrorHandler errorHandler = new StrictErrorHandler(); digester.setErrorHandler(errorHandler); // At the moment XML files for template tree's are only used // internally, // later we will define a schema and use it also for validation digester.push(this); digester.addObjectCreate("*/template", TemplateTree.class); digester.addSetProperties("*/template"); digester.addSetRoot("*/template", "setTemplate"); // digester.addSetNext("*/template/name", "setName"); // digester.addSetNext("*/template/owner", "setOwner"); PlanParser.addTreeParsingRulesToDigester(digester); digester.addObjectCreate("*/template/node", Node.class); digester.addSetProperties("*/template/node"); digester.addSetNext("*/template/node", "addChild"); digester.setUseContextClassLoader(true); templates = new ArrayList<TemplateTree>(); digester.parse(in); // FIXME: /* * for (TemplateTree t : templates) { log.info(t.getName() + * t.getOwner()); } */ return templates; } catch (Exception e) { throw new PlatoException("Failed to parse template tree.", e); } }
From source file:org.apache.commons.digester3.examples.api.addressbook.Main.java
private static void addRules(Digester d) { // -------------------------------------------------- // when we encounter a "person" tag, do the following: // create a new instance of class Person, and push that // object onto the digester stack of objects d.addObjectCreate("address-book/person", Person.class); // map *any* attributes on the tag to appropriate // setter-methods on the top object on the stack (the Person // instance created by the preceeding rule). //// ww w . j ava 2 s .co m // For example: // if attribute "id" exists on the xml tag, and method setId // with one parameter exists on the object that is on top of // the digester object stack, then a call will be made to that // method. The value will be type-converted from string to // whatever type the target method declares (where possible), // using the commons ConvertUtils functionality. // // Attributes on the xml tag for which no setter methods exist // on the top object on the stack are just ignored. d.addSetProperties("address-book/person"); // call the addPerson method on the second-to-top object on // the stack (the AddressBook object), passing the top object // on the stack (the recently created Person object). d.addSetNext("address-book/person", "addPerson"); // -------------------------------------------------- // when we encounter a "name" tag, call setName on the top // object on the stack, passing the text contained within the // body of that name element [specifying a zero parameter count // implies one actual parameter, being the body text]. // The top object on the stack will be a person object, because // the pattern address-book/person always triggers the // ObjectCreateRule we added previously. d.addCallMethod("address-book/person/name", "setName", 0); // -------------------------------------------------- // when we encounter an "email" tag, call addEmail on the top // object on the stack, passing two parameters: the "type" // attribute, and the text within the tag body. d.addCallMethod("address-book/person/email", "addEmail", 2); d.addCallParam("address-book/person/email", 0, "type"); d.addCallParam("address-book/person/email", 1); // -------------------------------------------------- // When we encounter an "address" tag, create an instance of class // Address and push it on the digester stack of objects. After // doing that, call addAddress on the second-to-top object on the // digester stack (a "Person" object), passing the top object on // the digester stack (the "Address" object). And also set things // up so that for each child xml element encountered between the start // of the address tag and the end of the address tag, the text // contained in that element is passed to a setXXX method on the // Address object where XXX is the name of the xml element found. d.addObjectCreate("address-book/person/address", Address.class); d.addSetNext("address-book/person/address", "addAddress"); d.addSetNestedProperties("address-book/person/address"); }
From source file:org.apache.commons.digester3.examples.api.catalog.Main.java
private static void addRules(Digester d) { // -------------------------------------------------- // when we encounter the root "catalog" tag, create an // instance of the Catalog class. ///*from w ww. j a v a 2s.c o m*/ // Note that this approach is different from the approach taken in // the AddressBook example, where an initial "root" object was // explicitly created and pushed onto the digester stack before // parsing started instead // // Either approach is fine. d.addObjectCreate("catalog", Catalog.class); // -------------------------------------------------- // when we encounter a book tag, we want to create a Book // instance. However the Book class doesn't have a default // constructor (one with no arguments), so we can't use // the ObjectCreateRule. Instead, we use the FactoryCreateRule. BookFactory factory = new BookFactory(); d.addFactoryCreate("catalog/book", factory); // and add the book to the parent catalog object (which is // the next-to-top object on the digester object stack). d.addSetNext("catalog/book", "addItem"); // we want each subtag of book to map the text contents of // the tag into a bean property with the same name as the tag. // eg <title>foo</title> --> setTitle("foo") d.addSetNestedProperties("catalog/book"); // ----------------------------------------------- // We are using the "AudioVisual" class to represent both // dvds and videos, so when the "dvd" tag is encountered, // create an AudioVisual object. d.addObjectCreate("catalog/dvd", AudioVisual.class); // add this dvd to the parent catalog object d.addSetNext("catalog/dvd", "addItem"); // We want to map every xml attribute onto a corresponding // property-setter method on the Dvd class instance. However // this doesn't work with the xml attribute "year-made", because // of the internal hyphen. We could use explicit CallMethodRule // rules instead, or use a version of the SetPropertiesRule that // allows us to override any troublesome mappings... // // If there was more than one troublesome mapping, we could // use the method variant that takes arrays of xml-attribute-names // and bean-property-names to override multiple mappings. // // For any attributes not explicitly mapped here, the default // processing is applied, so xml attribute "category" --> setCategory. d.addSetProperties("catalog/dvd", "year-made", "yearMade"); // We also need to tell this AudioVisual object that it is actually // a dvd; we can use the ObjectParamRule to pass a string to any // method. This usage is a little artificial - normally in this // situation there would be separate Dvd and Video classes. // Note also that equivalent behaviour could be implemented by // using factory objects to create & initialise the AudioVisual // objects with their type rather than using ObjectCreateRule. d.addCallMethod("catalog/dvd", "setType", 1); d.addObjectParam("catalog/dvd", 0, "dvd"); // pass literal "dvd" string // Each tag of form "<attr id="foo" value="bar"/> needs to map // to a call to setFoo("bar"). // // This is an alternative to the syntax used for books above (see // method addSetNestedProperties), where the name of the subtag // indicated which property to set. Using this syntax in the xml has // advantages and disadvantages both for the user and the application // developer. It is commonly used with the FactoryCreateRule variant // which allows the target class to be created to be specified in an // xml attribute; this feature of FactoryCreateRule is not demonstrated // in this example, but see the Apache Tomcat configuration files for // an example of this usage. // // Note that despite the name similarity, there is no link // between SetPropertyRule and SetPropertiesRule. d.addSetProperty("catalog/dvd/attr", "id", "value"); // ----------------------------------------------- // and here we repeat the dvd rules, but for the video tag. d.addObjectCreate("catalog/video", AudioVisual.class); d.addSetNext("catalog/video", "addItem"); d.addSetProperties("catalog/video", "year-made", "yearMade"); d.addCallMethod("catalog/video", "setType", 1); d.addObjectParam("catalog/video", 0, "video"); d.addSetProperty("catalog/video/attr", "id", "value"); }
From source file:org.apache.commons.digester3.examples.plugins.pipeline.CompoundTransform.java
public static void addRules(Digester d, String patternPrefix) { PluginCreateRule pcr = new PluginCreateRule(Transform.class); d.addRule(patternPrefix + "/subtransform", pcr); d.addSetNext(patternPrefix + "/subtransform", "addTransform"); }
From source file:org.apache.commons.digester3.examples.plugins.pipeline.Pipeline.java
public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: pipeline config-file"); System.exit(-1);//from ww w . j a v a2s. c o m } String configFile = args[0]; Digester digester = new Digester(); PluginRules rc = new PluginRules(); digester.setRules(rc); digester.addObjectCreate("pipeline", Pipeline.class); digester.addCallMethod("pipeline/source", "setSource", 1); digester.addCallParam("pipeline/source", 0, "file"); PluginCreateRule pcr = new PluginCreateRule(Transform.class); digester.addRule("pipeline/transform", pcr); digester.addSetNext("pipeline/transform", "setTransform"); digester.addCallMethod("pipeline/destination", "setDest", 1); digester.addCallParam("pipeline/destination", 0, "file"); Pipeline pipeline = null; try { pipeline = digester.parse(configFile); } catch (Exception e) { System.err.println("oops exception occurred during parse."); e.printStackTrace(); System.exit(-1); } try { pipeline.execute(); } catch (Exception e) { System.err.println("oops exception occurred during pipeline execution."); e.printStackTrace(); System.exit(-1); } }
From source file:org.azkfw.business.logic.LogicManager.java
/** * ?/*from w w w . ja v a 2s.co m*/ * * @param aNamespace ??? * @param aStream * @param aContext * @throws BusinessServiceException ???????? * @throws IOException ???????? */ @SuppressWarnings("unchecked") private void doLoad(final String aNamespace, final InputStream aStream, final Context aContext) throws BusinessServiceException, IOException { List<LogicEntity> logicList = null; try { Digester digester = new Digester(); digester.addObjectCreate("azuki/logics", ArrayList.class); digester.addObjectCreate("azuki/logics/logic", LogicEntity.class); digester.addSetProperties("azuki/logics/logic"); digester.addSetNext("azuki/logics/logic", "add"); logicList = digester.parse(aStream); } catch (SAXException ex) { error(ex); throw new IOException(ex); } catch (IOException ex) { error(ex); throw new IOException(ex); } Map<String, LogicData> m = null; if (logics.containsKey(aNamespace)) { m = logics.get(aNamespace); } else { m = new HashMap<String, LogicData>(); } for (int i = 0; i < logicList.size(); i++) { LogicEntity logic = logicList.get(i); info("Logic loading.[" + logic.name + "]"); if (m.containsKey(logic.getName())) { throw new BusinessServiceException("Duplicate logic name.[" + logic.getName() + "]"); } else { try { LogicData data = new LogicData(); Class<Logic> clazz = (Class<Logic>) Class.forName(logic.getLogic()); // XXX ???????? Map<String, Object> properties = new HashMap<String, Object>(); PropertyFile propertyFile = clazz.getAnnotation(PropertyFile.class); if (null != propertyFile) { String property = propertyFile.value(); if (StringUtility.isNotEmpty(property)) { InputStream is = aContext.getResourceAsStream(property); if (null != is) { Properties p = new Properties(); p.load(is); for (String key : p.stringPropertyNames()) { properties.put(key, p.getProperty(key)); } } else { throw new BusinessServiceException( "Not found logic property file.[" + property + "]"); } } } data.setLogic(clazz); data.setProperties(properties); data.setEntity(logic); m.put(logic.getName(), data); } catch (ClassNotFoundException ex) { error(ex); throw new BusinessServiceException(ex); } } } logics.put(aNamespace, m); }
From source file:org.azkfw.datasource.xml.XmlDatasourceBuilder.java
/** * ?// w w w. j av a 2s . com * * @return * @throws FileNotFoundException * @throws ParseException * @throws IOException */ @SuppressWarnings({ "unchecked", "rawtypes" }) public Datasource build() throws FileNotFoundException, ParseException, IOException { XmlDatasource datasource = new XmlDatasource(); datasource.name = datasourceName; InputStream stream = null; try { List<XmlTable> tables = new ArrayList<XmlTable>(); for (File file : xmlFiles) { List<XmlTableEntity> tableList = null; stream = new FileInputStream(file); Digester digester = new Digester(); digester.addObjectCreate("datasource/tables", ArrayList.class); digester.addObjectCreate("datasource/tables/table", XmlTableEntity.class); digester.addSetProperties("datasource/tables/table"); digester.addSetNext("datasource/tables/table", "add"); digester.addObjectCreate("datasource/tables/table/fields", ArrayList.class); digester.addSetNext("datasource/tables/table/fields", "setFields"); digester.addObjectCreate("datasource/tables/table/fields/field", XmlFieldEntity.class); digester.addSetProperties("datasource/tables/table/fields/field"); digester.addSetNext("datasource/tables/table/fields/field", "add"); digester.addObjectCreate("datasource/tables/table/records", ArrayList.class); digester.addSetNext("datasource/tables/table/records", "setRecords"); digester.addObjectCreate("datasource/tables/table/records/record", XmlRecordEntity.class); digester.addSetNext("datasource/tables/table/records/record", "add"); digester.addObjectCreate("datasource/tables/table/records/record/data", XmlRecordDataEntity.class); digester.addSetProperties("datasource/tables/table/records/record/data"); digester.addSetNext("datasource/tables/table/records/record/data", "add"); tableList = digester.parse(stream); for (XmlTableEntity t : tableList) { XmlTable table = new XmlTable(); table.label = t.label; table.name = t.name; // Read Field List<XmlField> fields = new ArrayList<XmlField>(); for (int col = 0; col < t.fields.size(); col++) { XmlFieldEntity f = t.fields.get(col); XmlField field = readField(col, f); fields.add(field); } // Read Data List<XmlRecord> records = new ArrayList<XmlRecord>(); for (int row = 0; row < t.records.size(); row++) { XmlRecordEntity r = t.records.get(row); if (r.data.size() == fields.size()) { XmlRecord record = readData(row, r, fields); records.add(record); } else { System.out.println("Skip row(unmatch field count).[table: " + table.getName() + "; row: " + r + ";]"); } } table.fields = (List) fields; table.records = (List) records; tables.add(table); } } datasource.tables = (List) tables; } catch (SAXException ex) { throw new ParseException(ex.getMessage(), -1); } catch (IOException ex) { throw ex; } finally { if (null != stream) { try { stream.close(); } catch (Exception ex) { ex.printStackTrace(); } } } return datasource; }
From source file:org.azkfw.datasource.xml.XmlDatasourceFactory.java
/** * XML???/*from www .j a v a2 s . co m*/ * * @param aName ?? * @param aFile XML * @return */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static Datasource generate(final String aName, final File aFile) throws FileNotFoundException, ParseException, IOException { XmlDatasource datasource = new XmlDatasource(); datasource.name = aName; List<XmlTableEntity> tableList = null; InputStream stream = null; try { stream = new FileInputStream(aFile); Digester digester = new Digester(); digester.addObjectCreate("datasource/tables", ArrayList.class); digester.addObjectCreate("datasource/tables/table", XmlTableEntity.class); digester.addSetProperties("datasource/tables/table"); digester.addSetNext("datasource/tables/table", "add"); digester.addObjectCreate("datasource/tables/table/fields", ArrayList.class); digester.addSetNext("datasource/tables/table/fields", "setFields"); digester.addObjectCreate("datasource/tables/table/fields/field", XmlFieldEntity.class); digester.addSetProperties("datasource/tables/table/fields/field"); digester.addSetNext("datasource/tables/table/fields/field", "add"); digester.addObjectCreate("datasource/tables/table/records", ArrayList.class); digester.addSetNext("datasource/tables/table/records", "setRecords"); digester.addObjectCreate("datasource/tables/table/records/record", XmlRecordEntity.class); digester.addSetNext("datasource/tables/table/records/record", "add"); digester.addObjectCreate("datasource/tables/table/records/record/data", XmlRecordDataEntity.class); digester.addSetProperties("datasource/tables/table/records/record/data"); digester.addSetNext("datasource/tables/table/records/record/data", "add"); tableList = digester.parse(stream); } catch (SAXException ex) { throw new ParseException(ex.getMessage(), -1); } catch (IOException ex) { throw ex; } finally { if (null != stream) { try { stream.close(); } catch (Exception ex) { ex.printStackTrace(); } } } List<XmlTable> tables = new ArrayList<XmlTable>(); for (XmlTableEntity t : tableList) { XmlTable table = new XmlTable(); table.label = t.label; table.name = t.name; // Read Field List<XmlField> fields = new ArrayList<XmlField>(); for (int col = 0; col < t.fields.size(); col++) { XmlFieldEntity f = t.fields.get(col); XmlField field = readField(col, f); fields.add(field); } // Read Data List<XmlRecord> records = new ArrayList<XmlRecord>(); for (int row = 0; row < t.records.size(); row++) { XmlRecordEntity r = t.records.get(row); if (r.data.size() == fields.size()) { XmlRecord record = readData(row, r, fields); records.add(record); } else { System.out.println( "Skip row(unmatch field count).[table: " + table.getName() + "; row: " + r + ";]"); } } table.fields = (List) fields; table.records = (List) records; tables.add(table); } datasource.tables = (List) tables; return datasource; }
From source file:org.esupportail.monitor.web.tools.InfosCollector.java
/** * Rcupre les informations d'un serveur distant * @param s L'objet remplit//from w w w . j av a2 s. c om * @return status vrai si tout se passe bien, faux sinon */ public static boolean fetch(ServerInfo s, boolean users) { Digester dig = new Digester(); dig.push(s); dig.addObjectCreate("runtimeinfo/memory", MemoryInfo.class); dig.addSetProperties("runtimeinfo/memory"); dig.addSetNext("runtimeinfo/memory", "setMemory"); dig.addObjectCreate("runtimeinfo/sessions", SessionInfo.class); dig.addSetProperties("runtimeinfo/sessions"); dig.addSetNext("runtimeinfo/sessions", "setSession"); dig.addCallMethod("runtimeinfo/users/user", "addUser", 1); dig.addCallParam("runtimeinfo/users/user", 0, "uid"); URL ressourceUrl = null; try { if (users) { ressourceUrl = new URL(s.getUrl() + "?xml=full"); } else { ressourceUrl = new URL(s.getUrl() + "?xml"); } } catch (MalformedURLException e) { // logger.error(e); System.out.print(e + "\n"); return false; } if (ressourceUrl != null) { try { // On parse le fichier dont le chemin est pass en paramtre dig.parse(new InputSource(ressourceUrl.toExternalForm())); } catch (IOException e) { //logger.error("InfosCollector::fetch() : Impossible d'ouvrir l'URL \n" + e); System.out.print("InfosCollector::fetch() : Impossible d'ouvrir l'URL \n" + e); return false; } catch (SAXException e) { //logger.error("InfosCollector::fetch() : SAXException :\n" + e); System.out.print("InfosCollector::fetch() : SAXException :\n" + e); return false; } catch (Exception e) { //logger.error("InfosCollector::fetch() : Exception :\n" + e); System.out.print("InfosCollector::fetch() : Exception :\n" + e); return false; } } else { //logger.error("InfosCollector::fetch() : L'URL est introuvable"); System.out.print("InfosCollector::fetch() : L'URL est introuvable"); return false; } return true; }