001:/*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: */
017:package org.apache.cocoon.components.xpointer.parser;
018:
019:/**
020: * @version CVS $Id: TokenMgrError.java 433543 2006-08-22 06:22:54Z crossley $
021: *
022: */
023:public class TokenMgrError extends Error {
024: /*
025: * Ordinals for various reasons why an Error of this type can be thrown.
026: */
027:
028: /**
029: * Lexical error occured.
030: */
031: static final int LEXICAL_ERROR = 0;
032:
033: /**
034: * An attempt wass made to create a second instance of a static token manager.
035: */
036: static final int STATIC_LEXER_ERROR = 1;
037:
038: /**
039: * Tried to change to an invalid lexical state.
040: */
041: static final int INVALID_LEXICAL_STATE = 2;
042:
043: /**
044: * Detected (and bailed out of) an infinite loop in the token manager.
045: */
046: static final int LOOP_DETECTED = 3;
047:
048: /**
049: * Indicates the reason why the exception is thrown. It will have
050: * one of the above 4 values.
051: */
052: int errorCode;
053:
054: /**
055: * Replaces unprintable characters by their espaced (or unicode escaped)
056: * equivalents in the given string
057: */
058: protected static final String addEscapes(String str) {
059: StringBuffer retval = new StringBuffer();
060: char ch;
061: for (int i = 0; i < str.length(); i++) {
062: switch (str.charAt(i)) {
063: case 0 :
064: continue;
065: case '\b' :
066: retval.append("\\b");
067: continue;
068: case '\t' :
069: retval.append("\\t");
070: continue;
071: case '\n' :
072: retval.append("\\n");
073: continue;
074: case '\f' :
075: retval.append("\\f");
076: continue;
077: case '\r' :
078: retval.append("\\r");
079: continue;
080: case '\"' :
081: retval.append("\\\"");
082: continue;
083: case '\'' :
084: retval.append("\\\'");
085: continue;
086: case '\\' :
087: retval.append("\\\\");
088: continue;
089: default :
090: if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
091: String s = "0000" + Integer.toString(ch, 16);
092: retval.append(
093: "\\u" + s.substring(s.length() - 4, s.length()));
094: } else {
095: retval.append(ch);
096: }
097: continue;
098: }
099: }
100: return retval.toString();
101: }
102:
103: /**
104: * Returns a detailed message for the Error when it is thrown by the
105: * token manager to indicate a lexical error.
106: * Parameters :
107: * EOFSeen : indicates if EOF caused the lexicl error
108: * curLexState : lexical state in which this error occured
109: * errorLine : line number when the error occured
110: * errorColumn : column number when the error occured
111: * errorAfter : prefix that was seen before this error occured
112: * curchar : the offending character
113: * Note: You can customize the lexical error message by modifying this method.
114: */
115: protected static String lexicalError(
116: boolean EOFSeen,
117: int lexState,
118: int errorLine,
119: int errorColumn,
120: String errorAfter,
121: char curChar) {
122: return (
123: "Lexical error at line "
124: + errorLine
125: + ", column "
126: + errorColumn
127: + ". Encountered: "
128: + (EOFSeen
129: ? "<EOF> "
130: : ("\"" + addEscapes(String.valueOf(curChar)) + "\"")
131: + " ("
132: + (int) curChar
133: + "), ")
134: + "after : \""
135: + addEscapes(errorAfter)
136: + "\"");
137: }
138:
139: /**
140: * You can also modify the body of this method to customize your error messages.
141: * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
142: * of end-users concern, so you can return something like :
143: *
144: * "Internal Error : Please file a bug report .... "
145: *
146: * from this method for such cases in the release version of your parser.
147: */
148: public String getMessage() {
149: return super .getMessage();
150: }
151:
152: /*
153: * Constructors of various flavors follow.
154: */
155:
156: public TokenMgrError() {
157: }
158:
159: public TokenMgrError(String message, int reason) {
160: super (message);
161: errorCode = reason;
162: }
163:
164: public TokenMgrError(
165: boolean EOFSeen,
166: int lexState,
167: int errorLine,
168: int errorColumn,
169: String errorAfter,
170: char curChar,
171: int reason) {
172: this(
173: lexicalError(
174: EOFSeen,
175: lexState,
176: errorLine,
177: errorColumn,
178: errorAfter,
179: curChar),
180: reason);
181: }
182:}
|