001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.util.pattern; 018 019import java.text.MessageFormat; 020 021/** 022 * Exception that is thrown when there is a problem with the pattern being parsed. 023 * 024 * @author Andy Clement 025 * @since 5.0 026 */ 027@SuppressWarnings("serial") 028public class PatternParseException extends IllegalArgumentException { 029 030 private final int position; 031 032 private final char[] pattern; 033 034 private final PatternMessage messageType; 035 036 private final Object[] inserts; 037 038 039 PatternParseException(int pos, char[] pattern, PatternMessage messageType, Object... inserts) { 040 super(messageType.formatMessage(inserts)); 041 this.position = pos; 042 this.pattern = pattern; 043 this.messageType = messageType; 044 this.inserts = inserts; 045 } 046 047 PatternParseException(Throwable cause, int pos, char[] pattern, PatternMessage messageType, Object... inserts) { 048 super(messageType.formatMessage(inserts), cause); 049 this.position = pos; 050 this.pattern = pattern; 051 this.messageType = messageType; 052 this.inserts = inserts; 053 } 054 055 056 /** 057 * Return a formatted message with inserts applied. 058 */ 059 @Override 060 public String getMessage() { 061 return this.messageType.formatMessage(this.inserts); 062 } 063 064 /** 065 * Return a detailed message that includes the original pattern text 066 * with a pointer to the error position, as well as the error message. 067 */ 068 public String toDetailedString() { 069 StringBuilder buf = new StringBuilder(); 070 buf.append(this.pattern).append('\n'); 071 for (int i = 0; i < this.position; i++) { 072 buf.append(' '); 073 } 074 buf.append("^\n"); 075 buf.append(getMessage()); 076 return buf.toString(); 077 } 078 079 public int getPosition() { 080 return this.position; 081 } 082 083 public PatternMessage getMessageType() { 084 return this.messageType; 085 } 086 087 public Object[] getInserts() { 088 return this.inserts; 089 } 090 091 092 /** 093 * The messages that can be included in a {@link PatternParseException} when there is a parse failure. 094 */ 095 public enum PatternMessage { 096 097 MISSING_CLOSE_CAPTURE("Expected close capture character after variable name '}'"), 098 MISSING_OPEN_CAPTURE("Missing preceding open capture character before variable name'{'"), 099 ILLEGAL_NESTED_CAPTURE("Not allowed to nest variable captures"), 100 CANNOT_HAVE_ADJACENT_CAPTURES("Adjacent captures are not allowed"), 101 ILLEGAL_CHARACTER_AT_START_OF_CAPTURE_DESCRIPTOR("Char ''{0}'' not allowed at start of captured variable name"), 102 ILLEGAL_CHARACTER_IN_CAPTURE_DESCRIPTOR("Char ''{0}'' is not allowed in a captured variable name"), 103 NO_MORE_DATA_EXPECTED_AFTER_CAPTURE_THE_REST("No more pattern data allowed after '{*...}' pattern element"), 104 BADLY_FORMED_CAPTURE_THE_REST("Expected form when capturing the rest of the path is simply '{*...}'"), 105 MISSING_REGEX_CONSTRAINT("Missing regex constraint on capture"), 106 ILLEGAL_DOUBLE_CAPTURE("Not allowed to capture ''{0}'' twice in the same pattern"), 107 REGEX_PATTERN_SYNTAX_EXCEPTION("Exception occurred in regex pattern compilation"), 108 CAPTURE_ALL_IS_STANDALONE_CONSTRUCT("'{*...}' can only be preceded by a path separator"); 109 110 private final String message; 111 112 PatternMessage(String message) { 113 this.message = message; 114 } 115 116 public String formatMessage(Object... inserts) { 117 return MessageFormat.format(this.message, inserts); 118 } 119 } 120 121}