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:lineage2.gameserver.data.xml.parser.RecipeParser.java

License:Open Source License

/**
 * Method readData./*  w w w  . ja  v a2s. 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 element = iterator.next();
        final int id = Integer.parseInt(element.attributeValue("id"));
        final int level = Integer.parseInt(element.attributeValue("level"));
        final int mpConsume = element.attributeValue("mp_consume") == null ? 0
                : Integer.parseInt(element.attributeValue("mp_consume"));
        final int successRate = Integer.parseInt(element.attributeValue("success_rate"));
        final int itemId = Integer.parseInt(element.attributeValue("item_id"));
        final boolean isDwarven = element.attributeValue("is_dwarven") == null ? false
                : Boolean.parseBoolean(element.attributeValue("is_dwarven"));
        RecipeTemplate recipe = new RecipeTemplate(id, level, mpConsume, successRate, itemId, isDwarven);

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

            if ("materials".equalsIgnoreCase(subElement.getName())) {
                for (Element e : subElement.elements()) {
                    if ("item".equalsIgnoreCase(e.getName())) {
                        int item_id = Integer.parseInt(e.attributeValue("id"));
                        long count = Long.parseLong(e.attributeValue("count"));
                        recipe.addMaterial(new RecipeComponent(item_id, count));
                    }
                }
            } else if ("products".equalsIgnoreCase(subElement.getName())) {
                for (Element e : subElement.elements()) {
                    if ("item".equalsIgnoreCase(e.getName())) {
                        int item_id = Integer.parseInt(e.attributeValue("id"));
                        long count = Long.parseLong(e.attributeValue("count"));
                        int chance = Integer.parseInt(e.attributeValue("chance"));
                        recipe.addProduct(new RecipeComponent(item_id, count, chance));
                    }
                }
            } else if ("npc_fee".equalsIgnoreCase(subElement.getName())) {
                for (Element e : subElement.elements()) {
                    if ("item".equalsIgnoreCase(e.getName())) {
                        int item_id = Integer.parseInt(e.attributeValue("id"));
                        long count = Long.parseLong(e.attributeValue("count"));
                        recipe.addNpcFee(new RecipeComponent(item_id, count));
                    }
                }
            }
        }

        getHolder().addRecipe(recipe);
    }
}

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

License:Open Source License

/**
 * Method readData.//from w  w  w.j av  a  2 s  . c  om
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    String impl = rootElement.attributeValue("impl");
    Class<?> clazz = null;
    StatsSet set = new StatsSet();

    for (Iterator<Attribute> iterator = rootElement.attributeIterator(); iterator.hasNext();) {
        Attribute element = iterator.next();
        set.set(element.getName(), element.getValue());
    }

    Residence residence = null;

    try {
        clazz = Class.forName("lineage2.gameserver.model.entity.residence." + impl);
        Constructor<?> constructor = clazz.getConstructor(StatsSet.class);
        residence = (Residence) constructor.newInstance(set);
        getHolder().addResidence(residence);
    } catch (Exception e) {
        error("fail to init: " + getCurrentFileName(), e);
        return;
    }

    for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element element = iterator.next();
        String nodeName = element.getName();
        int level = element.attributeValue("level") == null ? 0
                : Integer.valueOf(element.attributeValue("level"));
        int lease = (int) ((element.attributeValue("lease") == null ? 0
                : Integer.valueOf(element.attributeValue("lease"))) * Config.RESIDENCE_LEASE_FUNC_MULTIPLIER);
        int npcId = element.attributeValue("npcId") == null ? 0
                : Integer.valueOf(element.attributeValue("npcId"));
        int listId = element.attributeValue("listId") == null ? 0
                : Integer.valueOf(element.attributeValue("listId"));
        ResidenceFunction function = null;

        if (nodeName.equalsIgnoreCase("teleport")) {
            function = checkAndGetFunction(residence, ResidenceFunction.TELEPORT);
            List<TeleportLocation> targets = new ArrayList<>();

            for (Iterator<Element> it2 = element.elementIterator(); it2.hasNext();) {
                Element teleportElement = it2.next();

                if ("target".equalsIgnoreCase(teleportElement.getName())) {
                    int npcStringId = Integer.parseInt(teleportElement.attributeValue("name"));
                    long price = Long.parseLong(teleportElement.attributeValue("price"));
                    int itemId = teleportElement.attributeValue("item") == null ? ItemTemplate.ITEM_ID_ADENA
                            : Integer.parseInt(teleportElement.attributeValue("item"));
                    TeleportLocation loc = new TeleportLocation(itemId, price, npcStringId, 0);
                    loc.set(Location.parseLoc(teleportElement.attributeValue("loc")));
                    targets.add(loc);
                }
            }

            function.addTeleports(level, targets.toArray(new TeleportLocation[targets.size()]));
        } else if (nodeName.equalsIgnoreCase("support")) {
            if ((level > 9) && !Config.ALT_CH_ALLOW_1H_BUFFS) {
                continue;
            }

            function = checkAndGetFunction(residence, ResidenceFunction.SUPPORT);
            function.addBuffs(level);
        } else if (nodeName.equalsIgnoreCase("item_create")) {
            function = checkAndGetFunction(residence, ResidenceFunction.ITEM_CREATE);
            function.addBuylist(level, new int[] { npcId, listId });
        } else if (nodeName.equalsIgnoreCase("curtain")) {
            function = checkAndGetFunction(residence, ResidenceFunction.CURTAIN);
        } else if (nodeName.equalsIgnoreCase("platform")) {
            function = checkAndGetFunction(residence, ResidenceFunction.PLATFORM);
        } else if (nodeName.equalsIgnoreCase("restore_exp")) {
            function = checkAndGetFunction(residence, ResidenceFunction.RESTORE_EXP);
        } else if (nodeName.equalsIgnoreCase("restore_hp")) {
            function = checkAndGetFunction(residence, ResidenceFunction.RESTORE_HP);
        } else if (nodeName.equalsIgnoreCase("restore_mp")) {
            function = checkAndGetFunction(residence, ResidenceFunction.RESTORE_MP);
        } else if (nodeName.equalsIgnoreCase("skills")) {
            for (Iterator<Element> nextIterator = element.elementIterator(); nextIterator.hasNext();) {
                Element nextElement = nextIterator.next();
                int id2 = Integer.parseInt(nextElement.attributeValue("id"));
                int level2 = Integer.parseInt(nextElement.attributeValue("level"));
                Skill skill = SkillTable.getInstance().getInfo(id2, level2);

                if (skill != null) {
                    residence.addSkill(skill);
                }
            }
        } else if (nodeName.equalsIgnoreCase("banish_points")) {
            for (Iterator<Element> banishPointsIterator = element.elementIterator(); banishPointsIterator
                    .hasNext();) {
                Location loc = Location.parse(banishPointsIterator.next());
                residence.addBanishPoint(loc);
            }
        } else if (nodeName.equalsIgnoreCase("owner_restart_points")) {
            for (Iterator<Element> ownerRestartPointsIterator = element
                    .elementIterator(); ownerRestartPointsIterator.hasNext();) {
                Location loc = Location.parse(ownerRestartPointsIterator.next());
                residence.addOwnerRestartPoint(loc);
            }
        } else if (nodeName.equalsIgnoreCase("other_restart_points")) {
            for (Iterator<Element> otherRestartPointsIterator = element
                    .elementIterator(); otherRestartPointsIterator.hasNext();) {
                Location loc = Location.parse(otherRestartPointsIterator.next());
                residence.addOtherRestartPoint(loc);
            }
        } else if (nodeName.equalsIgnoreCase("chaos_restart_points")) {
            for (Iterator<Element> chaosRestartPointsIterator = element
                    .elementIterator(); chaosRestartPointsIterator.hasNext();) {
                Location loc = Location.parse(chaosRestartPointsIterator.next());
                residence.addChaosRestartPoint(loc);
            }
        } else if (nodeName.equalsIgnoreCase("related_fortresses")) {
            for (Iterator<Element> subElementIterator = element.elementIterator(); subElementIterator
                    .hasNext();) {
                Element subElement = subElementIterator.next();

                if (subElement.getName().equalsIgnoreCase("domain")) {
                    ((Castle) residence).addRelatedFortress(Fortress.DOMAIN,
                            Integer.parseInt(subElement.attributeValue("fortress")));
                } else if (subElement.getName().equalsIgnoreCase("boundary")) {
                    ((Castle) residence).addRelatedFortress(Fortress.BOUNDARY,
                            Integer.parseInt(subElement.attributeValue("fortress")));
                }
            }
        } else if (nodeName.equalsIgnoreCase("merchant_guards")) {
            for (Iterator<Element> subElementIterator = element.elementIterator(); subElementIterator
                    .hasNext();) {
                Element subElement = subElementIterator.next();
                int itemId = Integer.parseInt(subElement.attributeValue("item_id"));
                int npcId2 = Integer.parseInt(subElement.attributeValue("npc_id"));
                int maxGuard = Integer.parseInt(subElement.attributeValue("max"));
                IntSet intSet = new HashIntSet(3);
                String[] ssq = subElement.attributeValue("ssq").split(";");

                for (String q : ssq) {
                    if (q.equalsIgnoreCase("cabal_null")) {
                        intSet.add(0);
                    }
                }

                ((Castle) residence).addMerchantGuard(new MerchantGuard(itemId, npcId2, maxGuard, intSet));
            }
        }

        if (function != null) {
            function.addLease(level, lease);
        }
    }
}

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

License:Open Source License

/**
 * Method readData./*from  ww w .j  a v  a2s .c om*/
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    List<Pair<Territory, Map<Race, String>>> restartArea = new ArrayList<>();
    Map<String, RestartPoint> restartPoint = new HashMap<>();

    for (Iterator<Element> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element listElement = iterator.next();

        if ("restart_area".equals(listElement.getName())) {
            Territory territory = null;
            Map<Race, String> restarts = new HashMap<>();

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

                if ("region".equalsIgnoreCase(n.getName())) {
                    Rectangle shape;
                    Attribute map = n.attribute("map");
                    String s = map.getValue();
                    String val[] = s.split("_");
                    int rx = Integer.parseInt(val[0]);
                    int ry = Integer.parseInt(val[1]);
                    int x1 = World.MAP_MIN_X + ((rx - Config.GEO_X_FIRST) << 15);
                    int y1 = World.MAP_MIN_Y + ((ry - Config.GEO_Y_FIRST) << 15);
                    int x2 = (x1 + (1 << 15)) - 1;
                    int y2 = (y1 + (1 << 15)) - 1;
                    shape = new Rectangle(x1, y1, x2, y2);
                    shape.setZmin(World.MAP_MIN_Z);
                    shape.setZmax(World.MAP_MAX_Z);

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

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

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

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

                    territory.add(shape);
                } else if ("restart".equalsIgnoreCase(n.getName())) {
                    Race race = Race.valueOf(n.attributeValue("race"));
                    String locName = n.attributeValue("loc");
                    restarts.put(race, locName);
                }
            }

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

            if (restarts.isEmpty()) {
                throw new RuntimeException("RestartPointParser: restarts not defined!");
            }

            restartArea.add(new ImmutablePair<>(territory, restarts));
        } else if ("restart_loc".equals(listElement.getName())) {
            String name = listElement.attributeValue("name");
            int bbs = Integer.parseInt(listElement.attributeValue("bbs", "0"));
            int msgId = Integer.parseInt(listElement.attributeValue("msg_id", "0"));
            List<Location> restartPoints = new ArrayList<>();
            List<Location> PKrestartPoints = new ArrayList<>();

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

                if ("restart_point".equals(n.getName())) {
                    for (Iterator<Element> ii = n.elementIterator(); ii.hasNext();) {
                        Element d = ii.next();

                        if ("coords".equalsIgnoreCase(d.getName())) {
                            Location loc = Location.parseLoc(d.attribute("loc").getValue());
                            restartPoints.add(loc);
                        }
                    }
                } else if ("PKrestart_point".equals(n.getName())) {
                    for (Iterator<Element> ii = n.elementIterator(); ii.hasNext();) {
                        Element d = ii.next();

                        if ("coords".equalsIgnoreCase(d.getName())) {
                            Location loc = Location.parseLoc(d.attribute("loc").getValue());
                            PKrestartPoints.add(loc);
                        }
                    }
                }
            }

            if (restartPoints.isEmpty()) {
                throw new RuntimeException(
                        "RestartPointParser: restart_points not defined for restart_loc : " + name + "!");
            }

            if (PKrestartPoints.isEmpty()) {
                PKrestartPoints = restartPoints;
            }

            RestartPoint rp = new RestartPoint(name, bbs, msgId, restartPoints, PKrestartPoints);
            restartPoint.put(name, rp);
        }
    }

    for (Pair<Territory, Map<Race, String>> ra : restartArea) {
        Map<Race, RestartPoint> restarts = new HashMap<>();

        for (Map.Entry<Race, String> e : ra.getValue().entrySet()) {
            RestartPoint rp = restartPoint.get(e.getValue());

            if (rp == null) {
                throw new RuntimeException("RestartPointParser: restart_loc not found : " + e.getValue() + "!");
            }

            restarts.put(e.getKey(), rp);
            getHolder().addRegionData(new RestartArea(ra.getKey(), restarts));
        }
    }
}

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

License:Open Source License

/**
 * Method readData.//from w ww . j ava2s  . 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 shuttleElement = iterator.next();
        int shuttleId = Integer.parseInt(shuttleElement.attributeValue("id"));
        ShuttleTemplate template = new ShuttleTemplate(shuttleId);

        for (Iterator<?> doorsIterator = shuttleElement.elementIterator("doors"); doorsIterator.hasNext();) {
            Element doorsElement = (Element) doorsIterator.next();

            for (Iterator<?> doorIterator = doorsElement.elementIterator("door"); doorIterator.hasNext();) {
                Element doorElement = (Element) doorIterator.next();
                int doorId = Integer.parseInt(doorElement.attributeValue("id"));
                StatsSet set = new StatsSet();

                for (Iterator<?> setIterator = doorElement.elementIterator("set"); setIterator.hasNext();) {
                    Element setElement = (Element) setIterator.next();
                    set.set(setElement.attributeValue("name"), setElement.attributeValue("value"));
                }

                template.addDoor(new ShuttleDoor(doorId, set));
            }
        }

        getHolder().addTemplate(template);
    }
}

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

License:Open Source License

/**
 * Method readData.//from   w ww.j av  a  2 s .c  o m
 * @param rootElement Element
 * @throws Exception
 */
@Override
protected void readData(Element rootElement) throws Exception {
    Map<String, Territory> territories = new HashMap<>();

    for (Iterator<Element> spawnIterator = rootElement.elementIterator(); spawnIterator.hasNext();) {
        Element spawnElement = spawnIterator.next();

        if (spawnElement.getName().equalsIgnoreCase("territory")) {
            String terName = spawnElement.attributeValue("name");
            Territory territory = parseTerritory(terName, spawnElement);
            territories.put(terName, territory);
        } else if (spawnElement.getName().equalsIgnoreCase("spawn")) {
            String group = spawnElement.attributeValue("group");
            int respawn = spawnElement.attributeValue("respawn") == null ? 60
                    : Integer.parseInt(spawnElement.attributeValue("respawn"));
            int respawnRandom = spawnElement.attributeValue("respawn_random") == null ? 0
                    : Integer.parseInt(spawnElement.attributeValue("respawn_random"));
            int count = spawnElement.attributeValue("count") == null ? 1
                    : Integer.parseInt(spawnElement.attributeValue("count"));
            PeriodOfDay periodOfDay = spawnElement.attributeValue("period_of_day") == null ? PeriodOfDay.NONE
                    : PeriodOfDay.valueOf(spawnElement.attributeValue("period_of_day").toUpperCase());

            if (group == null) {
                group = periodOfDay.name();
            }

            SpawnTemplate template = new SpawnTemplate(periodOfDay, count, respawn, respawnRandom);

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

                if (subElement.getName().equalsIgnoreCase("point")) {
                    int x = Integer.parseInt(subElement.attributeValue("x"));
                    int y = Integer.parseInt(subElement.attributeValue("y"));
                    int z = Integer.parseInt(subElement.attributeValue("z"));
                    int h = subElement.attributeValue("h") == null ? -1
                            : Integer.parseInt(subElement.attributeValue("h"));
                    template.addSpawnRange(new Location(x, y, z, h));
                } else if (subElement.getName().equalsIgnoreCase("territory")) {
                    String terName = subElement.attributeValue("name");

                    if (terName != null) {
                        Territory g = territories.get(terName);

                        if (g == null) {
                            error("Invalid territory name: " + terName + "; " + getCurrentFileName());
                            continue;
                        }

                        template.addSpawnRange(g);
                    } else {
                        Territory temp = parseTerritory(null, subElement);
                        template.addSpawnRange(temp);
                    }
                } else if (subElement.getName().equalsIgnoreCase("npc")) {
                    int npcId = Integer.parseInt(subElement.attributeValue("id"));
                    int max = subElement.attributeValue("max") == null ? 0
                            : Integer.parseInt(subElement.attributeValue("max"));
                    MultiValueSet<String> parameters = StatsSet.EMPTY;

                    for (Element e : subElement.elements()) {
                        if (parameters.isEmpty()) {
                            parameters = new MultiValueSet<>();
                        }

                        parameters.set(e.attributeValue("name"), e.attributeValue("value"));
                    }

                    template.addNpc(new SpawnNpcInfo(npcId, max, parameters));
                }
            }

            if (template.getNpcSize() == 0) {
                warn("Npc id is zero! File: " + getCurrentFileName());
                continue;
            }

            if (template.getSpawnRangeSize() == 0) {
                warn("No points to spawn! File: " + getCurrentFileName());
                continue;
            }

            getHolder().addSpawn(group, template);
        }
    }
}

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

License:Open Source License

/**
 * Method readData./*from  w  w  w. j  a  v  a2s  .c o m*/
 * @param rootElement Element
 */
@Override
protected void readData(Element rootElement) {
    for (Iterator<?> iterator = rootElement.elementIterator(); iterator.hasNext();) {
        Element staticObjectElement = (Element) iterator.next();
        StatsSet set = new StatsSet();
        set.set("uid", staticObjectElement.attributeValue("id"));
        set.set("stype", staticObjectElement.attributeValue("stype"));
        set.set("path", staticObjectElement.attributeValue("path"));
        set.set("map_x", staticObjectElement.attributeValue("map_x"));
        set.set("map_y", staticObjectElement.attributeValue("map_y"));
        set.set("name", staticObjectElement.attributeValue("name"));
        set.set("x", staticObjectElement.attributeValue("x"));
        set.set("y", staticObjectElement.attributeValue("y"));
        set.set("z", staticObjectElement.attributeValue("z"));
        set.set("spawn", staticObjectElement.attributeValue("spawn"));
        getHolder().addTemplate(new StaticObjectTemplate(set));
    }
}

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

License:Open Source License

/**
 * Method parseLogicAnd.//from   w  ww . j a v a 2  s .  c  om
 * @param n Element
 * @return Condition
 */
private Condition parseLogicAnd(Element n) {
    ConditionLogicAnd cond = new ConditionLogicAnd();

    for (Iterator<Element> iterator = n.elementIterator(); iterator.hasNext();) {
        Element condElement = iterator.next();
        cond.add(parseCond(condElement));
    }

    if ((cond._conditions == null) || (cond._conditions.length == 0)) {
        error("Empty <and> condition in " + getCurrentFileName());
    }

    return cond;
}