Return-Path: Delivered-To: apmail-xml-xalan-j-users-archive@xml.apache.org Received: (qmail 40586 invoked by uid 500); 9 Oct 2002 00:14:19 -0000 Mailing-List: contact xalan-j-users-help@xml.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Delivered-To: mailing list xalan-j-users@xml.apache.org Received: (qmail 40576 invoked from network); 9 Oct 2002 00:14:19 -0000 Received: from cobra.ecnetwork.co.nz (202.135.116.44) by daedalus.apache.org with SMTP; 9 Oct 2002 00:14:19 -0000 Received: from serpent.ecnetwork.co.nz (serpent [202.135.190.10]) by cobra.ecnetwork.co.nz (8.11.4/8.11.3) with SMTP id g991KdG07110 for ; Wed, 9 Oct 2002 14:20:39 +1300 Received: (qmail 6191 invoked by uid 0); 9 Oct 2002 00:11:09 -0000 Received: from pcsimon.ecnnz.ecnetwork.co.nz (202.135.190.44) by serpent.ecnetwork.co.nz with SMTP; 9 Oct 2002 00:11:09 -0000 Subject: RE: How to throw custom defined exceptions, warnings from xsl pag e? From: Simon Kitching To: Prakash Sridharan Cc: xalan-j-users@xml.apache.org In-Reply-To: References: Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.8-3mdk Date: 09 Oct 2002 13:07:52 +1300 Message-Id: <1034122072.2391.169.camel@PCSIMON.ecnnz.ecnetwork.co.nz> Mime-Version: 1.0 X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Status: O X-Status: X-Keywords: Hi Prakash, I have to do exactly the same thing as you. Unfortunately, Xalan has some problems in this area, for which I am intending to submit some patches when I finally dig myself out from the pile of work I have at the moment. I hope that one of the suggestions below helps.. -- option 1: xsl:message In a stylesheet, you can use: Error X has occurred This will cause the ErrorListener associated with the current Transformer object to be called as follows: (a) the info method of the error listener is called, with the message body text specified in the stylesheet, then (b) the error method of the error listener is called with the message "stylesheet directed termination". The default ErrorListener's info/warn methods prints messages to stdout, then returns (which causes the stylesheet to continue). The error/fatal methods throw a TransformerException which ends up being thrown out of the Transformer.transform method, where your code can pick it up. You will probably want to set a custom ErrorListener on the transformer before doing the transform, in order to redirect the error messages to somewhere other than stdout (or is it stderr), or (as suggested below) cache it for later retrieval. The problem is that the exception that is thrown by Transformer.transform just contains the message "Stylesheet directed termination"; the actual message body of the xsl:message tag is not contained in this exception. What I therefore do is have an ErrorListener which caches the INFO level exception; when I get a TransformerException from the Transformer.transform method, I then get the "real" message from the ErrorListener. This isn't 100% reliable, as if something else reports an INFO message there can be confusion. However it works in practice. I am intending to create a patch which attaches the xsl:message body to the exception thrown to avoid this roundabout solution; I can't see any problem with this change. Comments/opinions anyone? Note also the following bug: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7357 Due to this bug, if you implement a custom ErrorListener, make sure that the error/fatal methods generate messages which start with "fatal" or your xsl processing will not terminate as expected. -- option 2: a custom extension tag to replace xsl:message I happen to have some extension elements already being used from my stylesheet, so I ended up adding another extension which basically implements the patch I suggested above. I won't provide this code, as it is currently mixed in with all my other stuff, and anyway the "proper" solution I think is to patch xsl:message. Regards, Simon On Tue, 2002-10-08 at 21:04, Prakash Sridharan wrote: > Hi Simon, > > I am giving a scenario in which I want to throw custom defined exceptions. > > The input xml looks like this, > > > > P > 12 > m > > > I am validating the data in the above xml using xsl. > Some of the validations are, > 1. Age should be greater than 18 > 2. sex should be "m" or "f" > > If the above conditions are not met, I want to throw some exceptions or > warnings > from the xsl. After rendering, I shoud be able to access all the exceptions > and > warnings from my java program where I am calling this xsl transformation. > > The xsl looks like this, > > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> > > > > > > > > > > > > > > > > > > > > > > Can you help me in this? > > Thank you, > Prakash > > -----Original Message----- > From: Simon Kitching [mailto:simon@ecnetwork.co.nz] > Sent: Tuesday, October 08, 2002 2:31 AM > To: Prakash Sridharan > Cc: xalan-j-users@xml.apache.org > Subject: Re: How to throw custom defined exceptions, warnings from xsl > page? > > > Hi Prakash, > > Can you provide some more details about what you are trying to do? > > Are you using standard xsl? In this case, what do you mean by "custom > defined exceptions"? Do you just mean generating your own error > messages? If this is the case, you can use together with a > custom ErrorListener class. > > Are you using some custom extension tags or functions, and want to > generate errors/exceptions from these? In this case, I can provide you > with some example code as I am doing this myself. > > Cheers, > > Simon > > On Tue, 2002-10-08 at 05:43, Prakash Sridharan wrote: > > Hi all, > > > > In my project, I am doing the data validation in an xml through using xsl. > > I want to throw some > > custom defined exceptions and warnings from xsl page while rendering based > > on the validations. > > Can anybody help me in doing this? > > > > Thank you, > > Prakash >