Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

XmlFileProfilDataFactory.java

00001 /*
00002  *        OpenMobileIS - a free Java Framework for mobile applications
00003  *
00004  *   Copyright (C) 2002  Philippe Delrieu.
00005  *
00006  *   This program is free software; you can redistribute it and/or
00007  *   modify it under the terms of the GNU General Public
00008  *   License as published by the Free Software Foundation; either
00009  *   version 2 of the License, or (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *   General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this library; if not, write to the Free
00018  *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  * 
00020  * Philippe Delrieu kept the rigth to distribute all code Copyrighted by philippe Delrieu
00021  *  under other licence term even commercial one.
00022  *  
00023  *  Modifications :
00024  *  2002 Creation P.Delrieu
00025  * 
00026  */
00027 
00028 package org.openmobileis.common.user.profil.impl;
00029 
00030 import org.xml.sax.*;
00031 import org.xml.sax.helpers.DefaultHandler;
00032 
00033 import java.util.*;
00034 
00035 import javax.xml.parsers.SAXParser;
00036 import javax.xml.parsers.SAXParserFactory;
00037 
00038 import org.openmobileis.common.user.profil.Profil;
00039 import org.openmobileis.common.user.profil.ProfilDataFactory;
00040 import org.openmobileis.common.user.profil.ProfilProperty;
00041 import org.openmobileis.common.user.profil.ProfilRubric;
00042 import org.openmobileis.common.util.PropertiesManager;
00043 import org.openmobileis.common.util.log.*;
00044 import org.openmobileis.common.util.file.FileUtilities;
00045 import org.openmobileis.common.util.exception.DefaultException;
00046 
00055 public class XmlFileProfilDataFactory implements ProfilDataFactory {
00056   private HashMap profilList;
00057 
00058   class ProfilListParser  extends DefaultHandler  {
00059     private String currentProfilName;
00060     private HashMap rubricList;
00061     private ArrayList propertyList;
00062 
00063     ProfilListParser()  {
00064       rubricList = new HashMap(10);
00065       propertyList = new ArrayList(10);
00066     }
00067 
00069     public void startDocument() {
00070     } // startDocument()
00071 
00073     public void startElement(java.lang.String uri,
00074                          java.lang.String qname,
00075                          java.lang.String name,
00076                          Attributes attrs)
00077                   throws SAXException {
00078       if (name.equals("PROFILNAME")) {
00079         rubricList.clear();
00080         propertyList.clear();
00081         currentProfilName = attrs.getValue("name");
00082         if (currentProfilName == null)  {
00083           throw new SAXException("TAG PROFILNAME with no attribut name");
00084         }
00085       } else if (name.equals("ENTRY"))  {
00086         String channel = attrs.getValue("channelName");
00087         if (channel == null)  {
00088           throw new SAXException("TAG ENTRY with no attribut channelName");
00089         }
00090         String rubric = attrs.getValue("rubricName");
00091         if (rubric == null)  {
00092           throw new SAXException("TAG ENTRY with no attribut rubricName");
00093         }
00094         ProfilRubric rub = new ProfilRubric(channel, rubric, null, new ProfilProperty[0]);
00095         String description = attrs.getValue("description");
00096         if (description != null)  {
00097           rub.description = description;
00098         } else  {
00099           rub.description = "";
00100         }
00101         rubricList.put(channel+"/"+rubric, rub);
00102      } else if (name.equals("PROPERTY")) {
00103         String key = attrs.getValue("key");
00104         if (key == null)  {
00105           throw new SAXException("TAG PROPERTY with no attribut key for property");
00106         }
00107         String value = attrs.getValue("value");
00108         if (value == null)  {
00109           throw new SAXException("TAG PROPERTY with no attribut value for property");
00110         }
00111         ProfilProperty prop = new ProfilProperty(key, value);
00112         String channelName = attrs.getValue("channel");
00113         if (channelName != null)  {
00114           String rubricName = attrs.getValue("rubric");
00115           if (rubricName == null) {
00116            throw new SAXException("TAG PROPERTY with no attribut rubric for property");
00117           }
00118           String channelRub = channelName+"/"+rubricName;
00119           ProfilRubric profRub = (ProfilRubric)rubricList.get(channelRub);
00120           if (profRub == null)  {
00121             throw new SAXException("TAG PROPERTY no Rubric define for a rubric property "+channelRub);
00122           }
00123           ProfilProperty[] oldList = profRub.propertiesList;
00124           ProfilProperty[] newList = new ProfilProperty[oldList.length+1];
00125           System.arraycopy(oldList, 0, newList, 0, oldList.length);
00126           newList[oldList.length] = prop;
00127           profRub.propertiesList = newList;
00128 
00129         } else  {
00130           propertyList.add(prop);
00131         }
00132       }
00133     } // startElement(String,AttributeList)
00134 
00135     public void endElement (String uri, String qname, String localName) throws SAXException {
00136       if (localName.equals("PROFILNAME")) {
00137         ProfilRubric[] rubs = new ProfilRubric[rubricList.size()];
00138         rubricList.values().toArray(rubs);
00139         ProfilProperty[] props = new ProfilProperty[propertyList.size()];
00140         propertyList.toArray(props);
00141         Profil newidlprofil = new Profil("0", props, rubs);
00142         profilList.put(currentProfilName, newidlprofil);
00143       }
00144     }
00145 
00146 
00148     public void characters(char ch[], int start, int length) {
00149     }
00150   }
00151 
00152   public XmlFileProfilDataFactory() {
00153     profilList = new HashMap(10);
00154     try {
00155       String file =  PropertiesManager.getManager().getProperty("org.openmobileis.common.user.profil.xmlprofilfile.path");
00156       if (file == null) {
00157         LogManager.traceError(LogServices.DEFAULTSERVICE, "No profil list XML file defined. Profil management is not initialized correctly");
00158         return;
00159       }
00160       file = FileUtilities.convertFileNameToSystem(file);
00161       ProfilListParser handler = new ProfilListParser();
00162       SAXParserFactory factory = SAXParserFactory.newInstance();
00163       // Parse the input 
00164       SAXParser saxParser = factory.newSAXParser();
00165       saxParser.parse(new InputSource(new java.io.FileReader(file)), handler);
00166     } catch (Exception e) {
00167       LogManager.trace(new DefaultException ("Problem occurs during loading profil list file", e));
00168     }
00169   }
00170 
00175   public Profil getProfil(String profilName)  {
00176     return (Profil) profilList.get(profilName);
00177   }
00178 }

Generated on Wed Dec 14 21:05:36 2005 for OpenMobileIS by  doxygen 1.4.4