Example usage for org.dom4j Element elementIterator

List of usage examples for org.dom4j Element elementIterator

Introduction

In this page you can find the example usage for org.dom4j Element elementIterator.

Prototype

Iterator<Element> elementIterator();

Source Link

Document

Returns an iterator over all this elements child elements.

Usage

From source file:javaapplication3.DataXmlWorker.java

private void setXmlData(ShortBuffer mem_data) {
    SAXReader reader = new SAXReader();

    File f = new File(file_name_from_unit_);

    if (!f.exists()) {
        System.out.println("no data file " + file_name_from_unit_);
    }/*from   w w  w.j a  v a 2  s.  c  o m*/

    try {
        Document document = reader.read(f);

        Element root = document.getRootElement().element("map");

        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();

            //System.out.println("addr[ " + element.attributeValue("addr") + "] = " + element.attributeValue("value"));

            int mem_adr = Integer.parseInt(element.attributeValue("addr")) - mem_shift_;
            //System.out.println("mem_adr = " + mem_adr);

            if (mem_adr < mem_data.limit()) {
                int data = mem_data.get(mem_adr);
                if (data < 0)
                    data += 65536;
                //System.out.println("in put attr to xml int = " + data);
                element.attribute("value").setValue(Integer.toString(data));
            }
        }

        //            XMLWriter writer = new XMLWriter( new FileWriter( "output.xml" ));
        XMLWriter writer = new XMLWriter(new FileWriter(file_name_from_unit_));
        writer.write(document);
        writer.close();

    } catch (DocumentException ex) {
        System.out.println("Exeption: " + ex.getMessage());
        Logger.getLogger(DataXmlWorker.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        System.out.println("Exeption: " + ex.getMessage());
        Logger.getLogger(DataXmlWorker.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:javaapplication3.TimeDataXmlWorker.java

private ShortBuffer getXmlData(ShortBuffer mem_data) {

    ShortBuffer exitData = ShortBuffer.allocate(mem_data.limit());
    exitData.put(mem_data.array());//w  w w.j  av  a2 s.  c  om

    //for (char ii = 0; ii < exitData.limit(); ii++){
    //    System.out.println("exitData[" + ii + "]=" + exitData.get(ii));
    //}

    SAXReader reader = new SAXReader();

    File f = new File(file_name_);

    if (!f.exists()) {
        System.out.println("no time data file " + file_name_);
    }

    try {
        Document document = reader.read(f);

        Element root = document.getRootElement();

        // iterate through child elements of root
        for (Iterator i = root.elementIterator(); i.hasNext();) {
            Element element = (Element) i.next();

            //System.out.println("addr[ " + element.attributeValue("addr") + "] = " + element.attributeValue("value"));

            int mem_adr = Integer.parseInt(element.attributeValue("addr")) - mem_shift_;
            if (mem_adr < mem_data.limit())
                exitData.put(mem_adr, Short.parseShort(element.attributeValue("value")));

        }

        return exitData;
    } catch (DocumentException ex) {
        System.out.println("Exeption: " + ex.getMessage());
        Logger.getLogger(DataXmlWorker.class.getName()).log(Level.SEVERE, null, ex);
    }

    return exitData;
}

From source file:job.tot.xml.DOM4JConfiguration.java

License:Apache License

/**
 * Loads and initializes from the XML file.
 *
 * @param element The element to start processing from.  Callers
 * should supply the root element of the document.
 * @param hierarchy//www  .  j  a  va2 s  .  c o m
 */
private void initProperties(Element element, StringBuffer hierarchy) {
    for (Iterator it = element.elementIterator(); it.hasNext();) {
        StringBuffer subhierarchy = new StringBuffer(hierarchy.toString());
        Element child = (Element) it.next();
        String nodeName = child.getName();
        String nodeValue = child.getTextTrim();
        subhierarchy.append(nodeName);
        if (nodeValue.length() > 0) {
            super.addProperty(subhierarchy.toString(), nodeValue);
        }

        // Add attributes as x.y{ATTRIB_START_MARKER}att{ATTRIB_END_MARKER}
        List attributes = child.attributes();
        for (int j = 0, k = attributes.size(); j < k; j++) {
            Attribute a = (Attribute) attributes.get(j);
            String attName = subhierarchy.toString() + '[' + ATTRIB_MARKER + a.getName() + ']';
            String attValue = a.getValue();
            super.addProperty(attName, attValue);
        }
        StringBuffer buf = new StringBuffer(subhierarchy.toString());
        initProperties(child, buf.append('.'));
    }
}

From source file:lineage2.gameserver.data.xml.parser.AirshipDockParser.java

License:Open Source License

/**
 * Method readData.//from  w  ww .  j a  v  a2  s .  c  o  m
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element dockElement = (Element) iterator.next();
        int id = Integer.parseInt(dockElement.attributeValue("id"));
        List<BoatPoint> teleportList = parsePoints(dockElement.element("teleportlist"));

        for (BoatPoint point : teleportList) {
            point.setTeleport(true);
            point.setSpeed1(-1);
            point.setSpeed2(-1);
        }

        List<AirshipDock.AirshipPlatform> platformList = new ArrayList<>(2);

        for (Iterator<?> platformIterator = dockElement.elementIterator("platform"); platformIterator
                .hasNext();) {
            Element platformElement = (Element) platformIterator.next();
            SceneMovie movie = SceneMovie.valueOf(platformElement.attributeValue("movie"));
            BoatPoint oustLoc = BoatPoint.parse(platformElement.element("oust"));
            BoatPoint spawnLoc = BoatPoint.parse(platformElement.element("spawn"));
            List<BoatPoint> arrivalList = parsePoints(platformElement.element("arrival"));
            List<BoatPoint> departList = parsePoints(platformElement.element("depart"));
            AirshipDock.AirshipPlatform platform = new AirshipDock.AirshipPlatform(movie, oustLoc, spawnLoc,
                    arrivalList, departList);
            platformList.add(platform);
        }

        getHolder().addDock(new AirshipDock(id, teleportList, platformList));
    }
}

From source file:lineage2.gameserver.data.xml.parser.AirshipDockParser.java

License:Open Source License

/**
 * Method parsePoints.//www.  j  ava 2s  . co m
 * @param listElement Element
 * @return List<BoatPoint>
 */
private List<BoatPoint> parsePoints(Element listElement) {
    if (listElement == null) {
        return Collections.emptyList();
    }

    List<BoatPoint> list = new ArrayList<>(5);

    for (Iterator<?> iterator = listElement.elementIterator(); iterator.hasNext();) {
        list.add(BoatPoint.parse((Element) iterator.next()));
    }

    return list.isEmpty() ? Collections.<BoatPoint>emptyList() : list;
}

From source file:lineage2.gameserver.data.xml.parser.ArmorSetsParser.java

License:Open Source License

/**
 * Method readData.//from   w  w w.  j a  v a2  s  .  co  m
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element element = iterator.next();

        if ("set".equalsIgnoreCase(element.getName())) {
            String[] chests = null, legs = null, head = null, gloves = null, feet = null, shield = null,
                    shield_skills = null, enchant6skills = null;

            if (element.attributeValue("chests") != null) {
                chests = element.attributeValue("chests").split(";");
            }

            if (element.attributeValue("legs") != null) {
                legs = element.attributeValue("legs").split(";");
            }

            if (element.attributeValue("head") != null) {
                head = element.attributeValue("head").split(";");
            }

            if (element.attributeValue("gloves") != null) {
                gloves = element.attributeValue("gloves").split(";");
            }

            if (element.attributeValue("feet") != null) {
                feet = element.attributeValue("feet").split(";");
            }

            if (element.attributeValue("shield") != null) {
                shield = element.attributeValue("shield").split(";");
            }

            if (element.attributeValue("shield_skills") != null) {
                shield_skills = element.attributeValue("shield_skills").split(";");
            }

            if (element.attributeValue("enchant6skills") != null) {
                enchant6skills = element.attributeValue("enchant6skills").split(";");
            }

            ArmorSet armorSet = new ArmorSet(chests, legs, head, gloves, feet, shield, shield_skills,
                    enchant6skills);

            for (Iterator<Element> subIterator = element.elementIterator(); subIterator.hasNext();) {
                Element subElement = subIterator.next();

                if ("set_skills".equalsIgnoreCase(subElement.getName())) {
                    int partsCount = Integer.parseInt(subElement.attributeValue("parts"));
                    String[] skills = subElement.attributeValue("skills").split(";");
                    armorSet.addSkills(partsCount, skills);
                }
            }

            getHolder().addArmorSet(armorSet);
        }
    }
}

From source file:lineage2.gameserver.data.xml.parser.ClassDataParser.java

License:Open Source License

@Override
protected void readData(Element rootElement) throws Exception {
    for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element element = iterator.next();
        int classId = Integer.parseInt(element.attributeValue("class_id"));
        ClassData template = new ClassData(classId);

        for (Iterator<Element> subIterator = element.elementIterator(); subIterator.hasNext();) {
            Element subElement = subIterator.next();

            if ("lvl_up_data".equalsIgnoreCase(subElement.getName())) {
                for (Element e : subElement.elements()) {
                    int lvl = Integer.parseInt(e.attributeValue("lvl"));
                    double hp = Double.parseDouble(e.attributeValue("hp"));
                    double mp = Double.parseDouble(e.attributeValue("mp"));
                    double cp = Double.parseDouble(e.attributeValue("cp"));
                    template.addLvlUpData(lvl, hp, mp, cp);
                }// www. ja va  2  s  . c o m
            }
        }

        getHolder().addClassData(template);
    }
}

From source file:lineage2.gameserver.data.xml.parser.CubicParser.java

License:Open Source License

/**
 * Method readData.//from www .j a  v a 2s  . c o m
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element cubicElement = (Element) iterator.next();
        int id = Integer.parseInt(cubicElement.attributeValue("id"));
        int level = Integer.parseInt(cubicElement.attributeValue("level"));
        int delay = Integer.parseInt(cubicElement.attributeValue("delay"));
        CubicTemplate template = new CubicTemplate(id, level, delay);
        getHolder().addCubicTemplate(template);

        for (Iterator<?> skillsIterator = cubicElement.elementIterator(); skillsIterator.hasNext();) {
            Element skillsElement = (Element) skillsIterator.next();
            int chance = Integer.parseInt(skillsElement.attributeValue("chance"));
            List<CubicTemplate.SkillInfo> skills = new ArrayList<>(1);

            for (Iterator<?> skillIterator = skillsElement.elementIterator(); skillIterator.hasNext();) {
                Element skillElement = (Element) skillIterator.next();
                int id2 = Integer.parseInt(skillElement.attributeValue("id"));
                int level2 = Integer.parseInt(skillElement.attributeValue("level"));
                int chance2 = skillElement.attributeValue("chance") == null ? 0
                        : Integer.parseInt(skillElement.attributeValue("chance"));
                boolean canAttackDoor = Boolean.parseBoolean(skillElement.attributeValue("can_attack_door"));
                CubicTemplate.ActionType type = CubicTemplate.ActionType
                        .valueOf(skillElement.attributeValue("action_type"));
                TIntIntHashMap set = new TIntIntHashMap();

                for (Iterator<?> chanceIterator = skillElement.elementIterator(); chanceIterator.hasNext();) {
                    Element chanceElement = (Element) chanceIterator.next();
                    int min = Integer.parseInt(chanceElement.attributeValue("min"));
                    int max = Integer.parseInt(chanceElement.attributeValue("max"));
                    int value = Integer.parseInt(chanceElement.attributeValue("value"));

                    for (int i = min; i <= max; i++) {
                        set.put(i, value);
                    }
                }

                if ((chance2 == 0) && set.isEmpty()) {
                    warn("Wrong skill chance. Cubic: " + id + "/" + level);
                }

                Skill skill = SkillTable.getInstance().getInfo(id2, level2);

                if (skill != null) {
                    skill.setCubicSkill(true);
                    skills.add(new CubicTemplate.SkillInfo(skill, chance2, type, canAttackDoor, set));
                }
            }

            template.putSkills(chance, skills);
        }
    }
}

From source file:lineage2.gameserver.data.xml.parser.DomainParser.java

License:Open Source License

/**
 * Method readData.//from   w  w w  .jav a 2  s. c o  m
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element listElement = iterator.next();

        if ("domain".equals(listElement.getName())) {
            int id = Integer.parseInt(listElement.attributeValue("id"));
            Territory territory = null;

            for (Iterator<Element> i = listElement.elementIterator(); i.hasNext();) {
                Element n = i.next();

                if ("polygon".equalsIgnoreCase(n.getName())) {
                    Polygon shape = ZoneParser.parsePolygon(n);

                    if (!shape.validate()) {
                        error("DomainParser: invalid territory data : " + shape + "!");
                    }

                    if (territory == null) {
                        territory = new Territory();
                    }

                    territory.add(shape);
                }
            }

            if (territory == null) {
                throw new RuntimeException("DomainParser: empty territory!");
            }

            getHolder().addRegionData(new DomainArea(id, territory));
        }
    }
}

From source file:lineage2.gameserver.data.xml.parser.DoorParser.java

License:Open Source License

/**
 * Method readData.//from  w  w  w.  j av a2s.  c om
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element doorElement = iterator.next();

        if ("door".equals(doorElement.getName())) {
            StatsSet doorSet = initBaseStats();
            StatsSet aiParams = null;
            doorSet.set("door_type", doorElement.attributeValue("type"));
            Element posElement = doorElement.element("pos");
            Location doorPos;
            int x = Integer.parseInt(posElement.attributeValue("x"));
            int y = Integer.parseInt(posElement.attributeValue("y"));
            int z = Integer.parseInt(posElement.attributeValue("z"));
            doorSet.set("pos", doorPos = new Location(x, y, z));
            Polygon shape = new Polygon();
            int minz = 0, maxz = 0;
            Element shapeElement = doorElement.element("shape");
            minz = Integer.parseInt(shapeElement.attributeValue("minz"));
            maxz = Integer.parseInt(shapeElement.attributeValue("maxz"));
            shape.add(Integer.parseInt(shapeElement.attributeValue("ax")),
                    Integer.parseInt(shapeElement.attributeValue("ay")));
            shape.add(Integer.parseInt(shapeElement.attributeValue("bx")),
                    Integer.parseInt(shapeElement.attributeValue("by")));
            shape.add(Integer.parseInt(shapeElement.attributeValue("cx")),
                    Integer.parseInt(shapeElement.attributeValue("cy")));
            shape.add(Integer.parseInt(shapeElement.attributeValue("dx")),
                    Integer.parseInt(shapeElement.attributeValue("dy")));
            shape.setZmin(minz);
            shape.setZmax(maxz);
            doorSet.set("shape", shape);
            doorPos.setZ(minz + 32);

            for (Iterator<Element> i = doorElement.elementIterator(); i.hasNext();) {
                Element n = i.next();

                if ("set".equals(n.getName())) {
                    doorSet.set(n.attributeValue("name"), n.attributeValue("value"));
                } else if ("ai_params".equals(n.getName())) {
                    if (aiParams == null) {
                        aiParams = new StatsSet();
                        doorSet.set("ai_params", aiParams);
                    }

                    for (Iterator<Element> aiParamsIterator = n.elementIterator(); aiParamsIterator
                            .hasNext();) {
                        Element aiParamElement = aiParamsIterator.next();
                        aiParams.set(aiParamElement.attributeValue("name"),
                                aiParamElement.attributeValue("value"));
                    }
                }
            }

            doorSet.set("uid", doorElement.attributeValue("id"));
            doorSet.set("name", doorElement.attributeValue("name"));
            doorSet.set("baseHpMax", doorElement.attributeValue("hp"));
            doorSet.set("basePDef", doorElement.attributeValue("pdef"));
            doorSet.set("baseMDef", doorElement.attributeValue("mdef"));
            doorSet.set("collision_height", (maxz - minz) & 0xfff0);
            doorSet.set("collision_radius",
                    Math.max(50, Math.min(doorPos.getX() - shape.getXmin(), doorPos.getY() - shape.getYmin())));
            DoorTemplate template = new DoorTemplate(doorSet);
            getHolder().addTemplate(template);
        }
    }
}