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.*;
018
019import org.apache.juneau.*;
020
021/**
022 * Exception that indicates invalid syntax encountered during parsing.
023 */
024public class ParseException extends FormattedException {
025
026   private static final long serialVersionUID = 1L;
027
028   /**
029    * Constructor.
030    * 
031    * @param location The location of the parse exception.
032    * @param message The exception message containing {@link MessageFormat}-style arguments.
033    * @param args Optional {@link MessageFormat}-style arguments.
034    */
035   public ParseException(ObjectMap location, String message, Object...args) {
036      super(getMessage(location, message, args));
037   }
038
039   /**
040    * Constructor.
041    * 
042    * @param message The exception message containing {@link MessageFormat}-style arguments.
043    * @param args Optional {@link MessageFormat}-style arguments.
044    */
045   public ParseException(String message, Object...args) {
046      super(getMessage(null, message, args));
047   }
048
049   /**
050    * Constructor.
051    * 
052    * @param location The location of the parse exception.
053    * @param causedBy The inner exception.
054    */
055   public ParseException(ObjectMap location, Exception causedBy) {
056      super(causedBy, getMessage(location, causedBy.getMessage()));
057   }
058
059   /**
060    * Constructor.
061    * 
062    * @param causedBy The inner exception.
063    */
064   public ParseException(Exception causedBy) {
065      super(causedBy, getMessage(null, causedBy.getMessage()));
066   }
067
068   private static String getMessage(ObjectMap location, String msg, Object... args) {
069      if (args.length != 0)
070         msg = format(msg, args);
071      if (location != null && ! location.isEmpty()) {
072         msg = "Parse exception occurred at " + location.toString() + ".  " + msg;
073      }
074      return msg;
075   }
076
077   /**
078    * Returns the highest-level <code>ParseException</code> in the stack trace.
079    * 
080    * <p>
081    * Useful for JUnit testing of error conditions.
082    * 
083    * @return The root parse exception, or this exception if there isn't one.
084    */
085   public ParseException getRootCause() {
086      ParseException t = this;
087      while (! (t.getCause() == null || ! (t.getCause() instanceof ParseException)))
088         t = (ParseException)t.getCause();
089      return t;
090   }
091
092   /**
093    * Sets the inner cause for this exception.
094    * 
095    * @param cause The inner cause.
096    * @return This object (for method chaining).
097    */
098   @Override /* Throwable */
099   public synchronized ParseException initCause(Throwable cause) {
100      super.initCause(cause);
101      return this;
102   }
103}