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

EmbeddedProfilChannelManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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  *  Modifications :
00025  *  2005 Creation P.Delrieu
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    // profilFile = WebServer.propsDir + "profilData";
00065   }
00066 
00067   /*
00068   * Get the channels / rubrics from initial profil and return them
00069   * but not selected
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   * Get the channels / rubrics from initial profil and return them
00097   * but not selected
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   *  Once the synchro manager get the response from openmobileis server for the profil, it asks
00120   * the receiver to store it (in a file).
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   * Parse XML elements and builds channels / rubrics
00135   * @return: CyberArray containing EmbeddedProfilChannel objects
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  * Return the channel/rubric selected for the profil
00153  * the choosen is an url like channel/rubric?context1=value1,channel2/rubric?context2=value2 ended
00154  * by a carriage return.
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           } // end for rubric
00170     }// end for channels
00171     // remove the last semicolon and replace it by a carriage return (can not use #replace method
00172     // from StringBuffer, not implemented in PersonnalJava VM
00173     String result = buffer.toString();
00174     return result.substring(0, buffer.length()-1) + "\n";
00175  }
00176 
00177  /*
00178  * Return the xml data relative to the channels stored in the array
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  // test program
00190   public static void main (String[] args) {
00191   //  WebServer.propsDir = "\\Program Files\\Java\\properties\\";
00192     Array channels = EmbeddedProfilChannelManager.getManager().getProfilChannels();
00193     System.out.println("Size : " + channels.size());
00194   }
00195 
00196 
00197 
00198 }

Generated on Thu Oct 6 10:06:32 2005 for OpenMobileIS by  doxygen 1.4.3