01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.bpmscript;
18:
19: import java.util.regex.Matcher;
20: import java.util.regex.Pattern;
21:
22: import com.thoughtworks.xstream.converters.ConversionException;
23: import com.thoughtworks.xstream.converters.basic.AbstractBasicConverter;
24: import com.thoughtworks.xstream.converters.extended.StackTraceElementFactory;
25:
26: /**
27: * Converts rhino stack traces for XStream
28: */
29: public class BpmStackTraceElementConverter extends
30: AbstractBasicConverter {
31:
32: public boolean canConvert(Class type) {
33: return (java.lang.StackTraceElement.class).equals(type);
34: }
35:
36: protected Object fromString(String str) {
37: Matcher matcher = PATTERN.matcher(str);
38: if (matcher.matches()) {
39: String declaringClass = matcher.group(1);
40: String methodName = matcher.group(2);
41: String fileName = matcher.group(3);
42: if (fileName.equals("Unknown Source"))
43: return factory.unknownSourceElement(declaringClass,
44: methodName);
45: if (fileName.equals("Native Method"))
46: return factory.nativeMethodElement(declaringClass,
47: methodName);
48: if (matcher.group(4) != null) {
49: int lineNumber = Integer.parseInt(matcher.group(5));
50: return factory.element(declaringClass, methodName,
51: fileName, lineNumber);
52: } else {
53: return factory.element(declaringClass, methodName,
54: fileName);
55: }
56: } else {
57: matcher = RHINO_PATTERN.matcher(str);
58: if (matcher.matches()) {
59: String declaringClass = matcher.group(1);
60: String methodName = matcher.group(2);
61: String fileName = matcher.group(3);
62: if (matcher.group(4) != null) {
63: int lineNumber = Integer.parseInt(matcher.group(5));
64: return factory.element(declaringClass, methodName,
65: fileName, lineNumber);
66: } else {
67: return factory.element(declaringClass, methodName,
68: fileName);
69: }
70: }
71: // script.pause(classpath:/org/bpmscript/jbi/bpmscriptlibrary.js:6)
72: throw new ConversionException(
73: "Could not parse StackTraceElement : " + str);
74: }
75: }
76:
77: private static final Pattern PATTERN = Pattern
78: .compile("^(.+)\\.([^\\(]+)\\(([^:]*)(:(\\d+))?\\)$");
79: private static final Pattern RHINO_PATTERN = Pattern
80: .compile("^(.+)\\.([^\\(]*)\\((.*?)(:(\\d+))?\\)$");
81: private final StackTraceElementFactory factory = new StackTraceElementFactory();
82: }
|