[hmath-commits] org.hmath.server/WEB-INF/src/org/hartmath/server/macro/table STableBuilder.java,NONE
Status: Pre-Alpha
Brought to you by:
jsurfer
|
From: Klaus H. <js...@us...> - 2004-04-02 18:29:25
|
Update of /cvsroot/hmath/org.hmath.server/WEB-INF/src/org/hartmath/server/macro/table In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10394/WEB-INF/src/org/hartmath/server/macro/table Added Files: STableBuilder.java Log Message: misc changes --- NEW FILE: STableBuilder.java --- /* * This file is part of "SnipSnap Radeox Rendering Engine". * * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel All Rights Reserved. * * Please visit http://radeox.org/ for updates and contact. * * --LICENSE NOTICE-- This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser * General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --LICENSE NOTICE-- */ package org.hartmath.server.macro.table; import java.util.ArrayList; import org.radeox.api.engine.context.RenderContext; /** * Built a table from a string * * @author stephan * @version $Id: STableBuilder.java,v 1.1 2004/04/02 18:17:10 jsurfer Exp $ */ public class STableBuilder { // public static Table build(String content, RenderContext context) { // Table table = new Table(context); // StringTokenizer tokenizer = new StringTokenizer(content, "|\n", true); // String lastToken = null; // while (tokenizer.hasMoreTokens()) { // String token = tokenizer.nextToken(); // if ("\n".equals(token)) { // // Handles "\n" - "|\n" // if (null == lastToken || "|".equals(lastToken)) { // table.addCell(" "); // } // table.newRow(); // } else if (!"|".equals(token)) { // table.addCell(token); // } else if (null == lastToken || "|".equals(lastToken)) { // // Handles "|" "||" // table.addCell(" "); // } // lastToken = token; // } // return table; // } public static Table build(String content, RenderContext context) { Table table = new Table(context); int index0 = content.indexOf('\n'); int index1 = -1; char currentChar = ' '; char lastChar = ' '; int startPosition = 0; int currentPosition = 0; ArrayList rowList = new ArrayList(); ArrayList columnPositionList = null; if (index0 >= 0) { rowList.add(content.substring(0,index0)); index1 = content.indexOf('\n', index0+1); if (index1 > (index0 + 1)) { char headerLine[] = new char[index1 - index0 - 1]; content.getChars(index0+1, index1, headerLine, 0); columnPositionList = new ArrayList(); boolean whiteSpace = true; try { while (true) { currentChar = headerLine[currentPosition++]; if (Character.isWhitespace(currentChar)) { whiteSpace = true; continue; } if (whiteSpace) { switch (currentChar) { case '=' : case '-' : columnPositionList.add(new Integer(currentPosition - 1)); whiteSpace = false; break; } } } } catch (IndexOutOfBoundsException e) { } columnPositionList.add(new Integer(headerLine.length)); } } if (columnPositionList == null || columnPositionList.size()==0) { return table; } index0 = index1+1; while ((index1 = content.indexOf('\n', index0))>0) { rowList.add(content.substring(index0,index1)); index0 = index1+1; } if (index0<content.length()) { rowList.add(content.substring(index0,content.length())); } for (int i = 0; i < rowList.size(); i++) { generateTable(table, (String)rowList.get(i), columnPositionList); table.newRow(); } // char firstRow[] = new char[index0]; // content.getChars(0, index0, firstRow, 0); // // // char[] source = new char[content.length() - index1 -1]; // content.getChars(index1+1, content.length(), source, 0); // generateTable(table, source, columnPositionList); return table; } private static void generateTable(Table table, String source, ArrayList list) { int j = ((Integer)list.get(0)).intValue(); int len = source.length(); for (int i = 1; i < list.size(); i++) { int k = ((Integer)list.get(i)).intValue(); if (j<len && k<=len) { table.addCell(source.substring(j,k)); } else if (j<len) { table.addCell(source.substring(j,len)); } else { table.addCell(""); } j = k; } } // private static void generateTable(Table table, char[] source, ArrayList list) { // char currentChar = ' '; // char lastChar = ' '; // int startPosition = 0; // int currentPosition = 0; // StringBuffer buffer = new StringBuffer(128); // // StringTokenizer tokenizer = new StringTokenizer(content, "|\n", true); // // String lastToken = null; // try { // while (true) { // currentChar = source[currentPosition++]; // switch (currentChar) { // case '\n' : // // Handles "\n" - "|\n" // if (lastChar == '|') { // table.addCell(" "); // } else { // table.addCell(buffer.toString()); // buffer = new StringBuffer(128); // } // table.newRow(); // break; // case '|' : // if (lastChar == '\\') { // buffer.insert(buffer.length() - 1, currentChar); // } else { // table.addCell(buffer.toString()); // buffer = new StringBuffer(128); // } // break; // case '{' : // if (lastChar == '\\') { // buffer.append(currentChar); // } else { // int tempPosition = currentPosition; // char tempChar = ' '; // try { // lastChar = currentChar; // buffer.append(currentChar); // while ((tempChar = source[tempPosition++]) != '}' || lastChar == '\\') { // buffer.append(tempChar); // lastChar = tempChar; // } // buffer.append(tempChar); // currentPosition = tempPosition; // } catch (IndexOutOfBoundsException e) { // table.addCell(buffer.toString()); // buffer = new StringBuffer(128); // currentPosition = tempPosition; // } // } // break; // default : // buffer.append(currentChar); // } // lastChar = currentChar; // } // } catch (IndexOutOfBoundsException e) { // // } // } } |