XmlModuleParser.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.module.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.module.profiles.terminal.ProfileRubricSynchroListener;
00038 import org.xmlpull.v1.XmlPullParserException;
00039 
00048 public class XmlModuleParser {
00049 
00050   private final static String TAG_XML = "xml";
00051 
00052   private final static String TAG_RUBRIC = "rubric";
00053 
00054   private final static String TAG_PROPERTY = "property";
00055 
00056   private final static String TAG_RUBRIC_LOADER = "loader";
00057 
00058   private final static String TAG_RUBRIC_SYNCHROLISTENER = "synchrolistener";
00059 
00060   private final static String TAG_MODULE = "module";
00061 
00062   private final static String TAG_MODDEPEND = "moddepend";
00063 
00064   private Reader reader;
00065 
00066   private ProfileModuleRubric currentRubric = null;
00067 
00068   private ProfileModule moduletoload = null;
00069 
00070   String currentTag;
00071 
00072   public XmlModuleParser(Reader reader) {
00073     this.reader = reader;
00074   }
00075 
00076   public void parse() throws XmlPullParserException, IOException {
00077     try {
00078       KXmlParser parser = new KXmlParser();
00079       parser.setInput(this.reader);
00080       int eventType = parser.getEventType();
00081       do {
00082         if (eventType == KXmlParser.START_DOCUMENT) {
00083         } else if (eventType == KXmlParser.END_DOCUMENT) {
00084         } else if (eventType == KXmlParser.START_TAG) {
00085           this.processStartElement(parser);
00086         } else if (eventType == KXmlParser.END_TAG) {
00087           this.processEndElement(parser);
00088         } else if (eventType == KXmlParser.TEXT) {
00089           this.processText(parser);
00090         }
00091         eventType = parser.next();
00092       } while (eventType != KXmlParser.END_DOCUMENT);
00093     } finally   {
00094       reader.close();
00095     }
00096   }
00097 
00098   private void processStartElement(KXmlParser parser) {
00099     String name = parser.getName();
00100 
00101     if (name.equals(XmlModuleParser.TAG_XML)) {
00102       // Do nothing
00103     } else if (name.equals(XmlModuleParser.TAG_MODULE)) {
00104       this.openModuleTag(parser);
00105    } else if (name.equals(XmlModuleParser.TAG_RUBRIC)) {
00106       this.openRubricTag(parser);
00107     } else if (name.equals(XmlModuleParser.TAG_PROPERTY)) {
00108       this.openPropertyTag(parser);
00109     } else if (name.equals(XmlModuleParser.TAG_RUBRIC_LOADER)) {
00110       this.openLoaderTag(parser);
00111     } else if (name.equals(XmlModuleParser.TAG_RUBRIC_SYNCHROLISTENER)) {
00112       this.openSynchroListenerTag(parser);
00113     } else if (name.equals(XmlModuleParser.TAG_MODDEPEND)) {
00114       this.openModDependTag(parser);
00115     }
00116   }
00117 
00118   private void openModuleTag(KXmlParser parser) {
00119     if (currentRubric != null) {
00120       currentRubric = null;
00121     }
00122     moduletoload = new ProfileModule();
00123     int count = parser.getAttributeCount();
00124     for (int i = 0; i < count; i++) {
00125       if (parser.getAttributeName(i).equals("name")) {
00126         moduletoload.setName(parser.getAttributeValue(i));
00127       } else if (parser.getAttributeName(i).equals("version")) {
00128         moduletoload.setVersion(parser.getAttributeValue(i));
00129       } else if (parser.getAttributeName(i).equals("serverjar")) {
00130         moduletoload.setServerjar(parser.getAttributeValue(i));
00131       } else if (parser.getAttributeName(i).equals("terminaljar")) {
00132         moduletoload.setTerminaljar(parser.getAttributeValue(i));
00133       } else if (parser.getAttributeName(i).equals("terminaltype")) {
00134         moduletoload.setTerminalType(parser.getAttributeValue(i));
00135       } else {
00136         LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, unrecognized attribute " + parser.getAttributeName(i) + " for modrubric");
00137       }
00138     }
00139   }
00140 
00141   private void openRubricTag(KXmlParser parser) {
00142     if (moduletoload == null) {
00143       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, starting a new rubric while module isn't opened.");
00144       return;
00145     }
00146     currentRubric = new ProfileModuleRubric();
00147     int count = parser.getAttributeCount();
00148     for (int i = 0; i < count; i++) {
00149       if (parser.getAttributeName(i).equals("name")) {
00150         currentRubric.setName(parser.getAttributeValue(i));
00151       } else if (parser.getAttributeName(i).equals("depends")) {
00152         String dep = parser.getAttributeValue(i).trim();
00153         Array deplist = new Array();
00154         StringTokenizer token = new StringTokenizer(dep, ",");
00155         while (token.hasMoreElements()) {
00156           deplist.add(token.nextElement());
00157         }
00158         String[] strdeplist = new String[deplist.size()];
00159         deplist.toArray(strdeplist);
00160         currentRubric.setDependsList(strdeplist);
00161       } 
00162     }
00163   }
00164 
00165   private void closeRubricTag() {
00166     if (currentRubric == null) {
00167       LogManager.traceError(LogServices.SYNCHROSERVICE, "Corrupted module file, found rubric closing tag while no rubric is open");
00168       return;
00169     }
00170     if (moduletoload == null) {
00171       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, end rubrictag while module isn't opened.");
00172       return;
00173     }
00174     moduletoload.addModuleRubric(currentRubric);
00175     currentRubric = null;
00176   }
00177 
00178   private void openPropertyTag(KXmlParser parser) {
00179     String propertyName = null;
00180     String propertyValue = null;
00181 
00182     int count = parser.getAttributeCount();
00183     for (int i = 0; i < count; i++) {
00184       if (parser.getAttributeName(i).equals("name")) {
00185         propertyName = parser.getAttributeValue(i);
00186       } else if (parser.getAttributeName(i).equals("value")) {
00187         propertyValue = parser.getAttributeValue(i);
00188       }
00189     }
00190     if (currentRubric != null) {
00191       currentRubric.addProperty(propertyName, propertyValue);
00192     } else if (moduletoload != null) {
00193       moduletoload.addProperty(propertyName, propertyValue);
00194     }
00195   }
00196 
00197   private void openLoaderTag(KXmlParser parser) {
00198     if (currentRubric == null) {
00199       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, loader tag found outside a rubric");
00200       return;
00201     }
00202    int count = parser.getAttributeCount();
00203     for (int i = 0; i < count; i++) {
00204       if (parser.getAttributeName(i).equals("class")) {
00205         String classname = parser.getAttributeValue(i).trim();
00206         currentRubric.setLoaderClass(classname) ;
00207       }
00208     }
00209   }
00210 
00211   private void openSynchroListenerTag(KXmlParser parser) {
00212     if (currentRubric == null) {
00213       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, synchrolistener tag found outside a rubric");
00214       return;
00215     }
00216     ProfileRubricSynchroListener synclistener = new ProfileRubricSynchroListener();
00217     int count = parser.getAttributeCount();
00218     for (int i = 0; i < count; i++) {
00219       if (parser.getAttributeName(i).equals("name")) {
00220         synclistener.name = parser.getAttributeValue(i).trim();
00221       } else if (parser.getAttributeName(i).equals("depends")) {
00222         String dep = parser.getAttributeValue(i).trim();
00223         Array deplist = new Array();
00224         StringTokenizer token = new StringTokenizer(dep, ",");
00225         while (token.hasMoreElements()) {
00226           deplist.add(token.nextElement());
00227         }
00228         String[] strdeplist = new String[deplist.size()];
00229         deplist.toArray(strdeplist);
00230         synclistener.dependsList = strdeplist;
00231       }
00232     }
00233     currentRubric.addSynchroListener(synclistener);
00234   }
00235 
00236   private void openModDependTag(KXmlParser parser) {
00237     if (moduletoload == null) {
00238       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, moddepend tag found outside a module");
00239       return;
00240     }
00241     int count = parser.getAttributeCount();
00242     DependProfileModule depend = new DependProfileModule();
00243     for (int i = 0; i < count; i++) {
00244       if (parser.getAttributeName(i).equals("modulename")) {
00245         depend.setModuleName(parser.getAttributeValue(i).trim());
00246       } else if (parser.getAttributeName(i).equals("version")) {
00247         depend.setModuleVersion(parser.getAttributeValue(i).trim());
00248       }
00249     }
00250     moduletoload.addDependProfilModule(depend);
00251   }
00252   
00253   private void processEndElement(KXmlParser parser) {
00254     String name = parser.getName();
00255 
00256     if (name.equals(XmlModuleParser.TAG_XML)) {
00257       // Do nothing
00258     } else if (name.equals(XmlModuleParser.TAG_RUBRIC)) {
00259       this.closeRubricTag();
00260     } 
00261   }
00262 
00263   private void processText(KXmlParser parser) throws XmlPullParserException {
00264     // Do nothing
00265   }
00266 
00267   public ProfileModule getModuletoload() {
00268     return moduletoload;
00269   }
00270 }

Generated on Mon Jan 14 17:29:50 2008 for OpenMobileIS by  doxygen 1.5.4