KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > junit > output > TroubleParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.junit.output;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25 import org.netbeans.modules.junit.output.Report.Trouble;
26 import static org.netbeans.modules.junit.output.RegexpUtils.NESTED_EXCEPTION_PREFIX;
27 import static org.netbeans.modules.junit.output.RegexpUtils.CALLSTACK_LINE_PREFIX;
28 import static org.netbeans.modules.junit.output.RegexpUtils.CALLSTACK_LINE_PREFIX_CATCH;
29         
30
31 /**
32  *
33  * @author Marian Petras
34  */

35 final class TroubleParser {
36
37     /** */
38     private static final boolean DONE = true;
39     /** */
40     private static final boolean WANT_MORE = false;
41
42     /** */
43     private final Trouble trouble;
44     /** */
45     private final RegexpUtils regexp;
46
47     /** */
48     private Trouble currTrouble;
49     /** */
50     private List JavaDoc<String JavaDoc> callstackBuffer;
51
52     /**
53      */

54     TroubleParser(Trouble trouble, RegexpUtils regexp) {
55         this.trouble = trouble;
56         this.regexp = regexp;
57         
58         currTrouble = trouble;
59     }
60
61     /**
62      * Processes a single line of output.
63      *
64      * @param msg line to be processed
65      * @return <code>{@value #DONE}</code> if parsing of the current
66      * trouble is finished, <code>{@value #WANT_MORE}</code>
67      * otherwise
68      */

69     boolean processMessage(final String JavaDoc msg) {
70         Matcher JavaDoc matcher;
71         if (trouble.exceptionClsName == null) {
72             matcher = regexp.getTestcaseExceptionPattern().matcher(msg);
73             if (matcher.matches()) {
74                 trouble.exceptionClsName = matcher.group(1);
75                 String JavaDoc exceptionMsg = matcher.group(2);
76                 if (exceptionMsg != null) {
77                     trouble.message = exceptionMsg;
78                 }
79             }
80             return WANT_MORE; //ignore other texts until
81
//we get exception class name
82
}
83         
84         String JavaDoc trimmed = RegexpUtils.specialTrim(msg);
85         if (trimmed.length() == 0) {
86             finishProcessing();
87             return DONE;
88         }
89         
90         if (msg.startsWith(NESTED_EXCEPTION_PREFIX)) {
91             if (callstackBuffer != null) {
92                 matcher = regexp.getNestedExceptionPattern().matcher(
93                         msg.substring(NESTED_EXCEPTION_PREFIX.length()));
94                 if (matcher.matches()) {
95                     fixateStackTrace();
96                     
97                     Trouble nestedTrouble = new Trouble(false);
98                     nestedTrouble.exceptionClsName = matcher.group(1);
99                     nestedTrouble.message = matcher.group(2);
100                     
101                     currTrouble.nestedTrouble = nestedTrouble;
102                     currTrouble = nestedTrouble;
103                     return WANT_MORE;
104                 }
105             }
106         } else {
107             if (trimmed.startsWith(CALLSTACK_LINE_PREFIX_CATCH)) {
108                 trimmed = trimmed.substring(CALLSTACK_LINE_PREFIX_CATCH.length());
109             }
110             if (trimmed.startsWith(CALLSTACK_LINE_PREFIX)) {
111                 matcher = regexp.getCallstackLinePattern().matcher(msg);
112                 if (matcher.matches()) {
113                     if (callstackBuffer == null) {
114                         callstackBuffer = new ArrayList JavaDoc<String JavaDoc>(8);
115                     }
116                     callstackBuffer.add(
117                             trimmed.substring(CALLSTACK_LINE_PREFIX.length()));
118                     return WANT_MORE;
119                 }
120             }
121         }
122         if ((callstackBuffer == null) && (currTrouble.message != null)) {
123             currTrouble.message = currTrouble.message + '\n' + msg;
124         }
125         /* else: just ignore the text */
126         return WANT_MORE;
127     }
128
129     /**
130      */

131     void finishProcessing() {
132         if (callstackBuffer != null) {
133             fixateStackTrace();
134         }
135     }
136
137     /**
138      */

139     private void fixateStackTrace() {
140         currTrouble.stackTrace = callstackBuffer.toArray(
141                                 new String JavaDoc[callstackBuffer.size()]);
142         callstackBuffer = null;
143     }
144     
145 }
146
Popular Tags