001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.function;
009:
010: //base classes
011: import java.io.IOException;
012: import java.util.ArrayList;
013: import java.util.HashMap;
014:
015: //project specific classes
016: import org.jfolder.common.UnexpectedSystemException;
017: import org.jfolder.common.tagging.AppraiseConceptTagContext;
018: import org.jfolder.common.tagging.BaseAppraiseConceptTagContext;
019: import org.jfolder.common.tagging.ConceptTag;
020: import org.jfolder.common.tagging.ConceptTagCharacteristic;
021: import org.jfolder.common.tagging.ConceptTagCharacteristicHolder;
022: import org.jfolder.common.tagging.ConceptTagConstraint;
023: import org.jfolder.common.tagging.ConceptTagConstraintHolder;
024: import org.jfolder.common.tagging.ConceptTagPreferences;
025: import org.jfolder.common.tagging.RootConceptTagHolder;
026: import org.jfolder.common.utils.misc.MiscHelper;
027: import org.jfolder.common.utils.xml.XMLHelper;
028:
029: //other classes
030:
031: public class WebPageTagInstanceViewContext extends
032: BaseAppraiseConceptTagContext {
033:
034: //
035: private ConceptTagPreferences parentCtp = null;
036: private WebPageTagInstanceViewContextInfo wptivci = null;
037:
038: private WebPageTagInstanceViewContext(ConceptTagPreferences inCtp)
039: throws IOException {
040: //
041: super (null, null);
042: //
043: this .parentCtp = inCtp;
044: }
045:
046: //
047: public final static WebPageTagInstanceViewContext newInstance(
048: ConceptTagPreferences inCtp) throws IOException {
049:
050: return new WebPageTagInstanceViewContext(inCtp);
051: }
052:
053: //
054: //
055: //
056: //
057: //
058: public WebPageTagInstanceViewContextInfo getInfoAsHtml() {
059: //String inDefaultConstraint) {
060:
061: WebPageTagInstanceViewContextInfo outValue = this .wptivci;
062:
063: outValue.markAsHtml();
064: //if (inDefaultConstraint != null) {
065: // outValue.setDefaultConstraint(inDefaultConstraint);
066: //}
067:
068: return outValue;
069: }
070:
071: //
072: public WebPageTagInstanceViewContextInfo getInfoAsNonHtml() {
073: //String inDefaultConstraint) {
074:
075: WebPageTagInstanceViewContextInfo outValue = this .wptivci;
076:
077: outValue.markAsNonHtml();
078: //if (inDefaultConstraint != null) {
079: // outValue.setDefaultConstraint(inDefaultConstraint);
080: //}
081:
082: return outValue;
083: }
084:
085: //
086: public WebPageTagInstanceViewContextInfo getInfoAsIterativeNonHtml() {
087: //String inDefaultConstraint) {
088:
089: WebPageTagInstanceViewContextInfo outValue = this .wptivci;
090:
091: outValue.markAsIterativeNonHtml();
092: //if (inDefaultConstraint != null) {
093: // outValue.setDefaultConstraint(inDefaultConstraint);
094: //}
095:
096: return outValue;
097: }
098:
099: //
100: //
101: public ConceptTagPreferences getPreferences() {
102: return this .parentCtp;
103: }
104:
105: //
106: //
107: //
108: //
109: public final static WebPageTagInstanceViewContextInfo displayRootView(
110: RootConceptTagHolder inRcth,
111: WebPageTagInstanceViewContext inWptivc) {
112: //
113: WebPageTagInstanceViewContextInfo outValue = null;
114:
115: //
116: WebPageTagPreferences wptp = new WebPageTagPreferences();
117: //String idHandle[] = new String[]{wptp.getRootConstraint(), "0"};
118: outValue = WebPageTagInstanceViewContextInfo.createRootInfo();
119: // idHandle);
120: //
121: ConceptTagCharacteristic localCtchar = inRcth
122: .getFirstCharacteristic();
123: displayView(localCtchar, outValue, inWptivc);
124:
125: return outValue;
126: }
127:
128: //
129: //
130: //
131: public final static void displayView(
132: ConceptTagCharacteristic inCtchar,
133: WebPageTagInstanceViewContextInfo inWptivci,
134: WebPageTagInstanceViewContext inWptivc) {
135: //
136: displayView(inCtchar, inWptivci, inWptivc, "???");
137: }
138:
139: public final static void displayView(
140: ConceptTagCharacteristic inCtchar,
141: WebPageTagInstanceViewContextInfo inWptivci,
142: WebPageTagInstanceViewContext inWptivc, String inZeroMessage) {
143: //
144: displayView(inCtchar, inWptivci, inWptivc, 0, inCtchar
145: .getValueLength(), inZeroMessage);
146: }
147:
148: public final static void displayView(
149: ConceptTagCharacteristic inCtchar,
150: WebPageTagInstanceViewContextInfo inWptivci,
151: WebPageTagInstanceViewContext inWptivc, int inStartIndex,
152: int inEndIndex) {
153: //
154: displayView(inCtchar, inWptivci, inWptivc, inStartIndex,
155: inEndIndex, "???");
156: }
157:
158: public final static void displayView(
159: ConceptTagCharacteristic inCtchar,
160: WebPageTagInstanceViewContextInfo inWptivci,
161: WebPageTagInstanceViewContext inWptivc, int inStartIndex,
162: int inEndIndex, String inZeroMessage) {
163:
164: //
165: //
166:
167: //ArrayList idHandle = getPartialIdHandle(inCtchar);//null;
168: //if (inWptivci.getIdHandle().length > 0) {
169: // //
170: // idHandle = new ArrayList();
171: // //
172: // String localIdHandle[] = inWptivci.getIdHandle();
173: // for (int i = 0; i < localIdHandle.length; i++) {
174: // idHandle.add(localIdHandle[i]);
175: // }
176: //}
177: //else {
178: // idHandle = getPartialIdHandle(inCtchar);
179: //}
180:
181: for (int i = inStartIndex; i < inEndIndex; i++) {
182: //
183: //idHandle.add(i + "");
184: //
185: //
186: WebPageTagInstanceViewContextInfo currentWptivci = null;
187: if (inWptivci.isMarkedAsHtml()) {
188: currentWptivci = inWptivci.createHtmlInfo(inCtchar, i,
189: null);
190: } else if (inWptivci.isMarkedAsNonHtml()) {
191: currentWptivci = inWptivci.createNonHtmlInfo(inCtchar,
192: i, null);
193: } else {
194: throw UnexpectedSystemException.unknownState();
195: }
196: //
197: //currentWptivci.setIdHandle(convertIdHandleToArray(idHandle));
198: //
199: //
200: WebPageTagInstanceViewContextInfo parentWptivci = inWptivc
201: .pushInfo(currentWptivci);
202: //
203: //
204: if (inCtchar.isStatic(i, inWptivc)) {
205: //
206: if (inWptivci.isMarkedAsHtml()) {
207: //
208: //currentWptivci = inWptivc.getInfoAsHtml(null);
209: String text = inCtchar
210: .getValueAsString(i, inWptivc);
211: if (text.length() == 0) {
212: text = "(Empty String)";
213: }
214: //text = convertTextToHtml(text);
215: currentWptivci.setHtmlContent(text, false);
216: } else if (inWptivci.isMarkedAsNonHtml()) {
217: //
218: //currentWptivci = inWptivc.getInfoAsNonHtml(null);
219: String text = inCtchar
220: .getValueAsString(i, inWptivc);
221: if (text.length() == 0) {
222: text = "(Empty String)";
223: }
224: currentWptivci.setNonHtmlContent(text);
225: } else {
226: throw UnexpectedSystemException.unknownState();
227: }
228: } else if (inCtchar.isHolderWithConceptTag(i, inWptivc)) {
229: //
230: ConceptTagCharacteristicHolder nextCtcharh = inCtchar
231: .getValueAsHolder(i, inWptivc);
232: ConceptTag nextCt = nextCtcharh.getConceptTag();
233: //
234: //
235: nextCt.appraise(inWptivc);
236: //
237: } else if (inCtchar.isHolderWithoutConceptTag(i, inWptivc)) {
238: //do nothing, handled by parent ConceptTag
239: } else {
240: throw UnexpectedSystemException.unknownState();
241: }
242: //
243: //
244: inWptivc.popInfo(parentWptivci);
245: //
246: //
247: //
248: //idHandle.remove(idHandle.size() - 1);
249: //
250: }
251: //
252: if (inCtchar.getValueLength() == 0 && inZeroMessage != null) {
253: //
254: if (inWptivci.isMarkedAsHtml()) {
255: //
256: WebPageTagInstanceViewContextInfo currentWptivci = inWptivci
257: .createHtmlInfo(inCtchar.getConstraintName());
258: //
259: currentWptivci.setHtmlContent(inZeroMessage, false);
260: } else if (inWptivci.isMarkedAsNonHtml()) {
261: //
262: WebPageTagInstanceViewContextInfo currentWptivci = inWptivci
263: .createNonHtmlInfo(inCtchar.getConstraintName());
264: //
265: currentWptivci.setNonHtmlContent(inZeroMessage);
266: } else {
267: throw UnexpectedSystemException.unknownState();
268: }
269: }
270: }
271:
272: private final static ArrayList getPartialIdHandle(
273: ConceptTagCharacteristic inCtchar) {
274: //
275: ArrayList outValue = new ArrayList();
276:
277: ConceptTagCharacteristicHolder localCtcharh = null;
278: while ((localCtcharh == null || !localCtcharh
279: .isConceptTagPresent())
280: && !(inCtchar == null && localCtcharh == null)) {
281: //
282: if (inCtchar != null) {
283: outValue.add(0, inCtchar.getConstraintName());
284: localCtcharh = inCtchar.getParentCharacteristicHolder();
285: inCtchar = null;
286: } else if (localCtcharh != null) {
287: //
288: //don't include root ctcharh's index
289: if (localCtcharh.getParentCharacteristic() != null) {
290: outValue.add(0, localCtcharh
291: .getParentCharacteristicIndex()
292: + "");
293: }
294: inCtchar = localCtcharh.getParentCharacteristic();
295: localCtcharh = null;
296: } else {
297: throw UnexpectedSystemException.unknownState();
298: }
299: }
300:
301: //MiscHelper.println("WebPagTagInstViewCon parHandle = " + outValue);
302:
303: return outValue;
304: }
305:
306: private final static String[] convertIdHandleToArray(
307: ArrayList inIdHandle) {
308:
309: String outValue[] = new String[inIdHandle.size()];
310:
311: for (int i = 0; i < inIdHandle.size(); i++) {
312: outValue[i] = ((String) inIdHandle.get(i));
313: }
314:
315: return outValue;
316: }
317:
318: //
319: //
320: //
321: private WebPageTagInstanceViewContextInfo pushInfo(
322: WebPageTagInstanceViewContextInfo inWptivci) {
323: //
324:
325: WebPageTagInstanceViewContextInfo outValue = this .wptivci;
326:
327: this .wptivci = inWptivci;
328:
329: return outValue;
330: }
331:
332: //
333: private void popInfo(
334: WebPageTagInstanceViewContextInfo inParentWptivci) {
335: //
336: this .wptivci = inParentWptivci;
337: }
338:
339: //
340: //
341: //
342: //
343: //
344: //
345: //
346: //
347: //
348: //
349: //
350: public final static String convertTextToHtml(String inValue) {
351:
352: String outValue = inValue;
353:
354: outValue = XMLHelper.fromStringToCData(outValue);
355: outValue = XMLHelper.fromStringToHTML(outValue);
356: outValue = substituteSpecialElements(outValue);
357:
358: return outValue;
359: }
360:
361: public final static String convertTextToStudioHtml(String inValue) {
362:
363: String outValue = inValue;
364:
365: //if (inValue == null) {
366: // MiscHelper.writeMessage(
367: // WebPageTagInstanceViewContext.class,
368: // "Null Found");
369: //}
370:
371: outValue = XMLHelper.fromStringToCData(outValue);
372: outValue = XMLHelper.fromStringToHTML(outValue);
373: outValue = substituteStudioSpecialElements(outValue);
374:
375: return outValue;
376: }
377:
378: //
379: //private final static String substituteSpecialCharacters(String inData) {
380: //
381: // String outValue = null;
382: //
383: // outValue = inData;
384: //
385: // outValue = outValue.replaceAll(" ", XMLHelper.NBSP_CHAR + "");
386: //
387: // return outValue;
388: //}
389: //
390: private final static String substituteSpecialElements(String inData) {
391:
392: String outValue = null;
393:
394: outValue = inData;
395: //make sure Windows newline doesn't show up twice
396: outValue = outValue.replaceAll("\\r\\n", "\n");
397: outValue = outValue.replaceAll("\r", "<br/>");
398: outValue = outValue.replaceAll("\n", "<br/>");
399:
400: return outValue;
401: }
402:
403: private final static String substituteStudioSpecialElements(
404: String inData) {
405:
406: String outValue = null;
407:
408: //
409: //carriage return symbol should be ↵
410: String replaceStr = "<span style=\"color: green\"><<</span><br/>";
411:
412: outValue = inData;
413: //make sure Windows newline doesn't show up twice
414: outValue = outValue.replaceAll("\\r\\n", "\n");
415: outValue = outValue.replaceAll("\r", replaceStr);
416: outValue = outValue.replaceAll("\n", replaceStr);
417:
418: return outValue;
419: }
420: }
|