1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
package net.mtu.eggplant.app; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.io.FileInputStream; |
32 | |
import java.io.FileNotFoundException; |
33 | |
import java.io.FileReader; |
34 | |
import java.io.IOException; |
35 | |
import java.io.Reader; |
36 | |
import java.util.prefs.Preferences; |
37 | |
|
38 | |
import javax.swing.JFileChooser; |
39 | |
import javax.swing.JOptionPane; |
40 | |
import javax.xml.XMLConstants; |
41 | |
import javax.xml.transform.Source; |
42 | |
import javax.xml.transform.stream.StreamSource; |
43 | |
import javax.xml.validation.Schema; |
44 | |
import javax.xml.validation.SchemaFactory; |
45 | |
|
46 | |
import org.w3c.dom.bootstrap.DOMImplementationRegistry; |
47 | |
import org.w3c.dom.ls.DOMImplementationLS; |
48 | |
import org.w3c.dom.ls.LSInput; |
49 | |
import org.w3c.dom.ls.LSResourceResolver; |
50 | |
import org.xml.sax.SAXException; |
51 | |
import org.xml.sax.SAXParseException; |
52 | |
|
53 | |
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
54 | |
import net.mtu.eggplant.util.BasicFileFilter; |
55 | |
import net.mtu.eggplant.util.gui.GraphicsUtils; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | public class SchemaValidator { |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public static void main(final String[] args) { |
66 | 0 | final File initialDirectory = getInitialDirectory(); |
67 | 0 | final JFileChooser chooser = new JFileChooser(initialDirectory); |
68 | 0 | chooser.setDialogTitle("Choose an XML schema to parse"); |
69 | 0 | chooser.setFileFilter(new BasicFileFilter("Schema Files", "xsd")); |
70 | |
while (true) { |
71 | 0 | final int state = chooser.showOpenDialog(null); |
72 | 0 | if (JFileChooser.APPROVE_OPTION == state) { |
73 | 0 | final File file = chooser.getSelectedFile(); |
74 | 0 | setInitialDirectory(file); |
75 | |
try { |
76 | 0 | parseSchema(file); |
77 | 0 | JOptionPane.showMessageDialog(null, "Parse Successful", "Error", JOptionPane.INFORMATION_MESSAGE); |
78 | 0 | } catch (final IOException e) { |
79 | 0 | GraphicsUtils.error(e.getMessage()); |
80 | 0 | } catch (final SAXParseException spe) { |
81 | 0 | GraphicsUtils.error("Error parsing schema " |
82 | 0 | + " line: " + spe.getLineNumber() + " column: " + spe.getColumnNumber() + " " + spe.getMessage()); |
83 | 0 | } catch (final SAXException e) { |
84 | 0 | GraphicsUtils.error(e.getMessage()); |
85 | 0 | } catch (final ClassCastException e) { |
86 | 0 | GraphicsUtils.error(e.getMessage()); |
87 | 0 | } catch (final ClassNotFoundException e) { |
88 | 0 | GraphicsUtils.error(e.getMessage()); |
89 | 0 | } catch (final InstantiationException e) { |
90 | 0 | GraphicsUtils.error(e.getMessage()); |
91 | 0 | } catch (final IllegalAccessException e) { |
92 | 0 | GraphicsUtils.error(e.getMessage()); |
93 | 0 | } |
94 | 0 | } else { |
95 | 0 | return; |
96 | |
} |
97 | 0 | } |
98 | |
} |
99 | |
|
100 | |
public static Schema parseSchema(final File xsdFile) throws SAXException, ClassCastException, ClassNotFoundException, |
101 | |
InstantiationException, IllegalAccessException, IOException { |
102 | 0 | FileInputStream stream = null; |
103 | |
try { |
104 | 0 | stream = new FileInputStream(xsdFile); |
105 | |
|
106 | |
|
107 | 0 | final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); |
108 | 0 | final DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); |
109 | |
|
110 | 0 | final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); |
111 | |
|
112 | 0 | factory.setResourceResolver(new LSResourceResolver() { |
113 | |
@SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION" }, justification = "Input must be cleaned up by caller") |
114 | |
public LSInput resolveResource(final String type, |
115 | |
final String namespaceURI, |
116 | |
final String publicId, |
117 | |
final String systemId, |
118 | |
final String baseURI) { |
119 | |
|
120 | 0 | if (null == systemId) { |
121 | 0 | return null; |
122 | |
} |
123 | 0 | final LSInput input = domImplementationLS.createLSInput(); |
124 | 0 | input.setBaseURI(baseURI); |
125 | 0 | input.setPublicId(publicId); |
126 | 0 | input.setSystemId(systemId); |
127 | |
|
128 | |
try { |
129 | |
final Reader inputStream; |
130 | 0 | if (systemId.startsWith("/")) { |
131 | 0 | inputStream = new FileReader(systemId); |
132 | |
} else { |
133 | 0 | final File resource = new File(xsdFile.getParent(), systemId); |
134 | 0 | inputStream = new FileReader(resource); |
135 | |
} |
136 | 0 | input.setCharacterStream(inputStream); |
137 | |
|
138 | 0 | return input; |
139 | 0 | } catch (final FileNotFoundException e) { |
140 | 0 | throw new RuntimeException(e); |
141 | |
} |
142 | |
} |
143 | |
}); |
144 | |
|
145 | 0 | final Source schemaFile = new StreamSource(xsdFile); |
146 | 0 | final Schema schema = factory.newSchema(schemaFile); |
147 | 0 | return schema; |
148 | |
} finally { |
149 | 0 | if (null != stream) { |
150 | 0 | stream.close(); |
151 | |
} |
152 | 0 | } |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
private static void setInitialDirectory(final File dir) { |
162 | |
|
163 | |
final File directory; |
164 | 0 | if (dir.isDirectory()) { |
165 | 0 | directory = dir; |
166 | |
} else { |
167 | 0 | directory = dir.getParentFile(); |
168 | |
} |
169 | |
|
170 | 0 | final Preferences preferences = Preferences.userNodeForPackage(SchemaValidator.class); |
171 | 0 | final String previousPath = preferences.get(INITIAL_DIRECTORY_PREFERENCE_KEY, null); |
172 | |
|
173 | 0 | if (!directory.toString().equals(previousPath)) { |
174 | 0 | preferences.put(INITIAL_DIRECTORY_PREFERENCE_KEY, directory.toString()); |
175 | |
} |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
private static File getInitialDirectory() { |
185 | 0 | final Preferences preferences = Preferences.userNodeForPackage(SchemaValidator.class); |
186 | 0 | final String path = preferences.get(INITIAL_DIRECTORY_PREFERENCE_KEY, null); |
187 | |
|
188 | 0 | File dir = null; |
189 | 0 | if (null != path) { |
190 | 0 | dir = new File(path); |
191 | |
} |
192 | 0 | return dir; |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
private static final String INITIAL_DIRECTORY_PREFERENCE_KEY = "InitialDirectory"; |
199 | |
|
200 | |
} |