forked from I2P_Developers/i2p.i2p
Added apps/q - the Q distributed file store framework, by aum
This commit is contained in:
1115
apps/q/java/src/HTML/Template.java
Normal file
1115
apps/q/java/src/HTML/Template.java
Normal file
File diff suppressed because it is too large
Load Diff
178
apps/q/java/src/HTML/Tmpl/Element/Conditional.java
Normal file
178
apps/q/java/src/HTML/Tmpl/Element/Conditional.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* Modified by David McNab (david@rebirthing.co.nz) to allow nesting of
|
||||
* templates (ie, passing a child Template object as a value argument
|
||||
* to a .setParam() invocation on a parent Template object).
|
||||
*
|
||||
*/
|
||||
|
||||
package HTML.Tmpl.Element;
|
||||
|
||||
import java.util.Vector;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Enumeration;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import HTML.*;
|
||||
|
||||
public class Conditional extends Element
|
||||
{
|
||||
private boolean control_val = false;
|
||||
private Vector [] data;
|
||||
|
||||
public Conditional(String type, String name)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if(type.equalsIgnoreCase("if"))
|
||||
this.type="if";
|
||||
else if(type.equalsIgnoreCase("unless"))
|
||||
this.type="unless";
|
||||
else
|
||||
throw new IllegalArgumentException(
|
||||
"Unrecognised type: " + type);
|
||||
|
||||
this.name = name;
|
||||
this.data = new Vector[2];
|
||||
this.data[0] = new Vector();
|
||||
}
|
||||
|
||||
public void addBranch() throws IndexOutOfBoundsException
|
||||
{
|
||||
if(data[1] != null)
|
||||
throw new IndexOutOfBoundsException("Already have two branches");
|
||||
|
||||
if(data[0] == null)
|
||||
data[0] = new Vector();
|
||||
else if(data[1] == null)
|
||||
data[1] = new Vector();
|
||||
}
|
||||
|
||||
public void add(String text)
|
||||
{
|
||||
if(data[1] != null)
|
||||
data[1].addElement(text);
|
||||
else
|
||||
data[0].addElement(text);
|
||||
}
|
||||
|
||||
public void add(Element node)
|
||||
{
|
||||
if(data[1] != null)
|
||||
data[1].addElement(node);
|
||||
else
|
||||
data[0].addElement(node);
|
||||
}
|
||||
|
||||
public void setControlValue(Object control_val)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this.control_val = process_var(control_val);
|
||||
}
|
||||
|
||||
public String parse(Hashtable params)
|
||||
{
|
||||
if(!params.containsKey(this.name))
|
||||
this.control_val = false;
|
||||
else
|
||||
setControlValue(params.get(this.name));
|
||||
|
||||
StringBuffer output = new StringBuffer();
|
||||
|
||||
Enumeration de;
|
||||
if(type.equals("if") && control_val ||
|
||||
type.equals("unless") && !control_val)
|
||||
de = data[0].elements();
|
||||
else if(data[1] != null)
|
||||
de = data[1].elements();
|
||||
else
|
||||
return "";
|
||||
|
||||
while(de.hasMoreElements()) {
|
||||
Object e = de.nextElement();
|
||||
String eType = e.getClass().getName();
|
||||
if(eType.endsWith(".String"))
|
||||
output.append((String)e);
|
||||
else if (eType.endsWith(".Template"))
|
||||
output.append(((Template)e).output());
|
||||
else
|
||||
output.append(((Element)e).parse(params));
|
||||
}
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
public String typeOfParam(String param)
|
||||
throws NoSuchElementException
|
||||
{
|
||||
for(int i=0; i<data.length; i++)
|
||||
{
|
||||
if(data[i] == null)
|
||||
continue;
|
||||
for(Enumeration e = data[i].elements();
|
||||
e.hasMoreElements();)
|
||||
{
|
||||
Object o = e.nextElement();
|
||||
if(o.getClass().getName().endsWith(".String"))
|
||||
continue;
|
||||
if(((Element)o).Name().equals(param))
|
||||
return ((Element)o).Type();
|
||||
}
|
||||
}
|
||||
throw new NoSuchElementException(param);
|
||||
}
|
||||
|
||||
private boolean process_var(Object control_val)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
String control_class = "";
|
||||
|
||||
if(control_val == null)
|
||||
return false;
|
||||
|
||||
control_class=control_val.getClass().getName();
|
||||
if(control_class.indexOf(".") > 0)
|
||||
control_class = control_class.substring(
|
||||
control_class.lastIndexOf(".")+1);
|
||||
|
||||
if(control_class.equals("String")) {
|
||||
return !(((String)control_val).equals("") ||
|
||||
((String)control_val).equals("0"));
|
||||
} else if(control_class.equals("Vector")) {
|
||||
return !((Vector)control_val).isEmpty();
|
||||
} else if(control_class.equals("Boolean")) {
|
||||
return ((Boolean)control_val).booleanValue();
|
||||
} else if(control_class.equals("Integer")) {
|
||||
return (((Integer)control_val).intValue() != 0);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unrecognised type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
66
apps/q/java/src/HTML/Tmpl/Element/Element.java
Normal file
66
apps/q/java/src/HTML/Tmpl/Element/Element.java
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl.Element;
|
||||
import java.util.Hashtable;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public abstract class Element
|
||||
{
|
||||
protected String type;
|
||||
protected String name="";
|
||||
|
||||
public abstract String parse(Hashtable params);
|
||||
public abstract String typeOfParam(String param)
|
||||
throws NoSuchElementException;
|
||||
|
||||
public void add(String data){}
|
||||
public void add(Element node){}
|
||||
|
||||
public boolean contains(String param)
|
||||
{
|
||||
try {
|
||||
return (typeOfParam(param) != null?true:false);
|
||||
} catch(NoSuchElementException nse) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public final String Type()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
public final String Name()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
}
|
||||
39
apps/q/java/src/HTML/Tmpl/Element/If.java
Normal file
39
apps/q/java/src/HTML/Tmpl/Element/If.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl.Element;
|
||||
|
||||
public class If extends Conditional
|
||||
{
|
||||
public If(String control_var) throws IllegalArgumentException
|
||||
{
|
||||
super("if", control_var);
|
||||
}
|
||||
}
|
||||
183
apps/q/java/src/HTML/Tmpl/Element/Loop.java
Normal file
183
apps/q/java/src/HTML/Tmpl/Element/Loop.java
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl.Element;
|
||||
import java.util.Vector;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Enumeration;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class Loop extends Element
|
||||
{
|
||||
private boolean loop_context_vars=false;
|
||||
private boolean global_vars=false;
|
||||
|
||||
private Vector control_val = null;
|
||||
private Vector data;
|
||||
|
||||
public Loop(String name)
|
||||
{
|
||||
this.type = "loop";
|
||||
this.name = name;
|
||||
this.data = new Vector();
|
||||
}
|
||||
|
||||
public Loop(String name, boolean loop_context_vars)
|
||||
{
|
||||
this(name);
|
||||
this.loop_context_vars=loop_context_vars;
|
||||
}
|
||||
|
||||
public Loop(String name, boolean loop_context_vars, boolean global_vars)
|
||||
{
|
||||
this(name);
|
||||
this.loop_context_vars=loop_context_vars;
|
||||
this.global_vars=global_vars;
|
||||
}
|
||||
|
||||
public void add(String text)
|
||||
{
|
||||
data.addElement(text);
|
||||
}
|
||||
|
||||
public void add(Element node)
|
||||
{
|
||||
data.addElement(node);
|
||||
}
|
||||
|
||||
public void setControlValue(Vector control_val)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this.control_val = process_var(control_val);
|
||||
}
|
||||
|
||||
public String parse(Hashtable p)
|
||||
{
|
||||
if(!p.containsKey(this.name))
|
||||
this.control_val = null;
|
||||
else {
|
||||
Object o = p.get(this.name);
|
||||
if(!o.getClass().getName().endsWith(".Vector") &&
|
||||
!o.getClass().getName().endsWith(".List"))
|
||||
throw new ClassCastException(
|
||||
"Attempt to set <tmpl_loop> with a non-list. tmpl_loop=" + this.name);
|
||||
setControlValue((Vector)p.get(this.name));
|
||||
}
|
||||
|
||||
if(control_val == null)
|
||||
return "";
|
||||
|
||||
StringBuffer output = new StringBuffer();
|
||||
Enumeration iterator = control_val.elements();
|
||||
|
||||
boolean first=true;
|
||||
boolean last=false;
|
||||
boolean inner=false;
|
||||
boolean odd=true;
|
||||
int counter=1;
|
||||
|
||||
while(iterator.hasMoreElements()) {
|
||||
Hashtable params = (Hashtable)iterator.nextElement();
|
||||
|
||||
if(params==null)
|
||||
params = new Hashtable();
|
||||
|
||||
if(global_vars) {
|
||||
for(Enumeration e = p.keys(); e.hasMoreElements();) {
|
||||
Object key = e.nextElement();
|
||||
if(!params.containsKey(key))
|
||||
params.put(key, p.get(key));
|
||||
}
|
||||
}
|
||||
|
||||
if(loop_context_vars) {
|
||||
if(!iterator.hasMoreElements())
|
||||
last=true;
|
||||
inner = !first && !last;
|
||||
|
||||
params.put("__FIRST__", first?"1":"");
|
||||
params.put("__LAST__", last?"1":"");
|
||||
params.put("__ODD__", odd?"1":"");
|
||||
params.put("__INNER__", inner?"1":"");
|
||||
params.put("__COUNTER__", "" + (counter++));
|
||||
}
|
||||
|
||||
Enumeration de = data.elements();
|
||||
while(de.hasMoreElements()) {
|
||||
|
||||
Object e = de.nextElement();
|
||||
if(e.getClass().getName().indexOf("String")>-1)
|
||||
output.append((String)e);
|
||||
else
|
||||
output.append(((Element)e).parse(params));
|
||||
}
|
||||
first = false;
|
||||
odd = !odd;
|
||||
}
|
||||
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
public String typeOfParam(String param)
|
||||
throws NoSuchElementException
|
||||
{
|
||||
for(Enumeration e = data.elements(); e.hasMoreElements();)
|
||||
{
|
||||
Object o = e.nextElement();
|
||||
if(o.getClass().getName().endsWith(".String"))
|
||||
continue;
|
||||
if(((Element)o).Name().equals(param))
|
||||
return ((Element)o).Type();
|
||||
}
|
||||
throw new NoSuchElementException(param);
|
||||
}
|
||||
|
||||
private Vector process_var(Vector control_val)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
String control_class = "";
|
||||
|
||||
if(control_val == null)
|
||||
return null;
|
||||
|
||||
control_class=control_val.getClass().getName();
|
||||
|
||||
if(control_class.indexOf("Vector") > -1) {
|
||||
if(control_val.isEmpty())
|
||||
return null;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unrecognised type");
|
||||
}
|
||||
|
||||
return control_val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
39
apps/q/java/src/HTML/Tmpl/Element/Unless.java
Normal file
39
apps/q/java/src/HTML/Tmpl/Element/Unless.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl.Element;
|
||||
|
||||
public class Unless extends Conditional
|
||||
{
|
||||
public Unless(String control_var) throws IllegalArgumentException
|
||||
{
|
||||
super("unless", control_var);
|
||||
}
|
||||
}
|
||||
144
apps/q/java/src/HTML/Tmpl/Element/Var.java
Normal file
144
apps/q/java/src/HTML/Tmpl/Element/Var.java
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* Modified by David McNab (david@rebirthing.co.nz) to allow nesting of
|
||||
* templates (ie, passing a child Template object as a value argument
|
||||
* to a .setParam() invocation on a parent Template object).
|
||||
*/
|
||||
|
||||
package HTML.Tmpl.Element;
|
||||
import java.util.Hashtable;
|
||||
import java.util.NoSuchElementException;
|
||||
import HTML.Tmpl.Util;
|
||||
import HTML.Template;
|
||||
|
||||
public class Var extends Element
|
||||
{
|
||||
public static final int ESCAPE_NONE = 0;
|
||||
public static final int ESCAPE_URL = 1;
|
||||
public static final int ESCAPE_HTML = 2;
|
||||
public static final int ESCAPE_QUOTE = 4;
|
||||
|
||||
public Var(String name, int escape, Object default_value)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this(name, escape);
|
||||
this.default_value = stringify(default_value);
|
||||
}
|
||||
|
||||
public Var(String name, int escape)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
if(name == null)
|
||||
throw new IllegalArgumentException("tmpl_var must have a name");
|
||||
this.type = "var";
|
||||
this.name = name;
|
||||
this.escape = escape;
|
||||
}
|
||||
|
||||
public Var(String name, String escape)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this(name, escape, null);
|
||||
}
|
||||
|
||||
public Var(String name, String escape, Object default_value)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this(name, ESCAPE_NONE, default_value);
|
||||
|
||||
if(escape.equalsIgnoreCase("html"))
|
||||
this.escape = ESCAPE_HTML;
|
||||
else if(escape.equalsIgnoreCase("url"))
|
||||
this.escape = ESCAPE_URL;
|
||||
else if(escape.equalsIgnoreCase("quote"))
|
||||
this.escape = ESCAPE_QUOTE;
|
||||
}
|
||||
|
||||
public Var(String name, boolean escape)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
this(name, escape?ESCAPE_HTML:ESCAPE_NONE);
|
||||
}
|
||||
|
||||
public String parse(Hashtable params)
|
||||
{
|
||||
String value = null;
|
||||
|
||||
if(params.containsKey(this.name))
|
||||
value = stringify(params.get(this.name));
|
||||
else
|
||||
value = this.default_value;
|
||||
|
||||
if(value == null)
|
||||
return "";
|
||||
|
||||
if(this.escape == ESCAPE_HTML)
|
||||
return Util.escapeHTML(value);
|
||||
else if(this.escape == ESCAPE_URL)
|
||||
return Util.escapeURL(value);
|
||||
else if(this.escape == ESCAPE_QUOTE)
|
||||
return Util.escapeQuote(value);
|
||||
else
|
||||
return value;
|
||||
}
|
||||
|
||||
public String typeOfParam(String param)
|
||||
throws NoSuchElementException
|
||||
{
|
||||
throw new NoSuchElementException(param);
|
||||
}
|
||||
|
||||
private String stringify(Object o)
|
||||
{
|
||||
if(o == null)
|
||||
return null;
|
||||
|
||||
String cname = o.getClass().getName();
|
||||
if(cname.endsWith(".String"))
|
||||
return (String)o;
|
||||
else if(cname.endsWith(".Integer"))
|
||||
return ((Integer)o).toString();
|
||||
else if(cname.endsWith(".Boolean"))
|
||||
return ((Boolean)o).toString();
|
||||
else if(cname.endsWith(".Date"))
|
||||
return ((java.util.Date)o).toString();
|
||||
else if(cname.endsWith(".Vector"))
|
||||
throw new ClassCastException("Attempt to set <tmpl_var> with a non-scalar. Var name=" + this.name);
|
||||
else if(cname.endsWith(".Template"))
|
||||
return ((Template)o).output();
|
||||
else
|
||||
throw new ClassCastException("Unknown object type: " + cname);
|
||||
}
|
||||
|
||||
// Private data starts here
|
||||
private int escape=ESCAPE_NONE;
|
||||
private String default_value=null;
|
||||
|
||||
}
|
||||
145
apps/q/java/src/HTML/Tmpl/Filter.java
Normal file
145
apps/q/java/src/HTML/Tmpl/Filter.java
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl;
|
||||
|
||||
/**
|
||||
* Pre-parse filters for HTML.Template templates.
|
||||
* <p>
|
||||
* The HTML.Tmpl.Filter interface allows you to write Filters
|
||||
* for your templates. The filter is called after the template
|
||||
* is read and before it is parsed.
|
||||
* <p>
|
||||
* You can use a filter to make changes in the template file before
|
||||
* it is parsed by HTML.Template, so for example, use it to replace
|
||||
* constants, or to translate your own tags to HTML.Template tags.
|
||||
* <p>
|
||||
* A common usage would be to do what you think you're doing when you
|
||||
* do <code><TMPL_INCLUDE file="<TMPL_VAR name="the_file">"></code>:
|
||||
* <p>
|
||||
* myTemplate.tmpl:
|
||||
* <pre>
|
||||
* <TMPL_INCLUDE file="<%the_file%>">
|
||||
* </pre>
|
||||
* <p>
|
||||
* myFilter.java:
|
||||
* <pre>
|
||||
* class myFilter implements HTML.Tmpl.Filter
|
||||
* {
|
||||
* private String myFile;
|
||||
* private int type=SCALAR
|
||||
*
|
||||
* public myFilter(String myFile) {
|
||||
* this.myFile = myFile;
|
||||
* }
|
||||
*
|
||||
* public int format() {
|
||||
* return this.type;
|
||||
* }
|
||||
*
|
||||
* public String parse(String t) {
|
||||
* // replace all <%the_file%> with myFile
|
||||
* return t;
|
||||
* }
|
||||
*
|
||||
* public String [] parse(String [] t) {
|
||||
* throw new UnsupportedOperationException();
|
||||
* }
|
||||
* }
|
||||
* </pre>
|
||||
* <p>
|
||||
* myClass.java:
|
||||
* <pre>
|
||||
* Hashtable params = new Hashtable();
|
||||
* params.put("filename", "myTemplate.tmpl");
|
||||
* params.put("filter", new myFilter("myFile.tmpl"));
|
||||
* Template t = new Template(params);
|
||||
* </pre>
|
||||
*
|
||||
* @author Philip S Tellis
|
||||
* @version 0.0.1
|
||||
*/
|
||||
public interface Filter
|
||||
{
|
||||
/**
|
||||
* Tells HTML.Template to call the parse(String) method of this filter.
|
||||
*/
|
||||
public final static int SCALAR=1;
|
||||
|
||||
/**
|
||||
* Tells HTML.Template to call the parse(String []) method of this
|
||||
* filter.
|
||||
*/
|
||||
public final static int ARRAY=2;
|
||||
|
||||
/**
|
||||
* Tells HTML.Template what kind of filter this is.
|
||||
* Should return either SCALAR or ARRAY to indicate which parse method
|
||||
* must be called.
|
||||
*
|
||||
* @return the values SCALAR or ARRAY indicating which parse method
|
||||
* is to be called
|
||||
*/
|
||||
public int format();
|
||||
|
||||
/**
|
||||
* parses the template as a single string, and returns the parsed
|
||||
* template as a single string.
|
||||
* <p>
|
||||
* Should throw an UnsupportedOperationException if it isn't implemented
|
||||
*
|
||||
* @param t a string containing the entire template
|
||||
*
|
||||
* @return a string containing the template after you've parsed it
|
||||
*
|
||||
* @throws UnsupportedOperationException if this method isn't
|
||||
* implemented
|
||||
*/
|
||||
public String parse(String t);
|
||||
|
||||
/**
|
||||
* parses the template as an array of strings, and returns the parsed
|
||||
* template as an array of strings.
|
||||
* <p>
|
||||
* Should throw an UnsupportedOperationException if it isn't implemented
|
||||
*
|
||||
* @param t an array of strings containing the template - one line
|
||||
* at a time
|
||||
*
|
||||
* @return an array of strings containing the parsed template -
|
||||
* one line at a time
|
||||
*
|
||||
* @throws UnsupportedOperationException if this method isn't
|
||||
* implemented
|
||||
*/
|
||||
public String [] parse(String [] t);
|
||||
}
|
||||
|
||||
385
apps/q/java/src/HTML/Tmpl/Parsers/Parser.java
Normal file
385
apps/q/java/src/HTML/Tmpl/Parsers/Parser.java
Normal file
@@ -0,0 +1,385 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl.Parsers;
|
||||
import java.util.*;
|
||||
import HTML.Tmpl.Element.*;
|
||||
import HTML.Tmpl.Util;
|
||||
|
||||
public class Parser
|
||||
{
|
||||
private boolean case_sensitive=false;
|
||||
private boolean strict=true;
|
||||
private boolean loop_context_vars=false;
|
||||
private boolean global_vars=false;
|
||||
|
||||
public Parser()
|
||||
{
|
||||
}
|
||||
|
||||
public Parser(String [] args)
|
||||
throws ArrayIndexOutOfBoundsException,
|
||||
IllegalArgumentException
|
||||
{
|
||||
if(args.length%2 != 0)
|
||||
throw new ArrayIndexOutOfBoundsException("odd number of arguments passed");
|
||||
|
||||
for(int i=0; i<args.length; i+=2) {
|
||||
if(args[i].equals("case_sensitive")) {
|
||||
String cs = args[i+1];
|
||||
if(cs.equals("") || cs.equals("0"))
|
||||
this.case_sensitive=false;
|
||||
else
|
||||
this.case_sensitive=true;
|
||||
} else if(args[i].equals("strict")) {
|
||||
String s = args[i+1];
|
||||
if(s.equals("") || s.equals("0"))
|
||||
this.strict=false;
|
||||
else
|
||||
this.strict=true;
|
||||
} else if(args[i].equals("loop_context_vars")) {
|
||||
String s = args[i+1];
|
||||
if(s.equals("") || s.equals("0"))
|
||||
this.loop_context_vars=false;
|
||||
else
|
||||
this.loop_context_vars=true;
|
||||
|
||||
} else if(args[i].equals("global_vars")) {
|
||||
String s = args[i+1];
|
||||
if(s.equals("") || s.equals("0"))
|
||||
this.global_vars=false;
|
||||
else
|
||||
this.global_vars=true;
|
||||
} else {
|
||||
throw new IllegalArgumentException(args[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Element getElement(Properties p)
|
||||
throws NoSuchElementException
|
||||
{
|
||||
String type = p.getProperty("type");
|
||||
|
||||
if(type.equals("if"))
|
||||
return new If(p.getProperty("name"));
|
||||
else if(type.equals("unless"))
|
||||
return new Unless(p.getProperty("name"));
|
||||
else if(type.equals("loop"))
|
||||
return new Loop(p.getProperty("name"),
|
||||
loop_context_vars, global_vars);
|
||||
else
|
||||
throw new NoSuchElementException(type);
|
||||
}
|
||||
|
||||
public Vector parseLine(String line)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
int pos=0, endpos;
|
||||
Vector parts = new Vector();
|
||||
|
||||
char [] c = line.toCharArray();
|
||||
int i=0;
|
||||
|
||||
StringBuffer temp = new StringBuffer();
|
||||
|
||||
for(i=0; i<c.length; i++) {
|
||||
if(c[i] != '<') {
|
||||
temp.append(c[i]);
|
||||
} else {
|
||||
// found a tag
|
||||
Util.debug_print("line so far: " + temp);
|
||||
StringBuffer tag = new StringBuffer();
|
||||
for(; i<c.length && c[i] != '>'; i++) {
|
||||
tag.append(c[i]);
|
||||
}
|
||||
// > is not allowed inside a template tag
|
||||
// so we can be sure that if this is a
|
||||
// template tag, it ends with a >
|
||||
|
||||
// add the closing > as well
|
||||
if(i<c.length)
|
||||
tag.append(c[i]);
|
||||
|
||||
// if this contains more < inside it,
|
||||
// then it could possibly be a template
|
||||
// tag inside a html tag
|
||||
// so remove external tag parts
|
||||
|
||||
while(tag.toString().substring(1).indexOf("<")
|
||||
> -1)
|
||||
{
|
||||
do {
|
||||
temp.append(tag.charAt(0));
|
||||
tag=new StringBuffer(
|
||||
tag.toString().substring(1));
|
||||
} while(tag.charAt(0) != '<');
|
||||
}
|
||||
|
||||
Util.debug_print("tag: " + tag);
|
||||
|
||||
String test_tag = tag.toString().toLowerCase();
|
||||
// if it doesn't contain tmpl_ it is not
|
||||
// a template tag
|
||||
if(test_tag.indexOf("tmpl_") < 0) {
|
||||
temp.append(tag);
|
||||
continue;
|
||||
}
|
||||
|
||||
// may be a template tag
|
||||
// check if it starts with tmpl_
|
||||
|
||||
test_tag = cleanTag(test_tag);
|
||||
|
||||
Util.debug_print("clean: " + test_tag);
|
||||
|
||||
// check if it is a closing tag
|
||||
if(test_tag.startsWith("/"))
|
||||
test_tag = test_tag.substring(1);
|
||||
|
||||
// if it still doesn't start with tmpl_
|
||||
// then it is not a template tag
|
||||
if(!test_tag.startsWith("tmpl_")) {
|
||||
temp.append(tag);
|
||||
continue;
|
||||
}
|
||||
|
||||
// now it must be a template tag
|
||||
String tag_type=getTagType(test_tag);
|
||||
|
||||
if(tag_type == null) {
|
||||
if(strict)
|
||||
throw new
|
||||
IllegalArgumentException(
|
||||
tag.toString());
|
||||
else
|
||||
temp.append(tag);
|
||||
}
|
||||
|
||||
Util.debug_print("type: " + tag_type);
|
||||
|
||||
// if this was an invalid key and we've
|
||||
// reached so far, then next iteration
|
||||
if(tag_type == null)
|
||||
continue;
|
||||
|
||||
// now, push the previous stuff
|
||||
// into the Vector
|
||||
if(temp.length()>0) {
|
||||
parts.addElement(temp.toString());
|
||||
temp = new StringBuffer();
|
||||
}
|
||||
|
||||
// it is a valid template tag
|
||||
// get its properties
|
||||
|
||||
Util.debug_print("Checking: " + tag);
|
||||
Properties tag_props =
|
||||
getTagProps(tag.toString());
|
||||
|
||||
if(tag_props.containsKey("name"))
|
||||
Util.debug_print("name: " +
|
||||
tag_props.getProperty("name"));
|
||||
else
|
||||
Util.debug_print("no name");
|
||||
|
||||
parts.addElement(tag_props);
|
||||
}
|
||||
}
|
||||
|
||||
if(temp.length()>0)
|
||||
parts.addElement(temp.toString());
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
private String cleanTag(String tag)
|
||||
throws IllegalArgumentException
|
||||
{
|
||||
String test_tag = new String(tag);
|
||||
// first remove < and >
|
||||
if(test_tag.startsWith("<"))
|
||||
test_tag = test_tag.substring(1);
|
||||
if(test_tag.endsWith(">"))
|
||||
test_tag = test_tag.substring(0, test_tag.length()-1);
|
||||
else
|
||||
throw new IllegalArgumentException("Tags must start " +
|
||||
"and end on the same line");
|
||||
|
||||
// remove any leading !-- and trailing
|
||||
// -- in case of comment style tags
|
||||
if(test_tag.startsWith("!--")) {
|
||||
test_tag=test_tag.substring(3);
|
||||
}
|
||||
if(test_tag.endsWith("--")) {
|
||||
test_tag=test_tag.substring(0, test_tag.length()-2);
|
||||
}
|
||||
// then leading and trailing spaces
|
||||
test_tag = test_tag.trim();
|
||||
|
||||
return test_tag;
|
||||
}
|
||||
|
||||
private String getTagType(String tag)
|
||||
{
|
||||
int sp = tag.indexOf(" ");
|
||||
String tag_type="";
|
||||
if(sp < 0) {
|
||||
tag_type = tag.toLowerCase();
|
||||
} else {
|
||||
tag_type = tag.substring(0, sp).toLowerCase();
|
||||
}
|
||||
if(tag_type.startsWith("tmpl_"))
|
||||
tag_type=tag_type.substring(5);
|
||||
|
||||
Util.debug_print("tag_type: " + tag_type);
|
||||
|
||||
if(tag_type.equals("var") ||
|
||||
tag_type.equals("if") ||
|
||||
tag_type.equals("unless") ||
|
||||
tag_type.equals("loop") ||
|
||||
tag_type.equals("include") ||
|
||||
tag_type.equals("else")) {
|
||||
return tag_type;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Properties getTagProps(String tag)
|
||||
throws IllegalArgumentException,
|
||||
NullPointerException
|
||||
{
|
||||
Properties p = new Properties();
|
||||
|
||||
tag = cleanTag(tag);
|
||||
|
||||
Util.debug_print("clean: " + tag);
|
||||
|
||||
if(tag.startsWith("/")) {
|
||||
p.put("close", "true");
|
||||
tag=tag.substring(1);
|
||||
} else {
|
||||
p.put("close", "");
|
||||
}
|
||||
|
||||
Util.debug_print("close: " + p.getProperty("close"));
|
||||
|
||||
p.put("type", getTagType(tag));
|
||||
|
||||
Util.debug_print("type: " + p.getProperty("type"));
|
||||
|
||||
if(p.getProperty("type").equals("else") ||
|
||||
p.getProperty("close").equals("true"))
|
||||
return p;
|
||||
|
||||
if(p.getProperty("type").equals("var"))
|
||||
p.put("escape", "");
|
||||
|
||||
int sp = tag.indexOf(" ");
|
||||
// if we've got so far, this must succeed
|
||||
|
||||
tag = tag.substring(sp).trim();
|
||||
Util.debug_print("checking params: " + tag);
|
||||
|
||||
// now, we should have either name=value pairs
|
||||
// or name space escape in case of old style vars
|
||||
|
||||
if(tag.indexOf("=") < 0) {
|
||||
// no = means old style
|
||||
// first will be var name
|
||||
// second if any will be escape
|
||||
|
||||
sp = tag.toLowerCase().indexOf(" escape");
|
||||
if(sp < 0) {
|
||||
// no escape
|
||||
p.put("name", tag);
|
||||
p.put("escape", "0");
|
||||
} else {
|
||||
tag = tag.substring(0, sp);
|
||||
p.put("name", tag);
|
||||
p.put("escape", "html");
|
||||
}
|
||||
} else {
|
||||
// = means name=value pairs.
|
||||
// use a StringTokenizer
|
||||
StringTokenizer st = new StringTokenizer(tag, " =");
|
||||
while(st.hasMoreTokens()) {
|
||||
String key, value;
|
||||
key = st.nextToken().toLowerCase();
|
||||
if(st.hasMoreTokens())
|
||||
value = st.nextToken();
|
||||
else if(key.equals("escape"))
|
||||
value = "html";
|
||||
else
|
||||
throw new NullPointerException(
|
||||
"parameter " + key + " has no value");
|
||||
|
||||
if(value.startsWith("\"") &&
|
||||
value.endsWith("\""))
|
||||
value = value.substring(1,
|
||||
value.length()-1);
|
||||
else if(value.startsWith("'") &&
|
||||
value.endsWith("'"))
|
||||
value = value.substring(1,
|
||||
value.length()-1);
|
||||
|
||||
if(value.length()==0)
|
||||
throw new NullPointerException(
|
||||
"parameter " + key + " has no value");
|
||||
|
||||
if(key.equals("escape"))
|
||||
value=value.toLowerCase();
|
||||
|
||||
p.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
String name = p.getProperty("name");
|
||||
// if not case sensitive, and not special variable, flatten case
|
||||
// never flatten case for includes
|
||||
if(!case_sensitive && !p.getProperty("type").equals("include")
|
||||
&& !( name.startsWith("__") && name.endsWith("__") ))
|
||||
{
|
||||
p.put("name", name.toLowerCase());
|
||||
}
|
||||
|
||||
if(!Util.isNameChar(name))
|
||||
throw new IllegalArgumentException(
|
||||
"parameter name may only contain " +
|
||||
"letters, digits, ., /, +, -, _");
|
||||
// __var__ is allowed in the template, but not in the
|
||||
// code. this is so that people can reference __FIRST__,
|
||||
// etc
|
||||
|
||||
return p;
|
||||
}
|
||||
}
|
||||
130
apps/q/java/src/HTML/Tmpl/Util.java
Normal file
130
apps/q/java/src/HTML/Tmpl/Util.java
Normal file
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* HTML.Template: A module for using HTML Templates with java
|
||||
*
|
||||
* Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
|
||||
*
|
||||
* This module is free software; you can redistribute it
|
||||
* and/or modify it under the terms of either:
|
||||
*
|
||||
* a) the GNU General Public License as published by the Free
|
||||
* Software Foundation; either version 1, or (at your option)
|
||||
* any later version, or
|
||||
*
|
||||
* b) the "Artistic License" which comes with this module.
|
||||
*
|
||||
* 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 either the GNU General Public License or the
|
||||
* Artistic License for more details.
|
||||
*
|
||||
* You should have received a copy of the Artistic License
|
||||
* with this module, in the file ARTISTIC. If not, I'll be
|
||||
* glad to provide one.
|
||||
*
|
||||
* 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 HTML.Tmpl;
|
||||
|
||||
public class Util
|
||||
{
|
||||
public static boolean debug=false;
|
||||
|
||||
public static String escapeHTML(String element)
|
||||
{
|
||||
String s = new String(element); // don't change the original
|
||||
String [] metas = {"&", "<", ">", "\""};
|
||||
String [] repls = {"&", "<", ">", """};
|
||||
for(int i = 0; i < metas.length; i++) {
|
||||
int pos=0;
|
||||
do {
|
||||
pos = s.indexOf(metas[i], pos);
|
||||
if(pos<0)
|
||||
break;
|
||||
|
||||
s = s.substring(0, pos) + repls[i] + s.substring(pos+1);
|
||||
pos++;
|
||||
} while(pos >= 0);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String escapeURL(String url)
|
||||
{
|
||||
StringBuffer s = new StringBuffer();
|
||||
String no_escape = "./-_";
|
||||
|
||||
for(int i=0; i<url.length(); i++)
|
||||
{
|
||||
char c = url.charAt(i);
|
||||
if(!Character.isLetterOrDigit(c) &&
|
||||
no_escape.indexOf(c)<0)
|
||||
{
|
||||
String h = Integer.toHexString((int)c);
|
||||
s.append("%");
|
||||
if(h.length()<2)
|
||||
s.append("0");
|
||||
s.append(h);
|
||||
} else {
|
||||
s.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
public static String escapeQuote(String element)
|
||||
{
|
||||
String s = new String(element); // don't change the original
|
||||
String [] metas = {"\"", "'"};
|
||||
String [] repls = {"\\\"", "\\'"};
|
||||
for(int i = 0; i < metas.length; i++) {
|
||||
int pos=0;
|
||||
do {
|
||||
pos = s.indexOf(metas[i], pos);
|
||||
if(pos<0)
|
||||
break;
|
||||
|
||||
s = s.substring(0, pos) + repls[i] + s.substring(pos+1);
|
||||
pos++;
|
||||
} while(pos >= 0);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
public static boolean isNameChar(char c)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isNameChar(String s)
|
||||
{
|
||||
String alt_valid = "./+-_";
|
||||
|
||||
for(int i=0; i<s.length(); i++)
|
||||
if(!Character.isLetterOrDigit(s.charAt(i)) &&
|
||||
alt_valid.indexOf(s.charAt(i))<0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void debug_print(String msg)
|
||||
{
|
||||
if(!debug)
|
||||
return;
|
||||
|
||||
System.err.println(msg);
|
||||
}
|
||||
|
||||
public static void debug_print(Object o)
|
||||
{
|
||||
debug_print(o.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user