Example #1
0
  /**
   * Parses the XML and produces an array of {@link Npc}s which are to be registered to the world..
   *
   * @return An {@link EventHandlerChainGroup}.
   */
  public Npc[] parse() {
    XmlNode rootNode = null;
    try {
      rootNode = parser.parse(is);
    } catch (final IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (final SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    final List<Npc> npcs = new ArrayList<Npc>();
    final int maxId = NpcDefinition.count() - 1;

    for (final XmlNode npcNode : rootNode) {

      final int id = Integer.parseInt(npcNode.getAttribute("id"));
      // We simply ignore any ids above the maximum for the current
      // release.
      if (id > maxId) continue;

      final XmlNode posNode = npcNode.getChild("position");
      final XmlNode nodeX = posNode.getChild("x");
      final XmlNode nodeY = posNode.getChild("y");

      final String nodeR = npcNode.getAttribute("rwalk");
      boolean rwalk = false;

      if (nodeR != null) rwalk = Boolean.parseBoolean(nodeR);

      final String nodeF = npcNode.getAttribute("face");
      int face = 0;

      if (nodeF != null) face = Integer.parseInt(nodeF);

      final int x = Integer.parseInt(nodeX.getValue());
      final int y = Integer.parseInt(nodeY.getValue());

      Position pos;

      final XmlNode nodeZ = posNode.getChild("height");
      if (nodeZ != null) {
        final int height = Integer.parseInt(nodeZ.getValue());
        pos = new Position(x, y, height);
      } else pos = new Position(x, y);

      final Npc npc = new Npc(id, pos);
      npc.setFace(face);
      npc.setRandomWalking(rwalk);
      npcs.add(npc);
    }

    return npcs.toArray(new Npc[npcs.size()]);
  }
 /**
  * Parses the XML and creates a meta data object.
  *
  * @return The meta data object.
  * @throws IOException if an I/O error occurs.
  * @throws SAXException if a SAX error occurs.
  */
 public PluginMetaData parse() throws IOException, SAXException {
   XmlNode rootNode = parser.parse(is);
   if (!rootNode.getName().equals("plugin")) {
     throw new IOException("root node must be named plugin");
   }
   XmlNode idNode = getElement(rootNode, "id");
   XmlNode nameNode = getElement(rootNode, "name");
   XmlNode descriptionNode = getElement(rootNode, "description");
   XmlNode authorsNode = getElement(rootNode, "authors");
   XmlNode scriptsNode = getElement(rootNode, "scripts");
   XmlNode dependenciesNode = getElement(rootNode, "dependencies");
   XmlNode versionNode = getElement(rootNode, "version");
   String id = idNode.getValue();
   String name = nameNode.getValue();
   String description = descriptionNode.getValue();
   int version = Integer.parseInt(versionNode.getValue());
   if (id == null || name == null || description == null) {
     throw new IOException("id, name and description must have values");
   }
   XmlNode[] authorNodes = authorsNode.getChildren().toArray(EMPTY_NODE_ARRAY);
   XmlNode[] scriptNodes = scriptsNode.getChildren().toArray(EMPTY_NODE_ARRAY);
   XmlNode[] dependencyNodes = dependenciesNode.getChildren().toArray(EMPTY_NODE_ARRAY);
   String[] authors = new String[authorNodes.length];
   String[] scripts = new String[scriptNodes.length];
   String[] dependencies = new String[dependencyNodes.length];
   for (int i = 0; i < authorNodes.length; i++) {
     authors[i] = authorNodes[i].getValue();
     if (authors[i] == null) {
       throw new IOException("author elements must have values");
     }
   }
   for (int i = 0; i < scriptNodes.length; i++) {
     scripts[i] = scriptNodes[i].getValue();
     if (scripts[i] == null) {
       throw new IOException("script elements must have values");
     }
   }
   for (int i = 0; i < dependencyNodes.length; i++) {
     dependencies[i] = dependencyNodes[i].getValue();
     if (dependencies[i] == null) {
       throw new IOException("dependency elements must have values");
     }
   }
   return new PluginMetaData(id, name, description, authors, scripts, dependencies, version);
 }