This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Need help with apply-templates to set variables



On Sunday, February 3, 2002, at 03:13 PM, Trevor Nash wrote:
> You probably need to change the signature (and internal logic) of
> addPage to handle DOM nodes instead of strings - without knowing what
> the structure of the overall application is, it is hard to give
> detailed advice.

Thanks. Got that far based on advice from another list. See the 
enclosed file, which still isn't working right. The StringWriter 
variable winds up containing an empty string. Any advice would 
be MOST welcome.

The point is to parse a multi-page document into individual 
Velocity template pages and to hold these in a cache of velocity 
template pages.

I never imagined such a simple task would be such a hassle. Why 
it makes sense for string() to to discard part of its contents 
(trees or not) completely eludes me.

public Page getPage(IntegerField pageNumber) throws Fault
	{
		System.err.println("pages:"+pages);
		System.err.println("lastModified"+
			" xml:"+ (lastModified-xmlFile.lastModified())+
			" xsl:"+ (lastModified-xslFile.lastModified())
		);
		if (pages == null ||
			xmlFile.lastModified() > lastModified ||
			xslFile.lastModified() > lastModified)
		{
			if (!xmlFile.exists()) throw new Fault(this + " 
can't find:"+xmlFile, null);
			if (!xslFile.exists()) throw new Fault(this + " 
can't find:"+xslFile, null);
			try
			{
				System.err.println("transforming:"+ xmlFile + " with " + xslFile);
				TransformerFactory tFactory = TransformerFactory.newInstance();
				Transformer transformer = tFactory.newTransformer(
					new StreamSource(xslFile.toString())
				);
				transformer.setParameter("task", this);
				StringWriter sw = new StringWriter();
				transformer.transform(
					new StreamSource(xmlFile.toString()),
					new StreamResult(sw)
				);
				System.err.println("discarded text:"+sw);
				this.lastModified = System.currentTimeMillis();
			}
			catch (Exception e)
			{
				throw new Fault("Exception 
transforming:"+xmlFile+ " with:"+xslFile, e);
			}
		}
		else System.err.println("reusing cached page");
		return (Page)pages.get(pageNumber.getInt());
	}
	public void addPage( String ident, org.w3c.dom.Node body)
	{
		try
		{
			StringWriter sw = new StringWriter();
			Serializer serializer = SerializerFactory.getSerializer
				(OutputProperties.getDefaultMethodProperties("html"));
			serializer.setWriter(sw);
			serializer.asDOMSerializer().serialize(body);
			System.err.println("addPage:"+sw);
			pages.add(new Page(this, ident, "<h2>sw</h2>"+sw, 
new IntegerField(pages.size())));
		}
		catch (IOException e)
		{
			System.err.println("IOException:" + ident);
			e.printStackTrace();
		}
	}


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]