Search in sources :

Example 1 with PluginMetaData

use of org.apollo.game.plugin.PluginMetaData in project apollo by apollo-rsps.

the class PluginMetaDataParser method parse.

/**
 * Parses the XML and creates a meta data object.
 *
 * @param base The base path for this plugin
 * @return The meta data object.
 * @throws SAXException If a SAX error occurs.
 * @throws IOException If an I/O error occurs.
 */
public PluginMetaData parse(File base) 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();
    double version = Double.parseDouble(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, base, name, description, authors, scripts, dependencies, version);
}
Also used : XmlNode(org.apollo.util.xml.XmlNode) PluginMetaData(org.apollo.game.plugin.PluginMetaData) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 PluginMetaData (org.apollo.game.plugin.PluginMetaData)1 XmlNode (org.apollo.util.xml.XmlNode)1