KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TestSongsDTD


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 // Generated classes
27
import samples.dtd.Artist;
28 import samples.dtd.Song;
29 import samples.dtd.Songs;
30 import samples.dtd.SongsUnmarshaller;
31
32 public class TestSongsDTD {
33
34     public static void main(String JavaDoc[] args) {
35         if (args.length != 1) {
36             System.out.println("Usage: java samples.TestSongsDTD " +
37                 "[songs.xml location]");
38             return;
39         }
40
41         // Turn on validation
42
System.setProperty("org.enhydra.zeus.validation", "true");
43
44         // Set the XML parser to use
45
String JavaDoc xmlParser = System.getProperty("org.xml.sax.driver");
46         if ((xmlParser == null) || (xmlParser.equals(""))) {
47             System.setProperty("org.xml.sax.driver",
48                                "org.apache.xerces.parsers.SAXParser");
49         }
50
51         try {
52             // New way of unmarshalling
53
Songs songs = SongsUnmarshaller.unmarshal(new File JavaDoc(args[0]), false);
54
55             List JavaDoc songList = songs.getSongList();
56             for (Iterator JavaDoc i = songList.iterator(); i.hasNext(); ) {
57                 Song song = (Song)i.next();
58                 System.out.println("Song title: '" +
59                     song.getTitle() + "'");
60                 List JavaDoc artists = song.getArtistList();
61                 for (Iterator JavaDoc j = artists.iterator(); j.hasNext(); ) {
62                     Artist artist = (Artist)j.next();
63                     System.out.println("Artist: '" + artist.getValue() + "'");
64                     System.out.println(" Type: '" + artist.getType() + "'");
65                 }
66                 System.out.println("-------------------------");
67             }
68             
69             for (Iterator JavaDoc i = songList.iterator(); i.hasNext(); ) {
70                 Song song = (Song)i.next();
71                 
72                 // Rename songs named 'The Finishing Touch' to 'Call It A Day'
73
if (song.getTitle().equals("The Finishing Touch")) {
74                     song.setTitle("Call It A Day");
75                 }
76             }
77
78             // New way of marshalling
79
songs.marshal(new File JavaDoc("output.xml"));
80         } catch (Exception JavaDoc e) {
81             e.printStackTrace();
82         }
83     }
84 }
85
Popular Tags