List of usage examples for org.dom4j Element elementIterator
Iterator<Element> elementIterator();
From source file:de.r2soft.empires.framework.util.MapParser.java
License:Open Source License
public void readXML(Element root) { HashSet<Planet> planetary = new HashSet<Planet>(); map = new GalaxyMap(); Element solarSystems = root.element("SolarSystems"); for (Iterator<Element> s = solarSystems.elementIterator(); s.hasNext();) { Element solarSystem = s.next(); SolarSystem temp = new SolarSystem(null); Element position = solarSystem.element("Position"); temp.setPosition(new GalaxyPosition(Integer.parseInt(position.attribute("PosX").getText()), Integer.parseInt(position.attribute("PosY").getText()))); temp.setClaim(new Player(solarSystem.attribute("Owner").getText())); Element planets = solarSystem.element("Planets"); for (Iterator<Element> p = planets.elementIterator(); p.hasNext();) { Element planet = p.next(); planetary.add(new Planet(Float.parseFloat(planet.attribute("Distance").getValue()), Float.parseFloat(planet.attribute("Size").getValue()))); }//w ww. j ava 2 s.c om temp.setPlanets(planetary); map.addSystem(temp); } map.setVersion(0); map.setSize(new IntVec2(1, 2)); }
From source file:de.start.InitializeDB.java
License:Open Source License
public static void setupDB(int year) { try {//from w w w .j a v a2s . co m String filename = "nvdcve-2008.xml"; Connection con = getConnection(); Statement sql = con.createStatement(); sql.execute("drop table if exists nvd"); //,primary key(id) sql.execute( "create table nvd(id varchar(20) not null,soft varchar(160) not null default 'ndefined',rng varchar(100) not null default 'undefined',lose_types varchar(100) not null default 'undefind',severity varchar(20) not null default 'unefined',access varchar(20) not null default 'unefined');"); SAXReader saxReader = new SAXReader(); for (int ct = 2002; ct <= year; ct++) { //String fname="/transient/mulval/oval/nvd/nvdcve-"+Integer.toString(ct)+".xml"; String fname = "nvd_xml_files/nvdcve-" + Integer.toString(ct) + ".xml"; Document document = saxReader.read(fname); List entry = document.selectNodes("/*[local-name(.)='nvd']/*[local-name(.)='entry']"); Iterator ent = entry.iterator(); int act = 0; while (ent.hasNext()) { // varchar(20) not null default 'name', Element id = (Element) ent.next(); String cveid = id.attributeValue("name"); String cvss = ""; String access = ""; // System.out.println(cveid + access); String sev = ""; String host = "localhost"; String sftw = ""; String rge = ""; String rge_tmp = ""; String lose_tmp = ""; String lose_types = ""; ArrayList<String> subele = new ArrayList<String>(); ArrayList<String> attr = new ArrayList<String>(); Iterator ei = id.elementIterator(); while (ei.hasNext()) { // put all of the subelements' // names(subelement of entry) to the // array list Element sube = (Element) ei.next(); subele.add(sube.getName()); } //System.out.println(id.getText()); Iterator i = id.attributeIterator(); while (i.hasNext()) { // put the attributes of the entries to // the arraylist Attribute att = (Attribute) i.next(); attr.add(att.getName()); } if (subele.contains("vuln_soft")) { Element vs = (Element) id.element("vuln_soft"); Iterator itr = vs.elementIterator("prod"); while (itr.hasNext()) { // record all of the softwares Element n = (Element) itr.next(); //sftw = sftw + n.attributeValue("name") + ","; sftw = n.attributeValue("name"); if (sftw.contains("'")) { sftw = sftw.replace("'", "''"); } break; } //int lsf = sftw.length(); //sftw = sftw.substring(0, lsf - 1);// delete the last comma } if (attr.contains("severity")) { sev = id.attributeValue("severity"); } if (attr.contains("CVSS_vector")) { cvss = id.attributeValue("CVSS_vector"); char ac = cvss.charAt(9); if (ac == 'L') access = "l"; else if (ac == 'M') access = "m"; else if (ac == 'H') access = "h"; else ; } if (subele.contains("range")) { // to get the range as a array Element vs = (Element) id.element("range"); Iterator rgi = vs.elementIterator(); while (rgi.hasNext()) { // record all of the softwares Element rg = (Element) rgi.next(); if (rg.getName().equals("user_init")) rge_tmp = "user_action_req"; else if (rg.getName().equals("local_network")) rge_tmp = "lan"; else if (rg.getName().equals("network")) rge_tmp = "remoteExploit"; else if (rg.getName().equals("local")) rge_tmp = "local"; else rge_tmp = "other"; rge = rge + "''" + rge_tmp + "'',"; } int lr = rge.length(); rge = rge.substring(0, lr - 1);// delete the last comma } if (subele.contains("loss_types")) { Element lt = (Element) id.element("loss_types"); Iterator lti = lt.elementIterator(); while (lti.hasNext()) { ArrayList<String> isecat = new ArrayList<String>(); Element ls = (Element) lti.next(); if (ls.getName().equals("avail")) lose_tmp = "availability_loss"; else if (ls.getName().equals("conf")) lose_tmp = "data_loss"; else if (ls.getName().equals("int")) lose_tmp = "data_modification"; else lose_tmp = "other"; lose_types = lose_types + "''" + lose_tmp + "'',"; } int ltp = lose_types.length(); lose_types = lose_types.substring(0, ltp - 1);// delete the // last // comma } //System.out.println(cveid + lose_types + rge + sftw + sev + access); String insert = "insert nvd values('" + cveid + "','" + sftw + "','" + rge + "','" + lose_types + "','" + sev + "','" + access + "')"; sql.execute(insert); } } sql.close(); con.close(); } catch (java.lang.ClassNotFoundException e) { System.err.println("ClassNotFoundException:" + e.getMessage()); } catch (SQLException ex) { System.err.println("SQLException:" + ex.getMessage()); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:de.tud.kom.p2psim.impl.scenario.DefaultConfigurator.java
License:Open Source License
/** * Create (if not existent yet) and configure a configurable component by * parsing the XML subtree./*from w ww.j ava 2 s . co m*/ * * @param elem * XML subtree with configuration data * @return configured component */ public Object configureComponent(Element elem) { String name = elem.getName(); if ("IfEqualStr".equalsIgnoreCase(name)) { return configureIfEqualStr(elem); } log.debug("Configure component " + elem.getName()); Object component = configurables.get(elem.getName()); // register new component (if not done yet) Set<String> consAttrs = new HashSet<String>(); // attributes that were // part of the // constructor if (component == null) { component = createComponent(elem, consAttrs); } // configure it if (component != null) { log.info("Configure component " + component.getClass().getSimpleName() + " with element " + elem.getName()); configureAttributes(component, elem, consAttrs); // configure subcomponents if (component instanceof Builder) { log.info("Configure builder " + component); Builder builder = (Builder) component; builder.parse(elem, this); } else { for (Iterator iter = elem.elementIterator(); iter.hasNext();) { Element child = (Element) iter.next(); if (!consAttrs.contains(child.getName().toLowerCase())) processChild(component, child); } } } else { // component cannot be created and has not been registered log.warn("Skip element " + elem.getName()); } return component; }
From source file:de.tud.kom.p2psim.impl.scenario.DefaultConfigurator.java
License:Open Source License
/** * You can create elements like/*from w w w.j av a2 s .c o m*/ * * <IfEqualStr arg0="$variable" arg1="value"> [...your configuration * ... ] </IfEqualStr>, and they will be applied only if the strings * are equal. * * @param ifClause * @param toExecuteOnTrue * @return */ private Object processIfEqualStr(Element ifClause, EqualStrRunnable toExecuteOnTrue) { String arg0 = ifClause.attributeValue("arg0"); String arg1 = ifClause.attributeValue("arg1"); String arg0p = parseValue(arg0); String arg1p = parseValue(arg1); if (arg0p == null) throw new RuntimeException("Variable " + arg0 + " not set or null."); if (arg1p == null) throw new RuntimeException("Variable " + arg1 + " not set or null."); if (arg0p.equals(arg1p)) { Iterator iter = ifClause.elementIterator(); if (!iter.hasNext()) log.warn("No component to configure in the ifEqualStr-clause (arg0=" + arg0 + ", arg1=" + arg1 + ")."); else { Element child = (Element) iter.next(); Object result = toExecuteOnTrue.run(child); if (iter.hasNext()) throw new RuntimeException("An IfEqualStr-clause may only contain one component to configure."); return result; } } return null; }
From source file:de.tud.kom.p2psim.impl.scenario.DefaultHostBuilder.java
License:Open Source License
public void parse(Element elem, Configurator config) { DefaultConfigurator defaultConfigurator = (DefaultConfigurator) config; // create groups for (Iterator iter = elem.elementIterator(); iter.hasNext();) { Element groupElem = (Element) iter.next(); String groupID = groupElem.attributeValue(GROUP_ID_TAG); if (groupID == null) { throw new IllegalArgumentException("Id of host/group " + groupElem.asXML() + " must not be null"); }// w w w. j a va2 s . c o m // either a group of hosts or a single host (=group with size 1) int groupSize; if (groupElem.getName().equals(HOST_TAG)) { groupSize = 1; } else if (groupElem.getName().equals(GROUP_TAG)) { String attributeValue = config.parseValue(groupElem.attributeValue(GROUP_SIZE_TAG)); groupSize = Integer.parseInt(attributeValue); } else { throw new IllegalArgumentException("Unexpected tag " + groupElem.getName()); } List<Host> group = new ArrayList<Host>(groupSize); // create hosts and instances of specified components for each host for (int i = 0; i < groupSize; i++) { DefaultHost host = new DefaultHost(); // initialize properties DefaultHostProperties hostProperties = new DefaultHostProperties(); host.setProperties(hostProperties); // minimal information for host properties is the group id hostProperties.setGroupID(groupID); // initialize layers and properties for (Iterator layers = groupElem.elementIterator(); layers.hasNext();) { Element layerElem = (Element) layers.next(); if (layerElem.getName().equals(Configurator.HOST_PROPERTIES_TAG)) { defaultConfigurator.configureAttributes(hostProperties, layerElem); } else { // layer component factory ComponentFactory layer = (ComponentFactory) defaultConfigurator .configureComponent(layerElem); Component comp = layer.createComponent(host); setComponent(host, comp); } } group.add(host); } log.debug("Created a group with " + group.size() + " hosts"); hosts.addAll(group); groups.put(groupID, group); } log.info("CREATED " + hosts.size() + " hosts"); if (hosts.size() != experimentSize) { log.warn("Only " + hosts.size() + " hosts were specified, though the experiment size was set to " + experimentSize); } }
From source file:de.tud.kom.p2psim.impl.skynet.SkyNetHostBuilder.java
License:Open Source License
public void parse(Element elem, Configurator config) { DefaultConfigurator defaultConfigurator = (DefaultConfigurator) config; // create groups for (Iterator iter = elem.elementIterator(); iter.hasNext();) { Element groupElem = (Element) iter.next(); String groupID = groupElem.attributeValue(GROUP_ID_TAG); if (groupID == null) { throw new IllegalArgumentException("Id of host/group " + groupElem.asXML() + " must not be null"); }// w w w. j ava 2s.c om // either a group of hosts or a single host (=group with size 1) int groupSize; if (groupElem.getName().equals(HOST_TAG)) { groupSize = 1; } else if (groupElem.getName().equals(GROUP_TAG)) { String attributeValue = config.parseValue(groupElem.attributeValue(GROUP_SIZE_TAG)); groupSize = Integer.parseInt(attributeValue); } else { throw new IllegalArgumentException("Unexpected tag " + groupElem.getName()); } List<Host> group = new ArrayList<Host>(groupSize); // create hosts and instances of specified components for each host for (int i = 0; i < groupSize; i++) { log.info((i + 1) + ". host of Group " + groupID + " is created"); DefaultHost host = new DefaultHost(); // initialize properties // Changed the constructor to SkyNetHostProperties to add some // additional properties SkyNetHostProperties hostProperties = new SkyNetHostProperties(); host.setProperties(hostProperties); // minimal information for host properties is the group id hostProperties.setGroupID(groupID); // initialize layers and properties for (Iterator layers = groupElem.elementIterator(); layers.hasNext();) { Element layerElem = (Element) layers.next(); if (layerElem.getName().equals(Configurator.HOST_PROPERTIES_TAG)) { defaultConfigurator.configureAttributes(hostProperties, layerElem); } else { // layer component factory ComponentFactory layer = (ComponentFactory) defaultConfigurator .configureComponent(layerElem); log.debug("Factory is " + layer + " for wanted elem " + layerElem.asXML()); Component comp = layer.createComponent(host); setComponent(host, comp); } } group.add(host); log.info("->" + host.toString()); } log.info("------Created group " + groupID + " with " + group.size() + " hosts------"); hosts.addAll(group); groups.put(groupID, group); } log.info("******CREATION OF HOSTS IS FINISHED. CREATED " + hosts.size() + " HOSTS******"); if (ChurnStatisticsAnalyzer.isActivated()) { ChurnStatisticsAnalyzer.setCreatedHost(getAllHosts()); } if (hosts.size() != experimentSize) { log.warn("Only " + hosts.size() + " hosts were specified, though the experiment size was set to " + experimentSize); } }
From source file:de.tud.kom.p2psim.impl.util.toolkits.Dom4jToolkit.java
License:Open Source License
public static Element getSubElementFromStrCaseInsensitive(Element e, String name) { Iterable<Element> it = new IterableDummy<Element>(e.elementIterator()); for (Element element : it) { if (element.getName().equalsIgnoreCase(name)) return element; }//from w w w. j a v a2s.c o m return null; }
From source file:de.tu_berlin.cit.intercloud.xmpp.component.ResourceContainerSocketManager.java
License:Apache License
/** * This method handles the IQ stanzas of type <tt>result</tt> * that are received by the component./*from ww w . ja v a 2 s .co m*/ * * @param iq * The IQ stanza of type <tt>result</tt> that was received by * this component. */ protected void handleIQResult(IQ iq) { logger.info("the following iq result stanza has been received:" + iq.toString()); // IQ get (and set) stanza's MUST be replied to. final Element childElement = iq.getChildElement(); String namespace = null; if (childElement != null) { namespace = childElement.getNamespaceURI(); } if (namespace == null) { logger.info("(serving component '{}') Invalid XMPP " + "- no child element or namespace in IQ " + "request (packetId {})", this.component.getName(), iq.getID()); // this isn't valid XMPP. return; } if (ResourceContainerComponent.NAMESPACE_DISCO_ITEMS.equals(namespace)) { logger.info("discovery item result."); @SuppressWarnings("rawtypes") Iterator iter = childElement.elementIterator(); String rootJID = null; ArrayList<String> exchangeJIDs = new ArrayList<String>(); ArrayList<String> gatewayJIDs = new ArrayList<String>(); while (iter.hasNext()) { Element item = (Element) iter.next(); // filter if (item.attributeValue("name").equals(ServiceNames.RootComponentName)) { rootJID = item.attributeValue("jid"); } else if (item.attributeValue("name").equals(ServiceNames.ExchangeComponentName)) { exchangeJIDs.add(item.attributeValue("jid")); } else if (item.attributeValue("name").equals(ServiceNames.GatewayComponentName)) { gatewayJIDs.add(item.attributeValue("jid")); } } IntercloudDiscoItems discoItems = new IntercloudDiscoItems(rootJID, exchangeJIDs, gatewayJIDs); try { discoItems = discoItemExchanger.exchange(discoItems); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (ResourceContainerComponent.NAMESPACE_DISCO_INFO.equals(namespace)) { logger.info("discovery info result."); @SuppressWarnings("rawtypes") Iterator iter = childElement.elementIterator(); ArrayList<String> features = new ArrayList<String>(); while (iter.hasNext()) { Element feature = (Element) iter.next(); if (feature.getName().equals("feature")) { features.add(feature.attributeValue("var")); } } IntercloudDiscoFeatures discoFeatures = new IntercloudDiscoFeatures(iq.getFrom().toBareJID(), features); try { discoFeatures = discoFeatureExchanger.exchange(discoFeatures); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (ResourceContainerComponent.NAMESPACE_REST_XWADL.equals(namespace)) { logger.info("received xwadl iq."); try { handleRestXWADL(iq.getID(), ResourceTypeDocument.Factory.parse(childElement.asXML())); } catch (XmlException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (ResourceContainerComponent.NAMESPACE_REST_XML.equals(namespace)) { logger.info("received rest xml iq."); try { handleRestXML(iq.getID(), ResourceDocument.Factory.parse(childElement.asXML())); } catch (XmlException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:de.tu_berlin.cit.rwx4j.component.ResourceContainerSocketManager.java
License:Apache License
/** * This method handles the IQ stanzas of type <tt>result</tt> * that are received by the component./* w ww. jav a2s . c om*/ * * @param iq * The IQ stanza of type <tt>result</tt> that was received by * this component. */ protected void handleIQResult(IQ iq) { logger.info("the following iq result stanza has been received:" + iq.toString()); // IQ get (and set) stanza's MUST be replied to. final Element childElement = iq.getChildElement(); String namespace = null; if (childElement != null) { namespace = childElement.getNamespaceURI(); } if (namespace == null) { logger.info("(serving component '{}') Invalid XMPP " + "- no child element or namespace in IQ " + "request (packetId {})", this.component.getName(), iq.getID()); // this isn't valid XMPP. return; } if (ResourceContainerComponent.NAMESPACE_DISCO_ITEMS.equals(namespace)) { logger.info("discovery item result."); @SuppressWarnings("rawtypes") Iterator iter = childElement.elementIterator(); IntercloudDiscoItems discoItems = new IntercloudDiscoItems(); // String rootJID = null; // ArrayList<String> exchangeJIDs = new ArrayList<String>(); // ArrayList<String> gatewayJIDs = new ArrayList<String>(); while (iter.hasNext()) { Element item = (Element) iter.next(); // filter discoItems.addItem(item.attributeValue("jid"), item.attributeValue("name")); // if(item.attributeValue("name").equals(ServiceNames.RootComponentName)) { // rootJID = item.attributeValue("jid"); // } else if(item.attributeValue("name").equals(ServiceNames.ExchangeComponentName)) { // exchangeJIDs.add(item.attributeValue("jid")); // } else if(item.attributeValue("name").equals(ServiceNames.GatewayComponentName)) { // gatewayJIDs.add(item.attributeValue("jid")); // } } // IntercloudDiscoItems discoItems = new IntercloudDiscoItems(rootJID, exchangeJIDs, gatewayJIDs); try { discoItems = discoItemExchanger.exchange(discoItems); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (ResourceContainerComponent.NAMESPACE_DISCO_INFO.equals(namespace)) { logger.info("discovery info result."); @SuppressWarnings("rawtypes") Iterator iter = childElement.elementIterator(); ArrayList<String> features = new ArrayList<String>(); while (iter.hasNext()) { Element feature = (Element) iter.next(); if (feature.getName().equals("feature")) { features.add(feature.attributeValue("var")); } } IntercloudDiscoFeatures discoFeatures = new IntercloudDiscoFeatures(iq.getFrom().toBareJID(), features); try { discoFeatures = discoFeatureExchanger.exchange(discoFeatures); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (ResourceContainerComponent.NAMESPACE_REST_XWADL.equals(namespace)) { logger.info("received xwadl iq."); try { handleRestXWADL(iq.getID(), XwadlDocument.Factory.parse(childElement.asXML())); } catch (XmlException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (ResourceContainerComponent.NAMESPACE_REST_XML.equals(namespace)) { logger.info("received rest xml iq."); try { handleRestXML(iq.getID(), RestDocument.Factory.parse(childElement.asXML())); } catch (XmlException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
From source file:de.xwic.sandbox.server.installer.XmlImport.java
License:Apache License
/** * @param root// w ww . jav a 2 s. c o m */ private void importPicklists(Element data) { IPicklisteDAO dao = (IPicklisteDAO) DAOSystem.getDAO(IPicklisteDAO.class); for (Iterator<?> it = data.elementIterator(); it.hasNext();) { Element elPl = (Element) it.next(); if (elPl.getName().equals("pickliste")) { String key = elPl.attributeValue("key"); IPickliste pickliste = (IPickliste) dao.createEntity(); pickliste.setKey(key); Element elBeschr = elPl.element("beschreibung"); if (elBeschr != null) { pickliste.setBeschreibung(elBeschr.getTextTrim()); } Element elTitle = elPl.element("title"); if (elTitle != null) { pickliste.setTitle(elTitle.getTextTrim()); } dao.update(pickliste); Element elEntries = elPl.element("entries"); if (elEntries != null) { for (Iterator<?> itEn = elEntries.elementIterator(); itEn.hasNext();) { Element elEntry = (Element) itEn.next(); if (elEntry.getName().equals("entry")) { IPicklistEntry entry = dao.createPickListEntry(pickliste); String oldId = elEntry.attributeValue("id"); if (oldId != null) { importedEntities.put( new EntityKey(IPicklistEntry.class.getName(), Integer.parseInt(oldId)), Integer.valueOf(entry.getId())); } boolean modified = false; if (elEntry.attributeValue("key") != null) { entry.setKey(elEntry.attributeValue("key")); modified = true; } if (elEntry.attributeValue("sortindex") != null) { entry.setSortIndex(Integer.parseInt(elEntry.attributeValue("sortindex"))); modified = true; } if (modified) { dao.update(entry); } for (Iterator<?> itT = elEntry.elementIterator(); itT.hasNext();) { Element elText = (Element) itT.next(); String langId = elText.attributeValue("lang"); dao.createBezeichnung(entry, langId, elText.getTextTrim()); } } } } } } }