[jdom-interest] Socket: Logical end of fle problem

Marcus Schneller schnemac at zhwin.ch
Thu Feb 14 04:37:18 PST 2002


Hello

I try to pass a BufferedReader from a Socket to the build(Reader reader) 
Method. To avoid that the parser closes the Stream i did following:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MyBufferedReader extends BufferedReader{
        public MyBufferedReader(InputStreamReader i) {
            super(i);
        }

        public void close() {
        }
}

To find the logical end of the file a did following;

import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.*;

public class SocketFilter extends XMLFilterImpl {

    /** Variablen, die das Ende des Documents erkennen sollen */
    private int level;
    private String firstTag;

    public void startElement(String uri, String localName,
      String qName, Attributes atts) throws SAXException {

      if (level++ == 0)
        firstTag = localName;
      super.startElement(uri, localName, qName, atts);
      System.out.println("start"+ localName);
    }

    public void endElement(String uri, String localName,
      String qName) throws SAXException {

      if ((--level == 0) || firstTag.equals(localName))
        throw new SAXException("Finished");
      super.endElement(uri, localName, qName);
      System.out.println("stop"+ localName);
    }
  }

The class I create the SaxBuilder looks like this:

import java.io.InputStream;
import java.io.*;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.Namespace;

  public class InputDocumentHandler{

    private Document doc;
    private SAXBuilder builder;
    private Namespace ns;
   
    public InputDocumentHandler() {
        builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
           builder.setXMLFilter(new SocketFilter());             
    }

    public void createDocument(Reader reader) throws JDOMException{
        try{
              doc = builder.build(reader);
              System.out.println(doc.toString());
              ns = doc.getRootElement().getNamespace();
        }
        catch(JDOMException j){
            throw new JDOMException("Fehler beim Erzeugen des Documents: 
"+ j.getMessage());
        }
    }

When running the System.out.println(doc.toString()); Statement says:
[Document:  No DOCTYPE declaration,  No root element]
and the ns = doc.getRootElement().getNamespace(); Statement throws a 
NullPointerEcxepiotn.

What do I do wrong?

I read the FAQ on http://xml.apache.org/xerces-j/faq-write.html#faq-11 
about the Socket problem, but didn't manage to get it to work with 
SAXBuilder.

Thanks for help

marcus




More information about the jdom-interest mailing list