KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > actions > QaIOReporter


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.tools.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import org.netbeans.api.xml.cookies.CookieMessage;
25 import org.netbeans.api.xml.cookies.CookieObserver;
26 import org.netbeans.api.xml.cookies.XMLProcessorDetail;
27
28 /**
29  * QA CookieObserver intended for testinng Validate and Check actions.
30  * @author mschovanek
31  */

32 public class QaIOReporter implements CookieObserver {
33     ArrayList JavaDoc messages = new ArrayList JavaDoc(5);
34
35     /** Creates a new instance of QaIOReporter */
36     public QaIOReporter() {
37     }
38
39     /** Receive a cookie message. Implementation (handling code) must not
40      * invoke directly or indirecly any source cookie method.
41      * Implementation should be as fast as possible.
42      * @param msg Received cookie message never <code>null</code>.
43      */

44     public void receive(CookieMessage msg) {
45         messages.add(msg);
46     }
47     
48     /** Returns received messages
49      * @return */

50     protected CookieMessage[] getMessages() {
51         return (CookieMessage[]) messages.toArray();
52     }
53     
54     /** Creates report from received messages.
55      * @return report */

56     protected String JavaDoc getReport() {
57         int[] lines = getErrLines();
58         Arrays.sort(lines);
59         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
60         if (lines.length > 1) {
61             buf.append("There are errors at lines: ");
62         } else if (lines.length > 0) {
63             buf.append("There is error at line: ");
64         } else {
65             buf.append("There is not errors.");
66         }
67         for (int i = 0; i < lines.length; i++) {
68             buf.append("" + lines[i] + "; ");
69         }
70         return buf.toString();
71     }
72     
73     /** Returns array of errors' line numbers.
74      * @return */

75     protected int[] getErrLines() {
76         int[] tmp = new int[messages.size()];
77         int index = 0;
78         for (int i = 0; i < messages.size(); i++) {
79             CookieMessage msg = (CookieMessage) messages.get(i);
80             int level = msg.getLevel();
81             if (level == msg.ERROR_LEVEL || level == msg.FATAL_ERROR_LEVEL) {
82                 Object JavaDoc detail = msg.getDetail(XMLProcessorDetail.class);
83                 if (detail instanceof XMLProcessorDetail) {
84                     tmp[index] = ((XMLProcessorDetail) detail).getLineNumber();
85                 } else {
86                     tmp[index] = -2; // unknown line number
87
}
88                 index++;
89             }
90         }
91         int[] lines = new int[index];
92         System.arraycopy(tmp, 0, lines, 0, lines.length);
93         return lines;
94     }
95     
96     /** Returns number of errors.
97      * @return */

98     protected int getBugCount() {
99         return getErrLines().length;
100     }
101 }
Popular Tags