001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.framework;
020:
021: import java.io.FileReader;
022: import java.io.Reader;
023: import java.util.LinkedList;
024: import java.util.List;
025: import javax.swing.text.MutableAttributeSet;
026: import javax.swing.text.html.HTML;
027: import javax.swing.text.html.HTMLEditorKit;
028: import javax.swing.text.html.HTML.Tag;
029: import javax.swing.text.html.parser.ParserDelegator;
030:
031: /**
032: * A simple HTML parser for parsing MActor test-data files
033: *
034: * @author Lars Ivar Almli
035: */
036: public class HtmlTestSuiteParser {
037: private String testSpecPath;
038: private TestData data = new TestData();
039:
040: public static List<HtmlTestSuiteParser> parseFile(String filePath)
041: throws MactorException {
042: try {
043: HtmlFileParserCallback parserCallback = new HtmlFileParserCallback();
044: Reader reader = new FileReader(filePath);
045: new ParserDelegator().parse(reader, parserCallback, true);
046: return parserCallback.dataList;
047: } catch (Exception e) {
048: throw new MactorException("Failed to parse the file '"
049: + filePath + "'. Error: " + e, e);
050: }
051: }
052:
053: private static class HtmlFileParserCallback extends
054: HTMLEditorKit.ParserCallback {
055: List<HtmlTestSuiteParser> dataList = new LinkedList<HtmlTestSuiteParser>();
056: HtmlTestSuiteParser htmlData = null;
057: List<String> row = null;
058: boolean inCell = false;
059: int rowIndex = 0;
060: String cellValue = null;
061:
062: public void handleText(char[] data, int pos) {
063: if (inCell) {
064: cellValue = new String(data);
065: }
066: }
067:
068: public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
069: if (HTML.Tag.TABLE.equals(t)) {
070: htmlData = new HtmlTestSuiteParser();
071: rowIndex = -1;
072: } else if (HTML.Tag.TR.equals(t)) {
073: rowIndex++;
074: if (rowIndex > 1)
075: row = new LinkedList<String>();
076: } else if (HTML.Tag.TD.equals(t)) {
077: inCell = true;
078: }
079: }
080:
081: public void handleEndTag(Tag t, int pos) {
082: if (HTML.Tag.TABLE.equals(t)) {
083: if (htmlData != null) {
084: dataList.add(htmlData);
085: htmlData = null;
086: }
087: } else if (HTML.Tag.TR.equals(t)) {
088: if (row != null && htmlData != null) {
089: htmlData.data.addRow(row);
090: row = null;
091: }
092: } else if (HTML.Tag.TD.equals(t)) {
093: if (cellValue != null) {
094: if (rowIndex == 0) {
095: htmlData.testSpecPath = cellValue;
096: } else if (rowIndex == 1 && htmlData != null) {
097: htmlData.data.addColumn(cellValue);
098: } else if (htmlData != null && row != null) {
099: row.add(cellValue);
100: }
101: }
102: cellValue = null;
103: inCell = false;
104: }
105: }
106: }
107:
108: public String getTestSpecPath() {
109: return testSpecPath;
110: }
111:
112: public void setTestSpecPath(String testSpecPath) {
113: this .testSpecPath = testSpecPath;
114: }
115:
116: public String toString() {
117: return testSpecPath + "\n" + data;
118: }
119:
120: public TestData getData() {
121: return data;
122: }
123: }
|