@Override protected void readData(Element rootElement) throws Exception { for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext(); ) { Element e = iterator.next(); if ("fish".equals(e.getName())) { MultiValueSet<String> map = new MultiValueSet<String>(); for (Iterator<Attribute> attributeIterator = e.attributeIterator(); attributeIterator.hasNext(); ) { Attribute attribute = attributeIterator.next(); map.put(attribute.getName(), attribute.getValue()); } getHolder().addFish(new FishTemplate(map)); } else if ("lure".equals(e.getName())) { MultiValueSet<String> map = new MultiValueSet<String>(); for (Iterator<Attribute> attributeIterator = e.attributeIterator(); attributeIterator.hasNext(); ) { Attribute attribute = attributeIterator.next(); map.put(attribute.getName(), attribute.getValue()); } Map<FishGroup, Integer> chances = new HashMap<FishGroup, Integer>(); for (Iterator<Element> elementIterator = e.elementIterator(); elementIterator.hasNext(); ) { Element chanceElement = elementIterator.next(); chances.put( FishGroup.valueOf(chanceElement.attributeValue("type")), Integer.parseInt(chanceElement.attributeValue("value"))); } map.put("chances", chances); getHolder().addLure(new LureTemplate(map)); } else if ("distribution".equals(e.getName())) { int id = Integer.parseInt(e.attributeValue("id")); for (Iterator<Element> forLureIterator = e.elementIterator(); forLureIterator.hasNext(); ) { Element forLureElement = forLureIterator.next(); LureType lureType = LureType.valueOf(forLureElement.attributeValue("type")); Map<FishGroup, Integer> chances = new HashMap<FishGroup, Integer>(); for (Iterator<Element> chanceIterator = forLureElement.elementIterator(); chanceIterator.hasNext(); ) { Element chanceElement = chanceIterator.next(); chances.put( FishGroup.valueOf(chanceElement.attributeValue("type")), Integer.parseInt(chanceElement.attributeValue("value"))); } getHolder().addDistribution(id, lureType, chances); } } } }
/** * Method parseUsingCondition. * * @param element Element * @return Condition */ private Condition parseUsingCondition(Element element) { Condition cond = null; for (Iterator<Attribute> iterator = element.attributeIterator(); iterator.hasNext(); ) { Attribute attribute = iterator.next(); String name = attribute.getName(); String value = attribute.getValue(); if (name.equalsIgnoreCase("slotitem")) { StringTokenizer st = new StringTokenizer(value, ";"); int id = Integer.parseInt(st.nextToken().trim()); int slot = Integer.parseInt(st.nextToken().trim()); int enchant = 0; if (st.hasMoreTokens()) { enchant = Integer.parseInt(st.nextToken().trim()); } cond = joinAnd(cond, new ConditionSlotItemId(slot, id, enchant)); } else if (name.equalsIgnoreCase("kind") || name.equalsIgnoreCase("weapon")) { long mask = 0; StringTokenizer st = new StringTokenizer(value, ","); tokens: while (st.hasMoreTokens()) { String item = st.nextToken().trim(); for (WeaponTemplate.WeaponType wt : WeaponTemplate.WeaponType.VALUES) { if (wt.toString().equalsIgnoreCase(item)) { mask |= wt.mask(); continue tokens; } } for (ArmorTemplate.ArmorType at : ArmorTemplate.ArmorType.VALUES) { if (at.toString().equalsIgnoreCase(item)) { mask |= at.mask(); continue tokens; } } error("Invalid item kind: \"" + item + "\" in " + getCurrentFileName()); } if (mask != 0) { cond = joinAnd(cond, new ConditionUsingItemType(mask)); } } else if (name.equalsIgnoreCase("skill")) { cond = joinAnd(cond, new ConditionUsingSkill(Integer.parseInt(value))); } } return cond; }
/** * Convert dom4j attributes to SAX attributes. * * @param element dom4j Element * @return SAX Attributes */ public static Attributes getSAXAttributes(Element element) { final AttributesImpl result = new AttributesImpl(); for (Iterator i = element.attributeIterator(); i.hasNext(); ) { final org.dom4j.Attribute attribute = (org.dom4j.Attribute) i.next(); result.addAttribute( attribute.getNamespaceURI(), attribute.getName(), attribute.getQualifiedName(), ContentHandlerHelper.CDATA, attribute.getValue()); } return result; }
/** * Method parseZoneCondition. * * @param element Element * @return Condition */ private Condition parseZoneCondition(Element element) { Condition cond = null; for (Iterator<Attribute> iterator = element.attributeIterator(); iterator.hasNext(); ) { Attribute attribute = iterator.next(); String name = attribute.getName(); String value = attribute.getValue(); if (name.equalsIgnoreCase("type")) { cond = joinAnd(cond, new ConditionZoneType(value)); } } return cond; }
/** * Method parseTargetCondition. * * @param element Element * @return Condition */ private Condition parseTargetCondition(Element element) { Condition cond = null; for (Iterator<Attribute> iterator = element.attributeIterator(); iterator.hasNext(); ) { Attribute attribute = iterator.next(); String name = attribute.getName(); String value = attribute.getValue(); if (name.equalsIgnoreCase("pvp")) { cond = joinAnd(cond, new ConditionTargetPlayable(Boolean.valueOf(value))); } } return cond; }
/** * Method parsePlayerCondition. * * @param element Element * @return Condition */ private Condition parsePlayerCondition(Element element) { Condition cond = null; for (Iterator<Attribute> iterator = element.attributeIterator(); iterator.hasNext(); ) { Attribute attribute = iterator.next(); String name = attribute.getName(); String value = attribute.getValue(); if (name.equalsIgnoreCase("residence")) { String[] st = value.split(";"); cond = joinAnd( cond, new ConditionPlayerResidence( Integer.parseInt(st[1]), ResidenceType.valueOf(st[0]))); } else if (name.equalsIgnoreCase("classId")) { cond = joinAnd(cond, new ConditionPlayerClassId(value.split(","))); } else if (name.equalsIgnoreCase("olympiad")) { cond = joinAnd(cond, new ConditionPlayerOlympiad(Boolean.valueOf(value))); } else if (name.equalsIgnoreCase("instance_zone")) { cond = joinAnd(cond, new ConditionPlayerInstanceZone(Integer.parseInt(value))); } else if (name.equalsIgnoreCase("race")) { cond = joinAnd(cond, new ConditionPlayerRace(value)); } else if (name.equalsIgnoreCase("damage")) { String[] st = value.split(";"); cond = joinAnd( cond, new ConditionPlayerMinMaxDamage( Double.parseDouble(st[0]), Double.parseDouble(st[1]))); } else if (name.equalsIgnoreCase("castleLight")) { cond = joinAnd(cond, new ConditionCastleLight()); } else if (name.equalsIgnoreCase("castleLightClanLeader")) { cond = joinAnd(cond, new ConditionCastleLightClanLeader()); } else if (name.equalsIgnoreCase("castleDark")) { cond = joinAnd(cond, new ConditionCastleDark()); } else if (name.equalsIgnoreCase("castleDarkClanLeader")) { cond = joinAnd(cond, new ConditionCastleDarkClanLeader()); } } return cond; }
/* 538: */ /* 539: */ protected Attributes createAttributes( Element element, Attributes namespaceAttributes) /* 540: */ throws SAXException /* 541: */ { /* 542:822 */ this.attributes.clear(); /* 543:824 */ if (namespaceAttributes != null) { /* 544:825 */ this.attributes.setAttributes(namespaceAttributes); /* 545: */ } /* 546:828 */ for (Iterator iter = element.attributeIterator(); iter.hasNext(); ) /* 547: */ { /* 548:829 */ Attribute attribute = (Attribute) iter.next(); /* 549:830 */ this.attributes.addAttribute( attribute.getNamespaceURI(), attribute.getName(), attribute.getQualifiedName(), "CDATA", attribute.getValue()); /* 550: */ } /* 551:835 */ return this.attributes; /* 552: */ }
private void addEntity( String pkg, Element entityElement, Object parentEntity, String callString) { try { // 根据包名和类名,得到全路径类名 String clz = pkg + "." + entityElement.attributeValue("class"); // 根据全路径类名,创建实体对象 Object entity = Class.forName(clz).newInstance(); // 给entity对象赋值 // 即提取出当前Element中的所有属性,并用反射机制给entity对象赋值 Iterator iterator = entityElement.attributeIterator(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); String propertyName = attribute.getName(); if (!propertyName.equals("class") && !propertyName.equals("call") && !propertyName.equals("parentName")) { String propertyValue = attribute.getValue(); // 给entity相应的属性赋值,这里使用的是apache-commons-beanutils工具包,所以需要加入这个依赖包 BeanUtils.copyProperty(entity, propertyName, propertyValue); } } /** * 提取出当前Element下面所有的非entity元素,这些元素也当作是本Entity属性的一部分 举个例子: <entity class="Form" name="请假单" * content="这里是请假单的内容" ...></entity> 像上面这样定义当然是可以的,但是有些属性的值可能会包含特殊字符,或者内容比较庞大,所以,可能 * 像下面这样来定义更加合理,如: <entity class="Form" name="请假单" ...> <content> <![CDATA[ * * <p>这是一个请假单 请假者姓名:<input type="text" name="leaverName" > .... ]]> </content> </entity> * 这样,比较复杂的文本属性,也可以定义 */ // 查找本元素下所有名称不是entity的元素 List subPropertyElements = entityElement.selectNodes("*[name()!='entity']"); if (subPropertyElements != null && !subPropertyElements.isEmpty()) for (Iterator iterator2 = subPropertyElements.iterator(); iterator2.hasNext(); ) { Element e = (Element) iterator2.next(); String propertyName = e.getName(); String propertyValue = e.getText(); BeanUtils.copyProperty(entity, propertyName, propertyValue); } // 判断parentEntity是否为空,如果不是空,则给parent对象赋值 if (parentEntity != null) { String parentName = entityElement.attributeValue("parentName"); if (parentName == null) { // 如果不配置父属性的名称,默认为parent parentName = "parent"; } parentName = StringUtils.capitalize(parentName); // 首字母变成大写 String setParentName = "set" + parentName; Method[] ms = entity.getClass().getMethods(); for (Method m : ms) { if (m.getName().equals(setParentName)) { m.invoke(entity, parentEntity); } } } // 要调用哪个服务对象的哪个方法 String call = entityElement.attributeValue("call"); if (call != null) { callString = call; } if (callString == null) { throw new RuntimeException("没有找到call属性,无法获知要调用哪个服务对象的哪个方法!请配置call属性!"); } // 得到服务对象的ID String serviceId = callString.substring(0, callString.indexOf(".")); // 得到要调用的方法名称 String methodName = callString.substring(callString.indexOf(".") + 1); // 通过BeanFactory得到服务对象 Object service = factory.getBean(serviceId); // 得到service对象的所有方法 Method[] ms = service.getClass().getMethods(); for (Method m : ms) { if (m.getName().equals(methodName)) { // 调用其中我们想要调用的方法 m.invoke(service, entity); } } // 判断当前entity下面是否还有其他的entity元素 List subEntityElements = entityElement.elements("entity"); for (Iterator iterator2 = subEntityElements.iterator(); iterator2.hasNext(); ) { Element e = (Element) iterator2.next(); // 递归插入本entity元素下面的其它entity对象 addEntity(pkg, e, entity, callString); } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
public static Config getConfigs(InputStream in) { Document document = loadXML(in); if (document == null) return null; Element elementRoot = document.getRootElement(); Config config = new Config(); Element elementParent = elementRoot.element("host"); if (elementParent == null) return null; String tmp = elementParent.getText(); if (tmp == null || tmp.length() == 0) return null; config.setHost(tmp); elementParent = elementRoot.element("clientv"); if (elementParent == null) return null; tmp = elementParent.getText(); if (tmp == null || tmp.length() == 0) return null; config.setClientv(tmp); elementParent = elementRoot.element("userid"); if (elementParent == null) return null; tmp = elementParent.getText(); if (!Numeric.isNumber(tmp)) return null; try { config.setUserId(Long.parseLong(tmp)); } catch (Exception e) { return null; } elementParent = elementRoot.element("cookie"); if (elementParent == null) return null; tmp = elementParent.getText(); if (tmp == null || tmp.length() == 0) return null; config.setCookie(tmp); elementParent = elementRoot.element("autotowns"); if (elementParent == null) return null; tmp = elementParent.getText(); if (!Numeric.isNumber(tmp)) return null; try { config.setAutoTowns(Long.parseLong(tmp)); } catch (NumberFormatException e) { return null; } elementParent = elementRoot.element("equipment"); if (elementParent != null) { Element equipmentMax = elementParent.element("max"); Element equipmentTowns = elementParent.element("towns"); if (equipmentMax != null && equipmentTowns != null) { tmp = equipmentMax.getText(); if (Numeric.isNumber(tmp)) { try { Long max = Long.parseLong(tmp); tmp = equipmentTowns.getText(); if (max > 0 && tmp != null) { List<Long> towns = new Vector<Long>(); if (tmp.indexOf(",") > -1) { String[] arrays = tmp.split(","); for (String array : arrays) { if (Numeric.isNumber(array)) towns.add(Long.parseLong(array)); } } else { if (Numeric.isNumber(tmp)) towns.add(Long.parseLong(tmp)); } if (towns.size() > 0) { config.setEquipmentMax(max); config.setEquipmentTowns(towns); } } } catch (NumberFormatException e) { } } } } Iterator<Element> elements = elementRoot.elementIterator("towns"); if (elements != null) { List<ConfigTown> configTowns = new Vector<ConfigTown>(); while (elements.hasNext()) { try { elementParent = elements.next(); ConfigTown configTown = new ConfigTown(); Attribute attribute = elementParent.attribute("id"); if (attribute == null) continue; tmp = attribute.getText(); if (!Numeric.isNumber(tmp)) continue; configTown.setTownId(Long.parseLong(tmp)); Element element = elementParent.element("autoupgrade"); if (element != null && element.getText() != null) { tmp = element.getText(); if (tmp.equals("true")) { Attribute priority = element.attribute("priority"); if (priority != null && priority.getText() != null) { tmp = priority.getText(); if (tmp.indexOf(",") > -1) { String[] arrays = tmp.split(","); if (arrays != null) { List<Long> prioritys = new Vector<Long>(); for (String array : arrays) { if (Numeric.isNumber(array)) prioritys.add(Long.parseLong(array)); } if (prioritys.size() > 0) { configTown.setUpgradePriority(prioritys); configTown.setAutoUpgrade(true); } else configTown.setAutoUpgrade(false); } else configTown.setAutoUpgrade(false); } else { if (Numeric.isNumber(tmp)) { List<Long> prioritys = new Vector<Long>(); prioritys.add(Long.parseLong(tmp)); configTown.setUpgradePriority(prioritys); configTown.setAutoUpgrade(true); } else configTown.setAutoUpgrade(false); } } else configTown.setAutoUpgrade(false); } else configTown.setAutoUpgrade(false); } else configTown.setAutoUpgrade(false); element = elementParent.element("autoattack"); if (element != null && element.getText() != null) { tmp = element.getText(); if (tmp != null && tmp.equals("true")) { Attribute level = element.attribute("level"); if (level != null && level.getText() != null) { tmp = level.getText(); if (tmp.indexOf("-") > -1) { String[] array = tmp.split("-"); if (array != null && array.length == 2 && Numeric.isNumber(array[0]) && Numeric.isNumber(array[1])) { configTown.setAutoAttack(true); configTown.setAttackLevelMin(Long.parseLong(array[0])); configTown.setAttackLevelMax(Long.parseLong(array[1])); } else configTown.setAutoAttack(false); } else configTown.setAutoAttack(false); } else configTown.setAutoAttack(false); } else configTown.setAutoAttack(false); } element = elementParent.element("autorecruit"); if (element != null && element.getText() != null) { tmp = element.getText(); if (tmp.equals("true")) configTown.setAutoRecruit(true); else configTown.setAutoRecruit(false); } else configTown.setAutoRecruit(false); element = elementParent.element("sells"); if (element != null && element.getText() != null) { tmp = element.getText(); Iterator<Attribute> attributes = element.attributeIterator(); if (attributes != null) { HashMap<String, Double> sells = new HashMap<String, Double>(); while (attributes.hasNext()) { attribute = attributes.next(); String name = attribute.getName(); String value = attribute.getText(); if (!Numeric.isNumber(value)) continue; Double rate = Double.parseDouble(value) / 100D; sells.put(name, rate); } if (sells.size() > 0) configTown.setSells(sells); if (tmp.equals("true")) configTown.setSell(true); } } element = elementParent.element("buys"); if (element != null && element.getText() != null) { tmp = element.getText(); Iterator<Attribute> attributes = element.attributeIterator(); if (attributes != null) { HashMap<String, Double> buys = new HashMap<String, Double>(); while (attributes.hasNext()) { attribute = attributes.next(); String name = attribute.getName(); String value = attribute.getText(); if (!Numeric.isNumber(value)) continue; Double rate = Double.parseDouble(value) / 100D; buys.put(name, rate); } if (buys.size() > 0) configTown.setBuys(buys); if (tmp.equals("true")) configTown.setBuy(true); } } configTowns.add(configTown); } catch (NumberFormatException e) { } } config.setConfigTowns(configTowns); } return config; }
/** * @方法功能描述:得到指定节点的属性的迭代器 @方法名:getAttrIterator * * @param e @返回类型:Iterator<Attribute> */ @SuppressWarnings("unchecked") public static Iterator<Attribute> getAttrIterator(Element e) { if (e == null) return null; Iterator<Attribute> attrIterator = e.attributeIterator(); return attrIterator; }
public void parseAttribute(Element element, ElementNode treeNode) { HashMap userObject = null; for (Iterator attributeIterator = element.attributeIterator(); attributeIterator.hasNext(); ) { Attribute attribute = (Attribute) attributeIterator.next(); String attributeName = attribute.getName().trim(); String attributeText = attribute.getText().trim(); if (attributeName.equals(indexTag)) { treeNode.setIndex(Integer.parseInt(attributeText)); } else if (attributeName.equals(nameTag)) { treeNode.setName(attributeText); } else if (attributeName.equals(textTag)) { treeNode.setText(attributeText); } else if (attributeName.equals(iconTag)) { String iconName = attributeText + (iconExtensionName != null ? iconExtensionName : ""); switch (iconFolderMode) { case ICON_FOLDER_MODE_SWING: treeNode.setIcon(IconFactory.getSwingIcon(iconName)); break; case ICON_FOLDER_MODE_CONTEXT: treeNode.setIcon(IconFactory.getContextIcon(iconName)); break; case ICON_FOLDER_MODE_FULL: treeNode.setIcon(IconFactory.getIcon(iconName)); break; default: treeNode.setIcon(IconFactory.getContextIcon(iconName)); break; } } else if (attributeName.equals(toolTipTextTag)) { treeNode.setToolTipText(attributeText); } else if (attributeName.equals(classTag)) { if (userObject == null) { userObject = new HashMap(); treeNode.setUserObject(userObject); } Object instance = null; try { instance = Class.forName(attributeText).newInstance(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } userObject.put(classTag, instance); } else { if (userObject == null) { userObject = new HashMap(); treeNode.setUserObject(userObject); } userObject.put(attributeName, attributeText); } if (treeNode.getText() == null && treeNode.getName() != null) { treeNode.setText(treeNode.getName()); } if (treeNode.getToolTipText() == null && treeNode.getName() != null) { treeNode.setToolTipText(treeNode.getName()); } } }