Exemplo n.º 1
0
  public synchronized Vector<IOption> loadOptions(File file, boolean print) {
    ParsedXML root = null;
    InputStream is = null;
    Vector<IOption> changedOptions = new Vector<IOption>(1, 1);

    try {
      is = new FileInputStream(file);
    } catch (FileNotFoundException e) {
      return changedOptions;
    }

    try {
      root = TinyParser.parseXML(is);
    } catch (ParseException e) {
      System.out.println("Error parsing game options xml file."); // $NON-NLS-1$
      e.printStackTrace(System.out);
      return changedOptions;
    }

    Enumeration<?> rootChildren = root.elements();
    ParsedXML optionsNode = (ParsedXML) rootChildren.nextElement();

    if (optionsNode.getName().equals("options")) { // $NON-NLS-1$
      Enumeration<?> children = optionsNode.elements();

      while (children.hasMoreElements()) {
        IOption option = parseOptionNode((ParsedXML) children.nextElement(), print);

        if (null != option) {
          changedOptions.addElement(option);
        }
      }

      return changedOptions;
    }
    System.out.println(
        "Root node of game options file is incorrectly named. Name should be 'options' but name is "
            + "'"
            + optionsNode.getName()
            + "'"); //$NON-NLS-1$ //$NON-NLS-2$
    return changedOptions;
  }
Exemplo n.º 2
0
  private IOption parseOptionNode(ParsedXML node, boolean print) {
    IOption option = null;

    if (node.getName().equals("gameoption")) { // $NON-NLS-1$
      Enumeration<?> children = node.elements();
      String name = null;
      Object value = null;

      while (children.hasMoreElements()) {
        ParsedXML child = (ParsedXML) children.nextElement();

        if (child.getName().equals("optionname")) { // $NON-NLS-1$
          name = ((ParsedXML) child.elements().nextElement()).getContent();
        } else if (child.getName().equals("optionvalue")) { // $NON-NLS-1$
          value = ((ParsedXML) child.elements().nextElement()).getContent();
        }
      }

      if ((null != name) && (null != value)) {
        IOption tempOption = getOption(name);

        if (null != tempOption) {
          if (!tempOption.getValue().toString().equals(value.toString())) {
            try {
              switch (tempOption.getType()) {
                case IOption.STRING:
                  tempOption.setValue((String) value);
                  break;

                case IOption.BOOLEAN:
                  tempOption.setValue(new Boolean(value.toString()));
                  break;

                case IOption.INTEGER:
                  tempOption.setValue(new Integer(value.toString()));
                  break;

                case IOption.FLOAT:
                  tempOption.setValue(new Float(value.toString()));
                  break;
              }
              if (print) {
                System.out.println(
                    "Set option '"
                        + name //$NON-NLS-1$
                        + "' to '"
                        + value
                        + "'."); //$NON-NLS-1$ //$NON-NLS-2$
              }

              option = tempOption;
            } catch (IllegalArgumentException iaEx) {
              System.out.println(
                  "Error trying to load option '"
                      + name
                      + "' with a value of '"
                      + value
                      + "'."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            }
          }
        } else {
          System.out.println("Invalid option '" + name + "' when trying to load options file.");
          //$NON-NLS-1$ //$NON-NLS-2$
        }
      }
    }

    return option;
  }