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