You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Jarlath Lyons <ja...@activeindexing.com> on 2000/04/14 21:21:46 UTC
Stylesheet fix (more legible?)
The attached class should be of some use ....
I happen to be starting with a stream, so I use it like below ....
It's a rather simple api ... you create a Hashtable of lines you want to add by
setting the Hashtable key to the destination line number.
To make it really flexible, you'd probably need to use a Vector instead of a
Hashtable in order to insert multiple lines at 'line 1' .... but I want higher performance ...
I don't want to be constantly scanning through all the Vector elements. ..../
----------
Hashtable styleSheetInfo = new Hashtable();
String styleSheet = ResourceBundle.....getString("stylesheet.vanilla");
...OR ...
String styleSheet = "<?xml-stylesheet href=\"http://a.b.c/std/xsl/vanilla.xsl\" type=\"text/xsl\"?>\n<?cocoon-process
type=\"xslt\"?>";
//
// we want to add the stylesheet tags right after the first line ...
//
styleSheetInfo.put(new Integer(1),styleSheet);
FilterLineNumberReader flnr = new FilterLineNumberReader(new InputStreamReader(... input stream ....),styleSheetInfo);
... read away .....
------------------------
import java.io.Reader;
import java.io.LineNumberReader;
import java.io.IOException;
import java.util.Hashtable;
public class FilterLineNumberReader extends LineNumberReader
{
private Hashtable insertLines = null;
String line = null;
Integer lineNum = null;
public FilterLineNumberReader(Reader in, Hashtable insertLines)
{
super(in);
this.insertLines = insertLines;
}
public int read(char[] cbuf, int off, int len)
throws IOException
{
if (len < 1)
return -1;
lineNum = new Integer(getLineNumber());
//
// should we insert from the hashtable or read from the stream?
//
if (null != insertLines && null != insertLines.get(lineNum))
{
line = (String) insertLines.get(lineNum);
//
// now make sure we remove this line from the list ...
//
insertLines.remove(lineNum);
}
else
line = readLine();
if (null != line)
{
if (len < line.length())
{
System.out.println("FilterLineNumberReader.read(): big problem ... couldn't finish
writing from the stream ...");
return -1;
}
for (int i = 0; i < line.length(); i++)
{
cbuf[off + i] = line.charAt(i);
}
cbuf[off + line.length()] = '\n';
return line.length() + 1;
}
return -1;
}
}
--
Jar Lyons
Senior Software Engineer
ActiveIndexing.com
jar.lyons@activeindexing.com
(206) 387-5117
100 West Harrison, Suite 310
Seattle, WA 98119