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.xmlpull.v1.XmlPullParserException;
00038 
00047 public class XmlModuleParser {
00048         
00049         class FileReadAndParseInputStream extends Reader        {
00050                 private Reader reader;
00051                 private StringBuffer readdata;
00052                 
00053                 FileReadAndParseInputStream(Reader reader)      {
00054                         this.reader = reader;
00055                         this.readdata = new StringBuffer();
00056                 }
00057                 
00058                 public int read(char[] cbuf, int off, int len) throws IOException {
00059                         int read = this.reader.read(cbuf, off, len);
00060                         if (read >-1){
00061                                 char[] readbuff = new char[read];
00062                                 System.arraycopy(cbuf, off, readbuff, 0, read);
00063                                 this.readdata.append(readbuff);
00064                         }
00065                         return read;
00066                 }
00067                 
00068                 protected Object clone() throws CloneNotSupportedException {
00069                         FileReadAndParseInputStream newone = new FileReadAndParseInputStream((Reader)this.reader);
00070                         return newone;
00071                 }
00072                 
00073                 public void close() throws IOException {
00074                         this.reader.close();
00075                 }
00076                 
00077                 public void mark(int readAheadLimit)    throws IOException      {
00078                         this.reader.mark(readAheadLimit);
00079                 }
00080                 
00081                 public boolean markSupported()  {
00082                         return this.reader.markSupported();
00083                 }
00084                 
00085                 public int read() throws IOException {
00086                         int c =  this.reader.read();
00087                         this.readdata.append(c);
00088                         return c;
00089                 }
00090                 
00091                 public int read(char[] cbuf) throws IOException {
00092                         return this.reader.read(cbuf);
00093                 }
00094 
00095                 public boolean ready() throws IOException {
00096                         return this.reader.ready();
00097                 }
00098                 
00099     public void reset() throws IOException {
00100         this.readdata.setLength(0);
00101         this.reader.reset();
00102     }
00103     
00104     public long skip(long n) throws IOException {
00105         return this.reader.skip(n);
00106     }
00107     
00108     public String getReadData() {
00109         return this.readdata.toString();
00110     }
00111                 
00112         }
00113 
00114   private final static String TAG_XML = "xml";
00115 
00116   private final static String TAG_PROPERTY = "property";
00117 
00118   private final static String TAG_MODULE_LOADER = "loader";
00119 
00120   private final static String TAG_RUBRIC_SYNCHROLISTENER = "synchrolistener";
00121 
00122   private final static String TAG_MODULE = "module";
00123 
00124   private final static String TAG_MODDEPEND = "moddepend";
00125 
00126   private Reader reader;
00127 
00128   private ProfileModule moduletoload = null;
00129 
00130   String currentTag;
00131 
00132   public XmlModuleParser(Reader reader) {
00133     this.reader = reader;
00134   }
00135 
00136   public void parse() throws XmlPullParserException, IOException {
00137     try {
00138       KXmlParser parser = new KXmlParser();
00139       FileReadAndParseInputStream instrem = new FileReadAndParseInputStream(this.reader);
00140       parser.setInput(instrem);
00141       int eventType = parser.getEventType();
00142       do {
00143         if (eventType == KXmlParser.START_DOCUMENT) {
00144         } else if (eventType == KXmlParser.END_DOCUMENT) {
00145         } else if (eventType == KXmlParser.START_TAG) {
00146           this.processStartElement(parser);
00147         } else if (eventType == KXmlParser.END_TAG) {
00148           this.processEndElement(parser);
00149         } else if (eventType == KXmlParser.TEXT) {
00150           this.processText(parser);
00151         }
00152         eventType = parser.next();
00153       } while (eventType != KXmlParser.END_DOCUMENT);
00154       this.moduletoload.setXMLFile(instrem.getReadData());
00155     } finally   {
00156       reader.close();
00157     }
00158   }
00159 
00160   private void processStartElement(KXmlParser parser) {
00161     String name = parser.getName();
00162 
00163     if (name.equals(XmlModuleParser.TAG_XML)) {
00164       // Do nothing
00165     } else if (name.equals(XmlModuleParser.TAG_MODULE)) {
00166       this.openModuleTag(parser);
00167     } else if (name.equals(XmlModuleParser.TAG_PROPERTY)) {
00168       this.openPropertyTag(parser);
00169     } else if (name.equals(XmlModuleParser.TAG_MODULE_LOADER)) {
00170       this.openModuleLoaderTag(parser);
00171     } else if (name.equals(XmlModuleParser.TAG_RUBRIC_SYNCHROLISTENER)) {
00172       this.openSynchroListenerTag(parser);
00173     } else if (name.equals(XmlModuleParser.TAG_MODDEPEND)) {
00174       this.openModDependTag(parser);
00175     }
00176   }
00177 
00178   private void openModuleTag(KXmlParser parser) {
00179     moduletoload = new ProfileModule();
00180     int count = parser.getAttributeCount();
00181     for (int i = 0; i < count; i++) {
00182       if (parser.getAttributeName(i).equals("name")) {
00183         moduletoload.setName(parser.getAttributeValue(i));
00184       } else if (parser.getAttributeName(i).equals("version")) {
00185         moduletoload.setVersion(parser.getAttributeValue(i));
00186       } else if (parser.getAttributeName(i).equals("serverjar")) {
00187         moduletoload.setServerjar(parser.getAttributeValue(i));
00188       } else if (parser.getAttributeName(i).equals("terminaljar")) {
00189         moduletoload.setTerminaljar(parser.getAttributeValue(i));
00190       } else if (parser.getAttributeName(i).equals("terminaltype")) {
00191         moduletoload.setTerminalType(parser.getAttributeValue(i));
00192       } else {
00193         LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, unrecognized attribute " + parser.getAttributeName(i) + " for modrubric");
00194       }
00195     }
00196   }
00197 
00198   private void openPropertyTag(KXmlParser parser) {
00199     String propertyName = null;
00200     String propertyValue = null;
00201 
00202     int count = parser.getAttributeCount();
00203     for (int i = 0; i < count; i++) {
00204       if (parser.getAttributeName(i).equals("name")) {
00205         propertyName = parser.getAttributeValue(i);
00206       } else if (parser.getAttributeName(i).equals("value")) {
00207         propertyValue = parser.getAttributeValue(i);
00208       }
00209     }
00210     if (moduletoload != null) {
00211       moduletoload.addProperty(propertyName, propertyValue);
00212     }
00213   }
00214 
00215   private void openModuleLoaderTag(KXmlParser parser) {
00216     if (moduletoload == null) {
00217       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, loader tag found outside a rubric");
00218       return;
00219     }
00220    int count = parser.getAttributeCount();
00221     for (int i = 0; i < count; i++) {
00222       if (parser.getAttributeName(i).equals("class")) {
00223         String classname = parser.getAttributeValue(i).trim();
00224         moduletoload.setModuleLoaderClass(classname) ;
00225       }
00226     }
00227   }
00228 
00229   private void openSynchroListenerTag(KXmlParser parser) {
00230     if (moduletoload == null) {
00231       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted profil file, synchrolistener tag found outside a Module");
00232       return;
00233     }
00234     ProfileModuleSynchroListener synclistener = new ProfileModuleSynchroListener();
00235     int count = parser.getAttributeCount();
00236     for (int i = 0; i < count; i++) {
00237       if (parser.getAttributeName(i).equals("name")) {
00238         synclistener.name = parser.getAttributeValue(i).trim();
00239       } else if (parser.getAttributeName(i).equals("target")) {
00240         String target = parser.getAttributeValue(i).trim();
00241         synclistener.targetClassName = target;
00242       } else if (parser.getAttributeName(i).equals("listener")) {
00243         String listener = parser.getAttributeValue(i).trim();
00244         synclistener.listernerClassName = listener;
00245       } else if (parser.getAttributeName(i).equals("depends")) {
00246         String dep = parser.getAttributeValue(i).trim();
00247         Array deplist = new Array();
00248         StringTokenizer token = new StringTokenizer(dep, ",");
00249         while (token.hasMoreElements()) {
00250           deplist.add(token.nextElement());
00251         }
00252         String[] strdeplist = new String[deplist.size()];
00253         deplist.toArray(strdeplist);
00254         synclistener.dependsList = strdeplist;
00255       }
00256     }
00257     moduletoload.addSynchroListener(synclistener);
00258   }
00259 
00260   private void openModDependTag(KXmlParser parser) {
00261     if (moduletoload == null) {
00262       LogManager.traceWarning(LogServices.SYNCHROSERVICE, "Corrupted module file, moddepend tag found outside a module");
00263       return;
00264     }
00265     int count = parser.getAttributeCount();
00266     DependProfileModule depend = new DependProfileModule();
00267     for (int i = 0; i < count; i++) {
00268       if (parser.getAttributeName(i).equals("modulename")) {
00269         depend.setModuleName(parser.getAttributeValue(i).trim());
00270       } else if (parser.getAttributeName(i).equals("version")) {
00271         depend.setModuleVersion(parser.getAttributeValue(i).trim());
00272       }
00273     }
00274     moduletoload.addDependProfilModule(depend);
00275   }
00276   
00277   private void processEndElement(KXmlParser parser) {
00278     String name = parser.getName();
00279 
00280     if (name.equals(XmlModuleParser.TAG_XML)) {
00281       // Do nothing
00282     }
00283   }
00284 
00285   private void processText(KXmlParser parser) throws XmlPullParserException {
00286     // Do nothing
00287   }
00288 
00289   public ProfileModule getModuletoload() {
00290     return moduletoload;
00291   }
00292 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4