src/examples/JavaExamples.st
branchjk_new_structure
changeset 1573 38d044a80b88
child 1628 5db2854824b5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/examples/JavaExamples.st	Mon Jul 30 22:45:40 2012 +0000
@@ -0,0 +1,231 @@
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+
+ New code and modifications done at SWING Research Group [1]:
+
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+
+ [1] Code written at SWING Research Group contains a signature
+     of one of the above copright owners. For exact set of such code,
+     see the differences between this version and version stx:libjava
+     as of 1.9.2010
+"
+"{ Package: 'stx:libjava/examples' }"
+
+Object subclass:#JavaExamples
+	instanceVariableNames:''
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Java-Utilities'
+!
+
+Object subclass:#CDDatabaseHandler
+	instanceVariableNames:'index title artist tag'
+	classVariableNames:''
+	poolDictionaries:''
+	privateIn:JavaExamples
+!
+
+!JavaExamples class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1996-2011 by Claus Gittinger
+
+ New code and modifications done at SWING Research Group [1]:
+
+ COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
+                            SWING Research Group, Czech Technical University in Prague
+
+ This software is furnished under a license and may be used
+ only in accordance with the terms of that license and with the
+ inclusion of the above copyright notice.   This software may not
+ be provided or otherwise made available to, or used by, any
+ other person.  No title to or ownership of the software is
+ hereby transferred.
+
+ [1] Code written at SWING Research Group contains a signature
+     of one of the above copright owners. For exact set of such code,
+     see the differences between this version and version stx:libjava
+     as of 1.9.2010
+
+"
+! !
+
+!JavaExamples class methodsFor:'examples'!
+
+example_1
+    "A CD .xml parsing example. The parser raises a UserNotification
+     whenever it parses a CD, it raises a UserNotification. The notification
+     is then handled and printed. Demonstrates:
+      - proxy creation
+      - exception handling
+
+    cd.xml may look like:
+
+    <?xml version='1.0' encoding='ISO-8859-1'?>
+    <catalog>
+        <cd>
+            <title>Empire Burlesque</title>
+            <artist>Bob Dylan</artist>
+            <country>USA</country>
+            <company>Columbia</company>
+            <price>10.90</price>
+            <year>1985</year>
+        </cd>
+    </catalog>
+
+
+
+    "
+
+    | factory parser |
+
+    factory := JAVA javax xml parsers SAXParserFactory newInstance.
+    parser := factory newSAXParser getXMLReader.
+    parser setContentHandler: JavaExamples::CDDatabaseHandler new.
+    [
+        parser parse: '/home/jv/Projects/libjava/demo/saxon/cd.xml'.
+    ] on: JAVA java io IOException do:[:ioe|
+        Transcript showCR: 'I/O error: ', ioe getMessage.
+        ioe printStackTrace
+    ] on: UserNotification do:[:un|
+        Transcript showCR: un messageText.
+        un proceed.
+    ]
+
+    "Created: / 13-05-2012 / 18:27:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaExamples::CDDatabaseHandler class methodsFor:'documentation'!
+
+documentation
+"
+    A simple XML CD database parser - a demonstration
+    of stx:libjava capabilities. See JavaExamples>>example_1
+
+    [author:]
+        Jan Vrany <jan.vrany@fit.cvut.cz>
+
+    [instance variables:]
+
+    [class variables:]
+
+    [see also:]
+
+"
+! !
+
+!JavaExamples::CDDatabaseHandler class methodsFor:'initialization'!
+
+initialize
+
+    self lookupObject: JavaLookup instance
+
+    "Created: / 13-05-2012 / 17:24:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!JavaExamples::CDDatabaseHandler methodsFor:'SAX2 interface'!
+
+characters: string offset: off length: len
+
+    tag = 'title'  ifTrue:[
+        title := string copyFrom: off + 1 to: off + len.
+        tag := nil.
+    ].
+    tag = 'artist' ifTrue:[
+        artist := string copyFrom: off + 1 to: off + len.
+        tag := nil.
+    ].
+
+    "Created: / 13-05-2012 / 17:27:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+endDocument
+
+    "Created: / 16-04-2005 / 12:28:31 / janfrog"
+!
+
+endDocumentFragment
+
+    "Created: / 10-08-2007 / 09:22:12 / janfrog"
+!
+
+endElement:namespace localName:localName qName:qName 
+
+
+    qName = 'cd' ifTrue:[
+        index := index + 1.
+        UserNotification notify: 
+            (index printString , '. ', title , ' - ' , artist)
+    ]
+
+    "Created: / 13-05-2012 / 17:38:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+endPrefixMapping:prefix
+
+    "Created: / 16-04-2005 / 12:29:45 / janfrog"
+!
+
+ignorableWhitespace:aString
+
+    "Created: / 16-04-2005 / 12:30:04 / janfrog"
+!
+
+processingInstruction:target data:data
+
+    "Created: / 16-04-2005 / 12:31:04 / janfrog"
+!
+
+setDocumentLocator:aLocator
+
+    "Created: / 16-04-2005 / 12:30:37 / janfrog"
+    "Modified: / 13-05-2012 / 17:26:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+skippedEnrity:aString
+
+    "Created: / 16-04-2005 / 12:31:18 / janfrog"
+!
+
+startDocument
+
+    index := 0
+
+    "Created: / 16-04-2005 / 12:31:25 / janfrog"
+    "Modified: / 13-05-2012 / 17:30:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+startDocumentFragment
+
+    "Created: / 10-08-2007 / 09:22:07 / janfrog"
+!
+
+startElement:namespace localName:localName qName:qName attributes:attributes
+
+    tag := qName.
+
+    "Created: / 13-05-2012 / 17:37:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+startPrefix:prefix mappingTo:uri
+
+    "Created: / 17-04-2005 / 08:47:18 / janfrog"
+! !
+
+!JavaExamples class methodsFor:'documentation'!
+
+version_SVN
+    ^ '$Id::                                                                                                                        $'
+! !
+
+JavaExamples::CDDatabaseHandler initialize!