TerminalXmlProfilParser.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  *  Author : Philippe Delrieu
00023  *
00024  */
00025 
00026 package org.openmobileis.module.profiles.terminal;
00027 
00028 import org.xmlpull.v1.XmlPullParserException;
00029 
00030 import java.io.IOException;
00031 import java.io.Reader;
00032 
00033 import org.kxml2.io.KXmlParser;
00034 import org.openmobileis.common.util.collection.Array;
00035 
00044 public class TerminalXmlProfilParser {
00045     private TerminalProfile profil;
00046     private final static String TAG_XML = "xml";
00047 
00048     private final static String TAG_PROFILNAME = "PROFILNAME";
00049 
00050     private final static String TAG_MODULE = "MODULE";
00051 
00052 
00053     private String currentProfilName;
00054     private Array moduleList;
00055 
00056     private Reader reader;
00057 
00058     TerminalXmlProfilParser(Reader reader)  {
00059       this.reader = reader;
00060       moduleList = new Array(10);
00061     }
00062 
00063     public TerminalProfile parse() throws XmlPullParserException, IOException {
00064       try   {
00065         KXmlParser parser = new KXmlParser();
00066         parser.setInput(this.reader);
00067 
00068         int eventType = parser.getEventType();
00069         do {
00070           if (eventType == KXmlParser.START_DOCUMENT) {
00071           } else if (eventType == KXmlParser.END_DOCUMENT) {
00072           } else if (eventType == KXmlParser.START_TAG) {
00073             this.processStartElement(parser);
00074           } else if (eventType == KXmlParser.END_TAG) {
00075             this.processEndElement(parser);
00076           } else if (eventType == KXmlParser.TEXT) {
00077             this.processText(parser);
00078           }
00079           eventType = parser.next();
00080         } while (eventType != KXmlParser.END_DOCUMENT);
00081       } finally {
00082         reader.close();
00083       }
00084       return this.profil;
00085     }
00086 
00087     private void processStartElement(KXmlParser parser) {
00088       String name = parser.getName();
00089 
00090      if (name.equals(TerminalXmlProfilParser.TAG_XML)) {
00091         // Do nothing
00092      } else if (name.equals(TerminalXmlProfilParser.TAG_PROFILNAME)) {
00093         this.openProfilTag(parser);
00094      } else if (name.equals(TerminalXmlProfilParser.TAG_MODULE)) {
00095        this.openModuleTag(parser);
00096      }
00097     }
00098 
00099     private void openProfilTag(KXmlParser parser) {
00100       moduleList.clear();
00101       int count = parser.getAttributeCount();
00102       for (int i = 0; i < count; i++) {
00103         if (parser.getAttributeName(i).equals("name")) {
00104           currentProfilName = parser.getAttributeValue(i);
00105         }
00106       }
00107     }
00108 
00109     private void openModuleTag(KXmlParser parser) {
00110       String modulename = null;
00111       String version = null;
00112       int count = parser.getAttributeCount();
00113       for (int i = 0; i < count; i++) {
00114         if (parser.getAttributeName(i).equals("moduleName")) {
00115                 modulename = parser.getAttributeValue(i);
00116         } else if (parser.getAttributeName(i).equals("version")) {
00117           version = parser.getAttributeValue(i);
00118         }
00119       }
00120       TerminalProfilModule module = new TerminalProfilModule();
00121       module.moduleName = modulename;
00122       module.moduleVersion = version;
00123       moduleList.add(module);
00124    }
00125 
00126     private void processEndElement(KXmlParser parser) {
00127       String name = parser.getName();
00128 
00129       if (name.equals(TerminalXmlProfilParser.TAG_XML)) {
00130         // Do nothing
00131       } else if (name.equals(TerminalXmlProfilParser.TAG_PROFILNAME)) {
00132         profil = new TerminalProfile();
00133         TerminalProfilModule[] modules = new TerminalProfilModule[moduleList.size()];
00134         moduleList.toArray(modules);
00135         profil.moduleList = modules;
00136         profil.profileName = currentProfilName;
00137       }
00138     }
00139 
00140     private void processText(KXmlParser parser) throws XmlPullParserException {
00141       // Do nothing
00142     }
00143  
00144 }

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