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/30 22:06:36 UTC

Subject: Problems with Xerces Parser in Xalan

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();
    }
    
}

/*
 * MessageEntityResolverReader.java
 *
 * Created on 29 March 2001, 19:24
 */

package CommonUtils;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import java.util.Hashtable;

/**
 *
 * @author  Arvind S Bassi
 * @version 1.0
 */
public class MessageEntityResolverReader{
    
    private static MessageEntityResolverReader
_instance;
    
    private static String FILTER = "";
    
    private static Hashtable fileHash;
    
    /** Creates new MessageEntityResolverReader */
    private MessageEntityResolverReader() {
        fileHash = new Hashtable();
        fileHash.clear();
    }
    
    public static synchronized
MessageEntityResolverReader getInstance(){
        if(_instance==null){
            _instance = new
MessageEntityResolverReader();
        }
        return _instance;
    }
    
    public static synchronized boolean
cacheEntities(File d){
        boolean result = false;
        
        fileHash.clear();
        
        File [] files;
        
        try{
            if(!d.isDirectory()){
                return result;
            }
            
            files = d.listFiles();
            
        } catch(Exception e){
            return result;
        }
        
        String s, str;
        ByteArrayInputStream bias;
        InputStream is;
        
        try{
            
            for(int i=0;i<files.length;i++){
                if(files[i].isFile()&&
                   
((files[i].getName()).toUpperCase()).endsWith(".DTD")){
                    s = "";
                    BufferedReader in = new
BufferedReader(
                    new FileReader(files[i]));
                    str = null;
                    
                    while ((str = in.readLine()) !=
null) {
                        s += str;
                        //System.out.println("Still
reading data");
                    }
                    in.close();
                    
                    bias = new
ByteArrayInputStream(s.getBytes());
                    is = (InputStream) bias;
                    
                   
//System.out.println("*"+files[i].getName()+"* has
been placed in Hashtable");
                    
                   
fileHash.put(files[i].getName(),is);
                }
            }
            
            result = true;
            
        } catch(Exception e){
           
System.out.println(_instance.getClass().getName()+":
Error has occurred Error = "+
            e.getMessage());
        }
        
        return result;
    }
    
    public static synchronized InputStream
getEntity(String s){
       
        InputStream is;
        
        is = (InputStream) fileHash.get(s);

        return is;
    }
}

____________________________________________________________
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