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.profil.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.profil.Profile;
00037 import org.openmobileis.common.user.profil.ProfileDataFactory;
00038 import org.openmobileis.common.user.profil.ProfileProperty;
00039 import org.openmobileis.common.user.profil.ProfileRubric;
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 rubricList;
00059     private ArrayList propertyList;
00060 
00061     ProfilListParser()  {
00062       rubricList = 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         rubricList.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 rubric = attrs.getValue("rubricName");
00090         if (rubric == null)  {
00091           throw new SAXException("TAG ENTRY with no attribut rubricName");
00092         }
00093         ProfileRubric rub = new ProfileRubric(module, rubric, null, new ProfileProperty[0]);
00094         String description = attrs.getValue("description");
00095         if (description != null)  {
00096           rub.description = description;
00097         } else  {
00098           rub.description = "";
00099         }
00100         rubricList.put(module+"/"+rubric, rub);
00101      } else if (name.equals("PROPERTY")) {
00102         String key = attrs.getValue("key");
00103         if (key == null)  {
00104           throw new SAXException("TAG PROPERTY with no attribut key for property");
00105         }
00106         String value = attrs.getValue("value");
00107         if (value == null)  {
00108           throw new SAXException("TAG PROPERTY with no attribut value for property");
00109         }
00110         ProfileProperty prop = new ProfileProperty(key, value);
00111         String moduleName = attrs.getValue("module");
00112         if (moduleName != null)  {
00113           prop.moduleName = moduleName;
00114         }
00115         propertyList.add(prop);
00116       }
00117     } // startElement(String,AttributeList)
00118 
00119     public void endElement (String uri, String qname, String localName) throws SAXException {
00120       if (localName.equals("PROFILNAME")) {
00121         ProfileRubric[] rubs = new ProfileRubric[rubricList.size()];
00122         rubricList.values().toArray(rubs);
00123         ProfileProperty[] props = new ProfileProperty[propertyList.size()];
00124         propertyList.toArray(props);
00125         Profile newidlprofil = new Profile("0", props, rubs);
00126         profilList.put(currentProfilName, newidlprofil);
00127       }
00128     }
00129 
00130 
00132     public void characters(char ch[], int start, int length) {
00133     }
00134   }
00135 
00136   public XmlFileProfilDataFactory() {
00137     profilList = new HashMap(10);
00138     try {
00139       String file =  PropertiesManager.getManager().getProperty("org.openmobileis.common.user.profil.xmlprofilfile.path");
00140       if (file == null) {
00141         LogManager.traceError(LogServices.DEFAULTSERVICE, "No profil list XML file defined. Profil management is not initialized correctly");
00142         return;
00143       }
00144       file = FileUtilities.convertFileNameToSystem(file);
00145       ProfilListParser handler = new ProfilListParser();
00146       SAXParserFactory factory = SAXParserFactory.newInstance();
00147       // Parse the input 
00148       SAXParser saxParser = factory.newSAXParser();
00149       saxParser.parse(new InputSource(new java.io.FileReader(file)), handler);
00150     } catch (Exception e) {
00151       LogManager.trace(new DefaultException ("Problem occurs during loading profil list file", e));
00152     }
00153   }
00154 
00159   public Profile getProfil(String profilName)  {
00160     return (Profile) profilList.get(profilName);
00161   }
00162 }

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