| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | import java.nio.CharBuffer; |
| 36 | | | import java.text.ParseException; |
| 37 | | | import java.util.regex.Matcher; |
| 38 | | | import java.util.regex.Pattern; |
| 39 | | | |
| 40 | | | |
| 41 | | | |
| 42 | | | |
| 43 | | | |
| 44 | | | <ul> |
| 45 | | | <li><code></code><code></code> |
| 46 | | | <li><code></code> |
| 47 | | | <li><code></code><code></code> |
| 48 | | | <li><code></code> |
| 49 | | | </ul> |
| 50 | | | |
| 51 | | | |
| 52 | | (1) | public final class StackTraceElementParser |
| 53 | | | { |
| 54 | 0 | | private static final Pattern FULL_LOCATION_PATTERN = Pattern.compile( |
| 55 | | | "^\\s*at\\s+[^\\s\\.]([^\\s\\(]+\\.)*[^\\s\\(]+\\(.*\\)\\s*$"); |
| 56 | 0 | | private static final Pattern LOCATION_PATTERN = Pattern.compile( |
| 57 | | | "^\\s*at\\s+"); |
| 58 | 0 | | private static final Pattern CLASS_PATTERN = Pattern.compile( |
| 59 | | | "([^\\s\\(]+\\.)*"); |
| 60 | 0 | | private static final Pattern METHOD_PATTERN = Pattern.compile( |
| 61 | | | "[^\\s\\(]+\\s*\\("); |
| 62 | 0 | | private static final Pattern LINE_PATTERN = Pattern.compile( |
| 63 | | | "[^\\d\\)]+"); |
| 64 | | | |
| 65 | 0 | | private static final Pattern FULL_CAUSE_PATTERN = Pattern.compile( |
| 66 | | | "^\\s*Caused by\\:\\s*.*$"); |
| 67 | 0 | | private static final Pattern CAUSE_PATTERN = Pattern.compile( |
| 68 | | | "^\\s*Caused by\\:\\s*"); |
| 69 | | | |
| 70 | 0 | | private static final Pattern FULL_MORE_PATTERN = Pattern.compile( |
| 71 | | | "^\\s*\\.+\\s*\\d+\\s+more\\s*$"); |
| 72 | 0 | | private static final Pattern MORE_PATTERN = Pattern.compile( |
| 73 | | | "^\\s*\\.+\\s*"); |
| 74 | | | |
| 75 | 0 | | private static final Pattern FULL_EXCEPTION_TEXT_PATTERN = Pattern.compile( |
| 76 | | | "^\\s*.*$"); |
| 77 | 0 | | private static final Pattern EXCEPTION_TEXT_PATTERN = Pattern.compile( |
| 78 | | | "^\\s*"); |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | |
| 83 | | | private StackTraceElementParser () |
| 84 | 0 | | { |
| 85 | | | |
| 86 | 0 | | } |
| 87 | | | |
| 88 | | | |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | @param |
| 93 | | | |
| 94 | | | @return |
| 95 | | | |
| 96 | | | |
| 97 | | | @throws |
| 98 | | | |
| 99 | | | public static StackTraceInfo parse (final CharBuffer buffer) |
| 100 | | | throws ParseException |
| 101 | | | { |
| 102 | 0 | | StackTraceInfo rc = null; |
| 103 | 0 | | StackTraceInfo stInfo = null; |
| 104 | | | |
| 105 | 0 | (2) | if ((stInfo = parseLocation(buffer)) != null) |
| 106 | | | { |
| 107 | 0 | | rc = stInfo; |
| 108 | | | } |
| 109 | 0 | (3) | else if ((stInfo = parseCause(buffer)) != null) |
| 110 | | | { |
| 111 | 0 | | rc = stInfo; |
| 112 | | | } |
| 113 | 0 | (4) | else if ((stInfo = parseMore(buffer)) != null) |
| 114 | | | { |
| 115 | 0 | | rc = stInfo; |
| 116 | | | } |
| 117 | 0 | (5) | else if ((stInfo = parseMore(buffer)) != null) |
| 118 | | | { |
| 119 | 0 | | rc = stInfo; |
| 120 | | | } |
| 121 | 0 | (6) | else if ((stInfo = parseExceptionText(buffer)) != null) |
| 122 | | | { |
| 123 | 0 | | rc = stInfo; |
| 124 | | | } |
| 125 | | | else |
| 126 | | | { |
| 127 | 0 | | throw new ParseException( |
| 128 | | | "Buffer does not match any of the defined patterns: " |
| 129 | | | + buffer, buffer.position()); |
| 130 | | | } |
| 131 | 0 | | return rc; |
| 132 | | | } |
| 133 | | | |
| 134 | | | private static StackTraceInfo parseLocation (final CharBuffer buffer) |
| 135 | | | throws ParseException |
| 136 | | | { |
| 137 | 0 | | StackTraceInfo rc = null; |
| 138 | | | |
| 139 | 0 | | Matcher matcher = FULL_LOCATION_PATTERN.matcher(buffer); |
| 140 | | | |
| 141 | 0 | | if (matcher.matches()) |
| 142 | | | { |
| 143 | 0 | | final int savePos = buffer.position(); |
| 144 | | | |
| 145 | 0 | | matcher = LOCATION_PATTERN.matcher(buffer); |
| 146 | 0 | | if (! matcher.lookingAt()) |
| 147 | | | { |
| 148 | 0 | | throw new ParseException("Cannot parse correctly: " + buffer, |
| 149 | | | buffer.position()); |
| 150 | | | } |
| 151 | 0 | | int pos = matcher.end(); |
| 152 | 0 | | buffer.position(buffer.position() + pos); |
| 153 | 0 | | matcher = CLASS_PATTERN.matcher(buffer); |
| 154 | 0 | | if (! matcher.lookingAt()) |
| 155 | | | { |
| 156 | 0 | | throw new ParseException("Cannot parse correctly: " + buffer, |
| 157 | | | buffer.position()); |
| 158 | | | } |
| 159 | 0 | | pos = matcher.end(); |
| 160 | 0 | | final CharBuffer classname = buffer.asReadOnlyBuffer(); |
| 161 | 0 | | classname.limit(classname.position() + pos - 1); |
| 162 | | | |
| 163 | 0 | | buffer.position(buffer.position() + pos); |
| 164 | 0 | | matcher = METHOD_PATTERN.matcher(buffer); |
| 165 | 0 | | if (! matcher.lookingAt()) |
| 166 | | | { |
| 167 | 0 | | throw new ParseException("Cannot parse correctly: " + buffer, |
| 168 | | | buffer.position()); |
| 169 | | | } |
| 170 | 0 | | pos = matcher.end(); |
| 171 | 0 | | final CharBuffer methodname = buffer.asReadOnlyBuffer(); |
| 172 | 0 | | methodname.limit(methodname.position() + pos - 1); |
| 173 | 0 | | int line = -1; |
| 174 | 0 | | matcher = LINE_PATTERN.matcher(buffer); |
| 175 | | | |
| 176 | 0 | | if (matcher.lookingAt()) |
| 177 | | | { |
| 178 | 0 | | pos = matcher.end(); |
| 179 | 0 | | buffer.position(buffer.position() + pos); |
| 180 | 0 | | boolean digit = false; |
| 181 | 0 | | int i = 0; |
| 182 | 0 | | while (Character.isDigit(buffer.charAt(i))) |
| 183 | | | { |
| 184 | 0 | | ++i; |
| 185 | 0 | | digit = true; |
| 186 | | | } |
| 187 | 0 | | if (digit) |
| 188 | | | { |
| 189 | 0 | | line = Integer.parseInt(buffer.subSequence(0, i).toString()); |
| 190 | | | } |
| 191 | | | } |
| 192 | 0 | | buffer.position(savePos); |
| 193 | 0 | | rc = new StackTraceInfo( |
| 194 | | | buffer.asReadOnlyBuffer(), classname, methodname, line); |
| 195 | | | } |
| 196 | 0 | | return rc; |
| 197 | | | } |
| 198 | | | |
| 199 | | | private static StackTraceInfo parseCause (final CharBuffer buffer) |
| 200 | | | throws ParseException |
| 201 | | | { |
| 202 | 0 | | StackTraceInfo rc = null; |
| 203 | 0 | | Matcher matcher = FULL_CAUSE_PATTERN.matcher(buffer); |
| 204 | 0 | | if (matcher.matches()) |
| 205 | | | { |
| 206 | 0 | | final int savePos = buffer.position(); |
| 207 | 0 | | matcher = CAUSE_PATTERN.matcher(buffer); |
| 208 | 0 | | if (! matcher.lookingAt()) |
| 209 | | | { |
| 210 | 0 | | throw new ParseException("Cannot parse a caused-by correctly: " |
| 211 | | | + buffer, buffer.position()); |
| 212 | | | } |
| 213 | 0 | | final int pos = matcher.end(); |
| 214 | 0 | | buffer.position(buffer.position() + pos); |
| 215 | 0 | | final CharBuffer cause = buffer.asReadOnlyBuffer(); |
| 216 | 0 | | buffer.position(savePos); |
| 217 | 0 | | rc = new StackTraceInfo(buffer.asReadOnlyBuffer(), cause, true); |
| 218 | | | } |
| 219 | 0 | | return rc; |
| 220 | | | } |
| 221 | | | |
| 222 | | | private static StackTraceInfo parseExceptionText (final CharBuffer buffer) |
| 223 | | | throws ParseException |
| 224 | | | { |
| 225 | 0 | | StackTraceInfo rc = null; |
| 226 | 0 | | Matcher matcher = FULL_EXCEPTION_TEXT_PATTERN.matcher(buffer); |
| 227 | 0 | | if (matcher.matches()) |
| 228 | | | { |
| 229 | 0 | | final int savePos = buffer.position(); |
| 230 | 0 | | matcher = EXCEPTION_TEXT_PATTERN.matcher(buffer); |
| 231 | 0 | | if (! matcher.lookingAt()) |
| 232 | | | { |
| 233 | 0 | | throw new ParseException( |
| 234 | | | "Cannot parse an exception text correctly: " + buffer, |
| 235 | | | buffer.position()); |
| 236 | | | } |
| 237 | 0 | | final int pos = matcher.end(); |
| 238 | 0 | | buffer.position(buffer.position() + pos); |
| 239 | 0 | | final CharBuffer exceptionText = buffer.asReadOnlyBuffer(); |
| 240 | 0 | | buffer.position(savePos); |
| 241 | 0 | | rc = new StackTraceInfo( |
| 242 | | | buffer.asReadOnlyBuffer(), exceptionText, false); |
| 243 | | | } |
| 244 | 0 | | return rc; |
| 245 | | | } |
| 246 | | | |
| 247 | | | private static StackTraceInfo parseMore (final CharBuffer buffer) |
| 248 | | | throws ParseException |
| 249 | | | { |
| 250 | 0 | | StackTraceInfo rc = null; |
| 251 | 0 | | Matcher matcher = FULL_MORE_PATTERN.matcher(buffer); |
| 252 | 0 | | if (matcher.matches()) |
| 253 | | | { |
| 254 | 0 | | final int savePos = buffer.position(); |
| 255 | 0 | | matcher = MORE_PATTERN.matcher(buffer); |
| 256 | 0 | | if (! matcher.lookingAt()) |
| 257 | | | { |
| 258 | 0 | | throw new ParseException("Cannot parse a more-line correctly: " |
| 259 | | | + buffer, buffer.position()); |
| 260 | | | } |
| 261 | 0 | | final int pos = matcher.end(); |
| 262 | 0 | | buffer.position(buffer.position() + pos); |
| 263 | 0 | | boolean digit = false; |
| 264 | 0 | | int i = 0; |
| 265 | 0 | | while (Character.isDigit(buffer.charAt(i))) |
| 266 | | | { |
| 267 | 0 | | ++i; |
| 268 | 0 | | digit = true; |
| 269 | | | } |
| 270 | 0 | | if (! digit) |
| 271 | | | { |
| 272 | 0 | | throw new ParseException("Number of more lines is missing: " |
| 273 | | | + buffer, buffer.position()); |
| 274 | | | } |
| 275 | 0 | | final int moreLines = Integer.parseInt( |
| 276 | | | buffer.subSequence(0, i).toString()); |
| 277 | 0 | | buffer.position(savePos); |
| 278 | 0 | | rc = new StackTraceInfo(buffer.asReadOnlyBuffer(), moreLines); |
| 279 | | | } |
| 280 | 0 | | return rc; |
| 281 | | | } |
| 282 | | | } |