use of android.sax.Element in project android_frameworks_base by ParanoidAndroid.
the class SafeSaxTest method testListener.
@SmallTest
public void testListener() throws Exception {
String xml = "<feed xmlns='http://www.w3.org/2005/Atom'>\n" + "<entry>\n" + "<id>a</id>\n" + "</entry>\n" + "<entry>\n" + "<id>b</id>\n" + "</entry>\n" + "</feed>\n";
RootElement root = new RootElement(ATOM_NAMESPACE, "feed");
Element entry = root.requireChild(ATOM_NAMESPACE, "entry");
Element id = entry.requireChild(ATOM_NAMESPACE, "id");
ElementCounter rootCounter = new ElementCounter();
ElementCounter entryCounter = new ElementCounter();
TextElementCounter idCounter = new TextElementCounter();
root.setElementListener(rootCounter);
entry.setElementListener(entryCounter);
id.setTextElementListener(idCounter);
Xml.parse(xml, root.getContentHandler());
assertEquals(1, rootCounter.starts);
assertEquals(1, rootCounter.ends);
assertEquals(2, entryCounter.starts);
assertEquals(2, entryCounter.ends);
assertEquals(2, idCounter.starts);
assertEquals("ab", idCounter.bodies);
}
use of android.sax.Element in project android_frameworks_base by ResurrectionRemix.
the class SafeSaxTest method testListener.
@SmallTest
public void testListener() throws Exception {
String xml = "<feed xmlns='http://www.w3.org/2005/Atom'>\n" + "<entry>\n" + "<id>a</id>\n" + "</entry>\n" + "<entry>\n" + "<id>b</id>\n" + "</entry>\n" + "</feed>\n";
RootElement root = new RootElement(ATOM_NAMESPACE, "feed");
Element entry = root.requireChild(ATOM_NAMESPACE, "entry");
Element id = entry.requireChild(ATOM_NAMESPACE, "id");
ElementCounter rootCounter = new ElementCounter();
ElementCounter entryCounter = new ElementCounter();
TextElementCounter idCounter = new TextElementCounter();
root.setElementListener(rootCounter);
entry.setElementListener(entryCounter);
id.setTextElementListener(idCounter);
Xml.parse(xml, root.getContentHandler());
assertEquals(1, rootCounter.starts);
assertEquals(1, rootCounter.ends);
assertEquals(2, entryCounter.starts);
assertEquals(2, entryCounter.ends);
assertEquals(2, idCounter.starts);
assertEquals("ab", idCounter.bodies);
}
use of android.sax.Element in project android_frameworks_base by AOSPA.
the class SafeSaxTest method testListener.
@SmallTest
public void testListener() throws Exception {
String xml = "<feed xmlns='http://www.w3.org/2005/Atom'>\n" + "<entry>\n" + "<id>a</id>\n" + "</entry>\n" + "<entry>\n" + "<id>b</id>\n" + "</entry>\n" + "</feed>\n";
RootElement root = new RootElement(ATOM_NAMESPACE, "feed");
Element entry = root.requireChild(ATOM_NAMESPACE, "entry");
Element id = entry.requireChild(ATOM_NAMESPACE, "id");
ElementCounter rootCounter = new ElementCounter();
ElementCounter entryCounter = new ElementCounter();
TextElementCounter idCounter = new TextElementCounter();
root.setElementListener(rootCounter);
entry.setElementListener(entryCounter);
id.setTextElementListener(idCounter);
Xml.parse(xml, root.getContentHandler());
assertEquals(1, rootCounter.starts);
assertEquals(1, rootCounter.ends);
assertEquals(2, entryCounter.starts);
assertEquals(2, entryCounter.ends);
assertEquals(2, idCounter.starts);
assertEquals("ab", idCounter.bodies);
}
use of android.sax.Element 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;
}
use of android.sax.Element 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;
}
Aggregations