KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > layout > TemplateSet


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.layout;
19
20 import freecs.Server;
21 import freecs.util.FileMonitor;
22 import freecs.interfaces.IReloadable;
23
24 import java.io.*;
25 import java.util.*;
26
27 public class TemplateSet implements IReloadable {
28     private Hashtable tpl;
29     private Properties props;
30     public String JavaDoc name;
31     private int hashCode=Integer.MIN_VALUE;
32     private boolean isValide;
33
34     private File msgSet;
35     private long lastModified;
36     private boolean msgSetPresent=false;
37     private TemplateManager tm;
38
39     public TemplateSet(File dir, TemplateManager tm) throws IOException {
40         this.tm = tm;
41         this.isValide = false;
42         this.name = dir.getName ();
43         this.tpl = createTemplates(dir);
44         if (!msgSetPresent) {
45             if (name.equals("default")) {
46                 Server.log (this, "Default-templateset doesn't have a message.set-file", Server.MSG_ERROR, Server.LVL_HALT);
47             } else {
48                 TemplateSet foreignTS = tm.getTemplateSet("default");
49                 if (foreignTS == null) {
50                     Server.log (this, "No default-templateset present", Server.MSG_ERROR, Server.LVL_MAJOR);
51                 } else {
52                     props = foreignTS.getMessageTemplateSet();
53                     msgSetPresent=true;
54                 }
55             }
56         }
57         if (!"admin".equals(name)) { // admin-templates are different
58
if (!checkTemplateCompleteness(templatesNeeded))
59                 return;
60             if (!msgSetPresent) {
61                 Server.log (this, "TemplateSet.construct: Templateset has no message.set! Ignoring.", Server.MSG_STATE, Server.LVL_MAJOR);
62                 this.isValide= false;
63                 return;
64             }
65         } else {
66             if (!checkTemplateCompleteness(adminTemplates)) {
67                 Server.log (this, "TemplateSet.construct: Admin-Templateset doesn't have a header and wount work without it", Server.MSG_STATE, Server.LVL_MAJOR);
68                 return;
69             }
70         }
71         this.isValide=true;
72     }
73
74     private Hashtable createTemplates(File dir) {
75         Hashtable tempTable = new Hashtable();
76         File tFiles[] = dir.listFiles ();
77         for (int i = 0; i < tFiles.length; i++) {
78             if (!tFiles[i].isFile ())
79                 continue;
80             if (tFiles[i].getName ().equalsIgnoreCase ("message.set")) try {
81                 readMessageSet (tFiles[i]);
82                 continue;
83             } catch (FileNotFoundException fnfe) {
84                 // doesn't happen
85
} catch (IOException ioe) {
86                 Server.debug(this, "message.set of " + name + " caused exception", ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
87             }
88             try {
89                 Template t = new Template(tFiles[i], this);
90                 if (t.isValide ())
91                     tempTable.put (t.getName(), t);
92             } catch (IOException ioe) {
93                 Server.debug(this, "constructing Template caused exception for file " + tFiles[i].getName(), ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
94             }
95         }
96         if (!msgSetPresent) {
97             if (name.equals("default")) {
98                 Server.log (this, "Default-templateset doesn't have a message.set-file", Server.MSG_ERROR, Server.LVL_HALT);
99             } else {
100                 TemplateSet foreignTS = tm.getTemplateSet("default");
101                 if (foreignTS == null) {
102                     Server.log (this, "No default-templateset present", Server.MSG_ERROR, Server.LVL_MAJOR);
103                 } else {
104                     props = foreignTS.getMessageTemplateSet();
105                     msgSetPresent=true;
106                 }
107             }
108             msgSet=null;
109             FileMonitor.getFileMonitor().removeMonitor(this);
110         } else {
111             FileMonitor.getFileMonitor ().addReloadable (this);
112         }
113         return tempTable;
114     }
115
116     public void reload(File dir) {
117         createTemplates(dir);
118     }
119     
120    public String JavaDoc getName () {
121       return name;
122    }
123
124    public Template getTemplate (String JavaDoc tName) {
125       return ((Template) tpl.get (tName));
126    }
127
128     public void removeTemplate (Template t) {
129         tpl.remove(t.getName());
130     }
131     public void addTemplate (Template t) {
132         tpl.put(t.getName(), t);
133     }
134
135    public String JavaDoc getMessageTemplate (String JavaDoc msgTplName) {
136       return props.getProperty (msgTplName);
137    }
138    public Properties getMessageTemplateSet () {
139         return props;
140    }
141
142    public boolean isValide () {
143       return isValide;
144    }
145
146     public void readMessageSet (File f) throws FileNotFoundException, IOException {
147         // we have found the messageset
148
msgSetPresent=true;
149         msgSet = f;
150         lastModified = msgSet.lastModified ();
151         FileInputStream fis = new FileInputStream (f);
152         if (props == null)
153             props = new Properties ();
154         props.load (fis);
155         fis.close ();
156         Properties tProps = (Properties) props.clone();
157         
158         
159         for (int i = 0; i < neededMessageTemplates.length; i++) {
160             if (props.get(neededMessageTemplates[i]) == null) {
161                 Server.log (this, "readMessageSet [" + name + "]: Message-template '" + neededMessageTemplates[i] + "' is not present", Server.MSG_STATE, Server.LVL_VERBOSE);
162             }
163             tProps.remove(neededMessageTemplates[i]);
164         }
165         for (Enumeration ig = tProps.keys(); ig.hasMoreElements(); ) {
166             String JavaDoc param = ig.nextElement().toString ();
167             int pos = param.indexOf(".");
168          
169             if (pos > 0) {
170                 String JavaDoc p = param.substring(0,pos);
171                 if (Ignore(p))
172                     tProps.remove(param);
173             }
174             
175         }
176         for (Enumeration e = tProps.keys(); e.hasMoreElements(); ) {
177             Server.log (this, "readMessageSet [" + name + "]: Unknown message-template found: " + e.nextElement().toString (), Server.MSG_STATE, Server.LVL_VERBOSE);
178         }
179     }
180
181    /* INTERFACE RELOADABLE */
182    public long lastModified () {
183       return lastModified;
184    }
185
186    public void changed () {
187       try {
188          FileInputStream fis = new FileInputStream (msgSet);
189          Properties tprop = new Properties ();
190          tprop.load (fis);
191          fis.close ();
192          props = tprop;
193          lastModified = msgSet.lastModified ();
194          Server.log (this, "reload: reloaded messagetemplates", Server.MSG_STATE, Server.LVL_MINOR);
195       } catch (Exception JavaDoc e) {
196          Server.debug (this, "reload: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
197       }
198    }
199
200     public void removed () {
201         if (name.equals("default")) {
202             Server.log (this, "WARNING: message.set has been removed for DEFAULT-layout! Default-Layout must have one!", Server.MSG_ERROR, Server.LVL_MAJOR);
203         } else {
204             Server.log (this, "WARNING: message.set has been removed for layout " + name, Server.MSG_ERROR, Server.LVL_MINOR);
205         }
206         TemplateSet foreignTS = tm.getTemplateSet("default");
207         if (foreignTS == null) {
208             Server.log (this, "No default-templateset present making it impossible to fall back to the default-layout's message.set. Keeping the old messag.set", Server.MSG_ERROR, Server.LVL_MAJOR);
209         } else {
210             props = foreignTS.getMessageTemplateSet();
211         }
212         msgSetPresent = false;
213     }
214
215     public boolean filePresent() {
216         return msgSetPresent;
217     }
218
219     public void created () {
220         changed();
221         msgSetPresent = true;
222     }
223
224    public File getFile () {
225       return msgSet;
226    }
227
228
229     private boolean checkTemplateCompleteness(String JavaDoc[] templates) {
230         for (int i=0; i < templates.length; i++) {
231             if (tpl.containsKey (templates[i]))
232                 continue;
233             this.isValide = false;
234             return false;
235         }
236         return true;
237     }
238     
239     private boolean Ignore(String JavaDoc param) {
240         if (param.equals("constant"))
241             return true;
242         return false;
243     }
244     
245     private volatile String JavaDoc strgVal;
246     public String JavaDoc toString() {
247         if (strgVal==null) {
248             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[TemplateSet: ");
249             sb.append (this.name);
250             sb.append ("]");
251             strgVal = sb.toString();
252         }
253         return strgVal;
254     }
255     
256     public boolean equals (Object JavaDoc obj) {
257         if (obj==null)
258             return false;
259         if (!(obj instanceof TemplateSet))
260             return false;
261         if (!this.name.equals(((TemplateSet) obj).name))
262             return false;
263         return true;
264     }
265     
266     public int hashCode () {
267         if (hashCode != Integer.MIN_VALUE)
268            return hashCode;
269         hashCode = ("TemplateSet" + name).hashCode();
270         return (hashCode);
271      }
272
273
274     private static final String JavaDoc templatesNeeded[] = {
275             "start",
276             "welcome",
277             "login_missing",
278             "login_failed",
279             "not_found" };
280     
281     private static final String JavaDoc adminTemplates[] = {
282             "admin_header"
283     };
284     
285     public static final String JavaDoc neededMessageTemplates[] =
286       { "error.group.notExisting",
287         "error.user.notOnServer.singular",
288         "error.user.notOnServer.plural",
289         "error.a.noLongerValid",
290         "error.me.noArg",
291         "error.m.cantHearYou",
292         "error.ban.noArg",
293         "error.ban.noRight",
294         "error.uban.noArg",
295         "error.uban.noRight",
296         "error.col.startingGroup",
297         "error.col.noRight",
298         "error.col.wrongCode",
299         "error.col.lockedColor",
300         "error.col.tooOften",
301         "error.td.noArg",
302         "error.td.noRight",
303         "error.td.wrong",
304         "error.td.toomany",
305         "error.ig.noArg",
306         "error.i.noRight",
307         "error.i.noArg",
308         "error.i.alreadyInvited",
309         "error.ia.all.noRight",
310         "error.ia.noRight",
311         "error.ia.inviteStartGroup",
312         "error.j.noArg",
313         "error.j.alreadyHere",
314         "error.j.banned",
315         "error.j.noRightToOpen",
316         "error.j.closed",
317         "error.j.noRight",
318         "error.jclosed.noRight",
319         "error.jclosed.groupNotExisting",
320         "error.ju.alreadyHere",
321         "error.ju.noArg",
322         "error.k.noRight",
323         "error.k.noGroupRight",
324         "error.kc.noroom",
325         "error.kh.noRight",
326         "error.fl.nofriends",
327         "error.f.noFriendOnline",
328         "error.wban.noRight",
329         "error.wban.nobodyBanned",
330         "error.vip.noVipOnline",
331         "error.l.noRight",
332         "error.ul.noRight",
333         "error.m.noArg",
334         "error.m.noMessage",
335         "error.m.vip.noRight",
336         "error.moderated.timelock",
337         "error.gag.noRight",
338         "error.gag.noArg",
339         "error.gag.protected",
340         "error.aq.noArg",
341         "error.rig.noArg",
342         "error.rsu.noArg",
343         "error.rsu.noRight",
344         "error.su.noArg",
345         "error.su.noRight",
346         "error.su.tooManyForStartgroup",
347         "error.sepa.noArg",
348         "error.sepa.noRight",
349         "error.sepa.l.noRight",
350         "error.sepa.alreadyExists",
351         "error.t.noRight",
352         "error.s.noArg",
353         "error.ip.noArg",
354         "error.w.nobodyHere",
355         "error.th.noArg",
356         "error.sys.noArg",
357         "error.user.punished",
358         "message.send",
359         "message.send.moderated",
360         "message.send.moderated.personal",
361         "message.a",
362         "message.a.personal",
363         "message.away.on",
364         "message.away.off",
365         "message.col",
366         "message.c",
367         "message.me",
368         "message.ban.personal",
369         "message.ban.plural",
370         "message.ban.singular",
371         "message.ban.confirm.plural",
372         "message.ban.confirm.singular",
373         "message.uban.personal",
374         "message.uban.plural",
375         "message.uban.singular",
376         "message.uban.confirm.plural",
377         "message.uban.confirm.singular",
378         "error.uban.notOnList.singular",
379         "error.uban.notOnList.plural",
380         "message.td",
381         "message.ig.plural",
382         "message.ig.singular",
383         "message.i.personal",
384         "message.i.plural",
385         "message.i.singular",
386         "message.ia.all",
387         "message.ia.group",
388         "message.j.created",
389         "message.j",
390         "message.jclosed",
391         "message.k.destination.singular",
392         "message.k.confirm.singular",
393         "message.k.singular",
394         "message.k.destination.plural",
395         "message.k.confirm.plural",
396         "message.k.plural",
397         "message.k.personal",
398         "message.kh.singular",
399         "message.kh.confirm.singular",
400         "message.kh.plural",
401         "message.kh.confirm.plural",
402         "message.kh.personal",
403         "message.kh.godinfo",
404         "message.kc.personal",
405         "message.kc.destination.singular",
406         "message.kc.confirm.singular",
407         "message.kc.singular",
408         "message.kc.destination.plural",
409         "message.kc.confirm.plural",
410         "message.kc.plural",
411         "message.kc.godinfo",
412         "message.f.headline",
413         "message.f.isOnline",
414         "message.f.joined",
415         "message.f",
416         "message.f.count",
417         "message.wban",
418         "message.wc.headline",
419         "message.m.vip",
420         "message.m.vip.confirm",
421         "message.vip.singular",
422         "message.vip.plural",
423         "message.l",
424         "message.ul",
425         "message.mycol",
426         "message.m",
427         "message.m.away",
428         "message.m.confirm",
429         "message.gag.singular",
430         "message.gag.plural",
431         "message.gag.confirm.singular",
432         "message.gag.confirm.plural",
433         "message.aq",
434         "message.q",
435         "message.rig.singular",
436         "message.rig.plural",
437         "message.rsu.personal",
438         "message.rsu.singular",
439         "message.rsu.plural",
440         "message.rsu.confirm.singular",
441         "message.rsu.confirm.plural",
442         "message.su.personal",
443         "message.su.singular",
444         "message.su.plural",
445         "message.su.confirm.singular",
446         "message.su.confirm.plural",
447         "message.sepa",
448         "message.sepa.confirm",
449         "message.s",
450         "message.ip",
451         "message.time",
452         "message.user.detail",
453         "message.t",
454         "message.t.removed",
455         "message.th",
456         "message.sys",
457         "message.fl.headline",
458         "message.fl.entry.offline",
459         "message.fl.entry.online",
460         "message.fl.count",
461         "message.user.short",
462         "message.user.short.seperator",
463         "message.user.overview",
464         "message.user.flooded",
465         "message.user.tooled",
466         "message.user.leaving.group",
467         "message.user.join.group",
468         "message.user.join.closedGroup",
469         "message.user.join.server",
470         "message.user.join.server.personal",
471         "message.user.vip",
472         "message.server.shutdown",
473         "message.wc.underline",
474         "message.softClose",
475         "list.users",
476         "constant.userListItem",
477         "status.showtime",
478         "status.showtime.timeformat",
479         "constant.defaultColor",
480         "constant.lockedGroup",
481         "constant.openGroup",
482         "constant.linkedName",
483         "error.ack.groupNotModerated",
484         "error.ack.noArg",
485         "error.ack.noMessage",
486         "error.ack.userNotInGroup",
487         "error.i.alreadyHere",
488         "error.rgag.noArg",
489         "error.rgag.noRight",
490         "error.sepa.alreadyHere",
491         "message.ack",
492         "message.gag.personal",
493         "message.rgag.personal",
494         "message.rgag.confirm.plural",
495         "message.rgag.confirm.singular",
496         "message.rgag.plural",
497         "message.rgag.singular",
498         "message.user.leaving.server",
499         "message.user.leaving.server.kicked",
500         "error.raq.noRight",
501         "message.raq",
502         "error.fcol.noRight",
503         "error.fcol.noArg",
504         "error.fcol.noUser",
505         "message.fcol.singular",
506         "message.fcol.plural",
507         "message.fcol.confirm.singular",
508         "message.fcol.confirm.plural",
509         "message.fcol.personal",
510         "error.noRight.isAdmin",
511         "error.noRight.isVip",
512         "error.noRight.noSuVipAdmin",
513         "error.noRight.noVipAdmin",
514         "error.noRight.noAdmin",
515         "error.noRight.noModAdmin",
516         "error.noRight.noMod",
517         "error.noRight.deactivated",
518         "error.noRight.isSuForbiddenMembership",
519         "error.mass.noRight.noVipAdmin",
520         "error.rc.noArg",
521         "error.rc.userNotFound",
522         "error.rc.rightNotFound",
523         "error.rc.notRight",
524         "rc.rightAssigned",
525         "rc.rightRevoked",
526         "rc.newAssignedRight",
527         "rc.newRevokedRight",
528         "rc.rightsReset",
529         "rc.newResetRight"};
530 }
Popular Tags