Example usage for org.dom4j Element add

List of usage examples for org.dom4j Element add

Introduction

In this page you can find the example usage for org.dom4j Element add.

Prototype

void add(Namespace namespace);

Source Link

Document

Adds the given Namespace to this element.

Usage

From source file:gjset.client.ConcreteClientGUIController.java

License:Open Source License

/**
 * Initiates a request to select a given card. Note that this may cause the game to declare a set.
 *
 * @param cardData// ww  w .  j  a v a  2s . c  o  m
 * @see gjset.client.ClientGUIController#selectCard(gjset.data.Card)
 */
public void selectCard(Card cardData) {
    if (model.canSelectCards()) {
        Element root = documentFactory.createElement("command");
        root.addAttribute("type", "selectcard");
        root.add(cardData.getXMLRepresentation());

        client.sendMessage(root);
    }
}

From source file:gjset.client.GameInitiator.java

License:Open Source License

/**
 * Handle the verification stage of this process.
 *
 * @param root/*from   w w  w. j  a va 2  s  . c om*/
 */
private void handleInitialization(Element root) {
    // First do verification
    String protocolVersion = root.element("version").getText();

    if (protocolVersion.equals(GameConstants.COMM_VERSION)) {
        // If everything checks out, request a new player.
        Element newPlayerRequest = documentFactory.createElement("command");
        newPlayerRequest.addAttribute("type", "joinasplayer");

        Element usernameElement = documentFactory.createElement("username");
        usernameElement.setText(username);
        newPlayerRequest.add(usernameElement);

        client.sendMessage(newPlayerRequest);
    } else {
        // The protocol version is wrong.  Kill ourselves and shut down the client.
        destroy();
        client.destroy();
    }
}

From source file:gjset.data.PlayerData.java

License:Open Source License

/**
 * Return a representation of the player.
 *
 * @return//from   ww  w.  j  a  va2 s.  c o m
 */
public Element getXMLRepresentation() {
    DocumentFactory documentFactory = DocumentFactory.getInstance();

    Element playerElement = documentFactory.createElement("player");
    playerElement.addAttribute("id", "" + id);

    Element pointsElement = documentFactory.createElement("points");
    pointsElement.setText("" + points);
    playerElement.add(pointsElement);

    Element penaltyElement = documentFactory.createElement("penalty");
    penaltyElement.setText("" + penalty);
    playerElement.add(penaltyElement);

    Element nameElement = documentFactory.createElement("name");
    nameElement.setText(name);
    playerElement.add(nameElement);

    Element wantsToDrawElement = documentFactory.createElement("wanttodraw");
    wantsToDrawElement.setText("" + wantsToDraw);
    playerElement.add(wantsToDrawElement);

    return playerElement;
}

From source file:gjset.server.game.GameController.java

License:Open Source License

/**
 * /* ww w  .j a  v  a2s. com*/
 * process the indicated message from the server.
 *
 * @param message
 */
private void processMessage(ServerMessage message) {
    // Start by getting the player id.
    int playerId = message.client.getPlayerId();

    // Now see if this was a command.
    Element responseElement = null;
    Element commandElement = message.rootElement.element("command");
    if (commandElement != null) {
        String commandType = commandElement.attributeValue("type", "");
        if (commandType.equals("drawcards")) {
            responseElement = requestDrawCards(playerId);
        } else if (commandType.equals("callset")) {
            responseElement = callSet(playerId);
        } else if (commandType.equals("selectcard")) {
            Element cardElement = commandElement.element("card");
            Card card = new Card(cardElement);
            responseElement = toggleSelection(playerId, card);
        } else if (commandType.equals("joinasplayer")) {
            // This is an initialization message.
            String username = commandElement.element("username").getText();

            responseElement = bindClient(message.client, username);
        } else if (commandType.equals("startgame")) {
            // For now, we'll just automatically start the game.
            startNewGame();
        } else if (commandType.equals("dropout")) {
            handleDropOut(message.client);
        }
    }

    // See if we need to send a response to the player.
    if (responseElement != null) {
        // Add the original command so that the client can cross reference it, if necessary.
        responseElement.add(commandElement.createCopy());

        // Send the result back to the player that sent it in.
        message.client.sendMessage(responseElement);

        // Check to see if we need to update all players with new data.
        String result = responseElement.attributeValue("result", "false");
        if (result.equals("success")) {
            updateAllPlayers();
        }
    }
}

From source file:gjset.server.game.GameController.java

License:Open Source License

/**
 * Request card selection for the indicated player.
 *
 * @param playerId//from ww  w  .j  av  a 2s  .  co  m
 * @param card
 * @return
 */
private Element toggleSelection(int playerId, Card card) {
    // First toggle selection on the card.
    int gameState = model.getGameState();
    Element commandResponse = null;

    if (gameState == GameConstants.GAME_STATE_IDLE) {
        // Call set.
        model.callSet(playerId);

        // Select the card.
        model.toggleCardSelection(card);

        commandResponse = getCommandResponse(true, null);
    } else if (gameState == GameConstants.GAME_STATE_SET_CALLED && playerId == model.getSetCaller().getId()) {
        // Allow the next card to be selected.
        model.toggleCardSelection(card);

        commandResponse = getCommandResponse(true, null);
    } else {
        // Abort early if we can't select a card.
        commandResponse = getCommandResponse(false, "You can't select cards");
    }

    // Now see if we've got 3 cards selected.
    List<Card> selectedCards = model.getCardTable().getSelectedCards();
    if (selectedCards.size() == 3) {
        // We do have 3 cards!  Check for a set!
        Element messageAddendum = documentFactory.createElement("setresult");

        // Check if this is a set.
        boolean setSelected = model.resolveSet(true);
        if (setSelected) {
            // Append a message about the result of the set.
            messageAddendum.addAttribute("isset", "true");
        } else {
            // Append a message about the result of the set.
            messageAddendum.addAttribute("isset", "false");
        }

        commandResponse.add(messageAddendum);
    }

    return commandResponse;
}

From source file:gjset.server.game.GameModel.java

License:Open Source License

/**
 * Create the XML representation of the GameModel as a game update.
 *
 * @return/*  ww  w  .  j  a v a2s.  c  om*/
 */
public synchronized Element getUpdateRepresentation() {
    Element root = documentFactory.createElement("gameupdate");

    // Start with the deck size.
    Element deckElement = documentFactory.createElement("deck");
    deckElement.setText("" + deck.getRemainingCards());
    root.add(deckElement);

    // Move onto the game state.
    Element gameStateElement = documentFactory.createElement("gamestate");
    gameStateElement.addAttribute("state", "" + gameState);

    // Include the set caller if appropriate.
    if (gameState == GameConstants.GAME_STATE_SET_CALLED
            || gameState == GameConstants.GAME_STATE_SET_FINISHED) {
        Element setCallerElement = documentFactory.createElement("setcaller");
        setCallerElement.setText("" + getSetCaller().getId());
        gameStateElement.add(setCallerElement);

        // Also include whether the set was valid or not, if appropriate.
        if (gameState == GameConstants.GAME_STATE_SET_FINISHED) {
            Element setCorrectElement = documentFactory.createElement("setcorrect");
            setCorrectElement.setText(new Boolean(isLastSetCorrect).toString());
            gameStateElement.add(setCorrectElement);
        }
    }

    root.add(gameStateElement);

    // Now do the players.
    Element playersElement = documentFactory.createElement("players");

    Iterator<PlayerData> iterator = getPlayers().iterator();
    while (iterator.hasNext()) {
        PlayerData player = iterator.next();
        playersElement.add(player.getXMLRepresentation());
    }
    root.add(playersElement);

    // Then the card table.
    Element cardTableElement = cardTable.getRepresentation();
    root.add(cardTableElement);

    return root;
}

From source file:gjset.server.ServerGameController.java

License:Open Source License

/**
 * Send a game update to the new client.
 *
 * @param client// w ww.jav  a 2 s  .co  m
 * @see gjset.server.ServerMessageHandler#handleNewClient(gjset.server.PlayerClientHandler)
 */
public Element bindClient(PlayerClientHandler client, String username) {
    // Verify that we can do this.
    Player player = model.getExistingPlayer(username);

    if (player == null) {
        if (model.getGameState() == GameConstants.GAME_STATE_NOT_STARTED
                && model.getPlayers().size() < GameConstants.MAX_PLAYERS) {
            // Create a new player in the model.
            player = model.addNewPlayer(username);
            client.setPlayer(player);

            // Return the player information.
            Element commandResponse = getCommandResponse(true, null);

            Element newPlayerElement = documentFactory.createElement("newplayer");
            newPlayerElement.add(player.getXMLRepresentation());

            commandResponse.add(newPlayerElement);

            return commandResponse;
        } else {
            return getCommandResponse(false, "Unable to create a new player at this time.");
        }

    } else {
        client.setPlayer(player);

        // Return the player information.
        Element commandResponse = getCommandResponse(true, null);

        Element newPlayerElement = documentFactory.createElement("newplayer");
        newPlayerElement.add(player.getXMLRepresentation());

        commandResponse.add(newPlayerElement);

        return commandResponse;
    }
}

From source file:gjset.server.ServerGameController.java

License:Open Source License

/**
 * Return your basic success response.// w w w . j  a  v a2 s.  c om
 *
 * @param success  True if successful, false if not.
 * @param reason Adds a reason that can be added to the command response.
 * 
 * @return
 */
private Element getCommandResponse(boolean success, String reason) {
    // Create our response
    String resultText = "failed";
    if (success) {
        resultText = "success";
    }

    Element responseElement = documentFactory.createElement("commandresponse");
    responseElement.addAttribute("result", resultText);

    if (reason != null) {
        Element reasonElement = documentFactory.createElement("reason");
        reasonElement.setText(reason);
        responseElement.add(reasonElement);
    }

    return responseElement;
}

From source file:gjset.tools.MessageUtils.java

License:Open Source License

/**
 * Wraps a message with enclosing tags and a comm version.
 *
 * @param messageElement// ww  w.j a va  2 s .c  om
 * @return
 */
public static Element wrapMessage(Element messageElement) {
    DocumentFactory documentFactory = DocumentFactory.getInstance();

    Element rootElement = documentFactory.createElement("combocards");

    Element versionElement = documentFactory.createElement("version");
    versionElement.setText(GameConstants.COMM_VERSION);
    rootElement.add(versionElement);

    rootElement.add(messageElement);

    return rootElement;
}

From source file:gr.abiss.calipso.domain.Field.java

License:Open Source License

private void copyTo(Element e) {
    // appending empty strings to create new objects for "clone" support
    e.addAttribute(NAME, name + "");
    e.addAttribute(PRIORITY, priority + "");

    if (this.groupId != null) {
        e.addAttribute(GROUP_ID, this.groupId);
    }/*from  www  .  j a  v  a 2s  .com*/
    if (fieldType != null) {
        e.addAttribute(FIELDTYPE, fieldType);
    }
    if (validationExpressionId != null && validationExpressionId.longValue() != 0) {
        e.addAttribute(VALIDATIONEXPR, validationExpressionId + "");
    }
    if (this.defaultValueExpression != null) {
        e.addAttribute(DEFAULT_VALUE, this.defaultValueExpression);
    }
    if (this.getName().isFreeText()) {
        e.addAttribute(LINECOUNT, this.lineCount.toString());
        e.addAttribute(MULTIVALUE, this.multivalue.toString());
    }
    e.addAttribute(LABEL, label + "");
    Element configElem = FieldConfig.asDom4j(this.xmlConfig);
    if (configElem != null) {
        e.add(configElem);
    }
    if (optional) {
        e.addAttribute(OPTIONAL, TRUE);
    }
    if (options == null) {
        return;
    }
    for (Map.Entry<String, String> entry : options.entrySet()) {
        Element option = e.addElement(OPTION);
        option.addAttribute(VALUE, entry.getKey() + "");
        option.addText((String) entry.getValue() + "");
    }
}