- From: Pasquale Di Feo <pasdif@netfly.it>
- Date: Wed, 17 Jul 1996 13:21:03 +0200
- To: Anselm Baird-Smith <abaird@w3.org>
- Cc: www-jigsaw@w3.org
I have developed a simple filter that replace a <--#counter--> tag in
a HTML document with an graphical counter.
I'm waiting for your suggestions.
Bye
Pasquale
----------------------------------------------------------------------------
--------------------------------
/*
* @(#)ImageCountFilter.java 0.01 17 Jul. 96 Pasquale Di Feo
*
*/
package pdf.jigsaw.filters;
import w3c.jigsaw.http.*;
import w3c.jigsaw.resources.*;
import java.io.*;
import java.lang.*;
/**
* The target is display a graphic counter on an html document.
*
* The use is very easy. Add the filter to an HTML document and set the
parameters<br>
* <ul>
* <li>Counter - start counter
* <li>imgDir - dir (relative to document) that contains the images
* <li>imgPrefix - filename prefix to images (ie: digit_0.gif,
digit_1.gif -> imgPrefix = digit_ )
* <li>imgExtension - file extension (ie: .gif or .jpg, etc.)
* <li>imgWidth - width of images
* <li>imgHeight - height of images
* <li>nDigits - digits to display
* </ul>
*
* Create a directory (in the parent dir of document) with 'imgDir' name and
copy the images in it.
* In the document the tag <!--#counter--> will be replaced with links to
images that represent the
* digits of 'counter' attribute.
* The 'counter' attribute will be incrementated on every page access.
* The images must have the same size.
*
* @version 0.01 17 Jul 1996
* @author Pasquale Di Feo
*/
public class ImageCountFilter extends ResourceFilter {
/**
* Attribute index - The counter attribute.
*/
protected static int ATTR_COUNTER = -1 ; // counter
protected static int ATTR_IMGDIR = -1 ; // images dir (relative to
document)
protected static int ATTR_IMGPREFIX = -1 ; // image prefix filename
(ie: digit_
protected static int ATTR_IMGEXT = -1 ; // image extension (ie: .gif)
protected static int ATTR_NWIDTH = -1 ; // image width
protected static int ATTR_NHEIGHT = -1 ; // image height
protected static int ATTR_NDIGITS = -1 ; // filling with 0 digit
protected final static String COUNTER_TAG = "<!--#counter-->"; // tag
to replace
static {
Attribute a = null ;
Class cls = null ;
try
{
cls = Class.forName("pdf.jigsaw.filters.ImageCountFilter") ;
} catch (Exception ex) {
ex.printStackTrace() ;
System.exit(1) ;
}
// Declare the counter attribute
a = new IntegerAttribute("counter" , new Integer(0),
Attribute.EDITABLE) ;
ATTR_COUNTER = AttributeRegistery.registerAttribute(cls, a) ;
a = new StringAttribute("imgDir", "img", Attribute.EDITABLE) ;
ATTR_IMGDIR = AttributeRegistery.registerAttribute(cls, a) ;
a = new StringAttribute("imgPrefix", "digit_", Attribute.EDITABLE) ;
ATTR_IMGPREFIX = AttributeRegistery.registerAttribute(cls, a) ;
a = new StringAttribute("imgExtension", ".gif", Attribute.EDITABLE) ;
ATTR_IMGEXT = AttributeRegistery.registerAttribute(cls, a) ;
a = new IntegerAttribute("imageWidth", new Integer(15),
Attribute.EDITABLE) ;
ATTR_NWIDTH = AttributeRegistery.registerAttribute(cls, a) ;
a = new IntegerAttribute("imageHeight", new Integer(20),
Attribute.EDITABLE) ;
ATTR_NHEIGHT = AttributeRegistery.registerAttribute(cls, a) ;
a = new IntegerAttribute("nDigits", new Integer(5),
Attribute.EDITABLE) ;
ATTR_NDIGITS = AttributeRegistery.registerAttribute(cls, a) ;
}
/**
* We count all accesses, even the one that failed.
* @param request The request being processed.
*/
public synchronized int ingoingFilter(Request request) {
// System.out.println("Valore del counter = " + getInt(ATTR_COUNTER,
0));
int i = getInt (ATTR_COUNTER, 0) ;
setInt(ATTR_COUNTER, i+1) ;
return CallOutgoing ;
}
/**
* Take the input streamand re-create a new stream for reply
* with replace of <!--counter--> tag with the links at the images.
*
* @param request The request being processed.
* @param reply Reply to send to client
*/
public Reply outgoingFilter(Request request, Reply reply) throws
HTTPException
{
// test if imgPrefix is set corretly
String imgPrefix = getString(ATTR_IMGPREFIX, "");
if (imgPrefix.equals(""))
return reply;
// test if imgExtention is set corretly
String imgExt = getString(ATTR_IMGEXT, "");
if (imgPrefix.equals(""))
return reply;
// Open the original stream
InputStream in = reply.openStream() ;
if ( in == null )
{
// System.out.println("InputStream == null");
return reply ;
}
// create a DataInputStream for reading document lines
DataInputStream ds = new DataInputStream(in);
if (ds == null)
{
// System.out.println("DataInputStream == null");
return reply;
}
String strTmp;
StringBuffer buffer = new StringBuffer();
int nPos = -1;
try
{
// Read the lines
while ( (strTmp = ds.readLine()) != null)
{
// System.out.println(strTmp);
// test if the line contain COUNTER_TAG
if ( (nPos = strTmp.indexOf(COUNTER_TAG) ) != -1)
{
// System.out.println("counter found");
// replace the tag with links at images
strTmp = insertCounter(strTmp, nPos);
}
buffer.append(strTmp).append("\n");
}
}
catch (IOException e)
{
}
// create a new stream
StringBufferInputStream strIS = new
StringBufferInputStream(buffer.toString());
// set the replay stream
reply.setStream(strIS);
return reply;
}
/**
* Replace the tag with the links at images
*
* @param strIn String in input
* @param nPos start position of tag
* @return String replaced
*/
protected String insertCounter(String strIn, int nPos)
{
String strCount = Integer.toString(getInt(ATTR_COUNTER, 0)) ;
int nDigits = getInt(ATTR_NDIGITS, 5);
StringBuffer strOut = new StringBuffer(strIn.substring(0, nPos));
int nFill = (nDigits <= 0) ? -1 : nDigits - strCount.length();
// if nDigits is > 0 fill with 0 digits
for (int i = 0; i < nFill; i++)
strOut.append(createImgLink('0'));
// create the links
for (int i = 0; i < strCount.length(); i++)
strOut.append(createImgLink( strCount.charAt(i) ) );
strOut.append(strIn.substring(nPos + COUNTER_TAG.length() ) );
return strOut.toString();
}
/**
* Create the html link to the digit image
*
* @param digit
*/
protected String createImgLink(char digit )
{
String imgDir = getString(ATTR_IMGDIR , "");
String imgPrefix = getString(ATTR_IMGPREFIX, "digit_");
String imgExt = getString(ATTR_IMGEXT , ".gif");
int nWidth = getInt(ATTR_NWIDTH , 5);
int nHeight = getInt(ATTR_NHEIGHT , 5);
StringBuffer strLink = new StringBuffer("<img src=\"");
// start of tag
strLink.append(imgDir).append("/");
strLink.append(imgPrefix).append(digit).append(imgExt).append("\"");
// img source
strLink.append(" width=").append(nWidth);
// img width
strLink.append(" height=").append(nHeight);
// img height
strLink.append(">");
// end of tag
return strLink.toString();
}
}
Received on Wednesday, 17 July 1996 07:31:38 UTC