001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.parser;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import java.text.*;
018import java.util.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.internal.*;
022
023/**
024 * Exception that indicates invalid syntax encountered during parsing.
025 */
026public class ParseException extends FormattedException {
027
028   private static final long serialVersionUID = 1L;
029
030   /**
031    * Constructor.
032    *
033    * @param message The {@link MessageFormat}-style message.
034    * @param args Optional {@link MessageFormat}-style arguments.
035    */
036   public ParseException(String message, Object...args) {
037      super(message, args);
038   }
039
040   /**
041    * Constructor.
042    *
043    * @param causedBy The cause of this exception.
044    * @param message The {@link MessageFormat}-style message.
045    * @param args Optional {@link MessageFormat}-style arguments.
046    */
047   public ParseException(Throwable causedBy, String message, Object...args) {
048      super(causedBy, message, args);
049   }
050
051   /**
052    * Constructor.
053    *
054    * @param causedBy The cause of this exception.
055    */
056   public ParseException(Throwable causedBy) {
057      super(causedBy);
058   }
059
060   /**
061    * Constructor.
062    *
063    * @param session The parser session.
064    * @param message The exception message containing {@link MessageFormat}-style arguments.
065    * @param args Optional {@link MessageFormat}-style arguments.
066    */
067   public ParseException(ParserSession session, String message, Object...args) {
068      super(getMessage(session, message, args));
069   }
070
071   /**
072    * Constructor.
073    *
074    * @param session The parser session.
075    * @param causedBy The cause of this exception.
076    * @param message The exception message containing {@link MessageFormat}-style arguments.
077    * @param args Optional {@link MessageFormat}-style arguments.
078    */
079   public ParseException(ParserSession session, Throwable causedBy, String message, Object...args) {
080      super(causedBy, getMessage(session, message, args));
081   }
082
083   /**
084    * Constructor.
085    *
086    * @param session The parser session.
087    * @param causedBy The inner exception.
088    */
089   public ParseException(ParserSession session, Exception causedBy) {
090      super(causedBy, getMessage(session, causedBy.getMessage()));
091   }
092
093
094   private static String getMessage(ParserSession session, String msg, Object... args) {
095      if (args.length != 0)
096         msg = format(msg, args);
097
098      if (session != null) {
099         Position p = session.getPosition();
100
101         msg += "\n\tAt: " + p;
102
103         ObjectMap lastLocation = session.getLastLocation();
104         if (lastLocation != null) {
105            msg += "\n\tWhile parsing into: ";
106            for (Map.Entry<String,Object> e : lastLocation.entrySet())
107               msg += "\n\t\t" + e.getKey() + ": " + e.getValue();
108         }
109
110         String lines = session.getInputAsString();
111         if (lines == null)
112            msg += "\n\tUse BEAN_debug setting to display content.";
113         else {
114            int numLines = session.getDebugOutputLines();
115            int start = p.line - numLines, end = p.line + numLines;
116            msg += "\n---start--\n" + StringUtils.getNumberedLines(lines, start, end) + "---end---";
117         }
118      }
119      return msg;
120   }
121
122   /**
123    * Returns the highest-level <code>ParseException</code> in the stack trace.
124    *
125    * <p>
126    * Useful for JUnit testing of error conditions.
127    *
128    * @return The root parse exception, or this exception if there isn't one.
129    */
130   public ParseException getRootCause() {
131      ParseException t = this;
132      while (! (t.getCause() == null || ! (t.getCause() instanceof ParseException)))
133         t = (ParseException)t.getCause();
134      return t;
135   }
136
137   /**
138    * @deprecated Use {@link #ParseException(ParserSession, String, Object[])}
139    */
140   @SuppressWarnings("javadoc")
141   @Deprecated
142   public ParseException(ObjectMap location, String message, Object...args) {
143      super(getMessage(null, message, args));
144   }
145
146   /**
147    * @deprecated Use {@link #ParseException(ParserSession, Exception)}
148    */
149   @SuppressWarnings("javadoc")
150   @Deprecated
151   public ParseException(ObjectMap location, Exception causedBy) {
152      super(causedBy, getMessage(null, causedBy.getMessage()));
153   }
154
155   /**
156    * @deprecated Use {@link #ParseException(ParserSession, Exception)}
157    */
158   @SuppressWarnings("javadoc")
159   @Deprecated
160   public ParseException(Exception causedBy) {
161      super(causedBy, getMessage(null, causedBy.getMessage()));
162   }
163
164   /**
165    * @deprecated Use {@link #ParseException(ParserSession, Exception)}
166    */
167   @Override
168   @Deprecated
169   public synchronized ParseException initCause(Throwable cause) {
170      super.initCause(cause);
171      return this;
172   }
173}