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

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