public class BasePage extends java.lang.Object implements TemplatePage
<<:tag option1 option2 ...:>>
tag
with a supplied value, applying all
formatting options that follow the tag name.StringFormat
class that were previously registered into this class via registerExactTag(String, StringFormat)
or
registerRegexpTag(String, StringFormat)
.Esc
: HTML-escaped version of the stringEnc
: URL-encoded version of the stringJS
: JS-escaped version of the stringDash
: the same string with any sequence of non-alphanumeric characters replaced by a single dashUnder
: the same string with any sequence of non-alphanumeric characters replaced by a single underscoreChecked
: "checked" if the value is >=1NiceDate
: nice representation of the date onlyDate
: nicely formatted dateTime
: show only the time from the given dateDot
: display a long value as groups of 3 digits separated by dotSize
: consider that the value is a file size, in bytes, display it in a human readable formatUEnc
: URL-decoded version of the stringNtoBR
) : convert text new lines to html new linesDate
: the leftover is the SimpleDateFormat
formating string, with spaces replaced by underscoresCut
: take only the first N characters from the string, ignoring HTML tags and keeping only A,B,FONT,U and I from themDDot
: display a double number with the given number of digits after the decimal pointStrip
: strip HTML tags from the text, optionally leaving some of them behind. See the class description for detailsfillFromDB(DBFunctions)
callgetResDir()
, to avoid
hard coding template paths everywhere in your application.BasePage(OutputStream, String, boolean)
with the last argument
to false
to prevent the framework from caching the contents. For base performance switch
back to true
as soon as you finished changing the templates.
BasePage p = new BasePage(osOut, "/path/to/general/template.res", true);
BasePage pLine = new BasePage(null, "/path/to/a/single/line/template.res", false);
DB db = new DB("SELECT id,username,lastlogin FROM users;");
while (db.moveNext()){
pLine.fillFromDB(db);
p.append(pLine);
}
p.write();
pLine
could point to a file containing something like:
<tr>
<td><<:id db:>></td>
<td><<:username db esc:>></td>
<td><<:lastlogin db nicedate:>> <<:lastlogin db time:>></td>
</tr>
Modifier and Type | Field and Description |
---|---|
java.util.Map<java.lang.String,java.lang.Object> |
commonValues
Common values across iterations
|
java.util.Map<java.lang.String,java.lang.StringBuilder> |
mValues
Values for the tags
|
protected java.io.OutputStream |
os
Output stream, to write the generated HTML to.
|
java.util.Set<java.lang.String> |
sComments
What are the sections that are commented out?
|
java.lang.String |
sFile
Original file that was used to read the contents from.
|
protected java.io.Writer |
writer
For writing to a Writer instead of a OutputStream (from a JSP page for example).
|
Constructor and Description |
---|
BasePage()
Create an empty holder to whatever.
|
BasePage(java.io.OutputStream osOut,
java.lang.String sTemplateFile)
Create a page by reading the contents of the given file.
|
BasePage(java.io.OutputStream osOut,
java.lang.String sTemplateFile,
boolean bCached)
Create a page by reading the contents of the given file.
|
BasePage(java.lang.String sTemplateFile)
Create a page by reading the contents of the given file.
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.lang.Object oValue)
Append a value to the default tag, "content"
|
void |
append(java.lang.String sTag,
java.lang.Object oValue)
Append a value to a tag.
|
void |
append(java.lang.String sTagName,
java.lang.Object oValue,
boolean bBeginning)
Append a value to a tag, at the start or at the end.
|
static void |
clear()
Clear the page templates cache
|
void |
comment(java.lang.String sTagName,
boolean bShow)
Shortcut for easy hiding of pieces of html.
|
void |
fillFromDB(DBFunctions db)
For all the tags that have the "db" option attached to them try to get the columns with the same name from the database row.
|
java.lang.StringBuilder |
getContents()
Apply the dynamic values to the initial template to obtain a StringBuilder representation of the final data.
|
java.util.Set<java.lang.String> |
getDBTags()
Get the template fields that have the "db" option.
|
static BasePage |
getPage(java.lang.String sTemplate)
Create a page based on a predefined template.
|
protected java.lang.String |
getResDir()
This method should be overridden by an object internal to each zone, that knows where
all the HTML templates for that zone reside.
|
java.util.Set<java.lang.String> |
getTagsSet()
Get the set of all tags in the template
|
static void |
main(java.lang.String[] args)
Testing code
|
void |
modify(java.util.Map<?,?> m)
Set a bulk of tags in one move.
|
void |
modify(java.lang.String sTag,
java.lang.Object oValue)
Assign a value to a tag, only if this tag wasn't assigned at a previous time.
|
static void |
registerExactTag(java.lang.String sOption,
StringFormat sf)
This allows the programmer to implement new formatting options and to dynamically make them visible by registering them here.
|
static boolean |
registerRegexpTag(java.lang.String sPattern,
StringFormat sf)
This allows the programmer to implement new formatting options and to dynamically make them visible by registering them here.
|
void |
reset()
Cancel any changes done to the template.
|
java.lang.Object |
set(java.lang.String sTag,
java.lang.Object oValue)
Set a value that is not cleaned between write() or append() of this object (so it can be used across all lines in a table for example).
|
void |
setCallingServlet(ExtendedServlet servlet)
If a page contains modules that need access to the request / response this is the way to make it work.
|
void |
setOutputStream(java.io.OutputStream osOut)
If at a later time the user (programmer) decides that this object is to write itself into an output stream it can call this method.
|
void |
setWriter(java.io.Writer writer)
If you want to write directly to a writer (for example from a JSP).
|
java.lang.String |
toString()
Override the default toString() to obtain the dynamic data applied over the original template.
|
java.lang.Object |
unset(java.lang.String sTag) |
boolean |
write()
Try to send the generated content to the given output stream, assuming UTF-8 charset.
|
protected java.io.OutputStream os
protected java.io.Writer writer
public java.lang.String sFile
public final java.util.Map<java.lang.String,java.lang.StringBuilder> mValues
public java.util.Map<java.lang.String,java.lang.Object> commonValues
public final java.util.Set<java.lang.String> sComments
comment(String, boolean)
public BasePage()
public BasePage(java.lang.String sTemplateFile)
sTemplateFile
- file to read.public BasePage(java.io.OutputStream osOut, java.lang.String sTemplateFile)
write()
later on this
object, the generated contents will be written to the given OutputStream, if this is not null.osOut
- stream to write the generated HTML to at the endsTemplateFile
- file name to read the template frompublic BasePage(java.io.OutputStream osOut, java.lang.String sTemplateFile, boolean bCached)
write()
later on this
object, the generated contents will be written to the given OutputStream, if this is not null. With
this constructor you can control whether or not the contents of this file is kept precompiled in memory.
The default behavior of the simpler constructors is to cache everything. This is generally a good idea,
since the original files are anyway monitored for changes and the contents is automatically reloaded if
the underlying file is changed (1 minute between checks).osOut
- stream to write the generated HTML to at the endsTemplateFile
- file name to read the template frombCached
- controls the caching of this fileprotected java.lang.String getResDir()
public static void main(java.lang.String[] args)
args
- ignoredpublic void setOutputStream(java.io.OutputStream osOut)
osOut
- output stream to write the contents topublic void setWriter(java.io.Writer writer)
writer
- public java.lang.Object set(java.lang.String sTag, java.lang.Object oValue)
append(String, Object)
or modify(String, Object)
will have precedence over the common values.sTag
- tag that is modifiedoValue
- set valuepublic java.lang.Object unset(java.lang.String sTag)
sTag
- public void modify(java.lang.String sTag, java.lang.Object oValue)
sTag
- tag to assign a value tooValue
- the value to assign. If null
then the empty string will be put in the map.append(String, Object, boolean)
public void modify(java.util.Map<?,?> m)
m
- the <tag, value> mappingpublic void append(java.lang.Object oValue)
oValue
- value to be appended to the default tag.public void append(java.lang.String sTag, java.lang.Object oValue)
sTag
- tag nameoValue
- value to appendpublic void append(java.lang.String sTagName, java.lang.Object oValue, boolean bBeginning)
append
in interface TemplatePage
sTagName
- tag nameoValue
- value to appendbBeginning
- true to add at the start, false to add at the endpublic java.lang.StringBuilder getContents()
getContents
in interface Page
public java.lang.String toString()
toString
in class java.lang.Object
public boolean write()
public void reset()
modify(String, Object)
, append(String, Object)
etc are not
going to be used and you have to prepare the object for the next iteration.public void comment(java.lang.String sTagName, boolean bShow)
sTagName
- base name of the two tagsbShow
- true if you want to display the contents, false if you want to hide itpublic void fillFromDB(DBFunctions db)
db
- a database object that holds the result of a select query, having the same column names as the tags you want to fillpublic java.util.Set<java.lang.String> getDBTags()
null
public static final void registerExactTag(java.lang.String sOption, StringFormat sf)
sOption
- option for which to add formatting namesf
- an instance of the formatting class for this optionpublic static final boolean registerRegexpTag(java.lang.String sPattern, StringFormat sf)
sPattern
- pattern that the option matchessf
- an instance of the formatting class for this optionpublic void setCallingServlet(ExtendedServlet servlet)
servlet
- public static void clear()
public java.util.Set<java.lang.String> getTagsSet()
public static BasePage getPage(java.lang.String sTemplate)
sTemplate
-