List of usage examples for org.dom4j Element attributeValue
String attributeValue(QName qName);
From source file:com.bullx.heartbeat.Command.java
License:Open Source License
@SuppressWarnings("unchecked") public static List<Command> parse(Element e) { if (null == e) { return Collections.emptyList(); }//from ww w. j a v a2 s.c o m List<Element> commandNodeList = e.elements("command"); List<Command> retList = new ArrayList<Command>(); for (Element cmNode : commandNodeList) { Command c = new Command(); c.objid = cmNode.attributeValue("objid"); c.commandType = CommandType.valueOf(cmNode.attributeValue("type")); List<Element> actionNodeList = cmNode.elements(); for (Element a : actionNodeList) { c.action.put(a.attributeValue("name"), a.attributeValue("value")); } retList.add(c); } return retList; }
From source file:com.bullx.heartbeat.Result.java
License:Open Source License
public Result(Element e) { if (null == e) { return;/* w w w. j a v a 2 s .c o m*/ } this.code = Integer.valueOf(e.attributeValue("code")); if (0 != this.code) { Element errorXml = e.element("error"); if (null != errorXml) { this.errorcode = I2Error.codeOf(errorXml.attributeValue("errorcode")); List<Element> attrNodeList = errorXml.elements(); for (Element a : attrNodeList) { this.attr.put(a.attributeValue("name"), a.attributeValue("value")); } } } }
From source file:com.cc.framework.util.SettingUtils.java
License:Open Source License
/** * ?/*from w w w .jav a 2s . c om*/ * * @return */ public static Setting get() { Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME); net.sf.ehcache.Element cacheElement = cache.get(Setting.CACHE_KEY); Setting setting; if (cacheElement != null) { setting = (Setting) cacheElement.getObjectValue(); } else { setting = new Setting(); try { File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile(); Document document = new SAXReader().read(shopxxXmlFile); List<Element> elements = document.selectNodes("/shopxx/setting"); for (Element element : elements) { String name = element.attributeValue("name"); String value = element.attributeValue("value"); try { beanUtils.setProperty(setting, name, value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting)); } return setting; }
From source file:com.cc.framework.util.SettingUtils.java
License:Open Source License
/** * //from w ww . j a v a 2 s. c om * * @param setting * */ public static void set(Setting setting) { try { File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile(); Document document = new SAXReader().read(shopxxXmlFile); List<Element> elements = document.selectNodes("/shopxx/setting"); for (Element element : elements) { try { String name = element.attributeValue("name"); String value = beanUtils.getProperty(setting, name); Attribute attribute = element.attribute("value"); attribute.setValue(value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } FileOutputStream fileOutputStream = null; XMLWriter xmlWriter = null; try { OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setEncoding("UTF-8"); outputFormat.setIndent(true); outputFormat.setIndent(" "); outputFormat.setNewlines(true); fileOutputStream = new FileOutputStream(shopxxXmlFile); xmlWriter = new XMLWriter(fileOutputStream, outputFormat); xmlWriter.write(document); } catch (Exception e) { e.printStackTrace(); } finally { if (xmlWriter != null) { try { xmlWriter.close(); } catch (IOException e) { } } IOUtils.closeQuietly(fileOutputStream); } Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME); cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting)); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.chinarewards.license.util.XmlUtil_dom4j.java
public static String getAttributeValue(Element element, String attributeName) { String value = element.attributeValue(attributeName); return value; }
From source file:com.chingo247.settlercraft.structure.plan.PlanMenuManager.java
License:Open Source License
/** * Loads the PlanMenu from the menu.xml. If menu.xml doesn't exist, the menu.xml from the jar * will be written to the FileSystem./* w w w . jav a 2 s.com*/ * * @throws DocumentException When XML is invalid * @throws StructureAPIException When XML contains invalid data */ public final void init() throws DocumentException, StructureAPIException { File file = new File(PLUGIN_FOLDER, "menu.xml"); if (!file.exists()) { InputStream input = PlanMenuManager.class.getClassLoader() .getResourceAsStream(RESOURCE_FOLDER + "/menu.xml"); FileUtil.write(input, file); } Document d = new SAXReader().read(file); List<Node> rows = d.selectNodes("Menu/SlotRow"); if (rows.size() > 2) { throw new StructureAPIException("Max rows is 2 for menu.xml"); } planMenu = MenuAPI.createMenu(SettlerCraft.getInstance(), PLANSHOP_NAME, 54); boolean hasRow2 = false; for (int row = 0; row < rows.size(); row++) { Node rowNode = rows.get(row); List<Node> slotNodes = rowNode.selectNodes("Slot"); // Slot #1 is reserved for all category if (row == 0 && slotNodes.size() > 8) { throw new StructureAPIException(" 'SlotRow#1' has max 8 slots to customize"); } else if (slotNodes.size() > 9) { throw new StructureAPIException(" 'SlotRow#" + (row + 2) + "' has max 9 slots to customize"); } int count = 0; for (Node categorySlotNode : slotNodes) { if (!categorySlotNode.hasContent()) { count++; continue; } Node mId = categorySlotNode.selectSingleNode("MaterialID"); Node cat = categorySlotNode.selectSingleNode("Category"); Node ali = categorySlotNode.selectSingleNode("Aliases"); if (mId == null) { throw new StructureAPIException("Missing 'MaterialID' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } if (cat == null) { throw new StructureAPIException( "Missing 'Category' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } int id; try { id = Integer.parseInt(mId.getText()); } catch (NumberFormatException nfe) { throw new StructureAPIException("Invalid number for 'MaterialID' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } String category = cat.getText(); if (category.isEmpty()) { Element catEl = (Element) cat; category = catEl.attributeValue("value"); } if (category.trim().isEmpty()) { throw new StructureAPIException("Empty 'Category' element in 'SlotRow#" + (row + 1) + "' and 'Slot#" + (count + 1) + "'"); } category = category.replaceAll(" AND ", "&"); String[] aliases; if (ali == null) { aliases = new String[0]; } else { List<Node> aliasNodes = ali.selectNodes("Alias"); aliases = new String[aliasNodes.size()]; for (int j = 0; j < aliasNodes.size(); j++) { String alias = aliasNodes.get(j).getText(); if (alias.isEmpty()) { Element aliasEl = (Element) cat; alias = aliasEl.attributeValue("value"); } if (alias.trim().isEmpty()) { throw new StructureAPIException("Empty 'Alias' element in 'SlotRow#" + (row + 1) + "' and 'Slot#" + (count + 1) + "' and 'Alias#" + (j + 1) + "'"); } aliases[j] = aliasNodes.get(j).getText(); } } int slot = count; if (row == 0) { slot += 1; // slot 0 is reserved... } else { hasRow2 = true; } planMenu.putCategorySlot((row * 9) + slot, category, Material.getMaterial(id), aliases); count++; } // fill remaining if (count < 8 && row == 0) { for (int i = count; i < 8; i++) { planMenu.putLocked(i); } } else if (row > 0 && count < 9) { for (int i = count; i < 9; i++) { planMenu.putLocked((row * 9) + i); } } } if (hasRow2) { planMenu.putLocked(19, 20, 21, 22, 23, 24, 25); planMenu.putActionSlot(18, "Previous", Material.COAL_BLOCK); planMenu.putActionSlot(26, "Next", Material.COAL_BLOCK); } else { planMenu.putLocked(10, 11, 12, 13, 14, 15, 16); planMenu.putActionSlot(9, "Previous", Material.COAL_BLOCK); planMenu.putActionSlot(17, "Next", Material.COAL_BLOCK); } }
From source file:com.chingo247.structureapi.menus.plans.StructurePlanMenuReader.java
License:Open Source License
public CategoryMenu read(File file) throws DocumentException, SettlerCraftException { Preconditions.checkArgument(file.exists(), "File '" + file.getAbsolutePath() + "' does not exist!"); Document d = new SAXReader().read(file); CategoryMenu menu = new DefaultCategoryMenu("Buy & Build"); List<Node> rows = d.selectNodes("Menu/SlotRow"); if (rows.size() > 2) { throw new SettlerCraftException("Max rows is 2 for menu.xml"); }/*from w ww . j ava2s. com*/ boolean hasRow2 = false; for (int row = 0; row < rows.size(); row++) { Node rowNode = rows.get(row); List<Node> slotNodes = rowNode.selectNodes("Slot"); // Slot #1 is reserved for all category if (row == 0 && slotNodes.size() > 8) { throw new SettlerCraftException(" 'SlotRow#1' has max 8 slots to customize"); } else if (slotNodes.size() > 9) { throw new SettlerCraftException(" 'SlotRow#" + (row + 2) + "' has max 9 slots to customize"); } int count = 0; for (Node categorySlotNode : slotNodes) { if (!categorySlotNode.hasContent()) { count++; continue; } Node mId = categorySlotNode.selectSingleNode("MaterialID"); Node cat = categorySlotNode.selectSingleNode("Category"); // Node ali = categorySlotNode.selectSingleNode("Aliases"); if (mId == null) { throw new SettlerCraftException("Missing 'MaterialID' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } if (cat == null) { throw new SettlerCraftException( "Missing 'Category' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } int id; try { id = Integer.parseInt(mId.getText()); } catch (NumberFormatException nfe) { throw new SettlerCraftException("Invalid number for 'MaterialID' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } Node catNameNode = cat.selectSingleNode("Name"); if (catNameNode == null) { throw new SettlerCraftException( "Missing 'Name' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'"); } String category = catNameNode.getText(); if (category.isEmpty()) { Element catEl = (Element) cat; category = catEl.attributeValue("value"); } if (category.trim().isEmpty()) { throw new SettlerCraftException("Empty 'Category' element in 'SlotRow#" + (row + 1) + "' and 'Slot#" + (count + 1) + "'"); } category = category.replaceAll(" AND ", "&"); Node synonymsNode = cat.selectSingleNode("Synonyms"); // Set aliases String[] synonyms; if (synonymsNode == null) { synonyms = new String[0]; } else { List<Node> synonymNodes = synonymsNode.selectNodes("Synonym"); synonyms = new String[synonymNodes.size()]; for (int j = 0; j < synonymNodes.size(); j++) { String synonym = synonymNodes.get(j).getText(); if (synonym.isEmpty()) { Element synoEl = (Element) cat; synonym = synoEl.attributeValue("value"); } if (synonym.trim().isEmpty()) { throw new SettlerCraftException("Empty 'Synonym' element in 'SlotRow#" + (row + 1) + "' and 'Slot#" + (count + 1) + "' and 'Synonym#" + (j + 1) + "'"); } synonyms[j] = synonymNodes.get(j).getText(); } } int slot = count; if (row == 0) { slot += 1; // slot 0 is reserved... } else { hasRow2 = true; } CategorySlot categorySlot = SlotFactory.getInstance().createCategorySlot(category, id); categorySlot.addSynonyms(synonyms); menu.setCategorySlot((row * 9) + slot, categorySlot); count++; } // fill remaining if (count < 8 && row == 0) { for (int i = count; i < 8; i++) { menu.setLocked(i); } } else if (row > 0 && count < 9) { for (int i = count; i < 9; i++) { menu.setLocked((row * 9) + i); } } } if (hasRow2) { menu.setLocked(19, 20, 21, 22, 23, 24, 25); menu.setActionSlot(18, "Previous", 173); // block of coal menu.setActionSlot(26, "Next", 173); } else { menu.setLocked(10, 11, 12, 13, 14, 15, 16); menu.setActionSlot(9, "Previous", 173); menu.setActionSlot(17, "Next", 173); } return menu; }
From source file:com.chingo247.structureapi.plan.io.document.StructurePlanDocument.java
License:Open Source License
private Flag<?> makeFlag(LineElement node) { String name;/*from w w w. jav a2s . c o m*/ String value; Element e = node.getElement(); if (e.attribute("name") != null) { if (e.attribute("value") == null) { throw new PlanException("No 'value' attribute for element in '" + getFile().getAbsolutePath() + "' on line " + node.getLine()); } name = e.attributeValue("name"); value = e.attributeValue("value"); } else if (e.selectSingleNode("Name") != null) { if (e.selectSingleNode("Name") == null) { throw new PlanException("No 'Value' element within element of '" + getFile().getAbsolutePath() + "' on line " + node.getLine()); } Node nameNode = e.selectSingleNode("Name"); Node valueNode = e.selectSingleNode("Value"); name = nameNode.getStringValue(); value = valueNode.getStringValue(); } else { throw new PlanException("Invalid flag element in '" + getFile().getAbsolutePath() + "' on line " + node.getLine() + ": No 'Name' or 'Value' defined"); } if (name.trim().isEmpty()) { throw new PlanException( "Name was emtpy in '" + getFile().getAbsolutePath() + "' on line " + node.getLine()); } if (value.trim().isEmpty()) { throw new PlanException( "Value was emtpy in '" + getFile().getAbsolutePath() + "' on line " + node.getLine()); } if (NumberUtils.isNumber(value)) { try { double d = Double.parseDouble(value); return new DoubleFlag(name, d); } catch (NumberFormatException nfe) { Integer i = Integer.parseInt(value); return new IntegerFlag(name, i); } } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true")) { return new BooleanFlag(name, value.equalsIgnoreCase("true")); } else { return new StringFlag(name, value); } }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
/** * With all of our namespace woes, getting an XPath expression * to work has proven futile, so we'll recurse through the tree * ourselves to find what we need./*from ww w . j a v a2 s. c om*/ */ private void findSlots(Element element, Map slots) { //System.err.println( "element: " + element.getName() ); for (Iterator i = element.attributes().iterator(); i.hasNext();) { Attribute attribute = (Attribute) i.next(); //System.err.println( "\t" + attribute.getName() + "\t" + attribute.getQualifiedName() ); } // Look for our attribute //String qualifiedAttributeName = this.metalNamespacePrefix + ":fill-slot"; //String name = element.attributeValue( qualifiedAttributeName ); String name = element.attributeValue("fill-slot"); if (name != null) { slots.put(name, new SlotImpl(element)); } // Recurse into child elements for (Iterator i = element.elementIterator(); i.hasNext();) { findSlots((Element) i.next(), slots); } }
From source file:com.christophermrossi.jpt.PageTemplateImpl.java
License:Open Source License
/** * With all of our namespace woes, getting an XPath expression * to work has proven futile, so we'll recurse through the tree * ourselves to find what we need./*w ww.j av a2 s . c om*/ */ private void findMacros(Element element, Map macros) { // Process any declared namespaces for (Iterator i = element.declaredNamespaces().iterator(); i.hasNext();) { Namespace namespace = (Namespace) i.next(); namespaces.put(namespace.getPrefix(), namespace.getURI()); //if ( namespace.getURI().equals( TAL_NAMESPACE_URI ) ) { // this.talNamespacePrefix = namespace.getPrefix(); //} //else if ( namespace.getURI().equals( METAL_NAMESPACE_URI ) ) { // this.metalNamespacePrefix = namespace.getPrefix(); //} } // Look for our attribute //String qualifiedAttributeName = this.metalNamespacePrefix + ":define-macro"; //String name = element.attributeValue( qualifiedAttributeName ); String name = element.attributeValue("define-macro"); //if ( name == null ) { // name = element.attributeValue // ( new QName( "define-macro", new Namespace( metalNamespacePrefix, METAL_NAMESPACE_URI ) ) ); //} if (name != null) { macros.put(name, new MacroImpl(element)); } // Recurse into child elements for (Iterator i = element.elementIterator(); i.hasNext();) { findMacros((Element) i.next(), macros); } }