Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:org.jivesoftware.xmpp.workgroup.Agent.java

License:Open Source License

public Element getAgentInfo() {
    // Create an agent element
    Element element = DocumentHelper.createElement(QName.get("agent", "http://jabber.org/protocol/workgroup"));
    element.addAttribute("jid", getAgentJID().toString());
    // Add the name of the agent
    if (getNickname() != null) {
        element.addElement("name", "http://jivesoftware.com/protocol/workgroup").setText(getNickname());
    }/* w  w w . j a v  a2 s. c  o  m*/

    return element;
}

From source file:org.jivesoftware.xmpp.workgroup.request.InvitationRequest.java

License:Open Source License

@Override
public Element getSessionElement() {
    // Add the workgroup of the original user request
    QName qName = DocumentHelper.createQName("session",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element sessionElement = DocumentHelper.createElement(qName);
    sessionElement.addAttribute("id", sessionID);
    sessionElement.addAttribute("workgroup", userRequest.getWorkgroup().getJID().toString());
    return sessionElement;
}

From source file:org.jivesoftware.xmpp.workgroup.request.Request.java

License:Open Source License

public Element getMetaDataElement() {
    QName qName = DocumentHelper.createQName("metadata",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element metaDataElement = DocumentHelper.createElement(qName);

    for (String name : metaData.keySet()) {
        List<String> values = metaData.get(name);

        for (String value : values) {
            Element elem = metaDataElement.addElement("value");
            elem.addAttribute("name", name).setText(value);
        }/*from  www  .  j av a2 s. c o  m*/
    }
    return metaDataElement;
}

From source file:org.jivesoftware.xmpp.workgroup.request.UserRequest.java

License:Open Source License

@Override
public Element getSessionElement() {
    QName qName = DocumentHelper.createQName("session",
            DocumentHelper.createNamespace("", "http://jivesoftware.com/protocol/workgroup"));
    Element sessionElement = DocumentHelper.createElement(qName);
    sessionElement.addAttribute("id", requestID);
    sessionElement.addAttribute("workgroup", getWorkgroup().getJID().toString());
    return sessionElement;
}

From source file:org.jogre.server.data.xml.GameInfoXML.java

License:Open Source License

/**
 * Method for flattening a GameInfo object into an XML element.
 * //from w  ww. jav  a2s . c o  m
 * @param gameInfo   Game information object.
 * @return           XML (dom4j) version of Game info object. 
 */
public static Element flatten(GameInfo gameInfo) {
    Element elm = DocumentHelper.createElement(XML_ELM_GAME_INFO);

    elm.addAttribute(XML_ATT_PLAYERS, gameInfo.getPlayers());
    elm.addAttribute(XML_ATT_RESULTS, gameInfo.getResults());
    elm.addAttribute(XML_ATT_START_TIME, JogreUtils.valueOf(gameInfo.getStartTime(), IJogre.DATE_FORMAT_FULL));
    elm.addAttribute(XML_ATT_END_TIME, JogreUtils.valueOf(gameInfo.getEndTime(), IJogre.DATE_FORMAT_FULL));

    elm.addAttribute(XML_ATT_SCORE, gameInfo.getGameScore());
    elm.addAttribute(XML_ATT_HISTORY, gameInfo.getGameHistory());

    return elm;
}

From source file:org.jogre.server.data.xml.GameSummaryXML.java

License:Open Source License

/**
 * Method for converting a gameSummary java object into an 
 * XML Element (dom4j).//from w  w w. j a  v  a 2 s  .c  om
 * 
 * @param gameSummary       Game summary object.
 * @return                  Game summary object as XML "game_summary".
 */
public static Element flatten(GameSummary gameSummary) {
    Element elm = DocumentHelper.createElement(XML_ELM_GAME_SUMMARY);

    elm.addAttribute(XML_ATT_GAME, gameSummary.getGameKey());
    elm.addAttribute(XML_ATT_RATING, String.valueOf(gameSummary.getRating()));
    elm.addAttribute(XML_ATT_WINS, String.valueOf(gameSummary.getWins()));
    elm.addAttribute(XML_ATT_LOSES, String.valueOf(gameSummary.getLoses()));
    elm.addAttribute(XML_ATT_DRAWS, String.valueOf(gameSummary.getDraws()));
    elm.addAttribute(XML_ATT_STREAK, String.valueOf(gameSummary.getStreak()));

    return elm;
}

From source file:org.jogre.server.data.xml.ServerDataXML.java

License:Open Source License

/**
 * Add game information to data and update user scores.
 *
 * @see org.jogre.server.data.IServerData#addGame(org.jogre.server.data.GameInfo, boolean)
 *///from www  .  j a  v  a  2s.c  om
public GameOver addGame(GameInfo gameInfo, boolean eloRatings) {
    // Declare GameOver object
    GameOver gameOver = null;

    // Update the game file
    if (gameDoc != null) {

        // Retrieve gameID
        String gameKey = gameInfo.getGameKey();

        // Retrieve game element for this game.
        String xpath = "//game[@id='" + gameKey + "']";
        Node node = gameDoc.selectSingleNode(xpath);
        Element gameElm = null;
        if (node != null)
            gameElm = (Element) node;
        else {
            gameElm = DocumentHelper.createElement(XML_ELM_GAME);
            gameElm.addAttribute(XML_ATT_ID, gameKey);
            gameDoc.getRootElement().add(gameElm);
        }

        // Create game info as XML
        Element gameInfoXML = GameInfoXML.flatten(gameInfo);
        gameElm.add(gameInfoXML);
        saveXMLFile(gameFile, gameDoc); // save to file

        // Retrieve players and results
        String[] players = JogreUtils.convertToStringArray(gameInfo.getPlayers());
        int[] results = JogreUtils.convertToIntArray(gameInfo.getResults());
        int numPlayers = players.length;

        // Loop through players at table and update them
        GameSummary[] gameSummary = new GameSummary[numPlayers];
        Element[] gameSummaryXML = new Element[numPlayers];
        for (int i = 0; i < numPlayers; i++) {
            String curPlayer = players[i];

            // Retrieve game summary from XML (create new if not exists).
            gameSummaryXML[i] = getGameSummaryXML(gameKey, curPlayer);

            // inflate to a game summary object
            gameSummary[i] = GameSummaryXML.inflate(gameKey, curPlayer, gameSummaryXML[i]);
        }

        // Check if scores need updating
        // NOTE: (currently only support 2 player games)
        if (eloRatings && numPlayers == EloRatingSystem.SUPPORTED_PLAYERS) {
            // Retrieve old ratings
            int[] oldRatings = { gameSummary[0].getRating(), gameSummary[1].getRating() };

            // Find new ratings
            EloRatingSystem elo = EloRatingSystem.getInstance(gameKey);
            int[] newRatings = { elo.getNewRating(oldRatings[0], oldRatings[1], results[0]),
                    elo.getNewRating(oldRatings[1], oldRatings[0], results[1]) };

            // Update game summaries,
            for (int i = 0; i < numPlayers; i++)
                gameSummary[i].update(results[i], newRatings[i]);

            // Create GameOver object with old/new ratings and return to users
            gameOver = new GameOver(players, results, oldRatings, newRatings);
        } else {
            // No elo ratings - so everyone keeps old ratings
            int[] oldRatings = new int[numPlayers];

            for (int i = 0; i < numPlayers; i++)
                oldRatings[i] = gameSummary[i].getRating();

            gameOver = new GameOver(players, results, oldRatings, oldRatings);
        }

        // Update XML attributes
        for (int i = 0; i < numPlayers; i++) {
            gameSummaryXML[i].setAttributes(GameSummaryXML.flatten(gameSummary[i]).attributes());
        }
    }

    // Save document to file.
    saveXMLFile(userFile, userDoc); // save to file

    // Now return gameOver object.
    return gameOver;
}

From source file:org.jogre.server.data.xml.ServerDataXML.java

License:Open Source License

/**
 * Reset the server snapshot.// w  w  w  .j av  a2s  .  com
 *
 * @see org.jogre.server.data.IServerData#resetSnapshot(java.util.Vector)
 */
public void resetSnapshot(Vector gameKeys) {
    // Create new document
    this.snapshotDoc = DocumentHelper.createDocument();
    Element snapshotElm = DocumentHelper.createElement(XML_ELM_SNAPSHOTS);
    snapshotDoc.add(snapshotElm);

    for (int i = 0; i < gameKeys.size(); i++) {
        String gameId = (String) gameKeys.get(i);
        snapshotElm.add(getGameSnapshotElm(gameId, 0, 0));
    }

    saveXMLFile(snapshotFile, snapshotDoc);
}

From source file:org.jogre.server.data.xml.ServerDataXML.java

License:Open Source License

/**
 * Add a new user.//from www. ja  va2s  . com
 * 
 * @see org.jogre.server.data.IServerData#newUser(org.jogre.server.data.User)
 */
public void newUser(User user) throws ServerDataException {
    Element userElm = DocumentHelper.createElement(XML_ELM_USER);

    // Check user doesn't exist 
    if (userDoc.selectSingleNode("users/user[@username='" + user.getUsername() + "']") == null) {
        userElm.addAttribute("username", user.getUsername());
        userElm.addAttribute("password", user.getPassword());
        userDoc.getRootElement().add(userElm);

        saveXMLFile(userFile, userDoc);
    } else
        throw new ServerDataException("User already exists: " + user.getUsername());
}

From source file:org.jogre.server.data.xml.ServerDataXML.java

License:Open Source License

/**
 * Return a game//from   w  w w. j av a2s.co m
 *
 * @param gameId
 * @param numOfUsers
 * @param numOfTables
 * @return
 */
private Element getGameSnapshotElm(String gameId, int numOfUsers, int numOfTables) {
    Element gameSnapShot = DocumentHelper.createElement(XML_ELM_GAME);

    gameSnapShot.addAttribute(XML_ATT_ID, gameId);
    gameSnapShot.addAttribute(XML_ATT_NUM_OF_USERS, String.valueOf(numOfUsers));
    gameSnapShot.addAttribute(XML_ATT_NUM_OF_TABLES, String.valueOf(numOfTables));

    return gameSnapShot;
}