Search in sources :

Example 1 with EndTextElementListener

use of android.sax.EndTextElementListener in project android_frameworks_base by crdroidandroid.

the class SafeSaxTest method testMixedContent.

@SmallTest
public void testMixedContent() throws Exception {
    String xml = "<feed><entry></entry></feed>";
    RootElement root = new RootElement("feed");
    root.setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
        }
    });
    try {
        Xml.parse(xml, root.getContentHandler());
        fail("expected exception not thrown");
    } catch (SAXException e) {
    // Expected.
    }
}
Also used : RootElement(android.sax.RootElement) EndTextElementListener(android.sax.EndTextElementListener) SAXException(org.xml.sax.SAXException) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 2 with EndTextElementListener

use of android.sax.EndTextElementListener in project SeriesGuide by UweTrottmann.

the class TvdbTools method parseEpisodes.

/**
     * Loads the given zipped XML and parses containing episodes to create an array of {@link
     * ContentValues} for new episodes.<br> Adds update ops for updated episodes and delete ops for
     * local orphaned episodes to the given {@link ContentProviderOperation} batch.
     */
private ArrayList<ContentValues> parseEpisodes(final ArrayList<ContentProviderOperation> batch, final Show show, String url) throws TvdbException {
    final long dateLastMonthEpoch = (System.currentTimeMillis() - (DateUtils.DAY_IN_MILLIS * 30)) / 1000;
    final DateTimeZone showTimeZone = TimeTools.getDateTimeZone(show.release_timezone);
    final LocalTime showReleaseTime = TimeTools.getShowReleaseTime(show.release_time);
    final String deviceTimeZone = TimeZone.getDefault().getID();
    RootElement root = new RootElement("Data");
    Element episode = root.getChild("Episode");
    final ArrayList<ContentValues> newEpisodesValues = new ArrayList<>();
    final HashMap<Integer, Long> localEpisodeIds = DBUtils.getEpisodeMapForShow(app, show.tvdb_id);
    final HashMap<Integer, Long> removableEpisodeIds = new HashMap<>(// just copy episodes list, then remove valid ones
    localEpisodeIds);
    final HashSet<Integer> localSeasonIds = DBUtils.getSeasonIdsOfShow(app, show.tvdb_id);
    // store updated seasons to avoid duplicate ops
    final HashSet<Integer> seasonIdsToUpdate = new HashSet<>();
    final ContentValues values = new ContentValues();
    // set handlers for elements we want to react to
    episode.setEndElementListener(new EndElementListener() {

        public void end() {
            Integer episodeId = values.getAsInteger(Episodes._ID);
            if (episodeId == null || episodeId <= 0) {
                // invalid id, skip
                return;
            }
            // don't clean up this episode
            removableEpisodeIds.remove(episodeId);
            // decide whether to insert or update
            if (localEpisodeIds.containsKey(episodeId)) {
                /*
                     * Update uses provider ops which take a long time. Only
                     * update if episode was edited on TVDb or is not older than
                     * a month (ensures show air time changes get stored).
                     */
                Long lastEditEpoch = localEpisodeIds.get(episodeId);
                Long lastEditEpochNew = values.getAsLong(Episodes.LAST_EDITED);
                if (lastEditEpoch != null && lastEditEpochNew != null && (lastEditEpoch < lastEditEpochNew || dateLastMonthEpoch < lastEditEpoch)) {
                    // complete update op for episode
                    batch.add(DBUtils.buildEpisodeUpdateOp(values));
                }
            } else {
                // episode does not exist, yet
                newEpisodesValues.add(new ContentValues(values));
            }
            Integer seasonId = values.getAsInteger(Seasons.REF_SEASON_ID);
            if (seasonId != null && !seasonIdsToUpdate.contains(seasonId)) {
                // add insert/update op for season
                batch.add(DBUtils.buildSeasonOp(values, !localSeasonIds.contains(seasonId)));
                seasonIdsToUpdate.add(values.getAsInteger(Seasons.REF_SEASON_ID));
            }
            values.clear();
        }
    });
    episode.getChild("id").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes._ID, body.trim());
        }
    });
    episode.getChild("EpisodeNumber").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.NUMBER, body.trim());
        }
    });
    episode.getChild("absolute_number").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.ABSOLUTE_NUMBER, body.trim());
        }
    });
    episode.getChild("SeasonNumber").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.SEASON, body.trim());
        }
    });
    episode.getChild("DVD_episodenumber").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.DVDNUMBER, body.trim());
        }
    });
    episode.getChild("FirstAired").setEndTextElementListener(new EndTextElementListener() {

        public void end(String releaseDate) {
            long releaseDateTime = TimeTools.parseEpisodeReleaseDate(app, showTimeZone, releaseDate, showReleaseTime, show.country, show.network, deviceTimeZone);
            values.put(Episodes.FIRSTAIREDMS, releaseDateTime);
        }
    });
    episode.getChild("EpisodeName").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.TITLE, body.trim());
        }
    });
    episode.getChild("Overview").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.OVERVIEW, body.trim());
        }
    });
    episode.getChild("seasonid").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Seasons.REF_SEASON_ID, body.trim());
        }
    });
    episode.getChild("seriesid").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Shows.REF_SHOW_ID, body.trim());
        }
    });
    episode.getChild("Director").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.DIRECTORS, body.trim());
        }
    });
    episode.getChild("GuestStars").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.GUESTSTARS, body.trim());
        }
    });
    episode.getChild("Writer").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.WRITERS, body.trim());
        }
    });
    episode.getChild("filename").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.IMAGE, body.trim());
        }
    });
    episode.getChild("IMDB_ID").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            values.put(Episodes.IMDBID, body.trim());
        }
    });
    episode.getChild("lastupdated").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            // system populated field, trimming not necessary
            try {
                values.put(Episodes.LAST_EDITED, Long.valueOf(body));
            } catch (NumberFormatException e) {
                values.put(Episodes.LAST_EDITED, 0);
            }
        }
    });
    downloadAndParse(root.getContentHandler(), url, true, "parseEpisodes: ");
    // add delete ops for leftover episodeIds in our db
    for (Integer episodeId : removableEpisodeIds.keySet()) {
        batch.add(ContentProviderOperation.newDelete(Episodes.buildEpisodeUri(episodeId)).build());
    }
    return newEpisodesValues;
}
Also used : ContentValues(android.content.ContentValues) EndElementListener(android.sax.EndElementListener) LocalTime(org.joda.time.LocalTime) HashMap(java.util.HashMap) Element(android.sax.Element) RootElement(android.sax.RootElement) ArrayList(java.util.ArrayList) DateTimeZone(org.joda.time.DateTimeZone) RootElement(android.sax.RootElement) HashSet(java.util.HashSet) EndTextElementListener(android.sax.EndTextElementListener)

Example 3 with EndTextElementListener

use of android.sax.EndTextElementListener in project SeriesGuide by UweTrottmann.

the class TvdbTools method searchShow.

/**
     * Search TheTVDB for shows which include a certain keyword in their title.
     *
     * @param language If not provided, will query for results in all languages.
     * @return At most 100 results (limited by TheTVDB API).
     */
@Nonnull
public List<SearchResult> searchShow(@NonNull String query, @Nullable final String language) throws TvdbException {
    final List<SearchResult> series = new ArrayList<>();
    final SearchResult currentShow = new SearchResult();
    RootElement root = new RootElement("Data");
    Element item = root.getChild("Series");
    // set handlers for elements we want to react to
    item.setEndElementListener(new EndElementListener() {

        public void end() {
            // only take results in the selected language
            if (language == null || language.equals(currentShow.language)) {
                series.add(currentShow.copy());
            }
        }
    });
    item.getChild("id").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            currentShow.tvdbid = Integer.valueOf(body);
        }
    });
    item.getChild("language").setEndTextElementListener(new EndTextElementListener() {

        @Override
        public void end(String body) {
            currentShow.language = body.trim();
        }
    });
    item.getChild("SeriesName").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            currentShow.title = body.trim();
        }
    });
    item.getChild("Overview").setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
            currentShow.overview = body.trim();
        }
    });
    // build search URL: encode query...
    String url;
    try {
        url = TVDB_API_GETSERIES + URLEncoder.encode(query, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new TvdbDataException("searchShow: " + e.getMessage(), e);
    }
    // ...and set language filter
    if (language == null) {
        url += TVDB_PARAM_LANGUAGE + "all";
    } else {
        url += TVDB_PARAM_LANGUAGE + language;
    }
    downloadAndParse(root.getContentHandler(), url, false, "searchShow: ");
    return series;
}
Also used : RootElement(android.sax.RootElement) EndElementListener(android.sax.EndElementListener) Element(android.sax.Element) RootElement(android.sax.RootElement) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SearchResult(com.battlelancer.seriesguide.items.SearchResult) EndTextElementListener(android.sax.EndTextElementListener) Nonnull(javax.annotation.Nonnull)

Example 4 with EndTextElementListener

use of android.sax.EndTextElementListener in project android_frameworks_base by ParanoidAndroid.

the class SafeSaxTest method testMixedContent.

@SmallTest
public void testMixedContent() throws Exception {
    String xml = "<feed><entry></entry></feed>";
    RootElement root = new RootElement("feed");
    root.setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
        }
    });
    try {
        Xml.parse(xml, root.getContentHandler());
        fail("expected exception not thrown");
    } catch (SAXException e) {
    // Expected.
    }
}
Also used : RootElement(android.sax.RootElement) EndTextElementListener(android.sax.EndTextElementListener) SAXException(org.xml.sax.SAXException) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 5 with EndTextElementListener

use of android.sax.EndTextElementListener in project platform_frameworks_base by android.

the class SafeSaxTest method testMixedContent.

@SmallTest
public void testMixedContent() throws Exception {
    String xml = "<feed><entry></entry></feed>";
    RootElement root = new RootElement("feed");
    root.setEndTextElementListener(new EndTextElementListener() {

        public void end(String body) {
        }
    });
    try {
        Xml.parse(xml, root.getContentHandler());
        fail("expected exception not thrown");
    } catch (SAXException e) {
    // Expected.
    }
}
Also used : RootElement(android.sax.RootElement) EndTextElementListener(android.sax.EndTextElementListener) SAXException(org.xml.sax.SAXException) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

EndTextElementListener (android.sax.EndTextElementListener)9 RootElement (android.sax.RootElement)9 SmallTest (android.test.suitebuilder.annotation.SmallTest)6 SAXException (org.xml.sax.SAXException)6 Element (android.sax.Element)3 EndElementListener (android.sax.EndElementListener)3 ArrayList (java.util.ArrayList)2 ContentValues (android.content.ContentValues)1 Message (android.os.Message)1 StartElementListener (android.sax.StartElementListener)1 SearchResult (com.battlelancer.seriesguide.items.SearchResult)1 FileInputStream (java.io.FileInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 Nonnull (javax.annotation.Nonnull)1 DateTimeZone (org.joda.time.DateTimeZone)1 LocalTime (org.joda.time.LocalTime)1