001: // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002: package com.canoo.webtest.steps;
003:
004: import com.gargoylesoftware.htmlunit.html.HTMLParserListener;
005:
006: import java.net.URL;
007: import java.util.ArrayList;
008: import java.util.List;
009:
010: /**
011: * Represents a message generated by an html parser.
012: * @author Marc Guillemot
013: */
014: public class HtmlParserMessage implements Comparable {
015: /**
016: * Safe enum to represent the type of a message.
017: */
018: public static final class Type implements Comparable {
019: public static final String ERROR_NAME = "error";
020: public static final String WARNING_NAME = "warning";
021: public static final Type ERROR = new Type(ERROR_NAME);
022: public static final Type WARNING = new Type(WARNING_NAME);
023: private String fName;
024:
025: /**
026: * Private constructor to prohibit external instantiations
027: * @param str
028: */
029: private Type(final String str) {
030: fName = str;
031: }
032:
033: /**
034: * Compares with following rule: error > warning
035: * (no need to override equals(): in this case it is equivalent to ==)
036: *
037: * @throws ClassCastException if the argument is not a {@link HtmlParserMessage.Type}
038: */
039: public int compareTo(final Object o) {
040: final Type other = (Type) o;
041: if (getName().equals(other.getName()))
042: return 0;
043: else
044: return isError() ? 1 : -1;
045: }
046:
047: /**
048: * Gets the name of the type
049: */
050: public String getName() {
051: return fName;
052: }
053:
054: public boolean isError() {
055: return ERROR_NAME.equals(getName());
056: }
057:
058: public boolean isWarning() {
059: return WARNING_NAME.equals(getName());
060: }
061:
062: /**
063: * Gets the name.
064: */
065: public String toString() {
066: return getName();
067: }
068: }
069:
070: /**
071: * Listener collecting the messages from parser.
072: */
073: public static class MessageCollector implements HTMLParserListener {
074: private List fMessages = new ArrayList();
075:
076: public void error(final String message, final URL url,
077: final int line, final int column, final String key) {
078: fMessages.add(new HtmlParserMessage(Type.ERROR, url,
079: message, line, column));
080: }
081:
082: public void warning(final String message, final URL url,
083: final int line, final int column, final String key) {
084: fMessages.add(new HtmlParserMessage(Type.WARNING, url,
085: message, line, column));
086: }
087:
088: /**
089: * Gets all messages received since last call to this method.
090: * @return a list of {@link HtmlParserMessage}
091: */
092: public List popAll() {
093: final List l = fMessages;
094: fMessages = new ArrayList();
095: return l;
096: }
097: }
098:
099: private int fColumn;
100: private int fLine;
101: private String fMessage;
102: private Type fType;
103: private URL fUrl;
104: private volatile int fHash;
105:
106: public HtmlParserMessage(final Type type, final URL url,
107: final String strMessage, final int iLine, final int iColumn) {
108: fType = type;
109: fUrl = url;
110: fMessage = strMessage;
111: fLine = iLine;
112: fColumn = iColumn;
113: }
114:
115: /**
116: * Compare to an other HtmlParserMessage according to (url, line, col, type, msg)
117: * @see java.lang.Comparable#compareTo(java.lang.Object)
118: */
119: public int compareTo(final Object o) {
120: final HtmlParserMessage other = (HtmlParserMessage) o;
121:
122: // first compare urls
123: int iRep = getURL().toString().compareTo(
124: other.getURL().toString());
125: if (iRep != 0)
126: return iRep;
127:
128: // then line in file
129: iRep = getLine() - other.getLine();
130: if (iRep != 0)
131: return iRep;
132:
133: // then column in line
134: iRep = getColumn() - other.getColumn();
135: if (iRep != 0)
136: return iRep;
137:
138: // error or warning
139: iRep = getType().compareTo(other.getType());
140: if (iRep != 0)
141: return iRep;
142:
143: // and at least the message
144: return getMessage().compareTo(other.getMessage());
145: }
146:
147: public int hashCode() {
148: if (fHash == 0)
149: fHash = calculateHash();
150: return fHash;
151: }
152:
153: private int calculateHash() {
154: // aggregate of primes multiplied by hashes of components
155: final int h1 = 3 * new Integer(fColumn).hashCode();
156: final int h2 = 5 * new Integer(fLine).hashCode();
157: final int h3 = 7 * fMessage.hashCode();
158: final int h4 = 11 * fType.hashCode();
159: final int h5 = 13 * fUrl.hashCode();
160: return h1 + h2 + h3 + h4 + h5;
161: }
162:
163: /**
164: * Test for equality with an other HtmlParserMessage according to (url, line, col, type, msg)
165: * @see java.lang.Object#equals(java.lang.Object)
166: */
167: public boolean equals(final Object o) {
168: return compareTo(o) == 0;
169: }
170:
171: /**
172: * Gets the column in the source file for which the message has been generated
173: */
174: public int getColumn() {
175: return fColumn;
176: }
177:
178: /**
179: * Gets the line in the source file for which the message has been generated
180: */
181: public int getLine() {
182: return fLine;
183: }
184:
185: /**
186: * Gets the description of the message
187: */
188: public String getMessage() {
189: return fMessage;
190: }
191:
192: /**
193: * Gets the type of the message determining its severity
194: */
195: public Type getType() {
196: return fType;
197: }
198:
199: /**
200: * Gets the url of the file which parsing caused this message.
201: */
202: public URL getURL() {
203: return fUrl;
204: }
205: }
|