ProfileParser.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  
00023  *  Modifications :
00024  *  2006 Creation P.Delrieu
00025  * 
00026  */
00027 package org.openmobileis.modules.profiles.old.embedded.core;
00028 
00029 import java.io.IOException;
00030 import java.io.Reader;
00031 import java.util.StringTokenizer;
00032 
00033 import org.kxml2.io.KXmlParser;
00034 import org.openmobileis.common.util.collection.Array;
00035 import org.openmobileis.common.util.log.LogManager;
00036 import org.openmobileis.common.util.log.LogServices;
00037 import org.openmobileis.modules.profiles.old.ProfileManager;
00038 import org.openmobileis.modules.profiles.old.ProfileModule;
00039 import org.openmobileis.modules.profiles.old.ProfileRubric;
00040 import org.openmobileis.modules.profiles.terminal.RubricLoader;
00041 import org.xmlpull.v1.XmlPullParserException;
00042 
00051 public class ProfileParser {
00052 
00053   private final static String TAG_XML = "xml";
00054 
00055   private final static String TAG_PROFIL = "profil";
00056 
00057   private final static String TAG_RUBRIC = "rubric";
00058 
00059   private final static String TAG_PROPERTY = "property";
00060 
00061   private final static String TAG_RUBRIC_LOADER = "loader";
00062 
00063   private final static String TAG_RUBRIC_SYNCHROLISTENER = "synchrolistener";
00064 
00065   private final static String TAG_MODULE = "module";
00066 
00067   private final static String TAG_MODRUBRIC = "modrubric";
00068 
00069   private final static String TAG_MODDEPEND = "moddepend";
00070 
00071   private Reader reader;
00072 
00073   private ProfileRubric currentRubric = null;
00074 
00075   private ProfileModule currentModule = null;
00076 
00077   private boolean propertyTagOpened = false;
00078 
00079   private String propertyName = null;
00080 
00081   private String propertyValue = null;
00082 
00083 //  private Array rubrics = new Array(5);
00084 
00085 //  private Array modules = new Array(5);
00086 
00087   String currentTag;
00088 
00089   public ProfileParser(Reader reader) {
00090     this.reader = reader;
00091   }
00092 
00093   public void parse() throws XmlPullParserException, IOException {
00094     KXmlParser parser = new KXmlParser();
00095     parser.setInput(this.reader);
00096 
00097     int eventType = parser.getEventType();
00098     do {
00099       if (eventType == KXmlParser.START_DOCUMENT) {
00100       } else if (eventType == KXmlParser.END_DOCUMENT) {
00101       } else if (eventType == KXmlParser.START_TAG) {
00102         this.processStartElement(parser);
00103       } else if (eventType == KXmlParser.END_TAG) {
00104         this.processEndElement(parser);
00105       } else if (eventType == KXmlParser.TEXT) {
00106         this.processText(parser);
00107       }
00108       eventType = parser.next();
00109     } while (eventType != KXmlParser.END_DOCUMENT);
00110   }
00111 
00112   private void openRubricTag(KXmlParser parser) {
00113     if (currentRubric != null) {
00114       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new rubric while the preceding one isn't closed");
00115       return;
00116     }
00117     if (currentModule != null) {
00118       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new rubric while the preceding module isn't closed");
00119       return;
00120     }
00121     currentRubric = new ProfileRubric();
00122     int count = parser.getAttributeCount();
00123     for (int i = 0; i < count; i++) {
00124       if (parser.getAttributeName(i).equals("name")) {
00125         currentRubric.setName(parser.getAttributeValue(i));
00126       } else {
00127         LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute " + parser.getAttributeName(i) + " for rubric");
00128       }
00129     }
00130   }
00131 
00132   private void closeRubricTag() {
00133     if (currentRubric == null) {
00134       LogManager.traceError(LogServices.SYNCHROSERVICE, "Corrupted profil file, found rubric closing tag while no rubric is open");
00135       return;
00136     }
00137     ProfileManager.getManager().addRubric(currentRubric);
00138     currentRubric = null;
00139   }
00140 
00141   private void openModuleTag(KXmlParser parser) {
00142     if (currentRubric != null) {
00143       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new module while the preceding rubric isn't closed");
00144       currentRubric = null;
00145     }
00146     if (currentModule != null) {
00147       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, starting a new module while the preceding one isn't closed");
00148       return;
00149     }
00150     currentModule = new ProfileModule();
00151     int count = parser.getAttributeCount();
00152     for (int i = 0; i < count; i++) {
00153       if (parser.getAttributeName(i).equals("name")) {
00154         currentModule.setName(parser.getAttributeValue(i));
00155       } else if (parser.getAttributeName(i).equals("version")) {
00156         currentModule.setVersion(parser.getAttributeValue(i));
00157       } else {
00158         LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute " + parser.getAttributeName(i) + " for modrubric");
00159       }
00160     }
00161   }
00162 
00163   private void closeModuleTag() {
00164     if (currentModule == null) {
00165       LogManager.traceError(LogServices.SYNCHROSERVICE, "Corrupted profil file, found module closing tag while no module is open");
00166       return;
00167     }
00168     ProfileManager.getManager().addModule(currentModule);
00169     currentModule = null;
00170   }
00171 
00172   private void openPropertyTag(KXmlParser parser) {
00173     if (currentRubric != null && propertyTagOpened == true) {
00174       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, opening a new rubric property while the preceding one isn't closed");
00175       return;
00176     } else if (currentModule != null && propertyTagOpened == true) {
00177       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, opening a new rubric property while the preceding one isn't closed");
00178       return;
00179     } else if (currentModule == null && currentRubric == null) {
00180       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, argument property must be included in a rubric or in a module");
00181       return;
00182     }
00183 
00184     propertyTagOpened = true;
00185     propertyName = null;
00186     propertyValue = null;
00187 
00188     int count = parser.getAttributeCount();
00189     for (int i = 0; i < count; i++) {
00190       if (parser.getAttributeName(i).equals("name")) {
00191         propertyName = parser.getAttributeValue(i);
00192       } else if (parser.getAttributeName(i).equals("value")) {
00193         propertyValue = parser.getAttributeValue(i);
00194       } else {
00195         LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, unrecognized attribute " + parser.getAttributeName(i) + " for modrubric");
00196       }
00197     }
00198 
00199     if (propertyName == null) {
00200       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, property tag with no name !");
00201     }
00202     if (propertyValue == null) {
00203       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, property tag " + propertyName + " with no value !");
00204     }
00205   }
00206 
00207   private void closePropertyTag() {
00208     if (propertyTagOpened == false) {
00209       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, closing tag property while it's not opened");
00210     } else if (propertyName == null) {
00211       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, closing tag property while it has no name defined");
00212     } else if (propertyValue == null) {
00213       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, closing tag property " + propertyName + " while it has no value defined");
00214     } else {
00215       if (currentRubric != null) {
00216         currentRubric.addProperty(propertyName, propertyValue);
00217       } else if (currentModule != null) {
00218         currentModule.addProperty(propertyName, propertyValue);
00219       } else {
00220         LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, property " + propertyName + " outside rubrics and modules");
00221       }
00222     }
00223     propertyTagOpened = false;
00224     propertyName = null;
00225     propertyValue = null;
00226   }
00227 
00228 
00229   private void openLoaderTag(KXmlParser parser) {
00230     if (currentRubric == null) {
00231       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, loader tag found outside a rubric");
00232       return;
00233     }
00234    int count = parser.getAttributeCount();
00235     for (int i = 0; i < count; i++) {
00236       if (parser.getAttributeName(i).equals("class")) {
00237         String classname = parser.getAttributeValue(i).trim();
00238         try {
00239           Object toload = Class.forName(classname).newInstance();
00240           if (toload instanceof RubricLoader) {
00241             currentRubric.setLoader((RubricLoader)toload) ;
00242           } else  {
00243             LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Bad profil file, rubric loader class is not a RubricLoader :"+classname);
00244           }      
00245         } catch (Throwable ex)  {
00246           LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Bad profil file, error during creating rubric loader instance :"+ex.getMessage());
00247         }
00248       }
00249     }
00250   }
00251 
00252   private void openSynchroListenerTag(KXmlParser parser) {
00253     if (currentRubric == null) {
00254       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, synchrolistener tag found outside a rubric");
00255       return;
00256     }
00257     ProfileRubricSynchroListener synclistener = new ProfileRubricSynchroListener();
00258     int count = parser.getAttributeCount();
00259     for (int i = 0; i < count; i++) {
00260       if (parser.getAttributeName(i).equals("name")) {
00261         synclistener.name = parser.getAttributeValue(i).trim();
00262       } else if (parser.getAttributeName(i).equals("depends")) {
00263         String dep = parser.getAttributeValue(i).trim();
00264         Array deplist = new Array();
00265         StringTokenizer token = new StringTokenizer(dep, ",");
00266         while (token.hasMoreElements()) {
00267           deplist.add(token.nextElement());
00268         }
00269         String[] strdeplist = new String[deplist.size()];
00270         deplist.toArray(strdeplist);
00271         synclistener.dependsList = strdeplist;
00272       }
00273     }
00274     currentRubric.addSynchroListener(synclistener);
00275   }
00276 
00277   private void openModDependTag(KXmlParser parser) {
00278     if (currentModule == null) {
00279       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, moddepend tag found outside a module");
00280       return;
00281     }
00282     int count = parser.getAttributeCount();
00283     DependProfileModule depend = new DependProfileModule();
00284     for (int i = 0; i < count; i++) {
00285       if (parser.getAttributeName(i).equals("modulename")) {
00286         depend.setModuleName(parser.getAttributeValue(i).trim());
00287       } else if (parser.getAttributeName(i).equals("version")) {
00288         depend.setModuleVersion(parser.getAttributeValue(i).trim());
00289       }
00290     }
00291     currentModule.addDependProfilModule(depend);
00292   }
00293 
00294   private void openModrubricTag(KXmlParser parser) {
00295     if (currentModule == null) {
00296       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, argument modrubric must be included in a module");
00297       return;
00298     }
00299  //   modrubricName = new ProfilModuleRubric();
00300     int count = parser.getAttributeCount();
00301     DependProfileRubric depend = new DependProfileRubric();
00302     for (int i = 0; i < count; i++) {
00303       if (parser.getAttributeName(i).equals("name")) {
00304         String name = parser.getAttributeValue(i);
00305         depend.setRubricName(name);
00306       } else if (parser.getAttributeName(i).equals("depends")) {
00307         String dep = parser.getAttributeValue(i).trim();
00308         Array deplist = new Array();
00309         StringTokenizer token = new StringTokenizer(dep, ",");
00310         while (token.hasMoreElements()) {
00311           deplist.add(token.nextElement());
00312         }
00313         String[] strdeplist = new String[deplist.size()];
00314         deplist.toArray(strdeplist);
00315         depend.setRubricDepends(strdeplist);
00316       }
00317     }
00318     currentModule.addModuleRubric(depend);
00319   }
00320 
00321   private void processStartElement(KXmlParser parser) {
00322     String name = parser.getName();
00323 
00324     if (name.equals(ProfileParser.TAG_XML)) {
00325       // Do nothing
00326     } else if (name.equals(ProfileParser.TAG_PROFIL)) {
00327       // Do nothing
00328     } else if (name.equals(ProfileParser.TAG_RUBRIC)) {
00329       this.openRubricTag(parser);
00330     } else if (name.equals(ProfileParser.TAG_MODULE)) {
00331       this.openModuleTag(parser);
00332     } else if (name.equals(ProfileParser.TAG_PROPERTY)) {
00333       this.openPropertyTag(parser);
00334     } else if (name.equals(ProfileParser.TAG_MODRUBRIC)) {
00335       this.openModrubricTag(parser);
00336     } else if (name.equals(ProfileParser.TAG_RUBRIC_LOADER)) {
00337       this.openLoaderTag(parser);
00338     } else if (name.equals(ProfileParser.TAG_RUBRIC_SYNCHROLISTENER)) {
00339       this.openSynchroListenerTag(parser);
00340     } else if (name.equals(ProfileParser.TAG_MODDEPEND)) {
00341       this.openModDependTag(parser);
00342     }
00343   }
00344 
00345   private void processEndElement(KXmlParser parser) {
00346     String name = parser.getName();
00347 
00348     if (name.equals(ProfileParser.TAG_XML)) {
00349       // Do nothing
00350     } else if (name.equals(ProfileParser.TAG_PROFIL)) {
00351       // Do nothing
00352     } else if (name.equals(ProfileParser.TAG_RUBRIC)) {
00353       this.closeRubricTag();
00354     } else if (name.equals(ProfileParser.TAG_MODULE)) {
00355       this.closeModuleTag();
00356     } else if (name.equals(ProfileParser.TAG_PROPERTY)) {
00357       this.closePropertyTag();
00358     }
00359   }
00360 
00361   private void processText(KXmlParser parser) throws XmlPullParserException {
00362     // Do nothing
00363   }
00364 }

Generated on Mon Dec 4 11:03:29 2006 for OpenMobileIS by  doxygen 1.5.1-p1