001/**
002 *
003 * Copyright © 2021 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jxmpp.xml.splitter;
018
019import java.io.IOException;
020
021public abstract class InvalidXmlException extends IOException {
022
023        private static final long serialVersionUID = 1L;
024
025        private final char unexpectedChar;
026        private final String xml;
027
028        protected InvalidXmlException(CharSequence message, char unexpectedChar, CharSequence xml) {
029                super(message.toString());
030                this.unexpectedChar = unexpectedChar;
031                this.xml = xml.toString();
032        }
033
034        public char getUnexpectedChar() {
035                return unexpectedChar;
036        }
037
038        public String getParsedXmlSoFar() {
039                return xml;
040        }
041
042        public static final class InvalidEmptyTagException extends InvalidXmlException {
043
044                private static final long serialVersionUID = 1L;
045
046                private InvalidEmptyTagException(CharSequence message, char c, CharSequence xml) {
047                        super(message, c, xml);
048                }
049
050                public static InvalidEmptyTagException create(char c, CharSequence xml) {
051                        StringBuilder message = new StringBuilder();
052                        message.append("Invalid empty tag, expected '>', but got '").append(c).append("'. Parsed xml so far: ")
053                                        .append(xml);
054                        return new InvalidEmptyTagException(message, c, xml);
055                }
056        }
057
058        public static final class InvalidAttributeDeclarationException extends InvalidXmlException {
059
060                private static final long serialVersionUID = 1L;
061
062                private InvalidAttributeDeclarationException(CharSequence message, char c, CharSequence xml) {
063                        super(message, c, xml);
064                }
065
066                public static InvalidAttributeDeclarationException create(char c, CharSequence xml) {
067                        StringBuilder message = new StringBuilder();
068                        message.append("Invalid attribute declaration, expected ''' or '\"', but got '").append(c).append("'. Parsed xml so far: ")
069                                        .append(xml);
070                        return new InvalidAttributeDeclarationException(message, c, xml);
071                }
072        }
073}