Search in sources :

Example 1 with MessageHandler

use of org.apollo.game.message.handler.MessageHandler 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)

Aggregations

IOException (java.io.IOException)1 MessageHandler (org.apollo.game.message.handler.MessageHandler)1 MessageHandlerChainSet (org.apollo.game.message.handler.MessageHandlerChainSet)1 World (org.apollo.game.model.World)1 Message (org.apollo.net.message.Message)1 XmlNode (org.apollo.util.xml.XmlNode)1