src/examples/JavaExamples.st
branchjk_new_structure
changeset 1573 38d044a80b88
child 1628 5db2854824b5
equal deleted inserted replaced
1572:e966fdad0a23 1573:38d044a80b88
       
     1 "
       
     2  COPYRIGHT (c) 1996-2011 by Claus Gittinger
       
     3 
       
     4  New code and modifications done at SWING Research Group [1]:
       
     5 
       
     6  COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
       
     7                             SWING Research Group, Czech Technical University in Prague
       
     8 
       
     9  This software is furnished under a license and may be used
       
    10  only in accordance with the terms of that license and with the
       
    11  inclusion of the above copyright notice.   This software may not
       
    12  be provided or otherwise made available to, or used by, any
       
    13  other person.  No title to or ownership of the software is
       
    14  hereby transferred.
       
    15 
       
    16  [1] Code written at SWING Research Group contains a signature
       
    17      of one of the above copright owners. For exact set of such code,
       
    18      see the differences between this version and version stx:libjava
       
    19      as of 1.9.2010
       
    20 "
       
    21 "{ Package: 'stx:libjava/examples' }"
       
    22 
       
    23 Object subclass:#JavaExamples
       
    24 	instanceVariableNames:''
       
    25 	classVariableNames:''
       
    26 	poolDictionaries:''
       
    27 	category:'Languages-Java-Utilities'
       
    28 !
       
    29 
       
    30 Object subclass:#CDDatabaseHandler
       
    31 	instanceVariableNames:'index title artist tag'
       
    32 	classVariableNames:''
       
    33 	poolDictionaries:''
       
    34 	privateIn:JavaExamples
       
    35 !
       
    36 
       
    37 !JavaExamples class methodsFor:'documentation'!
       
    38 
       
    39 copyright
       
    40 "
       
    41  COPYRIGHT (c) 1996-2011 by Claus Gittinger
       
    42 
       
    43  New code and modifications done at SWING Research Group [1]:
       
    44 
       
    45  COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
       
    46                             SWING Research Group, Czech Technical University in Prague
       
    47 
       
    48  This software is furnished under a license and may be used
       
    49  only in accordance with the terms of that license and with the
       
    50  inclusion of the above copyright notice.   This software may not
       
    51  be provided or otherwise made available to, or used by, any
       
    52  other person.  No title to or ownership of the software is
       
    53  hereby transferred.
       
    54 
       
    55  [1] Code written at SWING Research Group contains a signature
       
    56      of one of the above copright owners. For exact set of such code,
       
    57      see the differences between this version and version stx:libjava
       
    58      as of 1.9.2010
       
    59 
       
    60 "
       
    61 ! !
       
    62 
       
    63 !JavaExamples class methodsFor:'examples'!
       
    64 
       
    65 example_1
       
    66     "A CD .xml parsing example. The parser raises a UserNotification
       
    67      whenever it parses a CD, it raises a UserNotification. The notification
       
    68      is then handled and printed. Demonstrates:
       
    69       - proxy creation
       
    70       - exception handling
       
    71 
       
    72     cd.xml may look like:
       
    73 
       
    74     <?xml version='1.0' encoding='ISO-8859-1'?>
       
    75     <catalog>
       
    76         <cd>
       
    77             <title>Empire Burlesque</title>
       
    78             <artist>Bob Dylan</artist>
       
    79             <country>USA</country>
       
    80             <company>Columbia</company>
       
    81             <price>10.90</price>
       
    82             <year>1985</year>
       
    83         </cd>
       
    84     </catalog>
       
    85 
       
    86 
       
    87 
       
    88     "
       
    89 
       
    90     | factory parser |
       
    91 
       
    92     factory := JAVA javax xml parsers SAXParserFactory newInstance.
       
    93     parser := factory newSAXParser getXMLReader.
       
    94     parser setContentHandler: JavaExamples::CDDatabaseHandler new.
       
    95     [
       
    96         parser parse: '/home/jv/Projects/libjava/demo/saxon/cd.xml'.
       
    97     ] on: JAVA java io IOException do:[:ioe|
       
    98         Transcript showCR: 'I/O error: ', ioe getMessage.
       
    99         ioe printStackTrace
       
   100     ] on: UserNotification do:[:un|
       
   101         Transcript showCR: un messageText.
       
   102         un proceed.
       
   103     ]
       
   104 
       
   105     "Created: / 13-05-2012 / 18:27:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   106 ! !
       
   107 
       
   108 !JavaExamples::CDDatabaseHandler class methodsFor:'documentation'!
       
   109 
       
   110 documentation
       
   111 "
       
   112     A simple XML CD database parser - a demonstration
       
   113     of stx:libjava capabilities. See JavaExamples>>example_1
       
   114 
       
   115     [author:]
       
   116         Jan Vrany <jan.vrany@fit.cvut.cz>
       
   117 
       
   118     [instance variables:]
       
   119 
       
   120     [class variables:]
       
   121 
       
   122     [see also:]
       
   123 
       
   124 "
       
   125 ! !
       
   126 
       
   127 !JavaExamples::CDDatabaseHandler class methodsFor:'initialization'!
       
   128 
       
   129 initialize
       
   130 
       
   131     self lookupObject: JavaLookup instance
       
   132 
       
   133     "Created: / 13-05-2012 / 17:24:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   134 ! !
       
   135 
       
   136 !JavaExamples::CDDatabaseHandler methodsFor:'SAX2 interface'!
       
   137 
       
   138 characters: string offset: off length: len
       
   139 
       
   140     tag = 'title'  ifTrue:[
       
   141         title := string copyFrom: off + 1 to: off + len.
       
   142         tag := nil.
       
   143     ].
       
   144     tag = 'artist' ifTrue:[
       
   145         artist := string copyFrom: off + 1 to: off + len.
       
   146         tag := nil.
       
   147     ].
       
   148 
       
   149     "Created: / 13-05-2012 / 17:27:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   150 !
       
   151 
       
   152 endDocument
       
   153 
       
   154     "Created: / 16-04-2005 / 12:28:31 / janfrog"
       
   155 !
       
   156 
       
   157 endDocumentFragment
       
   158 
       
   159     "Created: / 10-08-2007 / 09:22:12 / janfrog"
       
   160 !
       
   161 
       
   162 endElement:namespace localName:localName qName:qName 
       
   163 
       
   164 
       
   165     qName = 'cd' ifTrue:[
       
   166         index := index + 1.
       
   167         UserNotification notify: 
       
   168             (index printString , '. ', title , ' - ' , artist)
       
   169     ]
       
   170 
       
   171     "Created: / 13-05-2012 / 17:38:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   172 !
       
   173 
       
   174 endPrefixMapping:prefix
       
   175 
       
   176     "Created: / 16-04-2005 / 12:29:45 / janfrog"
       
   177 !
       
   178 
       
   179 ignorableWhitespace:aString
       
   180 
       
   181     "Created: / 16-04-2005 / 12:30:04 / janfrog"
       
   182 !
       
   183 
       
   184 processingInstruction:target data:data
       
   185 
       
   186     "Created: / 16-04-2005 / 12:31:04 / janfrog"
       
   187 !
       
   188 
       
   189 setDocumentLocator:aLocator
       
   190 
       
   191     "Created: / 16-04-2005 / 12:30:37 / janfrog"
       
   192     "Modified: / 13-05-2012 / 17:26:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   193 !
       
   194 
       
   195 skippedEnrity:aString
       
   196 
       
   197     "Created: / 16-04-2005 / 12:31:18 / janfrog"
       
   198 !
       
   199 
       
   200 startDocument
       
   201 
       
   202     index := 0
       
   203 
       
   204     "Created: / 16-04-2005 / 12:31:25 / janfrog"
       
   205     "Modified: / 13-05-2012 / 17:30:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   206 !
       
   207 
       
   208 startDocumentFragment
       
   209 
       
   210     "Created: / 10-08-2007 / 09:22:07 / janfrog"
       
   211 !
       
   212 
       
   213 startElement:namespace localName:localName qName:qName attributes:attributes
       
   214 
       
   215     tag := qName.
       
   216 
       
   217     "Created: / 13-05-2012 / 17:37:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   218 !
       
   219 
       
   220 startPrefix:prefix mappingTo:uri
       
   221 
       
   222     "Created: / 17-04-2005 / 08:47:18 / janfrog"
       
   223 ! !
       
   224 
       
   225 !JavaExamples class methodsFor:'documentation'!
       
   226 
       
   227 version_SVN
       
   228     ^ '$Id::                                                                                                                        $'
       
   229 ! !
       
   230 
       
   231 JavaExamples::CDDatabaseHandler initialize!