You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Arvind Bassi <ar...@yahoo.co.uk> on 2001/03/31 00:32:17 UTC

Re: Subject: Problems with Xerces Parser in Xalan - Apology

Ooops. Sorry, but l forgot to use setErrorHandle.
(Thanks Myriam, it wasn't such a stupid question).

Therefore, this explains the reason why MessageParser
cannot pick up SAXParseException within itself.

HOWEVER, why are there not SAXParseException being
thrown when the xml document does not adhere to its
DTD?

Arvind.


--- Myriam_Midy@lotus.com wrote: > 
> This is probably a stupid question from me, but
> since I don't see you
> calling program in here, I'll ask anyway (even
> though I didn't really
> thouroughly look at your code)  . Are you setting
> MessageParser as the
> errorListener on the parser and/or on the
> transformer?
> 
> Myriam
> 
> 
>                                                     
>                                      
>                     Arvind Bassi                    
>                                      
>                     <arvind_bassi@ya        To:    
> xalan-dev@xml.apache.org              
>                     hoo.co.uk>              cc:    
> (bcc: Myriam Midy/CAM/Lotus)          
>                                             Subject:
>     Subject: Problems with Xerces    
>                     03/30/01 03:06          Parser
> in Xalan                               
>                     PM                              
>                                      
>                     Please respond                  
>                                      
>                     to xalan-dev                    
>                                      
>                                                     
>                                      
>                                                     
>                                      
> 
> 
> 
> 
> I have got the xalan-j_2_0_1.zip version of  Xalan
> Java 2.
> 
> I am new to Java XML Parsers and am experiencing
> difficulty with the following code.
> 
> The code is a rip-off of the DOMParser code which
> ships with Xerces. Therefore, l cannot see what is
> wrong with it....
> 
> I have imported all the classes necessary.
> 
> In terms of a EntityResolver, l have created my own.
> The problem that l had was that l wanted to reduce
> the
> amount of disk access, and so my
> MessageEntityResolverReader reads all the DTD files
> and stores them as InputStream in a Hashtable for
> later retrieval.
> 
> MessageEntityResolverReader is a singleton, and a
> class before MessageParser instantiates it and
> passes
> the file directory which contains the external DTDs.
> 
> I have two problems:
> 
> 1. A SAXParseException is only thrown if the xml
> document is NOT well-formed. If it does not adhere
> to
> the DTD then NO SAXParseException is thrown. I have
> set the validation on the DOMParser and still
> nothing.
> 
> WHY is the parser not ensuring that the xml document
> is valid?
> 
> 2. If the xml document is not well-formed, the
> SAXParseException bypasses the methods within
> MessageParser, and is caught by the calling program.
> Therefore although MessageParser implements
> ErrorHandler, and l have populated the necessary
> methods, these methods are never called.
> 
> WHAT am l doing wrong? WHY isn't the
> SAXParseException
> being caught within MessageParser?
> 
> Arvind.
> 
> /*
>  * MessageParser.java
>  *
>  * Created on 29 March 2001, 19:06
>  */
> 
> package CommonUtils;
> 
> import org.xml.sax.ErrorHandler;
> import org.apache.xerces.parsers.DOMParser;
> 
> import org.xml.sax.InputSource;
> 
> import org.xml.sax.SAXException;
> import org.xml.sax.SAXParseException;
> import org.xml.sax.SAXNotRecognizedException;
> import org.xml.sax.SAXNotSupportedException;
> 
> import org.w3c.dom.Document;
> 
> import java.io.ByteArrayInputStream;
> 
> /**
>  *
>  * @author  Arvind S Bassi
>  * @version 1.0
>  */
> public class MessageParser implements ErrorHandler{
> 
>     private DOMParser parser = new DOMParser();
>     private MessageEntityResolver mer;
> 
>     /** Creates new MessageParser */
>     public MessageParser() {
>         try{
> 
>
parser.setFeature("http://xml.org/sax/features/validation",true);
>         } catch(SAXException e){
>             System.out.println("Error in setting up
> parser feature Error = "+
>             e.getMessage());
>         }
> 
>         mer = new MessageEntityResolver();
> 
>         parser.setEntityResolver(mer);
>     }
> 
>     //testing only!!!!!!!!!!
>     public String parse(String s) throws Exception{
> 
>         String str = null;
> 
>         ByteArrayInputStream bias = new
> ByteArrayInputStream(s.getBytes());
>         InputSource is = new InputSource(bias);
> 
>         parser.parse(is);
> 
>         return str;
>     }
> 
>     public void setFeature(String featureId, boolean
> state)
>     throws SAXNotRecognizedException,
> SAXNotSupportedException{
>         parser.setFeature(featureId, state);
>     }
> 
>     public void warning(SAXParseException p1){
>         System.out.println("[Warning]"+
>                     getLocationString(p1)+": "+
>                     p1.getMessage());
>     }
> 
>     public void error(SAXParseException p1){
>         System.out.println("[Error]"+
>                     getLocationString(p1)+": "+
>                     p1.getMessage());
>     }
> 
>     public void fatalError(SAXParseException p1)
> throws SAXException {
>         System.out.println("[Fatal Error]"+
>                     getLocationString(p1)+": "+
>                     p1.getMessage());
>         throw p1;
>     }
> 
>     private String
> getLocationString(SAXParseException
> ex){
>         StringBuffer str = new StringBuffer();
> 
>         String systemId = ex.getSystemId();
> 
>         if(systemId != null){
>             int index = systemId.lastIndexOf('/');
>             if(index != -1){
>                 systemId =
> systemId.substring(index+1);
>             }
>             str.append(systemId);
>         }
> 
>         str.append(':');
>         str.append(ex.getLineNumber());
>         str.append(':');
>         str.append(ex.getColumnNumber());
> 
>         return str.toString();
>     }
> 
=== message truncated ===


____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie