00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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.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 }
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");
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 }
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
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 }