~ubuntu-branches/ubuntu/vivid/herold/vivid

« back to all changes in this revision

Viewing changes to java/org/dbdoclet/xiphias/XmlValidationResult.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2012-09-20 10:00:14 UTC
  • Revision ID: package-import@ubuntu.com-20120920100014-5pcwbw2err6on8yg
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (C) 2001-2012 Michael Fuchs
 
3
 *
 
4
 * This file is part of herold.
 
5
 * 
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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/>.  
 
18
 */
 
19
package org.dbdoclet.xiphias;
 
20
 
 
21
import java.io.File;
 
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;
 
27
 
 
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;
 
36
 
 
37
/**
 
38
 * Die Klasse <code>XmlValidationResult</code> wird benachrichtigt falls beim
 
39
 * Parsen einer XML-Datei ein Fehler auftritt.
 
40
 * 
 
41
 * Die Fehlermeldungen werden formatiert und auf <code>System.err</code> Ć¼ber
 
42
 * die Konsole ausgegeben.
 
43
 * 
 
44
 * @author <a href="mailto:michael.fuchs@unico-group.com">Michael Fuchs</a>
 
45
 * @version 1.0
 
46
 */
 
47
public class XmlValidationResult implements ErrorHandler, LSResourceResolver {
 
48
 
 
49
    private boolean hasErrors = false;
 
50
    private boolean fileNotFound = false;
 
51
    private boolean canNotRead = false;
 
52
 
 
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;
 
59
 
 
60
    private File xmlFile;
 
61
    private ResourceBundle res;
 
62
    private String systemId;
 
63
 
 
64
    public XmlValidationResult(File xmlFile, Locale locale) {
 
65
 
 
66
        if (locale == null) {
 
67
            throw new IllegalArgumentException("The argument locale must not be null!");
 
68
        }
 
69
 
 
70
        this.xmlFile = xmlFile;
 
71
        res = ResourceBundle.getBundle("org/dbdoclet/xiphias/Resources", locale);
 
72
 
 
73
        errorList = new ArrayList<String>();
 
74
        warningList = new ArrayList<String>();
 
75
        exceptionList = new ArrayList<Throwable>();
 
76
    }
 
77
 
 
78
    public XmlValidationResult(File xmlFile) {
 
79
 
 
80
        this(xmlFile, Locale.getDefault());
 
81
    }
 
82
 
 
83
    public XmlValidationResult(String systemId) {
 
84
 
 
85
        this(null, Locale.getDefault());
 
86
        this.systemId = systemId;
 
87
    }
 
88
 
 
89
    public File getXmlFile() {
 
90
        return xmlFile;
 
91
    }
 
92
 
 
93
    public String getSourceDescription() {
 
94
 
 
95
        if (xmlFile != null) {
 
96
            return xmlFile.getAbsolutePath();
 
97
        } else {
 
98
            
 
99
            if (systemId == null || systemId.length() == 0) {
 
100
                return "Anonymous InputStream";
 
101
            } else {
 
102
                return systemId;
 
103
            }
 
104
        }
 
105
    }
 
106
 
 
107
    public boolean failed() {
 
108
        return hasErrors;
 
109
    }
 
110
 
 
111
    public void setFileNotFound(boolean fileNotFound) {
 
112
 
 
113
        this.fileNotFound = fileNotFound;
 
114
 
 
115
        String msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_ERROR_FILE_NOT_FOUND"),
 
116
                getSourceDescription());
 
117
        errorList.add(msg);
 
118
        hasErrors = true;
 
119
        errorCounter++;
 
120
    }
 
121
 
 
122
    public boolean getFileNotFound() {
 
123
        return fileNotFound;
 
124
    }
 
125
 
 
126
    public void setCanNotRead(boolean canNotRead) {
 
127
 
 
128
        this.canNotRead = canNotRead;
 
129
 
 
130
        String msg = MessageServices.format(res
 
131
                .getString("C_XML_VALIDATION_ERROR_FILE_CAN_NOT_READ"), getSourceDescription());
 
132
        errorList.add(msg);
 
133
        hasErrors = true;
 
134
        errorCounter++;
 
135
    }
 
136
 
 
137
    public boolean getCanNotRead() {
 
138
        return canNotRead;
 
139
    }
 
140
 
 
141
    public int getNumOfErrors() {
 
142
        return errorCounter;
 
143
    }
 
144
 
 
145
    public void setThrowable(Throwable throwable) {
 
146
 
 
147
        if (throwable == null) {
 
148
            throw new IllegalArgumentException("The argument throwable must not be null!");
 
149
        }
 
150
 
 
151
        this.throwable = throwable;
 
152
 
 
153
        String msg = "(" + throwable.getClass().getName() + ") " + throwable.getMessage();
 
154
 
 
155
        if (msg == null || msg.length() == 0) {
 
156
            msg = throwable.getClass().getName();
 
157
        }
 
158
 
 
159
        msg = getSourceDescription() + ": " + msg;
 
160
        errorList.add(msg);
 
161
        hasErrors = true;
 
162
        errorCounter++;
 
163
    }
 
164
 
 
165
    public Throwable getThrowable() {
 
166
        return throwable;
 
167
    }
 
168
 
 
169
    /**
 
170
     * Die Methode <code>fatalError</code> wird bei Auftreten eines fatalen
 
171
     * Fehlers vom Sax-Parser aufgerufen.
 
172
     * 
 
173
     * @param oops
 
174
     *            <code>SAXParseException</code>
 
175
     */
 
176
    public void fatalError(SAXParseException oops) {
 
177
 
 
178
        errorCounter++;
 
179
 
 
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());
 
183
 
 
184
        errorList.add(msg);
 
185
        exceptionList.add(oops);
 
186
 
 
187
        hasErrors = true;
 
188
    }
 
189
 
 
190
    /**
 
191
     * Die Methode <code>error</code> wird bei Auftreten eines Fehlers vom
 
192
     * Sax-Parser aufgerufen.
 
193
     * 
 
194
     * @param oops
 
195
     *            <code>SAXParseException</code>
 
196
     */
 
197
    public void error(SAXParseException oops) {
 
198
 
 
199
        errorCounter++;
 
200
 
 
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());
 
204
 
 
205
        errorList.add(msg);
 
206
        exceptionList.add(oops);
 
207
 
 
208
        hasErrors = true;
 
209
    }
 
210
 
 
211
    /**
 
212
     * Die Methode <code>warning</code> wird bei Auftreten einer Warnung vom
 
213
     * SAX-Parser aufgerufen.
 
214
     * 
 
215
     * @param oops
 
216
     *            <code>SAXParseException</code>
 
217
     */
 
218
    public void warning(SAXParseException oops) {
 
219
 
 
220
        warningCounter++;
 
221
 
 
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());
 
225
 
 
226
        warningList.add(msg);
 
227
        // exceptionList.add(oops);
 
228
    }
 
229
 
 
230
    public String createTextReport() {
 
231
        return createReport(false);
 
232
    }
 
233
 
 
234
    public String createHtmlReport() {
 
235
        return createReport(true);
 
236
    }
 
237
 
 
238
    public String createReport(boolean isHtml) {
 
239
 
 
240
        String msg;
 
241
 
 
242
        StringBuffer buffer = new StringBuffer();
 
243
 
 
244
        buffer.append('\n');
 
245
 
 
246
        if (isHtml) {
 
247
            buffer.append("<p>");
 
248
        }
 
249
        
 
250
        msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_OF_FILE"),
 
251
                getSourceDescription());
 
252
        buffer.append(msg);
 
253
 
 
254
        if (isHtml) {
 
255
            buffer.append("</p>");
 
256
        }
 
257
 
 
258
        Iterator<String> iterator = warningList.iterator();
 
259
 
 
260
        while (iterator.hasNext()) {
 
261
 
 
262
            if (isHtml) {
 
263
                buffer.append("<pre>");
 
264
            }
 
265
 
 
266
            buffer.append('\n');
 
267
 
 
268
            msg = (String) iterator.next();
 
269
            buffer.append(msg);
 
270
 
 
271
            if (isHtml) {
 
272
                buffer.append("</pre>");
 
273
            }
 
274
 
 
275
            buffer.append('\n');
 
276
        }
 
277
 
 
278
        buffer.append('\n');
 
279
 
 
280
        if (failed() == false) {
 
281
 
 
282
            if (isHtml) {
 
283
                buffer.append("<p><b>");
 
284
                buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_SUCCESSFUL"));
 
285
                buffer.append("</b></p>");
 
286
            } else {
 
287
                buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_SUCCESSFUL"));
 
288
            }
 
289
 
 
290
            buffer.append('\n');
 
291
 
 
292
        } else {
 
293
 
 
294
            iterator = errorList.iterator();
 
295
 
 
296
            while (iterator.hasNext()) {
 
297
 
 
298
                if (isHtml) {
 
299
                    buffer.append("<pre>");
 
300
                }
 
301
 
 
302
                buffer.append('\n');
 
303
 
 
304
                msg = (String) iterator.next();
 
305
                buffer.append(msg);
 
306
 
 
307
                if (isHtml) {
 
308
                    buffer.append("</pre>");
 
309
                }
 
310
 
 
311
                buffer.append('\n');
 
312
            }
 
313
 
 
314
            buffer.append('\n');
 
315
            msg = MessageServices.format(ResourceServices.getString(res,"C_XML_VALIDATION_NUMBER_OF_ERRORS"), String
 
316
                    .valueOf(errorCounter));
 
317
            buffer.append(msg);
 
318
            buffer.append('\n');
 
319
 
 
320
            if (isHtml) {
 
321
                buffer.append("<p><b>");
 
322
                buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_FAILED"));
 
323
                buffer.append("</b></p>");
 
324
            } else {
 
325
                buffer.append(ResourceServices.getString(res,"C_XML_VALIDATION_FAILED"));
 
326
            }
 
327
 
 
328
            buffer.append('\n');
 
329
        }
 
330
 
 
331
        return buffer.toString();
 
332
    }
 
333
 
 
334
    public LSInput resolveResource(String type, String namespaceURI, String publicId,
 
335
            String systemId, String baseURI) {
 
336
 
 
337
//      System.out.println("XMLValidationResult: Resolving resource [type=" + type
 
338
//              + ", namespaceURI=" + namespaceURI + ", publicId=" + publicId + ", systemId="
 
339
//              + systemId + ", baseURI=" + baseURI);
 
340
 
 
341
        if (systemId != null && systemId.length() > 0) {
 
342
 
 
343
            if (systemId.startsWith("http://")) {
 
344
                
 
345
                try {
 
346
 
 
347
                    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 
348
                    DOMImplementationLS domImpl = (DOMImplementationLS) registry.getDOMImplementation("LS");
 
349
                    LSInput input = domImpl.createLSInput();
 
350
                    input.setCharacterStream(new StringReader(""));
 
351
                    return input;
 
352
                    
 
353
                } catch (Exception oops) {
 
354
                    oops.printStackTrace();
 
355
                }
 
356
                
 
357
            }
 
358
        }
 
359
        
 
360
        return null;
 
361
    }
 
362
}