BHVEssenParser.java :  » App » foodwatcher » de » peacei » android » foodwatcher » parser » Android Open Source

Android Open Source » App » foodwatcher 
foodwatcher » de » peacei » android » foodwatcher » parser » BHVEssenParser.java
/*
 *  FoodWatcher 
 *  presenting menue-data from HTML sources of the 
 *  Studentenwerk Bremen
 *  see <http://www.studentenwerk.bremen.de/files/main_info/essen/plaene/essenplan.htm> 
 *
 *  contact me via <mtthsfrdrch@gmail.com> or <http://twitter.com/peacei> 
 *
 *
 *  Copyright (C) 2010  Matthias Friedrich
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *  or see <http://www.gnu.org/licenses/>.
 */

package de.peacei.android.foodwatcher.parser;

import de.peacei.android.foodwatcher.data.Food;
import de.peacei.android.foodwatcher.data.Menu;


import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author peacei
 */
public class BHVEssenParser extends BremenParser implements FoodParser{
  
  private static BHVEssenParser instance = null;

    public static BHVEssenParser parse() {
        if(instance == null || instance.getMenu().getValuability() != Menu.VALUABLE)
          instance = new BHVEssenParser();
        
        return instance;
    }
    
    public static void erase() {
      instance = null;
    }
  
  
  private BHVEssenParser() {
    super("http://www.studentenwerk.bremen.de/files/main_info/essen/plaene/bhvessen.php");
    
    if(valuability == Menu.VALUABLE) {
      this.parseWeekNumber();
      this.parseFood();
    }
  }
  
    protected Food[] parse(String keyWord) {
      
      try {
        Food[] food = new Food[5];
      
        int startIndex = html.indexOf(keyWord);
        int endIndex = html.indexOf("</tr>", startIndex)-1;
        int index = endIndex;
        String htmlPart = html.substring(startIndex, endIndex);

        startIndex = 10;
        endIndex = startIndex;
        String dishPerDay;
        byte type;
        byte extra;
        Matcher matcher = null;

        for (byte i=0; i<5; i++) {
          type = Food.NO_TYPE;
          extra = Food.NO_EXTRA;

          startIndex = htmlPart.indexOf("<td", startIndex)+3;
          endIndex = htmlPart.indexOf("</td>", startIndex);

          dishPerDay = htmlPart.substring(startIndex-3, endIndex);
          
          // Zeilenumbruch durch Leerzeichen ersetzen
                dishPerDay=Pattern.compile("<br>").matcher(dishPerDay).
                        replaceAll(" ");
                
                // Parsen der Kategorie
                matcher = Pattern.compile("\\([VGSRFWL]\\)").matcher(dishPerDay);
                if(matcher.find()) {
                    type = (byte) dishPerDay.charAt(matcher.start()+1);
                    dishPerDay = matcher.replaceAll("");
                }
                else {
                  matcher = Pattern.compile(
                    "(schwein\\.gif|Rind\\.gif|Vegetarisch\\.gif|Huhn\\.gif|" +
                  "fisch\\.gif|SunFlower\\.gif|=\" \"|Wild\\.gif|wild\\.gif|Lamm\\.gif|lamm\\.gif)").matcher(dishPerDay);
                  if(matcher.find()) {
                    char c = dishPerDay.charAt(matcher.start());
                    switch (c) {
                    case 'R': type = Food.BEEF; break;
                    case 'V': type = Food.VEGETARIAN; break;
                    case 'H': type = Food.POULTRY; break;
                    case 's': type = Food.PORK; break;
                    case 'f': type = Food.FISH; break;
                    case 'S': type = Food.VEGAN; break;
                    case '=': case 'w': case 'W':  type = Food.GAME; break;
                    case 'l': case 'L': type = Food.LAMB; break;
                    default: type = Food.NO_TYPE;
                    }
                    dishPerDay = matcher.replaceAll("");
                  }
                }

                // Entfernen der HTML-Tags
                dishPerDay=Pattern.compile("<[^>]+>").matcher(dishPerDay).
                    replaceAll("");

                // Parsen und Entfernen der Tagessuppe bzw. des Desserts
                matcher = Pattern.compile("Tagessuppe").matcher(dishPerDay);
                if(matcher.find()) {
                  dishPerDay = matcher.replaceAll("");
                  extra = Food.SOUP;
                }
                else {
                  matcher = Pattern.compile("(Dessert|Tagesdessert)").matcher(dishPerDay);
                  if(matcher.find()) {
                    dishPerDay = matcher.replaceAll("");
                      extra = Food.DESSERT;
                  }
                }

                // Entfernen doppelter Leerzeichen
                dishPerDay=Pattern.compile("\\s{2,}").matcher(dishPerDay).
                    replaceAll(" ");

                // Entfernen eines Leerzeichens am Beginn
                if(dishPerDay.charAt(0)==' ')
                    dishPerDay = dishPerDay.substring(1);
                
                // Ersetzen des &-Zeichens
                dishPerDay=Pattern.compile("\\s&\\s").matcher(dishPerDay).
                  replaceAll(" und ");
                
                // Entfernen ungltiger Zeichen
                dishPerDay=Pattern.compile("([*<>\\.*+#&]|nbsp;)+").matcher(dishPerDay).
                  replaceAll("");

            
                if(dishPerDay.length()==0) dishPerDay = "Entfllt";
            
                if(keyWord.charAt(0)=='V')
                  food[i] = new Food("Vegetarisch", dishPerDay, type, extra);
                else if(keyWord.equals("EXTRA"))
                  food[i] = new Food("Extra", dishPerDay, type, extra);
                else if(keyWord.equals("ESSEN 1"))
                  food[i] = new Food("Essen 1", dishPerDay, type, extra);
                else if(keyWord.equals("ESSEN 2"))
                  food[i] = new Food("Essen 2", dishPerDay, type, extra);
        }
        
        htmlPart = html.substring(index+17, html.indexOf("</tr>", index+4));
        startIndex = index+4;
        endIndex = index+4;
        StringBuffer price;
        for(int i=0; i<5; i++) {
          price = new StringBuffer();
          for(int j=0; j<4; j++) {
            startIndex = htmlPart.indexOf("<td ", endIndex);
            endIndex = htmlPart.indexOf("</td>", startIndex);
            
            switch(j) {
                case 1: case 3:
                  matcher = Pattern.compile("\\d,\\d\\d").matcher(htmlPart.substring(startIndex, endIndex));
                  if(matcher.find()) {
                    price.append(matcher.group().trim());
                    price.append('');
                  }
                  else {
                    price.append("--");
                  }
                  break;
                case 2:
                  price.append('/');
                  break;
                default: break;    
            }
          }
          food[i].setPrice(price.toString());
        }
      
        return food;
        
      } catch(Exception e) {
            return null;
        }
    }

    public void parseFood() {
        
        food = new Vector<Food[]>(0);// new Food[2][5];

        Food[] foodArr;
        if((foodArr=parse("ESSEN 1"))!=null) food.add(foodArr);
        if((foodArr=parse("ESSEN 2"))!=null) food.add(foodArr);
        if((foodArr=parse("VEGETARISCH"))!=null) food.add(foodArr);
        if((foodArr=parse("EXTRA"))!=null) food.add(foodArr);
        if(food.size()==0) valuability = Menu.ERROR;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.