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

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

Generated on Mon Jul 10 10:29:33 2006 for OpenMobileIS by  doxygen 1.4.4