📄 template.java
字号:
/**
* 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.commands.AbstractCommand;
import freecs.commands.CommandSet;
import freecs.content.MessageState;
import freecs.core.*;
import freecs.util.FileMonitor;
import freecs.interfaces.IReloadable;
import freecs.interfaces.IRequest;
import java.io.*;
import java.util.*;
public class Template implements IReloadable {
private static final int DEPENDS_ON_NOTHING = 0;
private static final int DEPENDS_ON_SERVER_CONFIG = 1;
private static final int DEPENDS_ON_USER_COUNT = 2;
private static final int DEPENDS_ON_GROUP_COUNT = 4;
private static final int DEPENDS_ON_GROUP_LIST = 8;
private static final int DEPENDS_ON_SESSION =16;
private int dependsOn=DEPENDS_ON_NOTHING;
private volatile String eTag=null;
private volatile long modifiedSince=0;
private volatile String[] parts;
private String tsName;
private String name;
private boolean isRedirect;
private String destination;
private TemplateSet ts;
private File template;
private long lastModified;
private boolean tplPresent;
public String getDestination () {
return destination;
}
public boolean isRedirect () {
return isRedirect;
}
public String getTsName () {
return tsName;
}
public boolean hasToBeRendered (String eTag, long modifiedSince) {
if (eTag==null && modifiedSince==-1)
return true;
if (eTag != null) {
this.eTag = this.generateETag();
if ((this.dependsOn & Template.DEPENDS_ON_SESSION)!=0
|| !eTag.equals(this.eTag)) // FIXME: if-not-match may transfere more than one etag
return true;
} else if (this.modifiedSince>modifiedSince)
return true;
return false;
}
public String render (IRequest req) {
/* StringBuffer tsb = new StringBuffer ("rendering Template ").append (name).append (" of templateset ").append (tsName);
Server.log (this, tsb.toString (), Server.MSG_STATE, Server.LVL_VERY_VERBOSE); */
StringBuffer retVal = new StringBuffer ();
for (int i = 0; i < parts.length; i++) {
String part = parts[i];
if (!part.startsWith ("#")) {
retVal.append (part);
continue;
}
if (part.regionMatches
(true, 0, "#active_users", 0, "#active_users".length())) {
retVal.append (UserManager.mgr.getActiveUserCount ());
} else if (part.regionMatches
(true, 0, "#active_user_list", 0, "#active_user_list".length())) {
retVal.append(generateUserList());
} else if (part.regionMatches
(true, 0, "#users_in_group", 0, "#users_in_group".length())) {
retVal.append (generateUserList(req));
} else if (part.regionMatches
(true, 0, "#open_groups", 0, "#open_groups".length())) {
retVal.append (GroupManager.mgr.openGroupsCount());
} else if (part.regionMatches
(true, 0, "#selve", 0, "#selve".length())) {
retVal.append ("http://");
retVal.append (Server.srv.getUrl ());
} else if (part.regionMatches
(true, 0, "#token", 0, "#token".length())) {
StringBuffer c = new StringBuffer ();
while (c.length () < 16) {
char x = (char) Math.ceil(Math.random() * 34);
if (x < 10) x = (char) (x + 48);
else x = (char) (x + 87);
c.append(x);
}
retVal.append (c);
Server.srv.addToken(c.toString(), req.getCookie());
} else if (part.regionMatches
(true, 0, "#config.", 0, "#config.".length())) {
retVal.append(Server.srv.props.getProperty(part.substring(8).trim()));
}
}
String[] arr = new String[2];
return retVal.toString();
}
private String generateETag() {
if ((this.dependsOn & Template.DEPENDS_ON_SESSION) != 0)
return null;
if (modifiedSince < this.lastModified)
modifiedSince = this.lastModified;
StringBuffer sb = new StringBuffer();
sb.append (Long.toHexString(this.lastModified/500));
if ((this.dependsOn & Template.DEPENDS_ON_SERVER_CONFIG) != 0) {
sb.append ("pc").append(Long.toHexString(Server.srv.lastModified()/500));
if (modifiedSince < Server.srv.lastModified())
modifiedSince = Server.srv.lastModified();
}
if ((this.dependsOn & Template.DEPENDS_ON_USER_COUNT) != 0) {
sb.append ("puc").append(Long.toHexString(UserManager.mgr.lastModified()/500));
if (modifiedSince < UserManager.mgr.lastModified())
modifiedSince = UserManager.mgr.lastModified();
}
if ((this.dependsOn & Template.DEPENDS_ON_GROUP_COUNT) != 0) {
sb.append ("pgc").append(Long.toHexString(GroupManager.mgr.lastModified()/500));
if (modifiedSince < GroupManager.mgr.lastModified())
modifiedSince = GroupManager.mgr.lastModified();
}
if ((this.dependsOn & Template.DEPENDS_ON_GROUP_LIST) != 0) {
sb.append ("pgl").append(Long.toHexString(GroupManager.mgr.groupListLastModified()/500));
if (modifiedSince < GroupManager.mgr.groupListLastModified())
modifiedSince = GroupManager.mgr.groupListLastModified();
}
return sb.toString();
}
public String getName () {
return name;
}
public Template(File tpl, TemplateSet ts) throws IOException {
this.tsName = ts.getName();
this.ts = ts;
template = tpl;
name = tpl.getName ();
int pos = name.indexOf (".");
if (pos != -1)
name = name.substring (0, pos);
lastModified = tpl.lastModified ();
parts = parseFile(tpl);
tplPresent=true;
FileMonitor.getFileMonitor ().addReloadable (this);
}
public boolean isValide () {
return ((parts != null && parts.length > 0) ||
(destination != null && destination.length() > 1 && isRedirect == true));
}
private String[] parseFile(File tpl) throws IOException {
if (!tpl.canRead()) {
StringBuffer tsb = new StringBuffer ("Unable to read template '").append (tpl.getName()).append ("'");
throw new IOException(tsb.toString ());
}
this.dependsOn = Template.DEPENDS_ON_NOTHING;
Vector t = new Vector ();
FileReader fr = new FileReader(tpl);
char fcnt[] = new char[(int) tpl.length()];
int read = fr.read(fcnt);
if (read<1) return null;
String raw = String.copyValueOf(fcnt);
if (raw.toLowerCase ().startsWith ("#redirect#")) {
this.isRedirect = true;
destination = raw.substring (raw.lastIndexOf("#") + 1).trim ();
if (destination.toLowerCase ().startsWith ("$selve$")) {
StringBuffer tsb = new StringBuffer ("http://").append (Server.srv.getUrl ());
destination = tsb.toString ();
this.dependsOn = this.dependsOn | Template.DEPENDS_ON_SERVER_CONFIG;
} else if (!destination.toLowerCase ().startsWith ("http://")) {
StringBuffer tsb = new StringBuffer ("http://").append (destination);
destination = tsb.toString ();
}
}
boolean lt=false, placeholder=false;
StringTokenizer tok = new StringTokenizer (raw, "<%>", true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -