001: /*
002: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/publishing/markups/TKMarkupTree.java,v 1.6 2001/06/11 09:14:10 alex Exp $
003: *
004: */
005: package com.teamkonzept.publishing.markups;
006:
007: import java.util.*;
008:
009: import com.teamkonzept.lib.*;
010: import org.apache.log4j.Category;
011:
012: public class TKMarkupTree extends TKMarkupItemList {
013: private static final Category cat = Category
014: .getInstance(TKMarkupTree.class);
015:
016: public void dump(TKVector dump, int level) {
017:
018: if (dump == null)
019: return;
020:
021: Enumeration e = elements();
022: while (e.hasMoreElements()) {
023:
024: Object elem = e.nextElement();
025: if (elem == null)
026: continue;
027:
028: if (elem instanceof String)
029: dump.addElement(new TKMarkupDumpRecord(level, "String",
030: elem.toString()));
031:
032: else if (elem instanceof TKMarkupNode)
033: ((TKMarkupNode) elem).dump(dump, level);
034: else if (elem instanceof TKMarkupTree)
035: ((TKMarkupTree) elem).dump(dump, level);
036: else if (elem instanceof TKMarkup) {
037:
038: TKMarkup markup = (TKMarkup) elem;
039: dump.addElement(new TKMarkupDumpRecord(level, "Markup",
040: markup.toDisplay()));
041: }
042: }
043: }
044:
045: public String expand() {
046:
047: StringBuffer buffer = new StringBuffer();
048:
049: Enumeration e = elements();
050: while (e.hasMoreElements()) {
051:
052: Object elem = e.nextElement();
053: if (elem == null)
054: continue;
055:
056: if (elem instanceof String)
057: buffer.append(elem);
058: else if (elem instanceof TKMarkupNode)
059: buffer.append(((TKMarkupNode) elem).expand());
060: else if (elem instanceof TKMarkupTree)
061: buffer.append(((TKMarkupTree) elem).expand());
062: else if (elem instanceof TKMarkup)
063: buffer.append(((TKMarkup) elem).expand());
064: }
065:
066: return new String(buffer);
067: }
068:
069: public String markupByName2tmpl(TKMarkupStatics statics, String name) {
070:
071: if (name == null)
072: return " ";
073:
074: TKDefinedMarkup markup;
075:
076: try {
077: markup = new TKDefinedMarkup(name, new TKHashtable(), -1);
078: } catch (Exception ex) {
079:
080: cat.error("*** markupByName2tmpl: ", ex);
081: return " ";
082: }
083:
084: try {
085: markup.complete();
086: } catch (Exception ex) {
087:
088: cat.error("*** markupByName2tmpl: ", ex);
089: }
090:
091: return markup.convert2Tmpl();
092: }
093:
094: public String doCrControl(String text) {
095:
096: TKMarkupStatics statics = TKMarkupStatics.setup();
097: boolean doCrConversion = TKMarkupDefinition
098: .doCrConversion(statics);
099: String crMarkup = TKMarkupDefinition.crMarkup(statics);
100: String crcrMarkup = TKMarkupDefinition.crcrMarkup(statics);
101:
102: if (!doCrConversion || (text == null))
103: return text;
104:
105: StringBuffer digest = new StringBuffer();
106: int pos = 0;
107:
108: while (pos < text.length()) {
109:
110: char chr = text.charAt(pos);
111:
112: if ((chr != '\n') && (chr != '\r'))
113: digest.append(chr);
114:
115: else {
116: if (pos + 1 < text.length())
117: if ((chr == '\n') && (text.charAt(pos + 1) == '\r'))
118: pos++;
119: else if ((chr == '\r')
120: && (text.charAt(pos + 1) == '\n'))
121: pos++;
122:
123: if ((pos + 1 < text.length())
124: && ((text.charAt(pos + 1) == '\n') || (text
125: .charAt(pos + 1) == '\r'))) {
126:
127: pos++;
128: chr = text.charAt(pos);
129:
130: if (pos + 1 < text.length())
131: if ((chr == '\n')
132: && (text.charAt(pos + 1) == '\r'))
133: pos++;
134: else if ((chr == '\r')
135: && (text.charAt(pos + 1) == '\n'))
136: pos++;
137:
138: digest
139: .append(markupByName2tmpl(statics,
140: crcrMarkup));
141:
142: } else
143: digest.append(markupByName2tmpl(statics, crMarkup));
144: }
145:
146: pos++;
147: }
148:
149: return new String(digest);
150: }
151:
152: public String convert2Tmpl() {
153:
154: StringBuffer buffer = new StringBuffer();
155:
156: Enumeration e = elements();
157: while (e.hasMoreElements()) {
158:
159: Object elem = e.nextElement();
160: if (elem == null)
161: continue;
162:
163: if (elem instanceof String)
164: buffer.append(doCrControl(TKMarkupParser
165: .doEscapes((String) elem)));
166: else if (elem instanceof TKMarkupNode)
167: buffer.append(((TKMarkupNode) elem).convert2Tmpl());
168: else if (elem instanceof TKMarkupTree)
169: buffer.append(((TKMarkupTree) elem).convert2Tmpl());
170: else if (elem instanceof TKMarkup) {
171:
172: TKMarkup markup = (TKMarkup) elem;
173: buffer.append(markup.convert2Tmpl());
174:
175: if (elem instanceof TKDefinedMarkup) {
176:
177: if (!((TKDefinedMarkup) elem).isAtom)
178: buffer.append("</TK_TAG:").append(markup.name)
179: .append('>');
180:
181: } else
182: buffer.append("</").append(markup.name).append('>');
183: }
184: }
185:
186: return new String(buffer);
187: }
188:
189: public String convert2Xml() {
190:
191: StringBuffer buffer = new StringBuffer();
192:
193: Enumeration e = elements();
194: while (e.hasMoreElements()) {
195:
196: Object elem = e.nextElement();
197: if (elem == null)
198: continue;
199:
200: if (elem instanceof String)
201: buffer.append(doCrControl(elem.toString()));
202: else if (elem instanceof TKMarkupNode)
203: buffer.append(((TKMarkupNode) elem).convert2Xml());
204: else if (elem instanceof TKMarkupTree)
205: buffer.append(((TKMarkupTree) elem).convert2Xml());
206: else if (elem instanceof TKMarkup) {
207:
208: TKMarkup markup = (TKMarkup) elem;
209: buffer.append(markup.convert2Xml());
210:
211: if (!(elem instanceof TKDefinedMarkup)
212: || !((TKDefinedMarkup) elem).isAtom)
213: buffer.append("</").append(markup.name).append('>');
214: }
215: }
216:
217: return new String(buffer);
218: }
219: }
|