1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
2 | * |
3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
4 | * |
5 | * "Hilbert II" is free software; you can redistribute |
6 | * it and/or modify it under the terms of the GNU General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | */ |
15 | |
16 | package org.qedeq.kernel.xml.common; |
17 | |
18 | import java.io.IOException; |
19 | |
20 | import org.qedeq.kernel.se.common.ErrorCodes; |
21 | import org.qedeq.kernel.se.common.QedeqException; |
22 | import org.xml.sax.SAXException; |
23 | |
24 | |
25 | /** |
26 | * Exception that occurs during XML parsing. It specifies an syntactical error. |
27 | * It can also mark a lack of inner consistence of something. |
28 | * Also a programming error can lead to this exception. |
29 | * |
30 | * @author Michael Meyling |
31 | */ |
32 | public final class XmlSyntaxException extends QedeqException implements ErrorCodes { |
33 | |
34 | /** Error code for Exceptions thrown by the SAXParser. */ |
35 | public static final int SAX_PARSER_EXCEPTION = 9001; |
36 | |
37 | /** Error code for unexpected tag. */ |
38 | public static final int UNEXPECTED_TAG_CODE = 9002; |
39 | |
40 | /** Unexpected tag message text. */ |
41 | public static final String UNEXPECTED_TAG_TEXT = "XML structure problem. Unexpected tag: "; |
42 | |
43 | /** Error code for unexpected character data. */ |
44 | public static final int UNEXPECTED_DATA_CODE = 9003; |
45 | |
46 | /** Unexpected tag message text, part one. */ |
47 | public static final String UNEXPECTED_DATA_TEXT = "XML structure problem. Unexpected character data in tag: "; |
48 | |
49 | /** Error code for missing attribute. */ |
50 | public static final int MISSING_ATTRIBUTE_CODE = 9004; |
51 | |
52 | /** Missing attribute text. */ |
53 | public static final String MISSING_ATTRIBUTE_TEXT_1 = "XML structure problem. Missing neccessary attribute: "; |
54 | |
55 | /** Missing attribute, part two. */ |
56 | public static final String MISSING_ATTRIBUTE_TEXT_2 = " in tag: "; |
57 | |
58 | /** Error code for empty attribute. */ |
59 | public static final int EMPTY_ATTRIBUTE_CODE = 9004; |
60 | |
61 | /** Missing attribute text. */ |
62 | public static final String EMPTY_ATTRIBUTE_TEXT_1 = "Missing attribute: "; |
63 | |
64 | /** Missing attribute, part two. */ |
65 | public static final String EMPTY_ATTRIBUTE_TEXT_2 = " in tag: "; |
66 | |
67 | /** Error code for a programming error. */ |
68 | public static final int IO_ERROR_CODE = 9900; |
69 | |
70 | /** Unexpected tag message text, part one. */ |
71 | public static final String IO_ERROR_TEXT = "An IO error occurred."; |
72 | |
73 | /** Error code for a sax parser error. */ |
74 | public static final int SAX_ERROR_CODE = 9910; |
75 | |
76 | /** Unexpected tag message text, part one. */ |
77 | public static final String SAX_ERROR_TEXT = "A XML syntax error occurred."; |
78 | |
79 | /** Error code for a programming error. */ |
80 | public static final int PROGRAMMING_ERROR_CODE = 9999; |
81 | |
82 | /** Unexpected tag message text, part one. */ |
83 | public static final String PROGRAMMING_ERROR_TEXT = "A programming error occurred."; |
84 | |
85 | /** |
86 | * Constructor. |
87 | * |
88 | * @param code Error code. |
89 | * @param message Error message. |
90 | */ |
91 | private XmlSyntaxException(final int code, final String message) { |
92 | super(code, message); |
93 | } |
94 | |
95 | /** |
96 | * Constructor. |
97 | * |
98 | * @param code Error code. |
99 | * @param message Error message. |
100 | * @param e Error cause. |
101 | */ |
102 | private XmlSyntaxException(final int code, final String message, final Exception e) { |
103 | super(code, message, e); |
104 | } |
105 | |
106 | /** |
107 | * Create exception for unexpected tag. |
108 | * |
109 | * @param name Tag name. |
110 | * @return Exception. |
111 | */ |
112 | public static final XmlSyntaxException createUnexpectedTagException( |
113 | final String name) { |
114 | return new XmlSyntaxException(UNEXPECTED_TAG_CODE, UNEXPECTED_TAG_TEXT + name); |
115 | } |
116 | |
117 | /** |
118 | * Create exception for unexpected text data within a tag. |
119 | * |
120 | * @param name Tag name. |
121 | * @param value Data found. |
122 | * @return Exception. |
123 | */ |
124 | public static final XmlSyntaxException createUnexpectedTextDataException( |
125 | final String name, final String value) { |
126 | return new XmlSyntaxException(UNEXPECTED_DATA_CODE, UNEXPECTED_DATA_TEXT + name); |
127 | } |
128 | |
129 | /** |
130 | * Create exception for missing attribute within a tag. |
131 | * |
132 | * @param name Tag name. |
133 | * @param attribute Attribute name. |
134 | * @return Exception. |
135 | */ |
136 | public static final XmlSyntaxException createMissingAttributeException( |
137 | final String name, |
138 | final String attribute) { |
139 | return new XmlSyntaxException(MISSING_ATTRIBUTE_CODE, MISSING_ATTRIBUTE_TEXT_1 + attribute |
140 | + MISSING_ATTRIBUTE_TEXT_2 + name); |
141 | } |
142 | |
143 | /** |
144 | * Create exception for empty attribute within a tag. |
145 | * |
146 | * @param name Tag name. |
147 | * @param attribute Attribute name. |
148 | * @return Exception. |
149 | */ |
150 | public static final XmlSyntaxException createEmptyAttributeException( |
151 | final String name, final String attribute) { |
152 | return new XmlSyntaxException(EMPTY_ATTRIBUTE_CODE, EMPTY_ATTRIBUTE_TEXT_1 + attribute |
153 | + EMPTY_ATTRIBUTE_TEXT_2 + name); |
154 | } |
155 | |
156 | /** |
157 | * Create exception for a IO error. |
158 | * |
159 | * @param e Exception. |
160 | * @return Created exception. |
161 | */ |
162 | public static final XmlSyntaxException createByIOException( |
163 | final IOException e) { |
164 | return new XmlSyntaxException(IO_ERROR_CODE, IO_ERROR_TEXT, e); |
165 | } |
166 | |
167 | /** |
168 | * Create exception for a SAX parsing error. |
169 | * |
170 | * @param e Exception. |
171 | * @return Created exception. |
172 | */ |
173 | public static final XmlSyntaxException createBySAXException( |
174 | final SAXException e) { |
175 | return new XmlSyntaxException(SAX_ERROR_CODE, SAX_ERROR_TEXT, e); |
176 | } |
177 | |
178 | /** |
179 | * Create exception for a programming error. |
180 | * |
181 | * @param e Exception. |
182 | * @return Created exception. |
183 | */ |
184 | public static final XmlSyntaxException createByRuntimeException( |
185 | final RuntimeException e) { |
186 | return new XmlSyntaxException(PROGRAMMING_ERROR_CODE, PROGRAMMING_ERROR_TEXT, e); |
187 | } |
188 | |
189 | } |