|
1 /* |
|
2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 package com.sun.swingset3.demos.table; |
|
25 |
|
26 import java.io.BufferedInputStream; |
|
27 import java.io.IOException; |
|
28 import java.io.InputStream; |
|
29 import java.net.URL; |
|
30 import java.util.logging.Level; |
|
31 |
|
32 import javax.xml.parsers.ParserConfigurationException; |
|
33 import javax.xml.parsers.SAXParser; |
|
34 import javax.xml.parsers.SAXParserFactory; |
|
35 |
|
36 import org.xml.sax.Attributes; |
|
37 import org.xml.sax.SAXException; |
|
38 import org.xml.sax.SAXParseException; |
|
39 import org.xml.sax.helpers.DefaultHandler; |
|
40 |
|
41 public abstract class OscarDataParser extends DefaultHandler { |
|
42 private static final String[] CATEGORIES_IN = { |
|
43 "actor", "actress", "bestPicture", |
|
44 "actorSupporting", "actressSupporting", "artDirection", |
|
45 "assistantDirector", "director", "cinematography", |
|
46 "costumeDesign", "danceDirection", "docFeature", |
|
47 "docShort", "filmEditing", "foreignFilm", |
|
48 "makeup", "musicScore", "musicSong", |
|
49 "screenplayAdapted", "screenplayOriginal", "shortAnimation", |
|
50 "shortLiveAction", "sound", "soundEditing", |
|
51 "specialEffects", "visualEffects", "writing", |
|
52 "engEffects", "uniqueArtisticPicture" |
|
53 }; |
|
54 |
|
55 private static final String[] CATEGORIES_OUT = { |
|
56 "Best Actor", "Best Actress", "Best Picture", |
|
57 "Best Supporting Actor", "Best Supporting Actress", "Best Art Direction", |
|
58 "Best Assistant Director", "Best Director", "Best Cinematography", |
|
59 "Best Costume Design", "Best Dance Direction", "Best Feature Documentary", |
|
60 "Best Short Documentary", "Best Film Editing", "Best Foreign Film", |
|
61 "Best Makeup", "Best Musical Score", "Best Song", |
|
62 "Best Adapted Screenplay", "Best Original Screenplay", "Best Animation Short", |
|
63 "Best Live Action Short", "Best Sound", "Best Sound Editing", |
|
64 "Best Special Effects", "Best Visual Effects", "Best Engineering Effects", |
|
65 "Best Writing", "Most Unique Artistic Picture" |
|
66 }; |
|
67 |
|
68 |
|
69 private String tempVal; |
|
70 |
|
71 //to maintain context |
|
72 private OscarCandidate tempOscarCandidate; |
|
73 |
|
74 private int count = 0; |
|
75 |
|
76 public int getCount() { |
|
77 return count; |
|
78 } |
|
79 |
|
80 public void parseDocument(URL oscarURL) { |
|
81 //get a factory |
|
82 SAXParserFactory spf = SAXParserFactory.newInstance(); |
|
83 |
|
84 try { |
|
85 //get a new instance of parser |
|
86 SAXParser sp = spf.newSAXParser(); |
|
87 |
|
88 //parse the file and also register this class for call backs |
|
89 InputStream is = new BufferedInputStream(oscarURL.openStream()); |
|
90 sp.parse(is, this); |
|
91 System.out.println("done parsing count="+count); |
|
92 is.close(); |
|
93 |
|
94 } catch (SAXException se) { |
|
95 se.printStackTrace(); |
|
96 } catch (ParserConfigurationException pce) { |
|
97 pce.printStackTrace(); |
|
98 } catch (IOException ie) { |
|
99 ie.printStackTrace(); |
|
100 } |
|
101 } |
|
102 |
|
103 //Event Handlers |
|
104 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { |
|
105 //reset |
|
106 tempVal = ""; |
|
107 for (int i = 0; i < CATEGORIES_IN.length; i++) { |
|
108 if (qName.equalsIgnoreCase(CATEGORIES_IN[i])) { |
|
109 tempOscarCandidate = new OscarCandidate(CATEGORIES_OUT[i]); |
|
110 tempOscarCandidate.setYear(Integer.parseInt(attributes.getValue("year"))); |
|
111 if (CATEGORIES_IN[i].equals("screenplayOriginal") && |
|
112 tempOscarCandidate.getYear() == 2007) { |
|
113 } |
|
114 return; |
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 public void characters(char[] ch, int start, int length) throws SAXException { |
|
120 tempVal = new String(ch, start, length); |
|
121 } |
|
122 |
|
123 public void endElement(String uri, String localName, String qName) throws SAXException { |
|
124 if (qName.equalsIgnoreCase("won")) { |
|
125 tempOscarCandidate.setWinner(true); |
|
126 } else if (qName.equalsIgnoreCase("lost")) { |
|
127 tempOscarCandidate.setWinner(false); |
|
128 } else if (qName.equalsIgnoreCase("movie")) { |
|
129 tempOscarCandidate.setMovieTitle(tempVal); |
|
130 } else if (qName.equalsIgnoreCase("person")) { |
|
131 tempOscarCandidate.getPersons().add(tempVal); |
|
132 } else { |
|
133 // find category |
|
134 for (String category : CATEGORIES_IN) { |
|
135 if (qName.equalsIgnoreCase(category)) { |
|
136 //add it to the list |
|
137 count++; |
|
138 addCandidate(tempOscarCandidate); |
|
139 break; |
|
140 } |
|
141 } |
|
142 } |
|
143 } |
|
144 |
|
145 @Override |
|
146 public void error(SAXParseException ex) throws SAXException { |
|
147 TableDemo.logger.log(Level.SEVERE, "error parsing oscar data ", ex); |
|
148 } |
|
149 |
|
150 @Override |
|
151 public void fatalError(SAXParseException ex) throws SAXException { |
|
152 TableDemo.logger.log(Level.SEVERE, "fatal error parsing oscar data ", ex); |
|
153 } |
|
154 |
|
155 @Override |
|
156 public void warning(SAXParseException ex) { |
|
157 TableDemo.logger.log(Level.WARNING, "warning occurred while parsing oscar data ", ex); |
|
158 } |
|
159 |
|
160 @Override |
|
161 public void endDocument() throws SAXException { |
|
162 TableDemo.logger.log(Level.FINER, "parsed to end of oscar data."); |
|
163 } |
|
164 |
|
165 protected abstract void addCandidate(OscarCandidate candidate); |
|
166 } |
|
167 |