2
* Copyright (C) 2001-2012 Michael Fuchs
4
* This file is part of herold.
6
* herold is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* herold is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with herold. If not, see <http://www.gnu.org/licenses/>.
19
package org.dbdoclet.xiphias;
22
import java.io.StringReader;
23
import java.util.ArrayList;
24
import java.util.Iterator;
25
import java.util.Locale;
26
import java.util.ResourceBundle;
28
import org.dbdoclet.service.MessageServices;
29
import org.dbdoclet.service.ResourceServices;
30
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
31
import org.w3c.dom.ls.DOMImplementationLS;
32
import org.w3c.dom.ls.LSInput;
33
import org.w3c.dom.ls.LSResourceResolver;
34
import org.xml.sax.ErrorHandler;
35
import org.xml.sax.SAXParseException;
38
* Die Klasse <code>XmlValidationResult</code> wird benachrichtigt falls beim
39
* Parsen einer XML-Datei ein Fehler auftritt.
41
* Die Fehlermeldungen werden formatiert und auf <code>System.err</code> Ć¼ber
42
* die Konsole ausgegeben.
44
* @author <a href="mailto:michael.fuchs@unico-group.com">Michael Fuchs</a>
47
public class XmlValidationResult implements ErrorHandler, LSResourceResolver {
49
private boolean hasErrors = false;
50
private boolean fileNotFound = false;
51
private boolean canNotRead = false;
53
private int errorCounter = 0;
54
private int warningCounter = 0;
55
private ArrayList<String> errorList;
56
private ArrayList<String> warningList;
57
private ArrayList<Throwable> exceptionList;
58
private Throwable throwable;
61
private ResourceBundle res;
62
private String systemId;
64
public XmlValidationResult(File xmlFile, Locale locale) {
67
throw new IllegalArgumentException("The argument locale must not be null!");
70
this.xmlFile = xmlFile;
71
res = ResourceBundle.getBundle("org/dbdoclet/xiphias/Resources", locale);
73
errorList = new ArrayList<String>();
74
warningList = new ArrayList<String>();
75
exceptionList = new ArrayList<Throwable>();
78
public XmlValidationResult(File xmlFile) {
80
this(xmlFile, Locale.getDefault());
83
public XmlValidationResult(String systemId) {
85
this(null, Locale.getDefault());
86
this.systemId = systemId;
89
public File getXmlFile() {
93
public String getSourceDescription() {
95
if (xmlFile != null) {
96
return xmlFile.getAbsolutePath();
99
if (systemId == null || systemId.length() == 0) {
100
return "Anonymous InputStream";
107
public boolean failed() {
111
public void setFileNotFound(boolean fileNotFound) {
113
this.fileNotFound = fileNotFound;
115
String msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_ERROR_FILE_NOT_FOUND"),
116
getSourceDescription());
122
public boolean getFileNotFound() {
126
public void setCanNotRead(boolean canNotRead) {
128
this.canNotRead = canNotRead;
130
String msg = MessageServices.format(res
131
.getString("C_XML_VALIDATION_ERROR_FILE_CAN_NOT_READ"), getSourceDescription());
137
public boolean getCanNotRead() {
141
public int getNumOfErrors() {
145
public void setThrowable(Throwable throwable) {
147
if (throwable == null) {
148
throw new IllegalArgumentException("The argument throwable must not be null!");
151
this.throwable = throwable;
153
String msg = "(" + throwable.getClass().getName() + ") " + throwable.getMessage();
155
if (msg == null || msg.length() == 0) {
156
msg = throwable.getClass().getName();
159
msg = getSourceDescription() + ": " + msg;
165
public Throwable getThrowable() {
170
* Die Methode <code>fatalError</code> wird bei Auftreten eines fatalen
171
* Fehlers vom Sax-Parser aufgerufen.
174
* <code>SAXParseException</code>
176
public void fatalError(SAXParseException oops) {
180
String msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_FATAL"),
181
getSourceDescription(), String.valueOf(oops.getLineNumber()), String.valueOf(oops
182
.getColumnNumber()), oops.getMessage());
185
exceptionList.add(oops);
191
* Die Methode <code>error</code> wird bei Auftreten eines Fehlers vom
192
* Sax-Parser aufgerufen.
195
* <code>SAXParseException</code>
197
public void error(SAXParseException oops) {
201
String msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_ERROR"),
202
getSourceDescription(), String.valueOf(oops.getLineNumber()), String.valueOf(oops
203
.getColumnNumber()), oops.getMessage());
206
exceptionList.add(oops);
212
* Die Methode <code>warning</code> wird bei Auftreten einer Warnung vom
213
* SAX-Parser aufgerufen.
216
* <code>SAXParseException</code>
218
public void warning(SAXParseException oops) {
222
String msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_WARNING"),
223
getSourceDescription(), String.valueOf(oops.getLineNumber()), String.valueOf(oops
224
.getColumnNumber()), oops.getMessage());
226
warningList.add(msg);
227
// exceptionList.add(oops);
230
public String createTextReport() {
231
return createReport(false);
234
public String createHtmlReport() {
235
return createReport(true);
238
public String createReport(boolean isHtml) {
242
StringBuffer buffer = new StringBuffer();
247
buffer.append("<p>");
250
msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_OF_FILE"),
251
getSourceDescription());
255
buffer.append("</p>");
258
Iterator<String> iterator = warningList.iterator();
260
while (iterator.hasNext()) {
263
buffer.append("<pre>");
268
msg = (String) iterator.next();
272
buffer.append("</pre>");
280
if (failed() == false) {
283
buffer.append("<p><b>");
284
buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_SUCCESSFUL"));
285
buffer.append("</b></p>");
287
buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_SUCCESSFUL"));
294
iterator = errorList.iterator();
296
while (iterator.hasNext()) {
299
buffer.append("<pre>");
304
msg = (String) iterator.next();
308
buffer.append("</pre>");
315
msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_NUMBER_OF_ERRORS"), String
316
.valueOf(errorCounter));
321
buffer.append("<p><b>");
322
buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_FAILED"));
323
buffer.append("</b></p>");
325
buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_FAILED"));
331
return buffer.toString();
334
public LSInput resolveResource(String type, String namespaceURI, String publicId,
335
String systemId, String baseURI) {
337
// System.out.println("XMLValidationResult: Resolving resource [type=" + type
338
// + ", namespaceURI=" + namespaceURI + ", publicId=" + publicId + ", systemId="
339
// + systemId + ", baseURI=" + baseURI);
341
if (systemId != null && systemId.length() > 0) {
343
if (systemId.startsWith("http://")) {
347
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
348
DOMImplementationLS domImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
349
LSInput input = domImpl.createLSInput();
350
input.setCharacterStream(new StringReader(""));
353
} catch (Exception oops) {
354
oops.printStackTrace();