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

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