Android Open Source - Android-Universal-Notifier Tasks X M L Loader






From Project

Back to project page Android-Universal-Notifier.

License

The source code is released under:

Apache License

If you think the Android project Android-Universal-Notifier 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.mairos.universalnotifier.model;
//  ww w .j a v  a 2 s  . c  om
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

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

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class TasksXMLLoader {
  
  public static ArrayList<NotificationTask> loadAndParseFromFolder(String f_folderName, final Context f_context){
    
    Logger.addToLog("Parsing files from folder '" + f_folderName + "' started!", f_context);
    
    ArrayList<NotificationTask> resArr = new ArrayList<NotificationTask>();
    File directory = new File(f_folderName);
    final File[] files = directory.listFiles();
    
    if (files == null){
      Intent intentCT = new Intent();
      intentCT.setAction(Const.ACTION_SHOW_CURRENT_TASKS);
      intentCT.putExtra(Const.DIALOG_MESSAGE, "Update from '" + f_folderName + "' failed! Directory not found");
      f_context.sendBroadcast(intentCT);
      
      Logger.addToLog("Wrong folder", f_context);
    } else {
      XmlPullParserFactory factory = null;
        XmlPullParser xpp = null;
      try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        xpp = factory.newPullParser();
      } catch (XmlPullParserException e1) {
        e1.printStackTrace();
      }
  
      ArrayList<NotificationTask> tmpResArr = new ArrayList<NotificationTask>();
          for ( File file : files ) {
            FileInputStream fin = null;
        try {
          fin = new FileInputStream(file);    
          xpp.setInput(fin, "utf-8");
          tmpResArr = parseXML(xpp, f_context);
          for (NotificationTask notificationTask : tmpResArr) {
            resArr.add(notificationTask);
          }
          fin.close();
          
          if (tmpResArr.size() > 0)
            Logger.addToLog("File " + file.getName() + " succesfully parsed", f_context);
          else 
            Logger.addToLog("Currently described tasks in  " + file.getName() + " not found", f_context);
          
        } catch (FileNotFoundException e) {
          Logger.addToLog("FileNotFoundException " + file.getName(), f_context);
          e.printStackTrace();
        } catch (Exception e) {
          Logger.addToLog("Exception with access " + file.getName(), f_context);
          e.printStackTrace();
        }      
          }
    }
    
    return resArr;
  }
  
  private static ArrayList<NotificationTask> parseXML(XmlPullParser xpp, Context f_context){
    ArrayList<AttachmentData> attachments = new ArrayList<AttachmentData>();
    ArrayList<NotificationTask> resArr = new ArrayList<NotificationTask>();
    NotificationTask nt = null;
    
    String name = "";
    String JS_script = "";
    String repeate = "";
    String start_time = "";
    String finish_time = "";
    String interval = "";
    String lifetime = "";
    String channel = "";
    String email_login = "";
    String email_pwd = "";
    
    SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss", Locale.US);
    
    ArrayList<String> persons = new ArrayList<String>();
    
    boolean taskTagFound = false;
    boolean taskTagClosed = false;
    
    try {
      while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
          switch (xpp.getEventType()) {
              case XmlPullParser.START_DOCUMENT:
                break;
              case XmlPullParser.START_TAG:
                taskTagFound = true;
                //Log.d(Const.LOG_TAG, "START_TAG: name = " + xpp.getName() + ", depth = " + xpp.getDepth() + ", attrCount = " + xpp.getAttributeCount());        
                if (xpp.getName().equals("task")){
                  persons = new ArrayList<String>();
                  attachments = new ArrayList<AttachmentData>();
                  name = xpp.getAttributeValue(null, "name");
                  repeate = xpp.getAttributeValue(null, "repeate");
                  start_time = xpp.getAttributeValue(null, "start_time");
                  lifetime = xpp.getAttributeValue(null, "lifetime");
                  channel = xpp.getAttributeValue(null, "channel");
                  if (channel.equals(NotificationTask.EMAIL_CHANNEL)){
                    email_login = xpp.getAttributeValue(null, "email_login");
                    email_pwd = xpp.getAttributeValue(null, "email_pwd");
                  }
                  if (repeate.equals("regular")){
                    finish_time = xpp.getAttributeValue(null, "finish_time");
                    interval = xpp.getAttributeValue(null, "interval");
                  }
                } else if (xpp.getName().equals("script")) {
                  int token = xpp.nextToken();
                    while(token!=XmlPullParser.CDSECT){
                      token = xpp.nextToken();
                    }
                  JS_script = xpp.getText();
                } else if (xpp.getName().equals("person")) {
                  persons.add(xpp.getAttributeValue(null, "sent_to"));
                } else if (xpp.getName().equals("attachment")) {
                  if (xpp.getAttributeValue(null, "type").equals(AttachmentData.FTP_TYPE)){
                    attachments.add(new AttachmentData( AttachmentData.FTP_TYPE,
                                    xpp.getAttributeValue(null, "URL"),
                                    xpp.getAttributeValue(null, "login"),
                                    xpp.getAttributeValue(null, "password"),
                                    xpp.getAttributeValue(null, "fileName")));
                  } else if (xpp.getAttributeValue(null, "type").equals(AttachmentData.WEB_TYPE)){
                    attachments.add(new AttachmentData( AttachmentData.WEB_TYPE,
                                    xpp.getAttributeValue(null, "URL"),
                                    "","",""));
                  }
                }
                /*for (int i = 0; i < xpp.getAttributeCount(); i++) {
                    tmp = tmp + xpp.getAttributeName(i) + " = " + xpp.getAttributeValue(i) + ", ";
                }*/
                break;
              case XmlPullParser.END_TAG:
                if (xpp.getName().equals("task")){
                  taskTagClosed = true;
                  if (name != "" && name != null && JS_script != "" && JS_script != null && repeate != null && repeate.equals("once") && start_time != "" && start_time != null && channel != "" && channel != null && persons.size() > 0){
                    if (channel.equals(NotificationTask.SMS_CHANNEL) || (channel.equals(NotificationTask.EMAIL_CHANNEL) && email_login != "" && email_login != null && email_pwd != null && email_pwd != "")){
                      try {
                        Date time_start = fmt.parse(start_time);
                        int intLifetime = Integer.valueOf(lifetime);
                        nt = new NotificationTask(name, time_start, intLifetime, JS_script, channel, email_login, email_pwd, persons, attachments);
                        resArr.add(nt);
                      } 
                      catch (ParseException e) { Logger.addToLog("datetime wrong format", f_context); }
                      catch (NumberFormatException e) { Logger.addToLog("lifetime wrong format", f_context);}
                    }
                  }
                  else if (name != "" && name != null && JS_script != "" && JS_script != null && repeate != null && repeate.equals("regular") && start_time != "" && start_time != null && finish_time != "" && finish_time != null && interval != "" && interval != null && channel != "" && channel != null && persons.size() > 0){
                    try {                      
                      Date time_start = fmt.parse(start_time);
                      Date time_finish = fmt.parse(finish_time);
                      int inter = Integer.valueOf(interval);
                      int intLifetime = Integer.valueOf(lifetime);
                      nt = new NotificationTask(name, time_start, time_finish, inter, intLifetime, JS_script, channel, email_login, email_pwd, persons, attachments);
                      resArr.add(nt);
                    }
                    catch (ParseException e) { Logger.addToLog("datetime wrong format", f_context); }
                    catch (NumberFormatException e) { Logger.addToLog("interval or lifetime wrong format", f_context);}
                  }
                }
                //Log.d(Const.LOG_TAG, "END_TAG: name = " + xpp.getName());
                break;
              case XmlPullParser.TEXT:
                //Log.d(DataKeeper.TAG, "text = " + xpp.getText());
                break;
              default:
                break;
         }
         xpp.next();
       }
      
      if (!taskTagFound) Logger.addToLog("<task> tag not opened", f_context);
      if (!taskTagClosed) Logger.addToLog("<task> tag not closed", f_context);
      if (name == "" || name == null) Logger.addToLog("name not described propertly", f_context);
      if (JS_script == "" || JS_script == null) Logger.addToLog("java script CDATA section not described propertly", f_context);
      if (repeate == "" || repeate == null) Logger.addToLog("repeate not described propertly", f_context);
      if (start_time == "" || start_time == null) Logger.addToLog("start_time not described propertly", f_context);
      if (repeate != null && (finish_time == null || finish_time == "") && repeate.equals("regular")) Logger.addToLog("finish_time not described propertly", f_context);
      if (repeate != null && (interval == null || interval == "") && repeate.equals("regular")) Logger.addToLog("interval not described propertly", f_context);
      if (channel == "" || channel == null) Logger.addToLog("channel not described propertly", f_context);
      if (persons.size() == 0) Logger.addToLog("<persons> not described propertly", f_context);
      if (channel != null && channel.equals(NotificationTask.EMAIL_CHANNEL)){
        if (!(email_login != "" && email_login != null && email_pwd != null && email_pwd != ""))
          Logger.addToLog("xml parsinr error : describe login and password for email!", f_context);
      }
      
    } catch (MalformedURLException e) {
      Logger.addToLog("MalformedURLException", f_context);
      Log.d(Const.LOG_TAG, "MalformedURLException");
      e.printStackTrace();
    } catch (IOException e) {
      Logger.addToLog("XML IOException", f_context);
      Log.d(Const.LOG_TAG, "XML IOException");    
      e.printStackTrace();
    } catch (XmlPullParserException e) {
      Logger.addToLog("XmlPullParserException - XML format corrupted", f_context);
      Log.d(Const.LOG_TAG, "XmlPullParserException");
      e.printStackTrace();
    }
      
    return resArr;    
  }
  
  public static ArrayList<NotificationTask> loadAndParseFromWeb(String f_URL, final Context f_context){
    ArrayList<NotificationTask> resArr = new ArrayList<NotificationTask>();
    
    XmlPullParserFactory factory = null;
    try {
      factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XmlPullParser xpp = factory.newPullParser();
      URL url = new URL(f_URL);
      InputStream stream = url.openStream();
      xpp.setInput(stream, null);
      
      resArr = parseXML(xpp, f_context);
    } catch (XmlPullParserException e) {
      Log.d(Const.LOG_TAG, "XmlPullParserException");
      e.printStackTrace();
    } catch (IOException e) {
      Log.d(Const.LOG_TAG, "IOException");
      
          Intent intentCT = new Intent();
      intentCT.setAction(Const.ACTION_SHOW_CURRENT_TASKS);
      intentCT.putExtra(Const.DIALOG_MESSAGE, "Connection to " + TasksQueue.getTasksURL() + " failed! Please check tasks xml URL in settings!");
      f_context.sendBroadcast(intentCT);
      
      e.printStackTrace();
    }
    
    return resArr;
  }
}




Java Source Code List

com.mairos.universalnotifier.UI.LogFragmentTab.java
com.mairos.universalnotifier.UI.MainActivity.java
com.mairos.universalnotifier.UI.SettingsActivity.java
com.mairos.universalnotifier.UI.TabListener.java
com.mairos.universalnotifier.UI.TaskInfoActivity.java
com.mairos.universalnotifier.UI.TasksFragmentTab.java
com.mairos.universalnotifier.model.AlarmManagerBroadcastReceiver.java
com.mairos.universalnotifier.model.AttachmentData.java
com.mairos.universalnotifier.model.Const.java
com.mairos.universalnotifier.model.JSInterpreter.java
com.mairos.universalnotifier.model.LauncherBroadcastReceiver.java
com.mairos.universalnotifier.model.Logger.java
com.mairos.universalnotifier.model.NotificationService.java
com.mairos.universalnotifier.model.NotificationTask.java
com.mairos.universalnotifier.model.TasksQueue.java
com.mairos.universalnotifier.model.TasksXMLLoader.java
com.mairos.universalnotifier.network.GMailSender.java
com.mairos.universalnotifier.network.JSSEProvider.java
com.mairos.universalnotifier.network.NetworkOperations.java
com.mairos.universalnotifier.network.SMSSender.java