Search in sources :

Example 1 with XmlNode

use of org.apollo.util.xml.XmlNode 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)

Example 2 with XmlNode

use of org.apollo.util.xml.XmlNode in project apollo by apollo-rsps.

the class GameService method init.

/**
 * Initializes the game service.
 *
 * @throws IOException If there is an error accessing the file.
 * @throws SAXException If there is an error parsing the file.
 * @throws ReflectiveOperationException If a MessageHandler could not be created.
 */
private void init() throws IOException, SAXException, ReflectiveOperationException {
    try (InputStream input = new FileInputStream("data/messages.xml")) {
        MessageHandlerChainSetParser chainSetParser = new MessageHandlerChainSetParser(input);
        handlers = chainSetParser.parse(world);
    }
    try (InputStream input = new FileInputStream("data/synchronizer.xml")) {
        XmlParser parser = new XmlParser();
        XmlNode root = parser.parse(input);
        if (!root.getName().equals("synchronizer")) {
            throw new IOException("Invalid root node name.");
        }
        XmlNode active = root.getChild("active");
        if (active == null || !active.hasValue()) {
            throw new IOException("No active node/value.");
        }
        Class<?> clazz = Class.forName(active.getValue());
        synchronizer = (ClientSynchronizer) clazz.newInstance();
    }
}
Also used : XmlParser(org.apollo.util.xml.XmlParser) XmlNode(org.apollo.util.xml.XmlNode) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MessageHandlerChainSetParser(org.apollo.game.io.MessageHandlerChainSetParser)

Example 3 with XmlNode

use of org.apollo.util.xml.XmlNode in project apollo by apollo-rsps.

the class MessageHandlerChainSetParser method parse.

/**
 * Parses the XML and produces a group of {@link MessageHandlerChain}s.
 *
 * @param world The {@link World} this MessageHandlerChainGroup is for.
 * @return A {@link MessageHandlerChainSet}.
 * @throws IOException If an I/O error occurs.
 * @throws SAXException If a SAX error occurs.
 * @throws ReflectiveOperationException If a reflection error occurs.
 */
@SuppressWarnings("unchecked")
public MessageHandlerChainSet parse(World world) throws IOException, SAXException, ReflectiveOperationException {
    XmlNode messages = parser.parse(is);
    if (!messages.getName().equals("messages")) {
        throw new IOException("Root node name is not 'messages'.");
    }
    MessageHandlerChainSet chainSet = new MessageHandlerChainSet();
    for (XmlNode message : messages) {
        if (!message.getName().equals("message")) {
            throw new IOException("Only expected nodes named 'message' beneath the root node.");
        }
        XmlNode typeNode = message.getChild("type");
        if (typeNode == null) {
            throw new IOException("No node named 'type' beneath current message node.");
        }
        XmlNode chainNode = message.getChild("chain");
        if (chainNode == null) {
            throw new IOException("No node named 'chain' beneath current message node.");
        }
        String messageClassName = typeNode.getValue();
        if (messageClassName == null) {
            throw new IOException("Type node must have a value.");
        }
        Class<? extends Message> messageClass = (Class<? extends Message>) Class.forName(messageClassName);
        for (XmlNode handlerNode : chainNode) {
            if (!handlerNode.getName().equals("handler")) {
                throw new IOException("Only expected nodes named 'handler' beneath the root node.");
            }
            String handlerClassName = handlerNode.getValue();
            if (handlerClassName == null) {
                throw new IOException("Handler node must have a value.");
            }
            Class<? extends MessageHandler<? extends Message>> handlerClass = (Class<? extends MessageHandler<? extends Message>>) Class.forName(handlerClassName);
            MessageHandler<? extends Message> handler = handlerClass.getConstructor(World.class).newInstance(world);
            chainSet.putHandler(messageClass, handler);
        }
    }
    return chainSet;
}
Also used : XmlNode(org.apollo.util.xml.XmlNode) Message(org.apollo.net.message.Message) MessageHandler(org.apollo.game.message.handler.MessageHandler) MessageHandlerChainSet(org.apollo.game.message.handler.MessageHandlerChainSet) IOException(java.io.IOException) World(org.apollo.game.model.World)

Example 4 with XmlNode

use of org.apollo.util.xml.XmlNode in project apollo by apollo-rsps.

the class LoginService method init.

/**
 * Initialises the login service.
 *
 * @throws SAXException If there is an error parsing the XML file.
 * @throws IOException If there is an error accessing the file.
 * @throws ReflectiveOperationException If the {@link PlayerSerializer} implementation could not be created.
 */
private void init() throws SAXException, IOException, ReflectiveOperationException {
    XmlParser parser = new XmlParser();
    XmlNode rootNode;
    try (InputStream is = new FileInputStream("data/login.xml")) {
        rootNode = parser.parse(is);
    }
    if (!rootNode.getName().equals("login")) {
        throw new IOException("Unexpected root node name, expected 'login'.");
    }
    XmlNode serializer = rootNode.getChild("serializer");
    if (serializer == null || !serializer.hasValue()) {
        throw new IOException("No serializer child node or value.");
    }
    Class<?> clazz = Class.forName(serializer.getValue());
    this.serializer = (PlayerSerializer) clazz.getConstructor(World.class).newInstance(world);
}
Also used : XmlParser(org.apollo.util.xml.XmlParser) XmlNode(org.apollo.util.xml.XmlNode) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) World(org.apollo.game.model.World) FileInputStream(java.io.FileInputStream)

Aggregations

IOException (java.io.IOException)4 XmlNode (org.apollo.util.xml.XmlNode)4 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 World (org.apollo.game.model.World)2 XmlParser (org.apollo.util.xml.XmlParser)2 MessageHandlerChainSetParser (org.apollo.game.io.MessageHandlerChainSetParser)1 MessageHandler (org.apollo.game.message.handler.MessageHandler)1 MessageHandlerChainSet (org.apollo.game.message.handler.MessageHandlerChainSet)1 PluginMetaData (org.apollo.game.plugin.PluginMetaData)1 Message (org.apollo.net.message.Message)1