001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.macro.table;
025:
026: import java.util.StringTokenizer;
027:
028: /**
029: * Built a table from a string
030: *
031: * @author stephan
032: * @version $Id: TableBuilder.java 11567 2006-07-06 13:07:26Z andrew@caret.cam.ac.uk $
033: */
034:
035: public class TableBuilder {
036: private static final String BAR = "|";
037: private static final String NL = "\n";
038: private static final String OPEN_LINK = "[";
039: private static final String CLOSE_LINK = "]";
040: private static final String TOKENS_STRING = BAR + NL + OPEN_LINK
041: + CLOSE_LINK;
042:
043: private Table table = new Table();
044: private StringTokenizer tokenizer;
045: private String[] token;
046:
047: private TableBuilder(String content) {
048: tokenizer = new StringTokenizer(content, TOKENS_STRING, true);
049:
050: token = new String[5];
051: for (int i = 0; i < token.length; i++) {
052: token[i] = tokenizer.hasMoreTokens() ? tokenizer
053: .nextToken() : null;
054: }
055:
056: }
057:
058: public static Table build(String content) {
059: TableBuilder builder = new TableBuilder(content);
060: builder.build();
061: return builder.getTable();
062: }
063:
064: public void build() {
065: String lastToken = null;
066: while (token[0] != null) {
067: if ("\n".equals(token[0])) {
068: if (null != lastToken) {
069: table.newRow();
070: }
071:
072: } else if ("|".equals(token[0])) {
073: table.newCell();
074: } else if ("[".equals(token[0])) {
075: if (isText(token[1]) && isBar(token[2])
076: && isText(token[3]) && isCloseLink(token[4])) {
077: for (int i = 0; i < 4; i++) {
078: table.addText(token[0]);
079: step();
080: }
081: table.addText(token[0]);
082: } else {
083: table.addText(token[0]);
084: }
085: } else {
086: table.addText(token[0]);
087: //table.addCell(token);
088: }
089: lastToken = token[0];
090: step();
091: }
092: if (!"\n".equals(lastToken)) {
093: table.newRow();
094: }
095:
096: }
097:
098: private void step() {
099: for (int i = 1; i < token.length; i++) {
100: token[i - 1] = token[i];
101: }
102: token[token.length - 1] = tokenizer.hasMoreTokens() ? tokenizer
103: .nextToken() : null;
104: }
105:
106: private static boolean isBar(String token) {
107: return BAR.equals(token);
108: }
109:
110: private static boolean isOpenLink(String token) {
111: return OPEN_LINK.equals(token);
112: }
113:
114: private static boolean isCloseLink(String token) {
115: return CLOSE_LINK.equals(token);
116: }
117:
118: private static boolean isNewLine(String token) {
119: return NL.equals(token);
120: }
121:
122: private static boolean isText(String token) {
123: for (int i = 0; i < TOKENS_STRING.length(); i++) {
124: if (("" + TOKENS_STRING.charAt(i)).equals(token)) {
125: return false;
126: }
127: }
128: return true;
129: }
130:
131: public Table getTable() {
132: return table;
133: }
134:
135: }
|