1/*
2 * Copyright (c) 2003, 2015, 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 */
23package test.auctionportal;
24
25import org.xml.sax.SAXParseException;
26import org.xml.sax.helpers.DefaultHandler;
27
28/**
29 * ErrorHandler for error handling. Set state if any method in error, warning
30 * or fatalError was called.
31 */
32public final class MyErrorHandler extends DefaultHandler {
33    /**
34     * Enumeration for ErrorHandler's state.
35     */
36    private enum STATE { ERROR, FATAL, WARNING, NORMAL};
37
38    /**
39     * Set state as normal by default.
40     */
41    private volatile STATE state = STATE.NORMAL;
42
43    /**
44     * Keep exception for further investigation.
45     */
46    private volatile SAXParseException exception;
47
48    /**
49     * Save exception and set state to ERROR.
50     * @param e exception wrap error.
51     */
52    @Override
53    public void error (SAXParseException e) {
54        state = STATE.ERROR;
55        exception = e;
56    }
57
58    /**
59     * Save exception and set state to FATAL.
60     * @param e exception wrap error.
61     */
62    @Override
63    public void fatalError (SAXParseException e) {
64        state = STATE.FATAL;
65        exception = e;
66    }
67
68    /**
69     * Save exception and set state to WARNING.
70     * @param e exception wrap error.
71     */
72    @Override
73    public void warning (SAXParseException e) {
74        state = STATE.WARNING;
75        exception = e;
76    }
77
78    /**
79     * return ErrorHandle's state .
80     * @return true No error, fatalError and warning.
81     *         false there is any error, fatalError or warning in processing.
82     */
83    public boolean isAnyError() {
84        if (state != STATE.NORMAL)
85            System.out.println(exception);
86        return state != STATE.NORMAL;
87    }
88
89    /**
90     * return whether fatalError is the only error.
91     * @return true fatalError is the only error.
92     *         false there is no error, or other error besides fatalError.
93     */
94    public boolean isFatalError() {
95        if (state == STATE.FATAL)
96            System.out.println(exception);
97        return state == STATE.FATAL;
98    }
99
100}
101