List of usage examples for org.dom4j Element elementIterator
Iterator<Element> elementIterator();
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
@SuppressWarnings("unchecked") protected void parentXMLImport(Element dbImport, String dbTable, @SuppressWarnings("unused") String parentName, @SuppressWarnings("unused") long parentId) { // get the immediate parents List<String> immediateParents = new ArrayList<String>(); immediateParents = getImmediateParentTables(dbTable, immediateParents, false); // get all parents List<String> parents = new ArrayList<String>(); parents = getParentTables(dbTable, parents, false); // ignore agent for now Element dbElement = (Element) dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$ Iterator i = dbElement.elementIterator(); do {// do for each element in the record Element element = (Element) i.next(); String elementName = element.getName().toString(); // if there is an immediate parent if (immediateParents.contains(elementName)) { // check if three is a value if (element.getText().equals("") || element.getText().equals(null)) //$NON-NLS-1$ {// w w w .j a va 2 s .c om // remove from list immediateParents.remove(elementName); } else { // check if we can load it String className = immediateParents.get(immediateParents.indexOf(elementName)); int num = new Integer(className).intValue(); // make a new one importSingleDBObject(className, num, false); } } } while (i.hasNext()); }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
@SuppressWarnings("unchecked") protected void iterativeXMLImport(Element dbImport, String dbTable, String parentName, long parentId) { try {/*from w ww.ja v a 2 s. co m*/ DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$ String lowerdbTable = lowerFirstChar(dbTable); // List ids = dbImport.selectNodes("//"+lowerdbTable+"Id");//assume this is dbs id name if (records.size() < 1) { log.debug("Cannot import. Given database type:" + dbTable //$NON-NLS-1$ + " does not exsist in import file"); //$NON-NLS-1$ } else { // loop for each record for (int k = 0; k < records.size(); k++) { // keep this id to compare against it's collection // Element idElement = (Element)ids.get(k); // make the agent and the element Object agent = parentInfo.getClassObj().newInstance(); Map<String, Object> agentMap = new HashMap<String, Object>(); Element dbElement = (Element) records.get(k); Iterator i = dbElement.elementIterator(); do {// do for each element in the record Element element = (Element) i.next(); Object value = findTypeSequential(element, dbTable, parentId, parentName);// the // parent // is // itself, // just // a // dummy // variable // if(value!=null && value != "collection") if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$ { agentMap.put(element.getName(), value); } else // else, dont add it { // if it is an id, just ignore. otherwise print out error if (!element.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$ { log.debug("did not add " + element.getName() //$NON-NLS-1$ + " to the element " + dbTable); //$NON-NLS-1$ } } } while (i.hasNext()); // populate and save BeanUtils.populate(agent, agentMap); this.session.save(agent); } } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
/** * Imports a table from a given xml file * @param dbImport the opend xml file elememt * @param dbTable the class name of the table *///from w w w. j a v a 2 s. c om @SuppressWarnings("unchecked") protected Object dynamicXMLImportRecReturn(Element dbImport, String dbTable) { Object agent = new Object(); try { String id = new String(); DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$ String lowerdbTable = lowerFirstChar(dbTable); // List attributes = dbImport.selectNodes("//@"+lowerdbTable+"Id"); // TODO shouldl not assume this is the dbprimary key, use dbtablemgr List ids = dbImport.selectNodes("//" + lowerdbTable + "Id");// assume this is dbs id //$NON-NLS-1$ //$NON-NLS-2$ // name if (records.size() < 1) { log.debug("Cannot import. Given database type:" + dbTable //$NON-NLS-1$ + " does not exsist in import file"); //$NON-NLS-1$ } else { // loop for each record for (int k = 0; k < records.size(); k++) { // keep this id to compare against it's collection Element idElement = (Element) ids.get(k); // id = attribute.getText(); id = idElement.getStringValue(); // make the agent and the element agent = parentInfo.getClassObj().newInstance(); Map<String, Object> agentMap = new HashMap<String, Object>(); Element dbElement = (Element) records.get(k); Iterator i = dbElement.elementIterator(); do {// do for each element in the record Element element = (Element) i.next(); Object value = findType(element, dbTable, agent, " ");// the parent is //$NON-NLS-1$ // itself, just a // dummy variable if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$ { agentMap.put(element.getName(), value); } // ignore many-to-many for now else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$ {// RECURSE // is it a collection, add all associated records // get assoicated ids // TODO shouldl not assume things are in order List collectingevent_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$ + id + "]/" + element.getName() + "/*");// +upperElement); //$NON-NLS-1$ //$NON-NLS-2$ if (!collectingevent_ids.isEmpty()) { // add all the assoctions to aDbElement // get child dbName String childDbName = getDbName(collectingevent_ids); // make a parent object BeanUtils.populate(agent, agentMap); // Collection collection = xmlImportRecursion(locality, // upperElement, collectingevent_ids, parent, lowerdbTable); Set collection = xmlImportRecursion(agentMap, childDbName, collectingevent_ids, agent, lowerdbTable); if (collection != null) { agentMap.put(element.getName(), collection); } else { log.debug("error on the collection " //$NON-NLS-1$ + element.getName() + " with parent " + dbTable); //$NON-NLS-1$ } } } else // else, dont add it { // if it is an id, just ignore. otherwise print out error if (!element.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$ { log.debug("did not add " + element.getName() //$NON-NLS-1$ + " to the element " + dbTable); //$NON-NLS-1$ } } } while (i.hasNext()); // populate and save BeanUtils.populate(agent, agentMap); this.session.saveOrUpdate(agent); } } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } return agent; }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
/** * recurses over many-to-many or one-to-many record sets * @param parentMap the map of the parent record * @param dbTable the class name of the table * @param ids the list of associated ids * @param parentObject the parent record * @param parentName the parentName// w w w . j av a 2 s. c om * @return a set, or null if not found */ @SuppressWarnings("unchecked") protected Set xmlImportRecursion(Map parentMap, String dbTable, List ids, Object parentObject, String parentName) { try { HashSet<Object> set = new HashSet<Object>(); String lowerdbTable = lowerFirstChar(dbTable); DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); // open the new file File path = new File(importFolderPath + dbTable + ".xml"); //$NON-NLS-1$ Element dbImport = XMLHelper.readFileToDOM4J(path); // add each collectingevent to locality for (int j = 0; j < ids.size(); j++) { // the only way to get the value out of collectingevent_ids Element temp_id = (Element) ids.get(j); String id2 = temp_id.getText(); // select the node Element collectingevent = (Element) dbImport.selectSingleNode("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$ + id2 + "]");// temp_id.getText()+"]"); //$NON-NLS-1$ Iterator iter = collectingevent.elementIterator(); // make the element and the agent Map<String, Object> collectingEventMap = new HashMap<String, Object>(); Object agent = parentInfo.getClassObj().newInstance(); do { Element secondelement = (Element) iter.next(); Object value2 = findType(secondelement, dbTable, parentObject, parentName); if (value2 != null && value2 != "OneToMany" && value2 != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$ { collectingEventMap.put(secondelement.getName(), value2); } else if (value2 == "ManyToMany") //$NON-NLS-1$ { HashSet<Object> parentSet = new HashSet<Object>(); parentSet.add(parentObject); collectingEventMap.put(secondelement.getName(), parentSet); } else if (value2 == "OneToMany") //$NON-NLS-1$ { // RECURSE // is it a collection, add all associated records // get assoicated ids List associated_ids = secondelement.selectNodes("//" + dbTable + "[" + id2 //$NON-NLS-1$ //$NON-NLS-2$ + "]/" + secondelement.getName() + "/*");// +upperElement); //$NON-NLS-1$ //$NON-NLS-2$ if (!associated_ids.isEmpty()) { // add all the assoctions to aDbElement // get child database name String childDbName = getDbName(associated_ids); // make a parent object BeanUtils.populate(agent, collectingEventMap); Set collection = xmlImportRecursion(collectingEventMap, childDbName, associated_ids, agent, lowerdbTable); if (collection != null) { collectingEventMap.put(secondelement.getName(), collection); } else { log.debug("error on the collection " //$NON-NLS-1$ + secondelement.getName() + " with parent " + dbTable); //$NON-NLS-1$ } } } else { // if it is an id, just ignore. otherwise print out error if (!secondelement.getName().equals(lowerdbTable + "Id")) //$NON-NLS-1$ { log.debug("did not add " + secondelement.getName() //$NON-NLS-1$ + " to the element " + dbTable); //$NON-NLS-1$ } } } while (iter.hasNext()); // add to the set BeanUtils.populate(agent, collectingEventMap); set.add(agent); } // return the set; return set; } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); return null; } }
From source file:edu.ku.brc.dbsupport.ImportExportDB.java
License:Open Source License
@SuppressWarnings("unchecked") protected void sequentialDatabaseImport(Element dbImport, String dbTable, String parentName, long parentId, boolean recursion) { try {/*from ww w . j a va2 s . com*/ String id = new String(); DBTableInfo parentInfo = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); // get the records List records = dbImport.selectNodes("//" + dbTable); //$NON-NLS-1$ DBTableInfo info = DBTableIdMgr.getInstance().getInfoByTableName(dbTable.toLowerCase()); String primaryKey = info.getPrimaryKeyName(); List ids = dbImport.selectNodes("//" + primaryKey); //$NON-NLS-1$ if (records.size() < 1) { log.debug("Cannot import. Given database type:" + dbTable //$NON-NLS-1$ + " does not exsist in import file"); //$NON-NLS-1$ } else { // loop for each record for (int k = 0; k < records.size(); k++) { Vector collectionIds = new Vector(20); Vector collectionNames = new Vector(20); // keep this id to compare against it's collection Element idElement = (Element) ids.get(k); id = idElement.getStringValue(); // make the agent and the element Object agent = parentInfo.getClassObj().newInstance(); HashMap<String, Object> agentMap = new HashMap<String, Object>(); Element dbElement = (Element) records.get(k); Iterator i = dbElement.elementIterator(); do {// do for each element in the record Element element = (Element) i.next(); // Object value = findTypeSequential(element, dbTable, parentId, parentName // );//the parent is itself, just a dummy variable Object value = findTypeDataBase(element, dbTable, parentId, parentName);// the // parent // is // itself, // just // a // dummy // variable if (value != null && value != "OneToMany" && value != "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$ { agentMap.put(element.getName(), value); } // ignore many-to-many for now else if (value == "OneToMany" || value == "ManyToMany") //$NON-NLS-1$ //$NON-NLS-2$ {// RECURSE if (recursion) { // get assoicated ids List temp_collection_ids = element.selectNodes("//" + dbTable + "[" //$NON-NLS-1$ //$NON-NLS-2$ + primaryKey + " = \"" + id + "\"]/" + element.getName() //$NON-NLS-1$ //$NON-NLS-2$ + "/*"); //$NON-NLS-1$ // get collection info and still dont add it if (!temp_collection_ids.isEmpty()) { // get child dbName String childDbName = getDbName(temp_collection_ids); collectionNames.addElement(childDbName); for (int index = 0; index < temp_collection_ids.size(); index++) { collectionIds.addElement(temp_collection_ids.get(index)); } } } } else // else, dont add it { // if it is an id, just ignore. otherwise print out error if (!element.getName().equals(primaryKey)) { log.debug("did not add " + element.getName() //$NON-NLS-1$ + " to the element " + dbTable); //$NON-NLS-1$ } } } while (i.hasNext()); // populate and save BeanUtils.populate(agent, agentMap); this.session.save(agent); // if there was a collection, then recurse /* * if(!collectionIds.isEmpty()) { long newParentId = new * Long(session.getIdentifier(agent).toString()).longValue(); //import all * children sequentialXMLImportRecursion(collectionNames, collectionIds, * dbTable, newParentId); } */ } } } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(ImportExportDB.class, ex); ex.printStackTrace(); } }
From source file:edu.umd.cs.findbugs.filter.Filter.java
License:Open Source License
/** * Parse and load the given filter file. * /*from w w w . ja v a 2 s. co m*/ * @param fileName * name of the filter file * @throws IOException * @throws SAXException * @throws FilterException */ private void parse(String fileName) throws IOException, SAXException { if (true) { File file = new File(fileName); SAXBugCollectionHandler handler = new SAXBugCollectionHandler(this, file); XMLReader xr = XMLReaderFactory.createXMLReader(); xr.setContentHandler(handler); xr.setErrorHandler(handler); FileInputStream fileInputStream = new FileInputStream(file); try { Reader reader = Util.getReader(fileInputStream); xr.parse(new InputSource(reader)); } finally { Util.closeSilently(fileInputStream); } return; } Document filterDoc = null; FileInputStream fileInputStream = new FileInputStream(fileName); try { SAXReader reader = new SAXReader(); filterDoc = reader.read(new BufferedInputStream(fileInputStream)); } catch (DocumentException e) { throw new FilterException("Couldn't parse filter file " + fileName, e); } int count = 1; // Iterate over Match elements for (Object matchObj : XMLUtil.selectNodes(filterDoc, "/FindBugsFilter/Match")) { Element matchNode = (Element) matchObj; AndMatcher matchMatcher = new AndMatcher(); // Each match node may have either "class" or "classregex" // attributes Matcher classMatcher = null; String classAttr = matchNode.valueOf("@class"); if (!classAttr.equals("")) { classMatcher = new ClassMatcher(classAttr); } else { String classRegex = matchNode.valueOf("@classregex"); if (!classRegex.equals("")) classMatcher = new ClassMatcher("~" + classRegex); } if (classMatcher != null) matchMatcher.addChild(classMatcher); if (DEBUG) System.out.println("Match node"); // Iterate over child elements of Match node. Iterator<?> j = matchNode.elementIterator(); while (j.hasNext()) { Element child = (Element) j.next(); Matcher matcher = getMatcher(child); matchMatcher.addChild(matcher); } if (matchMatcher.numberChildren() == 0) throw new FilterException( "Match element #" + count + " (starting at 1) is invalid in filter file " + fileName); // Add the Match matcher to the overall Filter this.addChild(matchMatcher); count++; } if (this.numberChildren() == 0) throw new FilterException("Could not find any /FindBugsFilter/Match nodes in filter file " + fileName); }
From source file:edu.umd.cs.findbugs.filter.Filter.java
License:Open Source License
/** * Get a Matcher for given Element.//from w w w . ja va 2s. c o m * * @param element * the Element * @return a Matcher representing that element * @throws FilterException */ private static Matcher getMatcher(Element element) throws FilterException { // These will be either BugCode, Priority, Class, Method, Field, or Or // elements. String name = element.getName(); if (name.equals("BugCode")) { return new BugMatcher(element.valueOf("@name"), "", ""); } else if (name.equals("Local")) { return new LocalMatcher(element.valueOf("@name")); } else if (name.equals("BugPattern")) { return new BugMatcher("", element.valueOf("@name"), ""); } else if (name.equals("Bug")) { return new BugMatcher(element.valueOf("@code"), element.valueOf("@pattern"), element.valueOf("@category")); } else if (name.equals("Priority") || name.equals("Confidence")) { return new PriorityMatcher(element.valueOf("@value")); } else if (name.equals("Rank")) { return new RankMatcher(element.valueOf("@value")); } else if (name.equals("Class")) { Attribute nameAttr = element.attribute("name"); if (nameAttr == null) throw new FilterException("Missing name attribute in Class element"); return new ClassMatcher(nameAttr.getValue()); } else if (name.equals("Package")) { Attribute nameAttr = element.attribute("name"); if (nameAttr == null) throw new FilterException("Missing name attribute in Package element"); String pName = nameAttr.getValue(); pName = pName.startsWith("~") ? pName : "~" + pName.replace(".", "\\."); return new ClassMatcher(pName + "\\.[^.]+"); } else if (name.equals("Method")) { Attribute nameAttr = element.attribute("name"); String nameValue; Attribute paramsAttr = element.attribute("params"); Attribute returnsAttr = element.attribute("returns"); Attribute roleAttr = element.attribute("role"); if (nameAttr == null) if (paramsAttr == null || returnsAttr == null) throw new FilterException( "Method element must have eiter name or params and returnss attributes"); else nameValue = "~.*"; // any name else nameValue = nameAttr.getValue(); if ((paramsAttr != null || returnsAttr != null) && (paramsAttr == null || returnsAttr == null)) throw new FilterException( "Method element must have both params and returns attributes if either is used"); if (paramsAttr == null) if (roleAttr == null) return new MethodMatcher(nameValue); else return new MethodMatcher(nameValue, roleAttr.getValue()); else if (roleAttr == null) return new MethodMatcher(nameValue, paramsAttr.getValue(), returnsAttr.getValue()); else return new MethodMatcher(nameValue, paramsAttr.getValue(), returnsAttr.getValue(), roleAttr.getValue()); } else if (name.equals("Field")) { Attribute nameAttr = element.attribute("name"); String nameValue; Attribute typeAttr = element.attribute("type"); if (nameAttr == null) if (typeAttr == null) throw new FilterException("Field element must have either name or type attribute"); else nameValue = "~.*"; // any name else nameValue = nameAttr.getValue(); if (typeAttr == null) return new FieldMatcher(nameValue); else return new FieldMatcher(nameValue, typeAttr.getValue()); } else if (name.equals("Or")) { OrMatcher orMatcher = new OrMatcher(); Iterator<?> i = element.elementIterator(); while (i.hasNext()) { orMatcher.addChild(getMatcher((Element) i.next())); } return orMatcher; } else throw new FilterException("Unknown element: " + name); }
From source file:edu.wustl.cab2b.server.category.CategoryXmlParser.java
License:BSD License
/** * @param category The Document element which represents a Category * @return InputCategory Object for given Category Element. *//*from w w w.j a v a 2 s . com*/ private InputCategory getInputCategory(Element category) { InputCategory inputCategory = new InputCategory(); List<InputCategory> subCategories = new ArrayList<InputCategory>(); InputCategorialClass rootClass = null; Element rootCategorialClass = null; List<Element> elements = category.elements(); for (Element e : elements) { if (e.getName().equals("CategorialClass")) { rootCategorialClass = (Element) category.elementIterator().next(); rootClass = getInputCategorialClass(rootCategorialClass); } else if (e.getName().equals("Category")) { subCategories.add(getInputCategory(e)); } } String name = category.attribute("name").getValue(); inputCategory.setName(name); String description = category.attribute("description").getValue(); inputCategory.setDescription(description); inputCategory.setRootCategorialClass(rootClass); inputCategory.setSubCategories(subCategories); return inputCategory; }
From source file:eu.delving.metadata.RecordValidator.java
License:EUPL
private boolean validateElement(Element element, Path path, List<String> problems, Set<String> entries, Map<Path, Counter> counters) { path.push(Tag.create(element.getNamespacePrefix(), element.getName())); boolean hasElements = false; Iterator walk = element.elementIterator(); while (walk.hasNext()) { Element subelement = (Element) walk.next(); boolean remove = validateElement(subelement, path, problems, entries, counters); if (remove) { walk.remove();//w w w. jav a 2 s . c o m } hasElements = true; } if (!hasElements) { boolean fieldRemove = validatePath(element.getTextTrim(), path, problems, entries, counters); path.pop(); return fieldRemove; } path.pop(); return false; }
From source file:fr.gouv.culture.vitam.database.utils.swing.Dom4jTreeBuilder.java
License:Open Source License
public void addElement(Element curnode) { DefaultMutableTreeNode previousNode = currentNode; for (@SuppressWarnings("unchecked") Iterator<Element> it = curnode.elementIterator(); it.hasNext();) { Element elt = it.next();//ww w .j a v a 2 s .co m DefaultMutableTreeNode node = null; if (elt.getTextTrim().length() == 0) { node = new DefaultMutableTreeNode(elt.getName()); previousNode.add(node); } else { node = new DefaultMutableTreeNode(elt.getName()); DefaultMutableTreeNode subnode = new DefaultMutableTreeNode("T=" + elt.getText()); node.add(subnode); previousNode.add(node); } currentNode = node; addAttribute(elt); addElement(elt); } currentNode = previousNode; }