⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templateset.java

📁 使用工具jublider开发的一个聊天室实现基本功能,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package freecs.layout;

import freecs.Server;
import freecs.util.FileMonitor;
import freecs.interfaces.IReloadable;

import java.io.*;
import java.util.*;

public class TemplateSet implements IReloadable {
	private Hashtable tpl;
	private Properties props;
	public String name;
    private int hashCode=Integer.MIN_VALUE;
	private boolean isValide;

	private File msgSet;
	private long lastModified;
	private boolean msgSetPresent=false;
	private TemplateManager tm;

	public TemplateSet(File dir, TemplateManager tm) throws IOException {
		this.tm = tm;
		this.isValide = false;
		this.name = dir.getName ();
        this.tpl = createTemplates(dir);
		if (!msgSetPresent) {
			if (name.equals("default")) {
				Server.log (this, "Default-templateset doesn't have a message.set-file", Server.MSG_ERROR, Server.LVL_HALT);
			} else {
				TemplateSet foreignTS = tm.getTemplateSet("default");
				if (foreignTS == null) {
					Server.log (this, "No default-templateset present", Server.MSG_ERROR, Server.LVL_MAJOR);
				} else {
					props = foreignTS.getMessageTemplateSet();
                    msgSetPresent=true;
				}
			}
		}
        if (!"admin".equals(name)) { // admin-templates are different
            if (!checkTemplateCompleteness(templatesNeeded))
                return;
            if (!msgSetPresent) {
                Server.log (this, "TemplateSet.construct: Templateset has no message.set! Ignoring.", Server.MSG_STATE, Server.LVL_MAJOR);
                this.isValide= false;
                return;
            }
        } else {
            if (!checkTemplateCompleteness(adminTemplates)) {
                Server.log (this, "TemplateSet.construct: Admin-Templateset doesn't have a header and wount work without it", Server.MSG_STATE, Server.LVL_MAJOR);
                return;
            }
        }
		this.isValide=true;
	}

    private Hashtable createTemplates(File dir) {
        Hashtable tempTable = new Hashtable();
        File tFiles[] = dir.listFiles ();
        for (int i = 0; i < tFiles.length; i++) {
            if (!tFiles[i].isFile ()) 
                continue;
            if (tFiles[i].getName ().equalsIgnoreCase ("message.set")) try {
                readMessageSet (tFiles[i]);
                continue;
            } catch (FileNotFoundException fnfe) {
                // doesn't happen
            } catch (IOException ioe) {
                Server.debug(this, "message.set of " + name + " caused exception", ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
            }
            try {
                Template t = new Template(tFiles[i], this);
                if (t.isValide ()) 
                    tempTable.put (t.getName(), t);
            } catch (IOException ioe) {
                Server.debug(this, "constructing Template caused exception for file " + tFiles[i].getName(), ioe, Server.MSG_ERROR, Server.LVL_MAJOR);
            }
        }
        if (!msgSetPresent) {
            if (name.equals("default")) {
                Server.log (this, "Default-templateset doesn't have a message.set-file", Server.MSG_ERROR, Server.LVL_HALT);
            } else {
                TemplateSet foreignTS = tm.getTemplateSet("default");
                if (foreignTS == null) {
                    Server.log (this, "No default-templateset present", Server.MSG_ERROR, Server.LVL_MAJOR);
                } else {
                    props = foreignTS.getMessageTemplateSet();
                    msgSetPresent=true;
                }
            }
            msgSet=null;
            FileMonitor.getFileMonitor().removeMonitor(this);
        } else {
            FileMonitor.getFileMonitor ().addReloadable (this);
        }
        return tempTable;
    }

    public void reload(File dir) {
        createTemplates(dir);
    }
    
   public String getName () {
      return name;
   }

   public Template getTemplate (String tName) {
      return ((Template) tpl.get (tName));
   }

	public void removeTemplate (Template t) {
		tpl.remove(t.getName());
	}
	public void addTemplate (Template t) {
		tpl.put(t.getName(), t);
	}

   public String getMessageTemplate (String msgTplName) {
      return props.getProperty (msgTplName);
   }
   public Properties getMessageTemplateSet () {
   		return props;
   }

   public boolean isValide () {
      return isValide;
   }

	public void readMessageSet (File f) throws FileNotFoundException, IOException {
		// we have found the messageset
		msgSetPresent=true;
		msgSet = f;
		lastModified = msgSet.lastModified ();
		FileInputStream fis = new FileInputStream (f);
		if (props == null)
			props = new Properties ();
		props.load (fis);
		fis.close ();
		Properties tProps = (Properties) props.clone();
		
		
		for (int i = 0; i < neededMessageTemplates.length; i++) {
			if (props.get(neededMessageTemplates[i]) == null) {
				Server.log (this, "readMessageSet [" + name + "]: Message-template '" + neededMessageTemplates[i] + "' is not present", Server.MSG_STATE, Server.LVL_VERBOSE);
			}
			tProps.remove(neededMessageTemplates[i]);
		}
		for (Enumeration ig = tProps.keys(); ig.hasMoreElements(); ) {
		    String param = ig.nextElement().toString ();
		    int pos = param.indexOf(".");
		 
		    if (pos > 0) {
		        String p = param.substring(0,pos);
		        if (Ignore(p))
		    	    tProps.remove(param);
		    }
		    
		}
		for (Enumeration e = tProps.keys(); e.hasMoreElements(); ) {
			Server.log (this, "readMessageSet [" + name + "]: Unknown message-template found: " + e.nextElement().toString (), Server.MSG_STATE, Server.LVL_VERBOSE);
		}
	}

   /* INTERFACE RELOADABLE */
   public long lastModified () {
      return lastModified;
   }

   public void changed () {
      try {
         FileInputStream fis = new FileInputStream (msgSet);
         Properties tprop = new Properties ();
         tprop.load (fis);
         fis.close ();
         props = tprop;
         lastModified = msgSet.lastModified ();
         Server.log (this, "reload: reloaded messagetemplates", Server.MSG_STATE, Server.LVL_MINOR);
      } catch (Exception e) {
         Server.debug (this, "reload: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
      }
   }

	public void removed () {
   		if (name.equals("default")) {
			Server.log (this, "WARNING: message.set has been removed for DEFAULT-layout! Default-Layout must have one!", Server.MSG_ERROR, Server.LVL_MAJOR);
   		} else {
			Server.log (this, "WARNING: message.set has been removed for layout " + name, Server.MSG_ERROR, Server.LVL_MINOR);
   		}
		TemplateSet foreignTS = tm.getTemplateSet("default");
		if (foreignTS == null) {
			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);
		} else {
			props = foreignTS.getMessageTemplateSet();
		}
		msgSetPresent = false;
	}

	public boolean filePresent() {
		return msgSetPresent;
	}

	public void created () {
		changed();
		msgSetPresent = true;
	}

   public File getFile () {
      return msgSet;
   }


    private boolean checkTemplateCompleteness(String[] templates) {
        for (int i=0; i < templates.length; i++) {
            if (tpl.containsKey (templates[i])) 
                continue;
            this.isValide = false;
            return false;
        }
        return true;
    }
    
    private boolean Ignore(String param) {
    	if (param.equals("constant"))
    		return true;
    	return false;
    }
    
    private volatile String strgVal;
    public String toString() {
        if (strgVal==null) {
            StringBuffer sb = new StringBuffer("[TemplateSet: ");
            sb.append (this.name);
            sb.append ("]");
            strgVal = sb.toString();
        }
        return strgVal;
    }
    
    public boolean equals (Object obj) {
        if (obj==null)
            return false;
        if (!(obj instanceof TemplateSet))
            return false;
        if (!this.name.equals(((TemplateSet) obj).name))
            return false;
        return true;
    }
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -