Android Open Source - utexas-utilities Blackboard Dashboard Xml Parser






From Project

Back to project page utexas-utilities.

License

The source code is released under:

Apache License

If you think the Android project utexas-utilities listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nasageek.utexasutilities;
/*w w w . j  a va  2 s.c  om*/
import android.util.Xml;

import com.nasageek.utexasutilities.model.BBClass;
import com.nasageek.utexasutilities.model.FeedItem;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.Reader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

public class BlackboardDashboardXmlParser {

    private HashMap<String, BBClass> courses;
    private boolean feedParsed = false;
    // need this so we don't instantiate it every time we make a new FeedItem
    private SimpleDateFormat feedItemDateFormat;


    public List<MyPair<String, List<FeedItem>>> parse(Reader in) throws XmlPullParserException,
            IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in);
            parser.nextTag();
            return actuallyParse(parser);
        } finally {
            in.close();
        }
    }

    /*
     * Weird name? yeah. This probably isn't best practice, but I think it's
     * okay in order to remain easy to understand to calling classes with
     * minimal code duplication. This is where the actual parsing occurs.
     */
    private List<MyPair<String, List<FeedItem>>> actuallyParse(XmlPullParser parser)
            throws XmlPullParserException, IOException {
        List<FeedItem> feeditems = readDashboard(parser);

        // want to show most recent date at top and ListView's stackFromBottom
        // is broken by AmazingListView
        Collections.reverse(feeditems);
        List<MyPair<String, List<FeedItem>>> categorizedList = new ArrayList<>();
        String currentDate = "";
        ArrayList<FeedItem> currentDateList = null;
        DateFormat df = DateFormat.getDateInstance();
        for (int i = 0; i < feeditems.size(); i++) {
            String formattedDate = df.format(feeditems.get(i).getDate());

            // first FeedItem is always in a new category (the first category)
            if (i == 0) {
                currentDate = formattedDate;
                currentDateList = new ArrayList<>();
                currentDateList.add(feeditems.get(i));
            }
            // if the current FeedItem is not on the current date or
            // we're on the last FeedItem
            else if (!formattedDate.equals(currentDate)) {
                categorizedList.add(
                        new MyPair<String, List<FeedItem>>(currentDate, currentDateList));
                currentDate = formattedDate;
                currentDateList = new ArrayList<>();
                currentDateList.add(feeditems.get(i));
            }
            // otherwise just add to the current date
            else {
                currentDateList.add(feeditems.get(i));
            }
            // add final date at the end
            if (i == feeditems.size() - 1) {
                categorizedList.add(
                        new MyPair<String, List<FeedItem>>(currentDate, currentDateList));
            }

        }
        feedParsed = true;
        return categorizedList;
    }

    private List<FeedItem> readDashboard(XmlPullParser parser) throws XmlPullParserException,
            IOException {
        feedItemDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US);

        courses = new HashMap<>();
        List<FeedItem> feed = new ArrayList<>();
        parser.require(XmlPullParser.START_TAG, null, "mobileresponse");
        parser.nextTag();

        parser.require(XmlPullParser.START_TAG, null, "courses");
        while (parser.next() != XmlPullParser.END_DOCUMENT) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("course")) {
                BBClass clz = readCourse(parser);
                if (clz != null) {
                    courses.put(clz.getBbid(), clz);
                }
            } else if (name.equals("feeditem")) {
                FeedItem item = readFeedItem(parser);
                if (item != null) {
                    feed.add(item);
                }
            }
        }
        return feed;
    }

    private BBClass readCourse(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, null, "course");

        String name = parser.getAttributeValue(null, "name");
        String bbid = parser.getAttributeValue(null, "bbid");
        String fullcourseid = parser.getAttributeValue(null, "courseid");

        // case for system announcement course
        if (fullcourseid == null) {
            return null;
        }
        while (parser.next() != XmlPullParser.END_TAG) {
            // do nothing
        }
        return new BBClass(name, bbid, fullcourseid);
    }

    private FeedItem readFeedItem(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, null, "feeditem");
        // if sourcetype="AN" then it is a notification

        // String itemid = parser.getAttributeValue(null, "itemid");
        String type = parser.getAttributeValue(null, "type");

        // Don't care about system announcements
        if (type.equals("SYSTEM_ANNOUNCEMENT")) {
            return null;
        }
        String courseid = parser.getAttributeValue(null, "courseid");
        String message = parser.getAttributeValue(null, "message");
        String date = parser.getAttributeValue(null, "startdate");

        // could be null
        String contentid = parser.getAttributeValue(null, "contentid");
        String sourcetype = parser.getAttributeValue(null, "sourcetype");
        // String id = parser.getAttributeValue(null, "contentid");
        // TODO sourceid?
        while (parser.next() != XmlPullParser.END_TAG) {
            // do nothing
        }
        return new FeedItem(type, message, contentid, courseid, sourcetype, date,
                feedItemDateFormat);
    }

    public HashMap<String, BBClass> getCourses() {
        if (feedParsed) {
            return courses;
        } else {
            throw new IllegalStateException(
                    "You must parse the feed before you can call getCourses()");
        }
    }
}




Java Source Code List

com.google.maps.android.ui.MyIconGenerator.java
com.mapsaurus.paneslayout.ActivityDelegate.java
com.mapsaurus.paneslayout.FragmentLauncher.java
com.mapsaurus.paneslayout.MenuItemWrapper.java
com.mapsaurus.paneslayout.MySlidingPaneLayout.java
com.mapsaurus.paneslayout.PanesActivity.java
com.mapsaurus.paneslayout.PanesLayout.java
com.mapsaurus.paneslayout.PanesSizer.java
com.mapsaurus.paneslayout.PhoneDelegate.java
com.mapsaurus.paneslayout.SimpleScrollView.java
com.mapsaurus.paneslayout.TabletDelegate.java
com.nasageek.utexasutilities.AsyncTask.java
com.nasageek.utexasutilities.AttachmentDownloadService.java
com.nasageek.utexasutilities.AuthCookie.java
com.nasageek.utexasutilities.BlackboardDashboardXmlParser.java
com.nasageek.utexasutilities.BuildingDatabase.java
com.nasageek.utexasutilities.BuildingProvider.java
com.nasageek.utexasutilities.BuildingSaxHandler.java
com.nasageek.utexasutilities.ChangeLog.java
com.nasageek.utexasutilities.ChangeableContextTask.java
com.nasageek.utexasutilities.CourseMapSaxHandler.java
com.nasageek.utexasutilities.FixedListPreference.java
com.nasageek.utexasutilities.LoginWebViewClient.java
com.nasageek.utexasutilities.MarkerManager.java
com.nasageek.utexasutilities.MyPair.java
com.nasageek.utexasutilities.MyScrollView.java
com.nasageek.utexasutilities.PlacemarkSaxHandler.java
com.nasageek.utexasutilities.PnaAuthCookie.java
com.nasageek.utexasutilities.RouteSaxHandler.java
com.nasageek.utexasutilities.SecurePreferences.java
com.nasageek.utexasutilities.TempLoginException.java
com.nasageek.utexasutilities.UTilitiesApplication.java
com.nasageek.utexasutilities.UtdAuthCookie.java
com.nasageek.utexasutilities.Utility.java
com.nasageek.utexasutilities.WrappedViewPager.java
com.nasageek.utexasutilities.WrappingSlidingDrawer.java
com.nasageek.utexasutilities.activities.AboutMeActivity.java
com.nasageek.utexasutilities.activities.BalanceActivity.java
com.nasageek.utexasutilities.activities.BlackboardPanesActivity.java
com.nasageek.utexasutilities.activities.CampusMapActivity.java
com.nasageek.utexasutilities.activities.DataUsageActivity.java
com.nasageek.utexasutilities.activities.LoginActivity.java
com.nasageek.utexasutilities.activities.MenuActivity.java
com.nasageek.utexasutilities.activities.NutritionInfoActivity.java
com.nasageek.utexasutilities.activities.Preferences.java
com.nasageek.utexasutilities.activities.ScheduleActivity.java
com.nasageek.utexasutilities.activities.UTilitiesActivity.java
com.nasageek.utexasutilities.adapters.BBClassAdapter.java
com.nasageek.utexasutilities.adapters.CourseMapAdapter.java
com.nasageek.utexasutilities.adapters.MultiPanePagerAdapter.java
com.nasageek.utexasutilities.adapters.MyFragmentPagerAdapter.java
com.nasageek.utexasutilities.adapters.ScheduleClassAdapter.java
com.nasageek.utexasutilities.adapters.TransactionAdapter.java
com.nasageek.utexasutilities.fragments.ActionModeFragment.java
com.nasageek.utexasutilities.fragments.BlackboardAnnouncementsFragment.java
com.nasageek.utexasutilities.fragments.BlackboardCourseListFragment.java
com.nasageek.utexasutilities.fragments.BlackboardCourseMapFragment.java
com.nasageek.utexasutilities.fragments.BlackboardDashboardFragment.java
com.nasageek.utexasutilities.fragments.BlackboardDownloadableItemFragment.java
com.nasageek.utexasutilities.fragments.BlackboardExternalItemFragment.java
com.nasageek.utexasutilities.fragments.BlackboardFragment.java
com.nasageek.utexasutilities.fragments.BlackboardGradesFragment.java
com.nasageek.utexasutilities.fragments.BlackboardPagerFragment.java
com.nasageek.utexasutilities.fragments.CourseScheduleFragment.java
com.nasageek.utexasutilities.fragments.DatePickerFragment.java
com.nasageek.utexasutilities.fragments.DoubleDatePickerDialogFragment.java
com.nasageek.utexasutilities.fragments.ExamScheduleFragment.java
com.nasageek.utexasutilities.fragments.MenuFragment.java
com.nasageek.utexasutilities.fragments.NutritionInfoFragment.java
com.nasageek.utexasutilities.fragments.PickCalendarDialogFragment.java
com.nasageek.utexasutilities.fragments.TransactionsFragment.java
com.nasageek.utexasutilities.model.BBClass.java
com.nasageek.utexasutilities.model.Building.java
com.nasageek.utexasutilities.model.Classtime.java
com.nasageek.utexasutilities.model.CourseMapItem.java
com.nasageek.utexasutilities.model.FeedItem.java
com.nasageek.utexasutilities.model.Placemark.java
com.nasageek.utexasutilities.model.Transaction.java
com.nasageek.utexasutilities.model.UTClass.java
com.viewpagerindicator.MyTabPageIndicator.java