001 /****************************************************************************** 002 * Copyright (C) MActor Developers. 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 ******************************************************************************/ 020 package org.mactor.framework.spec; 021 022 public class TestDataBundleHtmlParser { 023 /* 024 * List<TestData> dataList = new LinkedList<TestData>(); public void 025 * add(TestData data) { dataList.add(data); } public void insertTable(int 026 * index, TestData data) { dataList.add(index, data); } public void 027 * removeTable(int index) { dataList.remove(index); } public void 028 * removeTable(TestData table) { dataList.remove(table); } public static 029 * TestDataBundleHtmlParser parseFile(File file) throws MactorException { 030 * try { HtmlFileParserCallback parserCallback = new 031 * HtmlFileParserCallback(); Reader reader = new FileReader(file); new 032 * ParserDelegator().parse(reader, parserCallback, true); return 033 * parserCallback.bundle; } catch (Exception e) { throw new 034 * MactorException("Failed to parse the file '" + file.getAbsolutePath() + 035 * "'. Error: " + e, e); } } public static TestDataBundleHtmlParser 036 * parseString(String content) throws MactorException { try { 037 * HtmlFileParserCallback parserCallback = new HtmlFileParserCallback(); 038 * Reader reader = new StringReader(content); new 039 * ParserDelegator().parse(reader, parserCallback, true); return 040 * parserCallback.bundle; } catch (Exception e) { throw new 041 * MactorException("Failed to parse the string. Error: " + e, e); } } 042 * private static class HtmlFileParserCallback extends 043 * HTMLEditorKit.ParserCallback { TestDataBundleHtmlParser bundle = new 044 * TestDataBundleHtmlParser(); TestData data = null; List<String> row = 045 * null; boolean inCell = false; int rowIndex = 0; String cellValue = null; 046 * public void handleText(char[] data, int pos) { if (inCell) { cellValue = 047 * new String(data); } } public void handleStartTag(Tag t, 048 * MutableAttributeSet a, int pos) { if (HTML.Tag.TABLE.equals(t)) { data = 049 * new TestData(); rowIndex = -1; } else if (HTML.Tag.TR.equals(t)) { 050 * rowIndex++; if (rowIndex > 1) row = new LinkedList<String>(); } else if 051 * (HTML.Tag.TD.equals(t)) { inCell = true; } } public void handleEndTag(Tag 052 * t, int pos) { if (HTML.Tag.TABLE.equals(t)) { if (data != null) { 053 * bundle.add(data); data = null; } } else if (HTML.Tag.TR.equals(t)) { if 054 * (row != null && data != null) { data.addRow(row); row = null; } } else if 055 * (HTML.Tag.TD.equals(t)) { if (cellValue != null) { if (rowIndex == 0) { 056 * data.setTestSpecPath(cellValue); } else if (rowIndex == 1 && data != 057 * null) { data.addColumn(cellValue); } else if (data != null && row != 058 * null) { row.add(cellValue); } } cellValue = null; inCell = false; } } } 059 * public void write(Writer out) throws IOException { for (TestData data : 060 * dataList) { out.write("<html><body>"); data.write(out); out.write("</body></html>"); } } 061 * @Override public String toString() { try { StringWriter sw = new 062 * StringWriter(); write(sw); return sw.toString(); } catch (IOException e) { 063 * throw new RuntimeException(e); // unexpected } } public List<TestData> 064 * getDataList() { return dataList; } 065 * 066 */ 067 }