Search in sources :

Example 11 with Game

use of com.nolanlawson.keepscore.db.Game in project KeepScore by nolanlawson.

the class GamesBackupSerializer method deserialize.

@SuppressWarnings("incomplete-switch")
public static GamesBackup deserialize(String xmlData) {
    int parserEvent = -1;
    XmlPullParser parser = null;
    Tag tag = null;
    GamesBackup gamesBackup = new GamesBackup();
    gamesBackup.setGames(new ArrayList<Game>());
    Game game = null;
    PlayerScore playerScore = null;
    Map<String, String> attributes = null;
    try {
        // calls service (referenced in url) to request XML serialized data
        parser = XmlHelper.loadData(xmlData);
        parserEvent = parser.getEventType();
        while (parserEvent != XmlPullParser.END_DOCUMENT) {
            switch(parserEvent) {
                case XmlPullParser.START_TAG:
                    tag = Tag.valueOf(parser.getName());
                    switch(tag) {
                        case Game:
                            game = new Game();
                            game.setPlayerScores(new ArrayList<PlayerScore>());
                            break;
                        case PlayerScore:
                            playerScore = new PlayerScore();
                            break;
                    }
                    // null or empty marker
                    if (parser.getAttributeCount() != -1) {
                        attributes = getAttributes(parser);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    tag = Tag.valueOf(parser.getName());
                    switch(tag) {
                        case Game:
                            gamesBackup.getGames().add(game);
                            break;
                        case PlayerScore:
                            game.getPlayerScores().add(playerScore);
                            break;
                    }
                    break;
                case XmlPullParser.TEXT:
                    String text = parser.getText();
                    if (!StringUtil.isEmptyOrWhitespace(text)) {
                        handleText(text, tag, attributes, gamesBackup, game, playerScore);
                    }
                    break;
            }
            parserEvent = parser.next();
        }
    } catch (XmlPullParserException e) {
        log.e(e, "unexpected");
    } catch (IOException e) {
        log.e(e, "unexpected");
    }
    applyVersionFixes(gamesBackup);
    // return de-serialized game backup
    return gamesBackup;
}
Also used : Game(com.nolanlawson.keepscore.db.Game) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Aggregations

Game (com.nolanlawson.keepscore.db.Game)11 GameDBHelper (com.nolanlawson.keepscore.db.GameDBHelper)5 PlayerScore (com.nolanlawson.keepscore.db.PlayerScore)5 GamesBackup (com.nolanlawson.keepscore.serialization.GamesBackup)4 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 ProgressDialog (android.app.ProgressDialog)2 Delta (com.nolanlawson.keepscore.db.Delta)2 List (java.util.List)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 AlertDialog (android.app.AlertDialog)1 LoadGamesBackupResult (com.nolanlawson.keepscore.data.LoadGamesBackupResult)1 Function (com.nolanlawson.keepscore.util.CollectionUtil.Function)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1