[prev] [up] [next]

Java Support - Java-Smalltalk Interoperability

In a Nutshell

Let's start with simple example - parsing XML file using Xerces, an XML parser written in Java.

Execute following code in the workspace:

  factory := JAVA javax xml parsers SAXParserFactory newInstance.
  parser := factory newSAXParser getXMLReader.
  parser setContentHandler: CDDatabaseHandler1 new.
  [
    parser parse: 'cd.xml'.
  ] on: JAVA java io IOException do:[:ioe|
    Transcript showCR: 'I/O error: ', ioe getMessage.
    ioe printStackTrace
  ]

This code makes use of Saxon, a xml processing library written in Java. 'cd.xml' contains data we want to process.

On the first line a typical way of accessing Java classes in STX:LIBJAVA can be seen. New instance of SaxParserFactory is created and used to get parser instance. Both these instances are instances of Java classes. Parser has to be given instance of org.xml.sax.ContentHandler, which is then used during the parsing.

In the example, a Smalltalk object is passed in instead. Sax events generated by the Java parser are therefore processed by Smalltalk object. Java code can throw java.io.IOException, which should be caught. In Smalltalk. Java exceptions are using standard Smalltalk exception-handling idioms - using #on:do: or handle:do:/

More to be written...


<info@exept.de> , <stxlibjava-dev@googlegroups.com>

CVS: $Id: java-interop.html,v 1.2 2013-02-25 11:15:32 vrany Exp $