XmlFileProfilDataFactory.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.*;
00029 import org.xml.sax.helpers.DefaultHandler;
00030 
00031 import java.util.*;
00032 
00033 import javax.xml.parsers.SAXParser;
00034 import javax.xml.parsers.SAXParserFactory;
00035 
00036 import org.openmobileis.common.user.profile.Profile;
00037 import org.openmobileis.common.user.profile.ProfileDataFactory;
00038 import org.openmobileis.common.user.profile.ProfileProperty;
00039 import org.openmobileis.common.user.profile.ProfileService;
00040 import org.openmobileis.common.util.PropertiesManager;
00041 import org.openmobileis.common.util.log.*;
00042 import org.openmobileis.common.util.file.FileUtilities;
00043 import org.openmobileis.common.util.exception.DefaultException;
00044 
00053 public class XmlFileProfilDataFactory implements ProfileDataFactory {
00054   private HashMap profilList;
00055 
00056   class ProfilListParser  extends DefaultHandler  {
00057     private String currentProfilName;
00058     private HashMap serviceList;
00059     private ArrayList propertyList;
00060 
00061     ProfilListParser()  {
00062       serviceList = new HashMap(10);
00063       propertyList = new ArrayList(10);
00064     }
00065 
00067     public void startDocument() {
00068     } // startDocument()
00069 
00071     public void startElement(java.lang.String uri,
00072                          java.lang.String qname,
00073                          java.lang.String name,
00074                          Attributes attrs)
00075                   throws SAXException {
00076       if (name.equals("PROFILNAME")) {
00077         serviceList.clear();
00078         propertyList.clear();
00079         currentProfilName = attrs.getValue("name");
00080         if (currentProfilName == null)  {
00081           throw new SAXException("TAG PROFILNAME with no attribut name");
00082         }
00083       } else if (name.equals("ENTRY"))  {
00084         String module = attrs.getValue("moduleName");
00085         if (module == null)  {
00086           module = attrs.getValue("channelName"); //put here for back compatibility with version before 1.0R5
00087          if (module == null) throw new SAXException("TAG ENTRY with no attribut moduleName");
00088         }
00089         String service = attrs.getValue("rubricName");
00090         if (service == null)  {
00091           throw new SAXException("TAG ENTRY with no attribut rubricName");
00092         }
00093         
00094         ProfileService rub = new ProfileService(module, service, null, new ProfileProperty[0]);
00095         String description = attrs.getValue("description");
00096         if (description != null)  {
00097           rub.description = description;
00098         } else  {
00099           rub.description = "";
00100         }
00101         String version = attrs.getValue("version");
00102         rub.version = version;
00103         serviceList.put(module+"/"+service, rub);
00104      } else if (name.equals("PROPERTY")) {
00105         String key = attrs.getValue("key");
00106         if (key == null)  {
00107           throw new SAXException("TAG PROPERTY with no attribut key for property");
00108         }
00109         String value = attrs.getValue("value");
00110         if (value == null)  {
00111           throw new SAXException("TAG PROPERTY with no attribut value for property");
00112         }
00113         ProfileProperty prop = new ProfileProperty(key, value);
00114         String moduleName = attrs.getValue("module");
00115         if (moduleName != null)  {
00116           prop.moduleName = moduleName;
00117         }
00118         propertyList.add(prop);
00119       }
00120     } // startElement(String,AttributeList)
00121 
00122     public void endElement (String uri, String qname, String localName) throws SAXException {
00123       if (localName.equals("PROFILNAME")) {
00124         ProfileService[] rubs = new ProfileService[serviceList.size()];
00125         serviceList.values().toArray(rubs);
00126         ProfileProperty[] props = new ProfileProperty[propertyList.size()];
00127         propertyList.toArray(props);
00128         Profile newidlprofil = new Profile("0", props, rubs);
00129         newidlprofil.profilName = currentProfilName;
00130         profilList.put(currentProfilName, newidlprofil);
00131       }
00132     }
00133 
00134 
00136     public void characters(char ch[], int start, int length) {
00137     }
00138   }
00139 
00140   public XmlFileProfilDataFactory() {
00141     profilList = new HashMap(10);
00142     try {
00143       String file =  PropertiesManager.getManager().getProperty("org.openmobileis.common.user.profil.xmlprofilfile.path");
00144       if (file == null) {
00145         LogManager.traceError(LogServices.DEFAULTSERVICE, "No profil list XML file defined. Profil management is not initialized correctly");
00146         return;
00147       }
00148       file = FileUtilities.convertFileNameToSystem(file);
00149       ProfilListParser handler = new ProfilListParser();
00150       SAXParserFactory factory = SAXParserFactory.newInstance();
00151       // Parse the input 
00152       SAXParser saxParser = factory.newSAXParser();
00153       saxParser.parse(new InputSource(new java.io.FileReader(file)), handler);
00154     } catch (Exception e) {
00155       LogManager.trace(new DefaultException ("Problem occurs during loading profil list file", e));
00156     }
00157   }
00158 
00163   public Profile getProfil(String profilName)  {
00164     return (Profile) profilList.get(profilName);
00165   }
00166 }

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