Example #1
0
  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;
  }