TerminalXmlFileProfilDataFactory.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.common.user.profile.impl;
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.user.profile.Profile;
00035 import org.openmobileis.common.user.profile.ProfileDataFactory;
00036 import org.openmobileis.common.user.profile.ProfileNotFoundException;
00037 import org.openmobileis.common.user.profile.ProfileProperty;
00038 import org.openmobileis.common.user.profile.ProfileRubric;
00039 import org.openmobileis.common.util.collection.Array;
00040 
00049 public class TerminalXmlFileProfilDataFactory implements ProfileDataFactory {
00050   private Profile profil;
00051 
00052   class TerminalProfilListParser    {
00053     private final static String TAG_XML = "xml";
00054 
00055     private final static String TAG_PROFILLIST = "PROFILLIST";
00056 
00057     private final static String TAG_PROFILNAME = "PROFILNAME";
00058 
00059     private final static String TAG_ENTRY = "ENTRY";
00060 
00061     private final static String TAG_PROPERTY = "PROPERTY";
00062 
00063 
00064     private String currentProfilName;
00065     private Array rubricList;
00066     private Array propertyList;
00067 
00068     private Reader reader;
00069 
00070     TerminalProfilListParser(Reader reader)  {
00071       this.reader = reader;
00072       rubricList = new Array(10);
00073       propertyList = new Array(10);
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(TerminalProfilListParser.TAG_XML)) {
00103         // Do nothing
00104       } else if (name.equals(TerminalProfilListParser.TAG_PROFILLIST)) {
00105        //Do nothing
00106      } else if (name.equals(TerminalProfilListParser.TAG_PROFILNAME)) {
00107         this.openProfilTag(parser);
00108      } else if (name.equals(TerminalProfilListParser.TAG_ENTRY)) {
00109        this.openEntryTag(parser);
00110      } else if (name.equals(TerminalProfilListParser.TAG_PROPERTY)) {
00111        this.openPropertyTag(parser);
00112       }
00113     }
00114 
00115     private void openProfilTag(KXmlParser parser) {
00116       rubricList.clear();
00117       propertyList.clear();
00118       int count = parser.getAttributeCount();
00119       for (int i = 0; i < count; i++) {
00120         if (parser.getAttributeName(i).equals("name")) {
00121           currentProfilName = parser.getAttributeValue(i);
00122         }
00123       }
00124     }
00125 
00126     private void openEntryTag(KXmlParser parser) {
00127       String module = null;
00128       String rubric = null;
00129       String description = null;
00130       String version = null;
00131       int count = parser.getAttributeCount();
00132       for (int i = 0; i < count; i++) {
00133         if (parser.getAttributeName(i).equals("moduleName")) {
00134           module = parser.getAttributeValue(i);
00135         } else if (parser.getAttributeName(i).equals("rubricName")) {
00136           rubric = parser.getAttributeValue(i);
00137         } else if (parser.getAttributeName(i).equals("description")) {
00138           description = parser.getAttributeValue(i);
00139         } else if (parser.getAttributeName(i).equals("version")) {
00140           version = parser.getAttributeValue(i);
00141         }
00142       }
00143       ProfileRubric rub = new ProfileRubric(module, rubric, null, new ProfileProperty[0]);
00144       if (description != null)  {
00145         rub.description = description;
00146       }
00147       if (version != null)  {
00148         rub.version = version;
00149       }
00150       rubricList.add(rub);
00151    }
00152 
00153     private void openPropertyTag(KXmlParser parser) {
00154       String key = null;
00155       String value = null;
00156       String moduleName = null;
00157       int count = parser.getAttributeCount();
00158       for (int i = 0; i < count; i++) {
00159         if (parser.getAttributeName(i).equals("key")) {
00160           key = parser.getAttributeValue(i);
00161         } else if (parser.getAttributeName(i).equals("value")) {
00162           value = parser.getAttributeValue(i);
00163         } else if (parser.getAttributeName(i).equals("module")) {
00164           moduleName = parser.getAttributeValue(i);
00165         }
00166       }
00167       ProfileProperty prop = new ProfileProperty(key, value);
00168       prop.moduleName = moduleName;
00169       propertyList.add(prop);
00170    }
00171 
00172     private void processEndElement(KXmlParser parser) {
00173       String name = parser.getName();
00174 
00175       if (name.equals(TerminalProfilListParser.TAG_XML)) {
00176         // Do nothing
00177       } else if (name.equals(TerminalProfilListParser.TAG_PROFILNAME)) {
00178         profil = new Profile();
00179         ProfileRubric[] rubs = new ProfileRubric[rubricList.size()];
00180         rubricList.toArray(rubs);
00181         profil.profilRubric = rubs;
00182         ProfileProperty[] props = new ProfileProperty[propertyList.size()];
00183         propertyList.toArray(props);
00184         profil.profilProperties = props;
00185         profil.profilName = currentProfilName;
00186       }
00187     }
00188 
00189     private void processText(KXmlParser parser) throws XmlPullParserException {
00190       // Do nothing
00191     }
00192   }
00193 
00194   public TerminalXmlFileProfilDataFactory(Reader reader) throws IOException, XmlPullParserException   {
00195     TerminalProfilListParser parser = new TerminalProfilListParser(reader);
00196     parser.parse();
00197   }
00198 
00199   public Profile getProfil(String profilName)  throws ProfileNotFoundException {
00200     if (!profilName.equals(profil.profilName)) throw new ProfileNotFoundException("profil not found :"+profilName);
00201     return profil;
00202   }
00203 
00204   public Profile getParsedProfil() {
00205     return profil;
00206   }
00207 
00208 
00209 }

Generated on Tue May 22 23:01:12 2007 for OpenMobileIS by  doxygen 1.5.1-p1