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
00027
00028
00029 package org.openmobileis.modules.profils.embedded.data;
00030
00031 import org.openmobileis.common.util.collection.Array;
00032 import org.openmobileis.embedded.webserver.WebServer;
00033 import com.microstar.xml.XmlParser;
00034 import java.io.StringReader;
00035 import org.openmobileis.common.util.log.*;
00036 import org.openmobileis.common.util.exception.ServerException;
00037
00038
00046 public class EmbeddedProfilChannelManager {
00047
00048 private static EmbeddedProfilChannelManager manager;
00049 private String profilFile;
00050 private Array profilChannels;
00051
00052 public static EmbeddedProfilChannelManager getManager() {
00053 if (manager == null) {
00054 synchronized(org.openmobileis.modules.profils.embedded.data.EmbeddedProfilChannelManager.class) {
00055 if (manager == null) {
00056 manager = new EmbeddedProfilChannelManager();
00057 }
00058 }
00059 }
00060 return manager;
00061 }
00062
00063 public EmbeddedProfilChannelManager() {
00064
00065 }
00066
00067
00068
00069
00070
00071 public Array getProfilChannels() {
00072 if(profilChannels != null) {
00073 return profilChannels;
00074 }
00075 try {
00076 java.io.FileInputStream file = new java.io.FileInputStream(this.profilFile);
00077 StringBuffer buffer = new StringBuffer();
00078 byte[] buf = new byte[512];
00079 int len;
00080 while ( ( len = file.read( buf ) ) != -1 ) {
00081 buffer.append(new String(buf, 0, len));
00082 }
00083 file.close();
00084 profilChannels = this.getChannels(buffer.toString(), true);
00085
00086 return profilChannels;
00087 } catch (java.io.FileNotFoundException e) {
00088 LogManager.traceError(LogServices.WEBSERVICE, "Can not read profil file : " + this.profilFile);
00089 } catch (java.io.IOException e) {
00090 LogManager.traceError(LogServices.WEBSERVICE, "Can not read profil file : " + this.profilFile);
00091 }
00092 return null;
00093 }
00094
00095
00096
00097
00098
00099 public Array getPDAAvailableChannels() {
00100 try {
00101 java.io.FileInputStream file = new java.io.FileInputStream(this.profilFile);
00102 StringBuffer buffer = new StringBuffer();
00103 byte[] buf = new byte[512];
00104 int len;
00105 while ( ( len = file.read( buf ) ) != -1 ) {
00106 buffer.append(new String(buf, 0, len));
00107 }
00108 file.close();
00109 return this.getChannels(buffer.toString(), false);
00110 } catch (java.io.FileNotFoundException e) {
00111 LogManager.traceError(LogServices.WEBSERVICE, "Can not read profil file : " + this.profilFile);
00112 } catch (java.io.IOException e) {
00113 LogManager.traceError(LogServices.WEBSERVICE, "Can not read profil file : " + this.profilFile);
00114 }
00115 return null;
00116 }
00117
00118
00119
00120
00121
00122 public void setInitialProfil(String xmldata) {
00123 try {
00124 java.io.FileOutputStream file = new java.io.FileOutputStream(this.profilFile);
00125 file.write(xmldata.getBytes());
00126 file.close();
00127 profilChannels = null;
00128 } catch (java.io.IOException e) {
00129 LogManager.traceError(LogServices.WEBSERVICE, "Can not read profil file : " + this.profilFile);
00130 }
00131 }
00132
00133
00134
00135
00136
00137 public Array getChannels (String xmlData, boolean takeSelectionAccount) {
00138 try {
00139 StringReader reader = new StringReader(xmlData);
00140 XmlParser parser = new XmlParser();
00141 EmbeddedProfilChannelsParser handler = new EmbeddedProfilChannelsParser(takeSelectionAccount);
00142 parser.setHandler(handler);
00143 parser.parse(null, null, reader);
00144 return handler.getChannels();
00145 } catch (Exception e) {
00146 LogManager.trace(new ServerException("Error during parsing XML data for profils ",e));
00147 return null;
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156 public String getChannelListData (String xmlData) {
00157 Array channels = this.getChannels(xmlData, true);
00158 StringBuffer buffer = new StringBuffer();
00159 for (int i=0; i < channels.size(); i++) {
00160 EmbeddedProfilChannel channel = (EmbeddedProfilChannel)channels.get(i);
00161 for (int j=0; j < channel.rubrics.size(); j++) {
00162 EmbeddedProfilRubric rubric = channel.getRubric(j);
00163 if (rubric.isSelected) {
00164 buffer.append(channel.getName());
00165 buffer.append("/");
00166 buffer.append(rubric.getName());
00167 buffer.append(",");
00168 }
00169 }
00170 }
00171
00172
00173 String result = buffer.toString();
00174 return result.substring(0, buffer.length()-1) + "\n";
00175 }
00176
00177
00178
00179
00180 public String getXmlDescription (Array channels) {
00181 StringBuffer buffer = new StringBuffer();
00182 for (int i=0; i < channels.size(); i++) {
00183 buffer.append( ((EmbeddedProfilChannel)channels.get(i)).getXmlDescription());
00184 }
00185 return buffer.toString();
00186 }
00187
00188
00189
00190 public static void main (String[] args) {
00191
00192 Array channels = EmbeddedProfilChannelManager.getManager().getProfilChannels();
00193 System.out.println("Size : " + channels.size());
00194 }
00195
00196
00197
00198 }