Android Open Source - UTHPortal-Android-Gradle Courses Parser






From Project

Back to project page UTHPortal-Android-Gradle.

License

The source code is released under:

MIT License

If you think the Android project UTHPortal-Android-Gradle 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.uth.uthportal.buffers;
/* w  w  w  .  j a v a  2  s.  c om*/
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.uth.uthportal.collections.AnnItem;
import com.uth.uthportal.collections.Announcements;
import com.uth.uthportal.collections.Course;
import com.uth.uthportal.collections.CourseInfo;
/**
 * Parses a JSON object from a buffer that contains courses
 * and populates a List<Course> so we can manage
 * the information received by the server with ease.
 * Check the actual JSON files from the server to get 
 * a better understanding of the parsing process.
 * @author GeorgeT
 *
 */
public class CoursesParser {
  
  public  Boolean wasSuccessful =true;//stays true if not changed
  public String errorMessage;
  public CoursesParser(){
  }

    public List<Course> parseCourses(List<String> coursesBuffers){
    List<Course> courses = new ArrayList<Course>();
        Course courseTemp;

    for(String buffer : coursesBuffers){
        courseTemp = parseCourse(buffer);
            if (courseTemp != null) {
                courses.add(courseTemp);
            } else {
                Log.w("e.cp.ParseCourses","Course parser failed to parse, skipping course");
            }
    }
    return courses;
  }

  private Course parseCourse(String JSONBuffer){
    JSONObject jObj;

    try {
      jObj= new JSONObject(JSONBuffer);
    } catch (JSONException e) {
      wasSuccessful = false;
      errorMessage = e.getMessage();
            Log.e("e.cp.ParseCourse:",errorMessage);
      return null;
    }

    String code;
    CourseInfo cInfo;
    Announcements announcements;

    try {
      code = jObj.getString("code");
      cInfo = parseCourseInfo(jObj.getJSONObject("info"));
      announcements = parseAnnouncements(jObj.getJSONObject("announcements"));
    } catch (JSONException e){
      wasSuccessful = false;
      errorMessage = e.getMessage();
            Log.e("e.cp.parseCourse",errorMessage);
            return null;
    }

        if (cInfo == null || announcements == null)
            return null;
    return new Course(code,cInfo,announcements);
  }

  private CourseInfo parseCourseInfo(JSONObject cInfo){
    String name;
    String link;

        try {
      name = cInfo.getString("name");
      link = cInfo.getString("link");
    } catch (JSONException e) {
      wasSuccessful = false;
      errorMessage = e.getMessage();
            Log.e("e.cp.parseCourseInfo:",errorMessage);
            return null;
    }

    return new CourseInfo(name,link);
  }

  private Announcements parseAnnouncements(JSONObject ann){
    String link;
    List<AnnItem> items;
        List<AnnItem> eclassItems;

    try {
      link = ann.getString("link");
      items = parseItems(ann.getJSONArray("site"));
            if (items == null)
                return null;
            eclassItems = parseItems(ann.getJSONArray("eclass"));
            if (eclassItems != null)
                items.addAll(eclassItems);
            //Sort items by date
            if (!items.isEmpty())
                Collections.sort(items, new Comparator<AnnItem>() {
                    @Override
                    public int compare(AnnItem annItem, AnnItem annItem2) {
                        return annItem.date.after(annItem2.date)? 1 : 0;
                    }
                });

    } catch (JSONException e){
            wasSuccessful = false;
            errorMessage = e.getMessage();
            Log.e("e.cp.parseAnnouncements",errorMessage);
            return null;
    }


    return new Announcements(link, new Date(), items);
  }

  private List<AnnItem> parseItems(JSONArray items){
    List<AnnItem> itemList = new ArrayList<AnnItem>(); //inittialize a list of items

        for(int i=0;i<items.length();i++){
      try {
        //parse the item and add it to list
        itemList.add(parseItem(items.getJSONObject(i)));
      } catch (JSONException ex) {
        wasSuccessful = false;
        errorMessage = ex.getMessage();
                Log.e("e.cp.ParseItems",errorMessage);
                return null;
      } catch (NullPointerException ex){
                wasSuccessful = false;
                errorMessage = ex.getMessage();
                Log.e("e.cp.ParseItems",errorMessage);
                return null;
            }
    }
    return itemList;
  }

  private AnnItem parseItem(JSONObject item){
    Date date;
    String html;
    Boolean hasTime;

    try {
      date = parseDateString(item.getString("date")); //parse and add date
            if (date == null)
                return null;

      html = item.getString("html"); //get html content
      hasTime = item.getBoolean("has_time"); // consider the time in date?
    } catch (JSONException e) {
      wasSuccessful = false;
      errorMessage = e.getMessage();
            Log.e("e.cp.ParseItem:",errorMessage);
            return null;
    }

    return new AnnItem(date,html,hasTime);
  }

  private Date parseDateString(String dateStr){
    //function to parse IsoDate
    SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); 
    Date result;

        try {
        result = format.parse(dateStr);     
    } catch (ParseException e) {
      wasSuccessful = false;
      errorMessage = e.getMessage();
            Log.e("e.cp.ParseDateString",errorMessage);
            return null;
    }

    return result;
  }
}




Java Source Code List

com.uth.uthportal.AboutScreen.java
com.uth.uthportal.CoursesFragment.java
com.uth.uthportal.DepartmentFragment.java
com.uth.uthportal.FoodFragment.java
com.uth.uthportal.MainScreen.java
com.uth.uthportal.SettingsScreen.java
com.uth.uthportal.adapter.AdapterManager.java
com.uth.uthportal.adapter.AdapterProvider.java
com.uth.uthportal.adapter.ExpandableListAdapter.java
com.uth.uthportal.adapter.SettingsAdapter.java
com.uth.uthportal.adapter.TabsPagerAdapter.java
com.uth.uthportal.buffers.AvailableCoursesParser.java
com.uth.uthportal.buffers.CoursesParser.java
com.uth.uthportal.buffers.FileOperation.java
com.uth.uthportal.buffers.FoodParser.java
com.uth.uthportal.buffers.GeneralAnnParser.java
com.uth.uthportal.buffers.SettingsManager.java
com.uth.uthportal.collections.AnnItem.java
com.uth.uthportal.collections.Announcements.java
com.uth.uthportal.collections.AvailableCourse.java
com.uth.uthportal.collections.CourseInfo.java
com.uth.uthportal.collections.Course.java
com.uth.uthportal.collections.DayMenu.java
com.uth.uthportal.collections.DefaultIntervals.java
com.uth.uthportal.collections.Dish.java
com.uth.uthportal.collections.Food.java
com.uth.uthportal.collections.GeneralAnnouncement.java
com.uth.uthportal.collections.Settings.java
com.uth.uthportal.network.ApiLinks.java
com.uth.uthportal.network.AppRater.java
com.uth.uthportal.network.AsyncJSONDownloader.java
com.uth.uthportal.network.JSONDownloader.java
com.uth.uthportal.service.DataSyncService.java
com.uth.uthportal.util.SystemUiHiderBase.java
com.uth.uthportal.util.SystemUiHiderHoneycomb.java
com.uth.uthportal.util.SystemUiHider.java