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.platforms.servers.base;
009:
010: //base classes
011: import java.io.IOException;
012: import java.io.StringWriter;
013: import java.math.BigDecimal;
014: import java.sql.Connection;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Statement;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.StringTokenizer;
022: import javax.naming.Context;
023: import javax.naming.InitialContext;
024: import javax.naming.NamingException;
025: import javax.rmi.PortableRemoteObject;
026: import javax.sql.DataSource;
027: import org.w3c.dom.Attr;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Node;
031: import org.w3c.dom.NodeList;
032: import org.w3c.dom.Text;
033:
034: //project specific classes
035: import org.jfolder.common.UnexpectedSystemException;
036: import org.jfolder.common.function.UnknownWebPageTag;
037: import org.jfolder.common.tagging.ConceptTag;
038: import org.jfolder.common.tagging.ConceptTagCharacteristic;
039: import org.jfolder.common.tagging.ConceptTagCharacteristicHolder;
040: import org.jfolder.common.tagging.ConceptTagFlagsHelper;
041: import org.jfolder.common.tagging.ConceptTagPreferences;
042: import org.jfolder.common.tagging.ConceptTagSetHolder;
043: import org.jfolder.common.tagging.RootConceptTagHolder;
044: import org.jfolder.common.tagging.StudioConceptTagInstanceInfoContext;
045: import org.jfolder.common.utils.xml.SpecializedXMLWriterHandler;
046: import org.jfolder.common.utils.xml.XMLHelper;
047: import org.jfolder.platforms.stores.base.ApplicationDataSourceResolver;
048: import org.jfolder.project.model.ProjectSystem;
049:
050: //other classes
051:
052: public class SpecializedWebPageWriterHandler extends
053: SpecializedXMLWriterHandler {
054:
055: //
056: private ConceptTagSetHolder ctsh = null;
057: private ConceptTagPreferences ctp = null;
058: //
059: private ArrayList reformatParts = null;
060:
061: private SpecializedWebPageWriterHandler(ConceptTagSetHolder inCtsh,
062: ConceptTagPreferences inCtp) {
063: //
064: super ();
065: //
066: this .ctsh = inCtsh;
067: this .ctp = inCtp;
068: }
069:
070: //
071: //
072: //
073: public final static String reformatWebPage(
074: RootConceptTagHolder inRcth, boolean inReplaceRoot) {
075:
076: try {
077:
078: String outValue = null;
079:
080: //
081: ConceptTagPreferences nextSourceCtp = inRcth
082: .getPreferences();
083: ConceptTagSetHolder nextSourceCtsh = nextSourceCtp
084: .getSetHolder();
085:
086: SpecializedWebPageWriterHandler swpwh = new SpecializedWebPageWriterHandler(
087: nextSourceCtsh, nextSourceCtp);
088:
089: //
090: if (!inReplaceRoot) {
091: reformatSpecialTextForWebPage(inRcth
092: .getFirstChildCharacteristicHolder());
093: } else {
094: reformatSpecialTextForWebPage(inRcth
095: .getRootCharacteristicHolder());
096: }
097: //
098: //
099: //
100: HashMap namespaceMappings = new HashMap();
101: //
102: if (swpwh.ctp.getStaticNamespace() != null) {
103: if (!namespaceMappings.containsKey(swpwh.ctp
104: .getStaticNamespace())) {
105: //
106: XMLHelper.assignNameSpacePrefix(swpwh.ctp
107: .getStaticNamespace(), namespaceMappings);
108: }
109: }
110: //
111: if (swpwh.ctp.getConstraintNamespace() != null) {
112: if (!namespaceMappings.containsKey(swpwh.ctp
113: .getConstraintNamespace())) {
114: //
115: XMLHelper.assignNameSpacePrefix(swpwh.ctp
116: .getConstraintNamespace(),
117: namespaceMappings);
118: }
119: }
120: //
121: Document d = null;
122:
123: if (inReplaceRoot) {
124: d = inRcth.getXMLRepButReplaceRootElement(
125: "http://java.sun.com/JSP/Page", "jsp", "root",
126: new String[] { null },
127: new String[] { "version" },
128: new String[] { null }, new String[] { "1.2" },
129: namespaceMappings);
130: //
131: Element rootEle = d.getDocumentElement();
132: if (nextSourceCtp.getConstraintNamespace() != null) {
133: String conNs = nextSourceCtp
134: .getConstraintNamespace();
135: String conName = nextSourceCtp.getConstraintName();
136: if (rootEle.hasAttributeNS(conNs, conName)) {
137: rootEle.removeAttributeNS(conNs, conName);
138: }
139: } else {
140: String conName = nextSourceCtp.getConstraintName();
141: if (rootEle.hasAttribute(conName)) {
142: rootEle.removeAttribute(conName);
143: }
144: }
145: } else {
146: d = inRcth.getXMLRepresentationAsDocument();
147: }
148: //
149: swpwh.realignUndeployableParts(d, namespaceMappings);
150: //reformatWebPage(
151: // d, d.getDocumentElement(), nextSourceCtp, nextSourceCtsh);
152: //
153: outValue = convertDocToString(d, namespaceMappings);
154:
155: return outValue;
156: } catch (IOException ioe) {
157: throw new UnexpectedSystemException(ioe);
158: }
159: }
160:
161: private void realignUndeployableParts(Document inDoc,
162: HashMap inNamespaces) throws IOException {
163: //
164: realignUndeployableParts(inDoc, inDoc.getDocumentElement(),
165: inNamespaces);
166: }
167:
168: private void realignUndeployableParts(Document inDoc,
169: Element inEle, HashMap inNamespaces) throws IOException {
170: //
171:
172: //
173: //
174: //
175: NodeList eleNl = inEle.getChildNodes();
176: for (int i = 0; i < eleNl.getLength(); i++) {
177: Node nextNode = eleNl.item(i);
178: if (nextNode instanceof Element) {
179: Element nextEle = ((Element) nextNode);
180: //
181: realignUndeployableParts(inDoc, nextEle, inNamespaces);
182: }
183: }
184:
185: //
186: //
187: //
188: //first determine if element is deployable, process only if deployable
189: if (isElementDeployable(inEle)) {
190: Element nextRealignEle = null;
191: while ((nextRealignEle = findUndeployableElement(inEle)) != null) {
192: //
193: this .reformatParts = new ArrayList();
194: StringBuffer sb = new StringBuffer();
195: //
196: XMLHelper.writeDocument(nextRealignEle, sb,
197: inNamespaces, this );
198: //
199: if (sb.length() > 0) {
200: //add final text node
201: appendStaticChild(inDoc, inEle, nextRealignEle, sb
202: .toString(), inNamespaces);
203: }
204: //
205: for (int i = (this .reformatParts.size() - 1); i >= 0; i--) {
206: Object nextPart = this .reformatParts.get(i);
207: if (nextPart instanceof Element) {
208: //
209: Element nextPartEle = ((Element) nextPart);
210: Element nextAdjustPartEle = ((Element) nextPartEle
211: .cloneNode(true));
212: //
213: inEle.insertBefore(nextAdjustPartEle,
214: nextRealignEle.getNextSibling());
215: //
216: copyConstraint(inDoc, nextRealignEle,
217: nextAdjustPartEle, inNamespaces);
218: } else if (nextPart instanceof String) {
219: //
220: String nextPartText = ((String) nextPart);
221: //
222: appendStaticChild(inDoc, inEle, nextRealignEle,
223: nextPartText, inNamespaces);
224: } else {
225: throw UnexpectedSystemException.unknownState();
226: }
227: }
228: //
229: //remove Element
230: inEle.removeChild(nextRealignEle);
231: }
232: }
233: }
234:
235: private void appendStaticChild(Document inDoc, Element inParentEle,
236: Element inChildEle, String inValue, HashMap inNamespaces) {
237: //
238: if (isConstraintPresent(inChildEle)) {
239: Element childStaticEle = null;
240: if (this .ctp.getStaticNamespace() != null) {
241: //
242: String statNs = this .ctp.getStaticNamespace();
243: String statNsPrefix = ((String) inNamespaces
244: .get(statNs));
245: String statName = this .ctp.getStaticName();
246: //
247: childStaticEle = inDoc.createElementNS(statNs,
248: (statNsPrefix + ":" + statName));
249: } else {
250: //
251: String statName = this .ctp.getStaticName();
252: //
253: childStaticEle = inDoc.createElement(statName);
254: }
255: //
256: inParentEle.insertBefore(childStaticEle, inChildEle
257: .getNextSibling());
258: //
259: Text textValue = inDoc.createTextNode(inValue);
260: childStaticEle.appendChild(textValue);
261: //
262: copyConstraint(inDoc, inChildEle, childStaticEle,
263: inNamespaces);
264: } else {
265: Text textValue = inDoc.createTextNode(inValue);
266: inParentEle.insertBefore(textValue, inChildEle
267: .getNextSibling());
268: }
269:
270: }
271:
272: //private void appendStaticChildOrig(
273: // Document inDoc, Element inParentEle, Element inChildEle,
274: // String inValue, HashMap inNamespaces) {
275: // //
276: // Element childStaticEle = null;
277: // if (this.ctp.getStaticNamespace() != null) {
278: // //
279: // String statNs = this.ctp.getStaticNamespace();
280: // String statNsPrefix = ((String)inNamespaces.get(statNs));
281: // String statName = this.ctp.getStaticName();
282: // //
283: // childStaticEle = inDoc.createElementNS(
284: // statNs, (statNsPrefix + ":" + statName));
285: // }
286: // else {
287: // //
288: // String statName = this.ctp.getStaticName();
289: // //
290: // childStaticEle = inDoc.createElement(statName);
291: // }
292: // //
293: // inParentEle.insertBefore(
294: // childStaticEle, inChildEle.getNextSibling());
295: // //
296: // Text textValue = inDoc.createTextNode(inValue);
297: // childStaticEle.appendChild(textValue);
298: // //
299: // copyConstraint(inDoc, inChildEle, childStaticEle, inNamespaces);
300: //
301: //}
302: private boolean isConstraintPresent(Element inSourEle) {
303:
304: boolean outValue = false;
305:
306: //
307: if (this .ctp.getConstraintNamespace() != null) {
308: //
309: String conNs = this .ctp.getConstraintNamespace();
310: String conName = this .ctp.getConstraintName();
311: //
312: outValue = inSourEle.hasAttributeNS(conNs, conName);
313: } else {
314: //
315: String conName = this .ctp.getConstraintName();
316: //
317: outValue = inSourEle.hasAttribute(conName);
318: }
319:
320: return outValue;
321: }
322:
323: private void copyConstraint(Document inDoc, Element inSourEle,
324: Element inDestEle, HashMap inNamespaces) {
325: //
326: if (this .ctp.getConstraintNamespace() != null) {
327: //
328: String conNs = this .ctp.getConstraintNamespace();
329: String conNsPrefix = ((String) inNamespaces.get(conNs));
330: String conName = this .ctp.getConstraintName();
331: //
332: if (inDestEle.hasAttributeNS(conNs, conName)) {
333: inDestEle.removeAttributeNS(conNs, conName);
334: }
335: //
336: if (inSourEle.hasAttributeNS(conNs, conName)) {
337: String conValue = inSourEle.getAttributeNS(conNs,
338: conName);
339: inDestEle.setAttributeNS(conNs,
340: (conNsPrefix + ":" + conName), conValue);
341: }
342: } else {
343: //
344: String conName = this .ctp.getConstraintName();
345: //
346: if (inDestEle.hasAttribute(conName)) {
347: inDestEle.removeAttribute(conName);
348: }
349: //
350: //if (inSourEle.hasAttribute(conName)) {
351: // String conValue = inSourEle.getAttribute(conName);
352: //}
353: //
354: if (inSourEle.hasAttribute(conName)) {
355: String conValue = inSourEle.getAttribute(conName);
356: inDestEle.setAttribute(conName, conValue);
357: }
358: }
359: }
360:
361: private Element findUndeployableElement(Element inEle) {
362:
363: Element outValue = null;
364:
365: NodeList eleNl = inEle.getChildNodes();
366: for (int i = (eleNl.getLength() - 1); i >= 0; i--) {
367: Node nextNode = eleNl.item(i);
368: if (nextNode instanceof Element) {
369: Element nextEle = ((Element) nextNode);
370: if (!isElementDeployable(nextEle)) {
371: outValue = nextEle;
372: break;
373: }
374: }
375: }
376:
377: return outValue;
378: }
379:
380: //private final static SpecializedWebPageWriterHandler newInstance(
381: // ConceptTagSetHolder inCtsh, ConceptTagPreferences inCtp) {
382: // //
383: // SpecializedWebPageWriterHandler outValue = null;
384: //
385: // outValue = new SpecializedWebPageWriterHandler(inCtsh, inCtp);
386: //
387: // return outValue;
388: //}
389:
390: //private void reformatDocument(Document inDoc) {
391: //
392: // this.reformatParts = new ArrayList();
393: //}
394:
395: //
396: public boolean isConstraintPrintable(Element inElement, Attr inAttr) {
397:
398: boolean outValue = true;
399:
400: //
401: //String eleNs = inElement.getNamespaceURI();
402: //String eleName = inElement.getLocalName();
403: //
404: String attrNs = inAttr.getNamespaceURI();
405: String attrName = inAttr.getLocalName();
406:
407: //
408: if (this .ctp.isConstraintUsed()) {
409:
410: String conNs = this .ctp.getConstraintNamespace();
411: String conName = this .ctp.getConstraintName();
412: //
413: boolean conHere = compareStrings(attrNs, conNs)
414: && compareStrings(attrName, conName);
415: //
416: if (conHere && !isElementDeployable(inElement)) {
417: outValue = false;
418: }
419: } else {
420: outValue = true;
421: }
422:
423: return outValue;
424: }
425:
426: //
427: public boolean isElementPrintable(Element inElement,
428: StringBuffer inSb) {
429:
430: boolean outValue = true;
431:
432: //
433: //String eleNs = inElement.getNamespaceURI();
434: //String eleName = inElement.getLocalName();
435:
436: if (!isElementDeployable(inElement)) {
437: outValue = true;
438: } else {
439: this .reformatParts.add(inSb.toString());
440: this .reformatParts.add(inElement);
441: inSb.delete(0, inSb.length());
442: outValue = false;
443: }
444:
445: return outValue;
446: }
447:
448: //
449: private boolean isElementDeployable(Element inElement) {
450:
451: boolean outValue = false;
452:
453: String eleNs = inElement.getNamespaceURI();
454: String eleName = inElement.getLocalName();
455:
456: if (this .ctsh.isConceptTagSetClassPresent(eleNs, eleName)) {
457: //
458: Class eleClass = this .ctsh.getConceptTagClass(eleNs,
459: eleName);
460: ConceptTag eleCt = this .ctsh
461: .getConceptTagFromClass(eleClass);
462: //
463: outValue = !ConceptTagFlagsHelper
464: .isConceptTagUndeployable(eleCt);
465: } else if (isElementStatic(inElement)) {
466: outValue = true;
467: } else if (isElementCompound(inElement)) {
468: outValue = true;
469: } else {
470: outValue = false;
471: }
472:
473: return outValue;
474: }
475:
476: private boolean isElementStatic(Element inElement) {
477:
478: boolean outValue = false;
479:
480: if (this .ctp.isStaticUsed()) {
481: //
482: String eleNs = inElement.getNamespaceURI();
483: String eleName = inElement.getLocalName();
484: //
485: outValue = compareStrings(eleNs, this .ctp
486: .getStaticNamespace())
487: && compareStrings(eleName, this .ctp.getStaticName());
488: }
489:
490: return outValue;
491: }
492:
493: private boolean isElementCompound(Element inElement) {
494:
495: boolean outValue = false;
496:
497: if (this .ctp.isStaticUsed()) {
498: //
499: String eleNs = inElement.getNamespaceURI();
500: String eleName = inElement.getLocalName();
501: //
502: outValue = compareStrings(eleNs, this .ctp
503: .getCompoundNamespace())
504: && compareStrings(eleName, this .ctp
505: .getCompoundName());
506: }
507:
508: return outValue;
509: }
510:
511: private boolean compareStrings(String inValue1, String inValue2) {
512:
513: boolean outValue = false;
514:
515: if (inValue1 != null && inValue2 != null) {
516: outValue = inValue1.equals(inValue2);
517: } else if (inValue1 == null && inValue2 == null) {
518: outValue = true;
519: } else {
520: outValue = false;
521: }
522:
523: return outValue;
524: }
525:
526: private final static void reformatWebPage(Document inDoc,
527: Element inParentElement, ConceptTagPreferences inCtp,
528: ConceptTagSetHolder inCtsh) {
529: //
530: String parEleNs = inParentElement.getNamespaceURI();
531: String parEleName = inParentElement.getLocalName();
532: //
533: boolean parEleUndeployable = true;
534: if (inCtsh.isConceptTagSetClassPresent(parEleNs, parEleName)) {
535: Class parEleClass = inCtsh.getConceptTagClass(parEleNs,
536: parEleName);
537: ConceptTag parEleCt = inCtsh
538: .getConceptTagFromClass(parEleClass);
539: parEleUndeployable = ConceptTagFlagsHelper
540: .isConceptTagUndeployable(parEleCt);
541: }
542:
543: //strip constraint if undeployable
544: if (inCtp.isConstraintUsed() && parEleUndeployable) {
545: //
546: String conNs = inCtp.getConstraintNamespace();
547: String conName = inCtp.getConstraintName();
548: //
549: if (conNs != null) {
550: if (inParentElement.hasAttributeNS(conNs, conName)) {
551: inParentElement.removeAttributeNS(conNs, conName);
552: }
553: } else {
554: if (inParentElement.hasAttribute(conName)) {
555: inParentElement.removeAttribute(conName);
556: }
557: }
558: }
559:
560: ////reformat text to make it web page presentable
561: //if (parEleUndeployable) {
562: // int icount = inParentElement.getChildNodes().getLength() - 1;
563: // for (int i = icount; i >= 0; i--) {
564: // Node nextNode = inParentElement.getChildNodes().item(i);
565: // //
566: // if ((nextNode instanceof CDATASection)
567: // || (nextNode instanceof Text)) {
568: // //
569: // String nextNodeText = nextNode.getNodeValue();
570: // reformatTextForWebPage(
571: // inDoc, inParentElement, nextNode, nextNodeText);
572: // }
573: // }
574: //}
575:
576: //check sub-nodes
577: NodeList nl = inParentElement.getChildNodes();
578: for (int i = 0; i < nl.getLength(); i++) {
579: Node nextNode = nl.item(i);
580: if (nextNode instanceof Element) {
581: Element nextElement = ((Element) nextNode);
582: reformatWebPage(inDoc, nextElement, inCtp, inCtsh);
583: }
584: }
585: }
586:
587: private final static String convertDocToString(Document inDoc,
588: HashMap inNsMappings) {
589: //
590: try {
591:
592: StringWriter outValue = new StringWriter();
593:
594: XMLHelper.writeDocument(inDoc, outValue, inNsMappings,
595: false);
596:
597: return outValue.toString();
598: } catch (IOException ioe) {
599: throw new UnexpectedSystemException(ioe);
600: }
601: }
602:
603: //
604: private final static void reformatSpecialTextForWebPage(
605: ConceptTagCharacteristicHolder inCtcharh) {
606: //
607:
608: //depth-first search
609: for (int i = 0; i < inCtcharh.getCharacteristicCount(); i++) {
610: //
611: ConceptTagCharacteristic nextCtchar = inCtcharh
612: .getCharacteristic(i);
613: //
614: for (int j = 0; j < nextCtchar.getValueLength(); j++) {
615: if (nextCtchar.isHolder(j, null)) {
616: reformatSpecialTextForWebPage(nextCtchar
617: .getValueAsHolder(j, null));
618: }
619: }
620: }
621: if (inCtcharh.getUndefinedCharacteristic() != null) {
622: ConceptTagCharacteristic undefCtchar = inCtcharh
623: .getUndefinedCharacteristic();
624: //
625: for (int i = 0; i < undefCtchar.getValueLength(); i++) {
626: if (undefCtchar.isHolder(i, null)) {
627: reformatSpecialTextForWebPage(undefCtchar
628: .getValueAsHolder(i, null));
629: }
630: }
631: }
632:
633: //reformat where appropriate
634: if (inCtcharh.isConceptTagPresent()) {
635: //
636: ConceptTag ct = inCtcharh.getConceptTag();
637: StudioConceptTagInstanceInfoContext sctiic = StudioConceptTagInstanceInfoContext
638: .newInstance();
639: ct.appraise(sctiic);
640: //
641: ConceptTagCharacteristic formatList[] = ConceptTagFlagsHelper
642: .getWebPageFormatCtchars(sctiic);
643: for (int i = 0; i < formatList.length; i++) {
644: ConceptTagCharacteristic nextCtchar = formatList[i];
645: for (int j = 0; j < nextCtchar.getValueLength(); j++) {
646: //
647: if (nextCtchar.isStatic(j, null)) {
648: //
649: reformatTextForWebPage(nextCtchar, j);
650: }
651: }
652: }
653: }
654: }
655:
656: //
657: private final static void reformatTextForWebPage(
658: ConceptTagCharacteristic inCtchar, int inIndex) {
659: //
660:
661: //do nothing, handled during runtime
662: /*
663: //make sure Windows newline doesn't show up twice
664: String nextData = inCtchar.getValueAsString(inIndex, null);
665: nextData = nextData.replaceAll("\\r\\n", "\n");
666: //
667: inCtchar.removeValueWithLocalFiles(inIndex);
668:
669: //
670: ArrayList futureData = new ArrayList();
671: StringTokenizer st = new StringTokenizer(nextData, "\r\n", true);
672: while (st.hasMoreTokens()) {
673: futureData.add(st.nextToken());
674: }
675:
676: //
677: for (int i = futureData.size() - 1; i >= 0; i--) {
678: //
679: String updateData = ((String)futureData.get(i));
680: //
681: if (updateData.equals("\n") || updateData.equals("\r")) {
682: ConceptTag unknownCt = new UnknownWebPageTag(null, "br");
683: inCtchar.addValue(inIndex, unknownCt, true);
684: }
685: else {
686: updateData = updateData.replaceAll(
687: " ", " " + XMLHelper.NBSP_XML);
688: //updateData = //updateData.replace(' ', XMLHelper.NBSP_CHAR);
689: inCtchar.addValue(inIndex, updateData);
690: }
691: }
692: */
693: }
694: }
|