• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

about Transformer Exception

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to retieve data from database and present it to the client as XML. I am using JDOM to build the XML and that is going fine. when i am transforming it to the client side a Transformer Exception is thrown.

i will give my code here

---This is my servlet---
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.jdom.*;
import com.demo.*;
import com.db.*;


public class Loginreply extends HttpServlet
{
Queryclass qrc;
UserPass up=new UserPass();
HttpServlet servlet;

public void doGet(HttpServletRequest request,HttpServletResponse

response)
throws ServletException,IOException
{
String username=request.getParameter("id");
String password=request.getParameter("pass");

try{
qrc=new Queryclass();
qrc.insertTable(username,password);}

catch(Exception e){}

Document doc=new Document(DemoXmlJDom.produceElement(this.up));

XSLTRenderHelper.render(this, doc, "demo.xsl", response);


--my Xslthelprenderer file---
import java.io.*;
import java.net.URL;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.jdom.*;
import org.jdom.output.*;

/**
* A helper class that makes rendering of XSLT easier. This
* eliminates the need to duplicate a lot of code for each
* of the web pages in this app.
*/
public class XSLTRenderHelper {

private static Map filenameCache = new HashMap();
static FileWriter fw=null;




public static void render(HttpServlet servlet, Document xmlJDOMData,
String xsltBaseName, HttpServletResponse response)
throws ServletException, IOException {

String xsltFileName = null;
try {
fw=new FileWriter("D://Surendar/flow.txt");
// figure out the complete XSLT stylesheet file name

synchronized (filenameCache) {
fw.write("I m in Sync");
xsltFileName = (String) filenameCache.get(xsltBaseName);

if (xsltFileName == null) {
fw.write("xsltFileName == null.....");
ServletContext ctx = servlet.getServletContext();
fw.write("getservletcontext....");
xsltFileName = ctx.getRealPath(
"/WEB-INF/xslt/" + xsltBaseName);
fw.write("get real path.....");
filenameCache.put(xsltBaseName, xsltFileName);
fw.write("basename,realname.....");
}
}

// write the JDOM data to a StringWriter

StringWriter sw = new StringWriter();
fw.write("stringwriter.....");
XMLOutputter xmlOut = new XMLOutputter("", false, "UTF-8");
fw.write("xmloutputter.....");
xmlOut.output(xmlJDOMData, sw);
fw.write("xmljdomdata.....");
response.setContentType("text/html");
fw.write("may be servlet exception.....");
Transformer trans = StylesheetCache.newTransformer(xsltFileName);
fw.write("by transformer.....");

// pass a parameter to the XSLT stylesheet
trans.setParameter("rootDir", "/demo/");
fw.write("checking parameter.....");
trans.transform(new StreamSource(new StringReader(sw.toString())),
new StreamResult(response.getWriter()));
fw.write("check stream output.....");
}

catch (IOException ioe) {
throw ioe;
}

catch (Exception ex) {
throw new ServletException(ex);
}
finally
{
fw.close();
}
}

private XSLTRenderHelper() {
}
}

--my Stylesheetcache-----

import java.util.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;


public class StylesheetCache {
// map xslt file names to MapEntry instances
// (MapEntry is defined below)


private static Map cache = new HashMap();

public static synchronized void flushAll() {
cache.clear();
}

public static synchronized void flush(String xsltFileName) {
cache.remove(xsltFileName);
}

/**
* Obtain a new Transformer instance for the specified XSLT file name.
* A new entry will be added to the cache if this is the first request
* for the specified file name.
*
* @param xsltFileName the file name of an XSLT stylesheet.
* @return a transformation context for the given stylesheet.
*/

public static synchronized Transformer newTransformer(String xsltFileName)
throws TransformerConfigurationException {



File xsltFile = new File(xsltFileName);

// determine when the file was last modified on disk
long xslLastModified = xsltFile.lastModified();
MapEntry entry = (MapEntry) cache.get(xsltFileName);

if (entry != null) {
// if the file has been modified more recently than the
// cached stylesheet, remove the entry reference
if (xslLastModified > entry.lastModified) {
entry = null;
}
}




// create a new entry in the cache if necessary
if (entry == null) {
Source xslSource = new StreamSource(xsltFile);

TransformerFactory transFact = TransformerFactory.newInstance();
Templates templates = transFact.newTemplates(xslSource);

entry = new MapEntry(xslLastModified, templates);
cache.put(xsltFileName, entry);
}


return entry.templates.newTransformer();
}

// prevent instantiation of this class
private StylesheetCache() {
}

/**
* This class represents a value in the cache Map.
*/
static class MapEntry {
long lastModified; // when the file was modified
Templates templates;

MapEntry(long lastModified, Templates templates) {
this.lastModified = lastModified;
this.templates = templates;
}
}
}

--my JDOMBuild class--
package com.demo;

import java.util.*;
import org.jdom.*;
import org.jdom.output.*;
import com.db.*;
import java.sql.*;


public class DemoXmlJDom

{
public static Element produceElement(UserPass up)

{
Element detelem=new Element("details");
detelem.addContent(new Element("username").setText(up.getUser()));
detelem.addContent(new Element("password").setText(up.getPass()));
return detelem;
}

private DemoXmlJDom(){}

}

here the i have not given the database code.and the Exception is thrown in XSLTReanderhelper class when i call newTransformer method in Style sheet cache class.

The Exception message........
javax.servlet.ServletException: javax.xml.transform.TransformerException: java.lang.IllegalStateException: can't declare any more prefixes in this context
XSLTRenderHelper.render(XSLTRenderHelper.java:84)

my web server is tomcat5.0. and my client is IE6.0.

and my DemoXmlJDom class is not throwing anything. i think when i pass (this) object ,to the render method is the reason for the exception...
and the render class and stylesheet cache class edited and pasted from one book as required by me. that may contain some unwanted methods aswell. the request is made by a html form.

if u need the remaining code also i will give u.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't really pinpoint the problem based on your post, but what exactly is line # 84 in the XMLRenderHelper class ?

- m
 
Get me the mayor's office! I need to tell him about this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic