XmlSyntaxException.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.xml.common;
017 
018 import java.io.IOException;
019 
020 import org.qedeq.kernel.se.common.ErrorCodes;
021 import org.qedeq.kernel.se.common.QedeqException;
022 import org.xml.sax.SAXException;
023 
024 
025 /**
026  * Exception that occurs during XML parsing. It specifies an syntactical error.
027  * It can also mark a lack of inner consistence of something.
028  * Also a programming error can lead to this exception.
029  *
030  @author  Michael Meyling
031  */
032 public final class XmlSyntaxException extends QedeqException implements ErrorCodes {
033 
034     /** Error code for Exceptions thrown by the SAXParser. */
035     public static final int SAX_PARSER_EXCEPTION = 9001;
036 
037     /** Error code for unexpected tag. */
038     public static final int UNEXPECTED_TAG_CODE = 9002;
039 
040     /** Unexpected tag message text. */
041     public static final String UNEXPECTED_TAG_TEXT = "XML structure problem. Unexpected tag: ";
042 
043     /** Error code for unexpected character data. */
044     public static final int UNEXPECTED_DATA_CODE = 9003;
045 
046     /** Unexpected tag message text, part one. */
047     public static final String UNEXPECTED_DATA_TEXT = "XML structure problem. Unexpected character data in tag: ";
048 
049     /** Error code for missing attribute. */
050     public static final int MISSING_ATTRIBUTE_CODE = 9004;
051 
052     /** Missing attribute text. */
053     public static final String MISSING_ATTRIBUTE_TEXT_1 = "XML structure problem. Missing neccessary attribute: ";
054 
055     /** Missing attribute, part two. */
056     public static final String MISSING_ATTRIBUTE_TEXT_2 = " in tag: ";
057 
058     /** Error code for empty attribute. */
059     public static final int EMPTY_ATTRIBUTE_CODE = 9004;
060 
061     /** Missing attribute text. */
062     public static final String EMPTY_ATTRIBUTE_TEXT_1 = "Missing attribute: ";
063 
064     /** Missing attribute, part two. */
065     public static final String EMPTY_ATTRIBUTE_TEXT_2 = " in tag: ";
066 
067     /** Error code for a programming error. */
068     public static final int IO_ERROR_CODE = 9900;
069 
070     /** Unexpected tag message text, part one. */
071     public static final String IO_ERROR_TEXT = "An IO error occurred.";
072 
073     /** Error code for a sax parser error. */
074     public static final int SAX_ERROR_CODE = 9910;
075 
076     /** Unexpected tag message text, part one. */
077     public static final String SAX_ERROR_TEXT = "A XML syntax error occurred.";
078 
079     /** Error code for a programming error. */
080     public static final int PROGRAMMING_ERROR_CODE = 9999;
081 
082     /** Unexpected tag message text, part one. */
083     public static final String PROGRAMMING_ERROR_TEXT = "A programming error occurred.";
084 
085     /**
086      * Constructor.
087      *
088      @param   code    Error code.
089      @param   message Error message.
090      */
091     private XmlSyntaxException(final int code, final String message) {
092         super(code, message);
093     }
094 
095     /**
096      * Constructor.
097      *
098      @param   code    Error code.
099      @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 }