/Users/lyon/j4p/src/xml/musicCatalog/MusicCatalog.java

1    /** 
2     * MusicCatalog.java 
3     * @author Thomas Rowland 
4     * @version 02-08-03 
5     */ 
6    package xml.musicCatalog; 
7     
8    import futils.Futil; 
9     
10   import java.io.File; 
11    
12   /* 
13    * This class acts as a driver for demonstrating different types of 
14    * parsing XML files, based on the MusicCatalog examples. 
15    * 
16    * The following files are used with these examples: 
17    * MusicCatalog.xml  - an XML file. 
18    * MusicCatalog1.xml - same XML file but containing a reference to a DTD. 
19    * MusicCatalog2.xml - same XML file but containing a reference to an XMLSchema. 
20    * MusicCatalog.dtd  - an XML DTD containing validation constraints. 
21    * MusicCatalog.xsd  - an XMLSchema validation constraints. 
22    * MusicCatalog.xsl  - a Stylesheet containing definitions for transformation to HTML. 
23    * 
24    */ 
25    
26   public class MusicCatalog { 
27    
28       public static void main(String argv []) { 
29           MusicCatalog music = new MusicCatalog(); 
30    
31           //Test (1) -- parse an xml file using the Xerces parser api directly 
32           music.parseTest(); 
33    
34           //Test (2) -- parse an xml file using JAXP 
35           music.parseTest2(); 
36    
37           //Test (3) -- parse an xml file with DTD validation using JAXP 
38           music.dtdTest(); 
39    
40           //Test (4) -- parse an xml file with XMLSchema validation using JAXP 
41           music.xsdTest(); 
42    
43           //Test (5) -- add a new entry to the MusicCatalog and update the XML file 
44           music.modTest(); 
45    
46           //Test (6) -- add a new entry to the MusicCatalog and update the XML file 
47           music.htmlTest(); 
48    
49           System.out.println("\nEnd."); 
50       } 
51    
52    
53       /* 
54        * Shows how to parse an XML file using the Xerces api. 
55        * No Validation. Does not use JAXP. 
56        */ 
57       public void parseTest() { 
58           try { 
59               System.out.println("\nParsing XML file using Xerces implementation api, No Validation..."); 
60               String file = selectFile("Select MusicCatalog.xml for No Validation."); 
61               System.out.println("Selected:\n" + file); 
62               MyDomParser.parse(file, System.out); 
63           } catch (Exception e) { 
64               System.out.println(e.getMessage()); 
65           } 
66       } 
67    
68       /* 
69        * Shows how to parse an XML file using JAXP. No Validation. 
70        */ 
71       private MyJaxpDomParser parseTest2() { 
72           MyJaxpDomParser dp = new MyJaxpDomParser(); 
73           ; 
74           try { 
75               System.out.println("\nParsing XML file using JAXP api, No Validation..."); 
76               String file = selectFile("Select MusicCatalog.xml for No Validation."); 
77               System.out.println("Selected:\n" + file); 
78               dp.parse(file, System.out); 
79           } catch (Exception e) { 
80               System.out.println(e.getMessage()); 
81           } 
82           return dp; 
83    
84       } 
85    
86       /* 
87        * Shows how to parse an XML file with DTD validation, using JAXP. 
88        */ 
89       private MyJaxpDomParser dtdTest() { 
90           MyJaxpDomParser dp = new MyJaxpDomParser(); 
91           ; 
92           try { 
93               System.out.println("\nParsing XML file with DTD validation, using JAXP..."); 
94               String file = selectFile("Select MusicCatalog1.xml for DTD Validation."); 
95               System.out.println("Selected:\n" + file); 
96               dp.dtdParse(file, System.out); 
97           } catch (Exception e) { 
98               System.out.println(e.getMessage()); 
99           } 
100          return dp; 
101      } 
102   
103      /* 
104       * Shows how to parse an XML file with XMLSchema validation, using JAXP. 
105       */ 
106      private MyJaxpDomParser xsdTest() { 
107          MyJaxpDomParser dp = new MyJaxpDomParser(); 
108          ; 
109          try { 
110              System.out.println("\nParsing XML file with XSD validation, using JAXP..."); 
111              String file = selectFile("Select MusicCatalog2.xml for XSD Validation."); 
112              System.out.println("Selected:\n" + file); 
113              dp.xsdParse(file, System.out); 
114          } catch (Exception e) { 
115              System.out.println(e.getMessage()); 
116          } 
117          return dp; 
118      } 
119   
120      /* 
121       * Shows how to modify content of an XML file by adding a new 
122       * <Item> entry to the MusicCatalog, and updating the XML file 
123       * using XSLT transforms. 
124       */ 
125      private void modTest() { 
126          try { 
127              MyJaxpDomParser dp; 
128              // you can use any of these for testing... 
129              //dp = parseTest2(); 
130              //dp = dtdTest(); 
131              dp = xsdTest(); 
132   
133              Item item = dp.createItem(); 
134              item.setMedia("cd"); 
135              item.setArtist("Aaron Copeland"); 
136              item.setTitle("Fanfare"); 
137              item.setYear("1970"); 
138              item.setMember("Aaron Copeland"); 
139              item.setMember("the rest of the band"); 
140              dp.addItem(item); 
141   
142              // write the xml back out to the file 
143              dp.toXml(); 
144          } catch (Exception e) { 
145              System.out.println(e.getMessage()); 
146          } 
147      } 
148   
149      /** 
150       * Shows how to transform an XML file into HTML 
151       * using XSLT transforms. 
152       */ 
153      private void htmlTest() { 
154          try { 
155              MyJaxpDomParser dp; 
156              // you can use any of these for testing... 
157              dp = parseTest2(); 
158              //dp = dtdTest(); 
159              //dp = xsdTest(); 
160   
161              File stylesheet = new File(selectFile( 
162                      "Select a stylesheet.")); 
163   
164              dp.toHtml(stylesheet); 
165          } catch (Exception e) { 
166              System.out.println(e.getMessage()); 
167          } 
168      } 
169   
170      /** 
171       * Utility function - Prompts for an XML file to parse 
172       */ 
173      private String selectFile(String prompt) { 
174          return Futil.getReadFile(prompt).getAbsolutePath(); 
175      } 
176   
177  }//