[JDEV] New to Client Side Jabber Programming.

brad cooper coobr01 at yahoo.com
Thu Oct 10 12:13:49 CDT 2002


Below is my simple tests code that is failing. Any
suggestions would be much appreciated :)

-Dorg.xml.sax.driver=org.apache.crimson.parser.XMLReaderImpl

Below code opens up the socket stream and then
attempts to tie stream to parser to get initial jabber
communication going and then print out the
xml....coarse I'm doing something wrong and I'm not
sure what it is. 
---------------------------------------------------


import java.io.*;
import java.net.*;
import java.util.*;

//import org.xml.sax.*;

import java.io.IOException;

import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;



public class MyJabberTest  extends DefaultHandler
{
    StringBuffer textBuffer;
    Socket socket = null;

    InputStream rx = null;
	PrintWriter tx = null;
	
	XMLReader       producer;
    DefaultHandler  consumer;

    
    public static void main(String argv[])
    {
		MyJabberTest Jabber = new MyJabberTest();
		    
		
		Jabber.test();
    }


	public void test()
	{
		try
		{
			socket = new Socket("jabber.org", 5222);
			rx = new
BufferedInputStream(socket.getInputStream());
	  	    tx = new PrintWriter(new
OutputStreamWriter(socket.getOutputStream()));
		} 
		catch (IOException e)
		{
			System.out.println("Unable to connect to
jabber.org");
			System.exit(1);
		}
		
		
		System.out.println("Sending Init XML...Begin");
		
		String xmlInit = "<stream:stream to=\"";
		xmlInit += "jabber.org" + "\"
xmlns=\"jabber:client\"
xmlns:stream=\"http://etherx.jabber.org/streams\">";
		tx.print(xmlInit);
		tx.flush();

		System.out.println("Sending Init XML...End");

	
		System.out.println("Initializing Parser...Begin");
				
				
  		// Get an instance of the default XML parser class
        try {
        	SAXParserFactory factory;
        	
        	factory = SAXParserFactory.newInstance();
        	//factory.setNamespaceAware(true);
        	//factory.setValidating(false);
        	producer =
factory.newSAXParser().getXMLReader();
        	
            producer =
XMLReaderFactory.createXMLReader ();
           
//producer.setFeature("http://xml.org/sax/features/namespaces"
,true);
           
//producer.setFeature("http://xml.org/sax/features/namespace-prefixes"
,false);
        }
        catch (SAXException e)
        {
            System.err.println ("Can't get parser,
check configuration: " + e.getMessage ());
            return;
        }
        catch (FactoryConfigurationError e)
        {
        	System.err.println ("Can't get parser, check
configuration: " + e.getMessage ());
            return;
        }
        catch (ParserConfigurationException e)
        {
        	System.err.println ("Can't get parser, check
configuration: " + e.getMessage ());
            return;
        }

		// set up the consumer
		try {

	   	    // Connect the most important standard
handler
		    producer.setContentHandler (new MyJabberTest());

		    // Arrange error handling
		    producer.setErrorHandler (new MyJabberTest());
		}
		catch (Exception e)
		{
	    	// consumer setup can uncover errors,
		    // though this simple one shouldn't
		    System.err.println (
	          "Can't set up consumers:" + e.getMessage
());
            return;
		}
	
	    // Do the parse!
        try
        {
	      	out = new OutputStreamWriter(System.out,
"UTF8");

            producer.parse (new InputSource (rx));
        } 
        catch (IOException e)
        {
            System.err.println ("I/O error: ");
	    	e.printStackTrace ();
        }
        catch (SAXException e)
        {
            System.err.println ("Parsing error: " +
e.getMessage());
	    	e.printStackTrace ();
        }
	
	
		System.out.println("Initializing Parser...End");
	
	
		
		System.out.println("Sending Ending XML...Begin");
		xmlInit = "</stream:stream>";
		tx.print(xmlInit);
		tx.flush();
		System.out.println("Sending Ending XML...End");



		try {
			rx.close();
			tx.close();
			socket.close();
		}
		catch (IOException ioe) {}

		
	}










    static private Writer  out;
    private String indentString = "    "; // Amount to
indent
    private int indentLevel = 0;

   
//===========================================================
    // SAX DocumentHandler methods
   
//===========================================================

    public void startDocument()
    throws SAXException
    {
        nl();
        nl();
        emit("START DOCUMENT");
        nl();
        emit("<?xml version='1.0'
encoding='UTF-8'?>");
    }

    public void endDocument()
    throws SAXException
    {
        nl(); 
        emit("END DOCUMENT");
        try {
            nl();
            out.flush();
        } catch (IOException e) {
            throw new SAXException("I/O error", e);
        }
    }

    public void startElement(String namespaceURI,
                             String sName, // simple
name
                             String qName, //
qualified name
                             Attributes attrs)
    throws SAXException
    {
        echoText();
        indentLevel++;
        nl(); 
        emit("ELEMENT: ");
        String eName = sName; // element name
        if ("".equals(eName)) eName = qName; // not
namespaceAware
        emit("<"+eName);
        if (attrs != null) {
            for (int i = 0; i < attrs.getLength();
i++) {
                String aName = attrs.getLocalName(i);
// Attr name 
                if ("".equals(aName)) aName =
attrs.getQName(i);
                nl();
                emit("   ATTR: ");
                emit(aName);
                emit("\t\"");
                emit(attrs.getValue(i));
                emit("\"");
            }
        }
        if (attrs.getLength() > 0) nl();
        emit(">");
    }

    public void endElement(String namespaceURI,
                           String sName, // simple
name
                           String qName  // qualified
name
                          )
    throws SAXException
    {
        echoText();
        nl();
        emit("END_ELM: ");
        String eName = sName; // element name
        if ("".equals(eName)) eName = qName; // not
namespaceAware
        emit("</"+eName+">");
        indentLevel--;
    }

    public void characters(char buf[], int offset, int
len)
    throws SAXException
    {
        if (textBuffer != null) {
          echoText();
          textBuffer = null;
        }
        String s = new String(buf, offset, len);
        if (textBuffer == null) {
           textBuffer = new StringBuffer(s);
        } else {
           textBuffer.append(s);
        }
    }
    
   
//===========================================================
    // Utility Methods ...
   
//===========================================================
    
    // Display text accumulated in the character
buffer
    private void echoText()
    throws SAXException
    {
        if (textBuffer == null) return;
        nl(); 
        emit("CHARS:   ");
        String s = ""+textBuffer;
        if (!s.trim().equals("")) emit(s);
        textBuffer = null;
    }

    // Wrap I/O exceptions in SAX exceptions, to
    // suit handler signature requirements
    private void emit(String s)
    throws SAXException
    {
        try {
            out.write(s);
            out.flush();
        } catch (IOException e) {
            throw new SAXException("I/O error", e);
        }
    }

    // Start a new line
    // and indent the next line appropriately
    private void nl()
    throws SAXException
    {
        String lineEnd = 
System.getProperty("line.separator");
        try {
            out.write(lineEnd);
            for (int i=0; i < indentLevel; i++)
out.write(indentString);
        } catch (IOException e) {
            throw new SAXException("I/O error", e);
        }
    }
}
-------------------------------------------------------

--- Ulrich Staudinger <chicago5 at gmx.de> wrote:
> I use TinyXML for java for e3. it runs like a dream
> and is only 6kb in
> size.
> 
> Ulrich
> 
> brad cooper wrote:
> > 
> > Ty for the quikc responses.
> > 
> > I am currently trying to use crimson parser but
> > getting the effects of
> > 
> > 1) I run the program
> > 2) Seems to hang a bit
> > 3) Givings me the below error messages
> > 
> > org.xml.sax.SAXParseException: End of entity not
> > allowed; an end tag is missing.
> >         at
> >
>
org.apache.crimson.parser.Parser2.fatal(Parser2.java:3182)
> >         at
> >
>
org.apache.crimson.parser.Parser2.fatal(Parser2.java:3170)
> >         at
> >
>
org.apache.crimson.parser.Parser2.content(Parser2.java:1837)
> >         at
> >
>
org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1507)
> >         at
> >
>
org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:500)
> >         at
> >
>
org.apache.crimson.parser.Parser2.parse(Parser2.java:305)
> >         at
> >
>
org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:442)
> >         at
> MyJabberTest.test(MyJabberTest.java:129)
> >         at MyJabberTest.main(MyJabberTest.java:45)
> > 
> > Would you happen to have a sample of how you
> > initialize the crimson parser and tie the stream
> to
> > the parse method?
> > 
> > Thank you so much,
> >   Brad
> > 
> > --- Chris Chen <ckchris at idream.net> wrote:
> > > Actually,
> > >
> > > If you use Crimson with JAXP, it will work. 
> Crimson
> > > will support streaming
> > > XML.  Xerces does not really support streaming
> XML..
> > > However, Xerces2 has an
> > > option you can use to enable streaming XML
> > > processing.  Thus, you have two
> > > XML APIs to use and test.
> > >
> > > Thanks,
> > > Chris
> > >
> > >
> > > On Thursday 10 October 2002 07:07, brad cooper
> > > wrote:
> > > > Hello all,
> > > >   I'm looking to learn more about the jabber
> > > protocol.
> > > > I want to start of making a simple java based
> > > client
> > > > that connects to a server and sends a login
> > > message. I
> > > > would like to write this in pure java not
> using
> > > any
> > > > java/jabber libs, so i can learn. I have the
> > > sockets
> > > > connected and am now at the point of
> over-coming
> > > the
> > > > (EOF) stream problem.  I would like to be able
> to
> > > hook
> > > > my JAXP XML parser right 2 the stream, but
> this
> > > seems
> > > > impossible because my parser wants the whole
> > > document
> > > > upfront. Any suggestions on over-coming this
> > > > limitation would be helpful (another parser?
> > > custom
> > > > built stream filter then send to a parser?)
> > > >
> > > > Thanks in advance,
> > > >   Brad
> > > >
> > > >
> __________________________________________________
> > > > Do you Yahoo!?
> > > > Faith Hill - Exclusive Performances, Videos &
> More
> > > > http://faith.yahoo.com
> > > >
> _______________________________________________
> > > > jdev mailing list
> > > > jdev at jabber.org
> > > > http://mailman.jabber.org/listinfo/jdev
> > >
> > > _______________________________________________
> > > jdev mailing list
> > > jdev at jabber.org
> > > http://mailman.jabber.org/listinfo/jdev
> > 
> > __________________________________________________
> > Do you Yahoo!?
> > Faith Hill - Exclusive Performances, Videos & More
> > http://faith.yahoo.com
> > _______________________________________________
> > jdev mailing list
> > jdev at jabber.org
> > http://mailman.jabber.org/listinfo/jdev
> _______________________________________________
> jdev mailing list
> jdev at jabber.org
> http://mailman.jabber.org/listinfo/jdev


__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com



More information about the JDev mailing list