KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > generator > GenDocument


1 package de.webman.generator;
2
3 import com.teamkonzept.lib.*;
4 import org.apache.log4j.Category;
5 import de.webman.template.jsp.*;
6 import de.webman.util.log4j.WebmanCategory;
7 import com.teamkonzept.web.*;
8 import com.teamkonzept.web.templates.*;
9 import com.teamkonzept.webman.*;
10 // import com.sitraka.jprobe.profiler.api.*;
11
import java.io.*;
12 import java.util.*;
13 import java.text.DateFormat JavaDoc;
14 import org.w3c.dom.*;
15 import de.webman.util.legacy.Legacy;
16
17 /**
18  * repraesentiert ein zu generierendes Dokument
19  * @author $Author: alex $
20  * @version $Revision: 1.30 $
21 */

22 public class GenDocument implements TKSortable, com.teamkonzept.lib.templates.TemplateTypes {
23
24     /**
25         Verweis zurück auf den aktuellen statischen Kontext des laufenden Threads
26     */

27     private GeneratorContext context;
28
29     /** Logging Category */
30     private static WebmanCategory cat = (WebmanCategory)WebmanCategory.getInstance(GenDocument.class.getName());
31
32     // JPHeapAPI heapAPI = null;
33
/** Knoten an dem das Dokument hängt */
34     private GenNode anchor;
35
36     /** Name des Dokuments */
37     private String JavaDoc docName;
38
39     /** Template fuer Dokument */
40     private String JavaDoc templateName;
41
42     /** URL des Dokuments, falls es ein externes Dokument ist */
43     private String JavaDoc externUrl;
44
45     /** Hashtable von Integrationsnamen der Inhalte (SiteContent) */
46     private TKHashtable contents;
47
48     /** Hashtable von Integrationsnamen der Referenzen (SiteReferences) */
49     private TKHashtable references;
50
51     /** Hashtable von Vektoren von Referenz-URL */
52     private TKHashtable realRefUrls;
53
54     /** Hashtable von Vektoren von Referenz-Dokumenten (GenDocument) */
55     private TKHashtable realRefDocs;
56
57     /** Hashtable von Vectoren von Referenz-Zielknoten (GenNode) */
58     private TKHashtable realRefNodes;
59
60     /** schon registriert ? */
61     private boolean registered = false;
62
63     public GenDocument( GeneratorContext context, GenNode anchor, String JavaDoc shortName, String JavaDoc templateName,
64                 String JavaDoc externUrl, TKHashtable contents, TKHashtable references )
65     {
66         this.context = context != null ? context : GeneratorContext.setup ();
67
68         this.anchor = anchor;
69         this.docName = shortName;
70         this.templateName = templateName;
71
72         this.externUrl = externUrl;
73         this.contents = contents;
74         this.references = references;
75         // heapAPI = JPHeapAPI.getInstance();
76

77         this.realRefUrls = null;
78         this.realRefDocs = null;
79         this.realRefNodes = null;
80     }
81
82     /**
83      * gibt den Namen des Dokuments zurueck
84      * @return den Namen
85      */

86     public String JavaDoc getDocumentName()
87     {
88         return docName;
89     }
90
91     /**
92      * @return Name des zugewiesenen Templates
93      */

94     public String JavaDoc getTemplateName()
95     {
96         return templateName;
97     }
98
99     /**
100      * @return die externe URL
101      */

102     public String JavaDoc getExternURL()
103     {
104         return externUrl;
105     }
106
107     /**
108      * @return den GenNode, an dem dieses Dokument haengt
109      */

110     public GenNode getAnchor()
111     {
112         return anchor;
113     }
114
115     /**
116      * ...
117      @return ...
118      **/

119     public TKHashtable getContents()
120     {
121         return contents;
122     }
123
124     public TKHashtable getReferences()
125     {
126         return references;
127     }
128
129     public int cmp( TKSortable otherObject )
130     {
131         // Gruppen auf Gruppen wird hier abgefangen, es wird einfach nicht sortiert !
132
if (!(otherObject instanceof GenDocument))
133         {
134             return 0;
135         }
136         GenDocument other = (GenDocument) otherObject;
137
138         SiteContent myDataCont = (SiteContent) contents.get("DATA");
139         SiteContent otherDataCont = (SiteContent) other.contents.get("DATA");
140
141         if( myDataCont == null ) {
142             return ( otherDataCont == null ? 0 : 1 );
143         }
144         else if( otherDataCont == null )
145         {
146             return -1;
147         }
148         return myDataCont.cmp( otherDataCont );
149     }
150
151     public void registerContents(boolean onlyStructureContents)
152     {
153         registered = true;
154
155         Enumeration e = contents.elements();
156         while( e.hasMoreElements() ) {
157             Object JavaDoc cont = e.nextElement();
158             if( cont instanceof TKVector ) {
159                 Enumeration f = ((TKVector) cont).elements();
160                 while( f.hasMoreElements() )
161                     ((SiteContent)f.nextElement()).registerDoc( this, onlyStructureContents );
162             }
163             else {
164                 ((SiteContent)cont).registerDoc( this, onlyStructureContents );
165             }
166         }
167     }
168
169     /**
170      * used by generator (registerReferences)
171      * generiert Referenzen fuer Generator ??
172      * @param refUrls referencing urls (?)
173      * @param refDocs referenced document (?)
174      * @param refNodes referenced nodes (?)
175      * @param destNode destination node (?)
176      * @param ref the site reference (?)
177     */

178     public void addRealReference( TKVector refUrls, TKVector refDocs, TKVector refNodes, GenNode destNode, SiteReference ref )
179     {
180         int insertPos = -1;
181         cat.debug( "add Reference "+destNode.getPath() );
182         if( ref.getDestinationContentSource() != null ) {
183             GenDocument destCont = destNode.getDocument( ref.getDestinationContentSource() );
184
185             // cat.debug( " with value of doc "+destCont.anchor.getPath()+destCont.docName );
186

187             if( destCont == null ) {
188                 throw new Error JavaDoc("could not find content-source-document '"+ref.getDestinationContentSource()+"' for "+ref.path()+" in node "+
189                     destNode.getPath() );
190
191             }
192
193             if (ref.getComponentType() == SiteReference.COMPONENT_TYPE_FREE) {
194                 refDocs.addElement( destCont );
195                 insertPos = -1;
196             } else {
197                 insertPos = 0;
198                 int docListSize = refDocs.size();
199                 boolean done = false;
200                 while( insertPos < docListSize && !done ) {
201                     GenDocument currDoc = (GenDocument) refDocs.get(insertPos);
202                     if( destCont.cmp( currDoc ) < 0 ) {
203                         refDocs.insertElementAt( destCont, insertPos );
204                         done = true;
205                     }
206                     else {
207                         insertPos++;
208                     }
209                 }
210                 if( !done ) {
211                     refDocs.addElement( destCont );
212                     insertPos = -1;
213                 }
214             }
215         }
216
217         GenDocument destRef = destNode.getDocument( ref.getDestinationShortName() );
218
219         // singh + 1999.03.11
220
if( insertPos == -1 ) {
221             refUrls.addElement ((destRef.externUrl != null ? destRef.externUrl
222                 : anchor.makePathRelativeTo (destNode)+ref.getDestinationShortName()));
223             refNodes.addElement( destNode );
224         }
225         else {
226             refUrls.insertElementAt ((destRef.externUrl != null ? destRef.externUrl
227                 : anchor.makePathRelativeTo (destNode)+ref.getDestinationShortName() ), insertPos);
228             refNodes.insertElementAt( destNode, insertPos );
229         }
230         // singh - 1999.03.11
231
}
232
233     /**
234         used by preview (makeReferences)
235         generiert Referenzen fuer Preview ??
236         @param ownUrl
237         @param refUrls
238         @param refDocs
239         @param refNodes
240         @param destNode
241         @param ref
242         @param primContent
243     */

244     public void addRealReference( String JavaDoc ownUrl, TKVector refUrls, TKVector refDocs, TKVector refNodes,
245                       SiteNode destNode, SiteReference ref, SiteContent primContent )
246     {
247         int primaryId = primContent != null ? primContent.getInstanceId() : -1;
248
249         GenNode gn = destNode.makeGenNode (primContent);
250         cat.debug("registerReal : " + gn);
251         if (gn == null) {
252             cat.warn("could not make genNode from siteNode for "+ref.path()+
253                 " in node "+destNode.path() );
254         }
255
256         int insertPos = -1;
257         if( ref.getDestinationContentSource() != null ) {
258
259             SiteDocument sd = destNode.getDocument( ref.getDestinationContentSource() );
260             if (sd == null) {
261                 cat.warn("could not find referenced document '"+ref.getDestinationShortName() +
262                     "' for "+ref.getPath()+" in node "+destNode.getPath() );
263             }
264
265             if (context.siteNodes.isReducedBuild()) sd.complete();
266
267             GenDocument destCont = sd != null ? sd.getGenDocument( primContent, gn ) : null;
268
269             if (destCont == null) {
270                 cat.warn("could not find gen content-source-document '"+ref.getDestinationContentSource()+
271                     "' for "+ref.getPath()+" in node "+destNode.getPath() );
272                 return;
273             }
274
275             if (ref.getComponentType() == SiteReference.COMPONENT_TYPE_FREE) {
276                 refDocs.addElement( destCont );
277                 insertPos = -1;
278             } else {
279                 insertPos = 0;
280                 int docListSize = refDocs.size();
281                 boolean done = false;
282                 while( insertPos < docListSize && !done ) {
283                     GenDocument currDoc = (GenDocument) refDocs.get(insertPos);
284                     cat.debug("cmp "+destCont.anchor.getPath()+destCont.docName+" with "+currDoc.anchor.getPath()+ currDoc.docName );
285                     if( destCont.cmp( currDoc ) <= 0 ) {
286                         refDocs.insertElementAt( destCont, insertPos );
287                         done = true;
288                     }
289                     else {
290                         insertPos++;
291                     }
292                 }
293
294                 if( !done ) {
295                     refDocs.addElement( destCont );
296                     insertPos = -1;
297                 }
298             }
299         }
300
301         SiteDocument destRef = destNode.getDocument( ref.getDestinationShortName() );
302         if (destRef == null) {
303             cat.warn("destRef ist null !" );
304             throw new Error JavaDoc("could not find referenced document '"+ref.getDestinationShortName()+
305                 "' for "+ref.getPath()+" in node "+destNode.getPath() );
306         }
307
308         // destRef.complete() hier nicht noetig ???
309

310         String JavaDoc docId = "null";
311         if (destRef.getURL() == null) {
312
313             TKConverter urlConv = TKConverter.getConverter ("URL");
314             docId = new String JavaDoc (urlConv.stringToBytes (destRef.getShortName()),0);
315         }
316
317         if( insertPos == -1 ) {
318
319             refUrls.addElement ((destRef.getURL() != null ? destRef.getURL()
320                 : ownUrl+"?TK_EV[CE_PREVIEW]=1"+
321                     "&TK_PAR[SNODE]="+destNode.getId()+
322                     "&TK_PAR[PID]="+primaryId+
323                     "&TK_PAR[DOCNAME]="+docId+
324                     "&TK_PAR[CHECKER]=1"));
325             refNodes.addElement( gn );
326         } else {
327             refUrls.insertElementAt ((destRef.getURL() != null ? destRef.getURL()
328                 : ownUrl+"?TK_EV[CE_PREVIEW]=1"+
329                     "&TK_PAR[SNODE]="+destNode.getId()+
330                     "&TK_PAR[PID]="+primaryId+
331                     "&TK_PAR[DOCNAME]="+docId+
332                     "&TK_PAR[CHECKER]=1"),insertPos);
333             refNodes.insertElementAt( gn, insertPos );
334         }
335     }
336
337     /**
338         used by generator
339     */

340     public void registerReferences()
341     {
342         try
343         {
344             registered = true;
345
346             int size = references.size();
347             if( size == 0 ) return;
348
349             TKVector refsToDelete = new TKVector();
350
351             realRefUrls = new TKHashtable( size );
352             realRefDocs = new TKHashtable( size );
353             realRefNodes = new TKHashtable( size );
354             //%%ANDI: hier muss noch die zweite Stufe der Ref-Selection-Klasse eingebaut werden
355
Enumeration e = references.keys();
356             while( e.hasMoreElements() )
357             {
358                 String JavaDoc intName = (String JavaDoc) e.nextElement();
359                 cat.debug("register : " + intName);
360                 SiteReference ref = (SiteReference) references.get(intName);
361                 TKVector currUrls = new TKVector();
362                 realRefUrls.put( intName, currUrls );
363                 TKVector currDocs = null;
364                 if( ref.getDestinationContentSource() != null ) {
365                     currDocs = new TKVector();
366                     realRefDocs.put( intName, currDocs );
367                 }
368
369                 TKVector currNodes = new TKVector();
370                 realRefNodes.put( intName, currNodes );
371
372                 // Hier muß darauf geachtet werden, daß Referenzen auf das eigene Dokument, z.B. bei
373
// Framesets, erhalten bleiben. Dies ist möglicherweise dann der Fall, wenn es sich
374
// um einen INTEGRATION_TYPE_SINGLE handelt und keine Selektionsklasse angegeben ist.
375
// In diesem Fall wird der refNode über den anchor des genDocuments ermittelt und
376
// nicht über die (Ziel-) siteNodes der Referenz ... (singh)
377

378                 if ((ref.getIntegrationType() == SiteReference.INTEGRATION_TYPE_SINGLE) &&
379                     (ref.getSelectionType() == null)) {
380                     if (ref.getDestinationDocuments().length == 0)
381                     {
382                         // Single Referenz nicht gefunden - Fehlermeldung und weiter !
383
cat.error("No destination for reference component: " + intName + " in document: " + anchor.getPath() + getDocumentName() + " found !");
384                         continue;
385                     }
386                     SiteNode refAnchor = (SiteNode) ref.getDestinationDocuments()[0].getAnchor();
387                     GenNode refNode = (refAnchor == anchor.getSiteNode())
388                         ? anchor
389                         : (GenNode)refAnchor.getGenNodes().get(0);
390                     addRealReference( currUrls, currDocs, currNodes, refNode, ref );
391                 } else {
392                     TKVector allNodes = new TKVector();
393                     for( int i=0; i<ref.getDestinationDocuments().length; i++ ) {
394                         allNodes.concat( ref.getDestinationDocuments()[i].getAnchor().getGenNodes() );
395                     }
396                     Enumeration f = allNodes.elements();
397                     while( f.hasMoreElements() ) {
398                         GenNode node3 = (GenNode)f.nextElement();
399                         addRealReference( currUrls, currDocs, currNodes, node3, ref );
400                     }
401
402                     if( ref.getSelectionType() != null )
403                         TKWMReferenceSelectorReg.reduceReferences(
404                             ref.getSelectionType(),
405                             ref.getSelectionData(),
406                             currUrls,
407                             currDocs,
408                             currNodes,
409                             this.anchor
410                         );
411                     if( ref.getIntegrationType() == SiteReference.INTEGRATION_TYPE_SINGLE ) {
412                         int remainSize = currUrls.size();
413                         if( remainSize == 0 ) refsToDelete.addElement( intName );
414                         else {
415                             for( int i=remainSize-1; i>0; i-- ) {
416                                 currUrls.removeElementAt(i);
417                                 if( currDocs!=null ) currDocs.removeElementAt(i);
418                                 currNodes.removeElementAt(i);
419                             }
420                         }
421                     }
422                 }
423             }
424             e = refsToDelete.elements();
425             while( e.hasMoreElements() ) {
426                 references.remove( e.nextElement() );
427             };
428
429             e = references.keys();
430             while( e.hasMoreElements() ) {
431                 String JavaDoc intName = (String JavaDoc) e.nextElement();
432                 SiteReference ref = (SiteReference) references.get(intName);
433                 Enumeration f = ((TKVector) realRefDocs.get( intName )).elements();
434                 while( f.hasMoreElements() ) {
435                     ((GenDocument)f.nextElement()).registerContents( ref.getComponentType() == SiteReference.COMPONENT_TYPE_FREE);
436                 }
437             }
438         }
439         catch (Throwable JavaDoc t)
440         {
441             t.printStackTrace(System.out);
442             cat.error("Unknown in registerReferences" + t.getMessage(), t);
443         }
444
445     }
446
447     /**
448         used by preview
449         @param ownUrl URL des WebmanServlets
450         @param primContent primaerer Content fuer den der Preview durchgefuehrt werden soll
451     */

452     public void makeReferences(String JavaDoc ownUrl, SiteContent primContent)
453     {
454         int size = references.size();
455         if( size == 0 ) return;
456
457         TKVector refsToDelete = new TKVector();
458
459         realRefUrls = new TKHashtable( size );
460         realRefDocs = new TKHashtable( size );
461         realRefNodes = new TKHashtable( size );
462
463         Enumeration e = references.keys();
464         while( e.hasMoreElements() ) {
465             String JavaDoc intName = (String JavaDoc) e.nextElement();
466
467             SiteReference ref = (SiteReference) references.get(intName);
468             TKVector currUrls = new TKVector();
469             realRefUrls.put( intName, currUrls );
470             TKVector currDocs = null;
471
472             if (ref.getDestinationContentSource() != null) {
473
474                 currDocs = new TKVector();
475                 realRefDocs.put (intName,currDocs);
476             }
477
478             TKVector currNodes = new TKVector();
479             realRefNodes.put( intName, currNodes );
480
481             // Hier gilt das gleiche wie schon bei registerReferences() bezüglich der
482
// Behandlung von Referenzen auf das eigene Dokument (z.B. Framesets), siehe dort
483
// (singh)
484

485             // Im Gegensatz zu registerReferences() gibt es hier keine genNodes, sie werden
486
// jeweils durch die Kombination (siteNode,primaryContents) repräsentiert und bei
487
// Bedarf von einer siteNode-basierten Version von addRealReference() on-demand,
488
// nicht rekursiv erzeugt (singh)
489

490             if ((ref.getIntegrationType() == SiteReference.INTEGRATION_TYPE_SINGLE ) &&
491                 (ref.getSelectionType() == null)) {
492                 if (ref.getDestinationDocuments().length == 0)
493                 {
494                     // Single Referenz nicht gefunden - Fehlermeldung und weiter !
495
cat.error("No destination for reference component: " + intName + " found !");
496                     continue;
497                 }
498
499                 SiteNode refAnchor = (SiteNode) ref.getDestinationDocuments()[0].getAnchor();
500
501                 if (refAnchor == anchor.getSiteNode()) {
502                     addRealReference( ownUrl, currUrls, currDocs, currNodes, refAnchor, ref, primContent );
503
504                 } else if (refAnchor.getType() == SiteNode.NODE_TYPE_GROUP) {
505
506                     TKVector primConts = null;
507
508                     if( refAnchor.getPrimaryContentSelectionKey() != null )
509                         primConts = context.integrations.getSelection( refAnchor.getPrimaryContentSelectionKey() );
510                     else if (refAnchor.getPrimaryContentNode() != null)
511                         primConts = refAnchor.getPrimaryContentNode().getContents();
512
513                     if (primConts == null) continue;
514
515                     Enumeration ee = primConts.elements();
516                     if (ee.hasMoreElements()) {
517
518                         SiteContent sc = (SiteContent) ee.nextElement();
519                         if (sc != null) addRealReference( ownUrl, currUrls, currDocs, currNodes, refAnchor, ref, sc );
520                     }
521                 } else {
522                     addRealReference( ownUrl, currUrls, currDocs, currNodes, refAnchor, ref, null );
523                 }
524             } else {
525                 for( int i=0; i<ref.getDestinationDocuments().length; i++ ) {
526                     if (ref.getDestinationDocuments()[i] != null)
527                     {
528                         SiteNode refAnchor = (SiteNode) ref.getDestinationDocuments()[i].getAnchor();
529
530                         if (refAnchor.getType() == SiteNode.NODE_TYPE_GROUP) {
531
532                             TKVector primConts = null;
533
534                             if( refAnchor.getPrimaryContentSelectionKey() != null )
535                                 primConts = context.integrations.getSelection( refAnchor.getPrimaryContentSelectionKey() );
536                             else if (refAnchor.getPrimaryContentNode() != null)
537                                 primConts = refAnchor.getPrimaryContentNode().getContents();
538
539                             if (primConts == null) continue;
540
541                             Enumeration ee = primConts.elements();
542                             while (ee.hasMoreElements())
543                             {
544                                 SiteContent sc = (SiteContent) ee.nextElement();
545                                 if (sc != null) addRealReference( ownUrl, currUrls, currDocs, currNodes, refAnchor, ref, sc );
546                             }
547                         } else {
548                             addRealReference( ownUrl, currUrls, currDocs, currNodes, refAnchor, ref, null );
549                         }
550                     }
551                 }
552
553                 if( ref.getSelectionType() != null )
554                     TKWMReferenceSelectorReg.reduceReferences(
555                         ref.getSelectionType(),
556                         ref.getSelectionData(),
557                         currUrls,
558                         currDocs,
559                         currNodes,
560                         this.anchor
561                     );
562
563                 if( ref.getIntegrationType() == SiteReference.INTEGRATION_TYPE_SINGLE ) {
564                     int remainSize = currUrls.size();
565                     if( remainSize == 0 ) refsToDelete.addElement( intName );
566                     else {
567                         for( int i=remainSize-1; i>0; i-- ) {
568                             currUrls.removeElementAt(i);
569                             if( currDocs!=null ) currDocs.removeElementAt(i);
570                             currNodes.removeElementAt(i);
571                         }
572                     }
573                 }
574             }
575         }
576
577         e = refsToDelete.elements();
578         while( e.hasMoreElements() ) {
579             references.remove( e.nextElement() );
580         };
581     }
582     // singh - 1999.04.06
583

584     public void prepareGeneration()
585     {
586         cat.debug("prepare document " + anchor.getPath() + "/" +docName);
587         registerContents(false);
588         registerReferences();
589         cat.debug("document prepared'"+docName);
590     }
591
592     public void deregisterContents (boolean onlyStructureContents)
593     {
594         Enumeration e = contents.elements();
595         while( e.hasMoreElements() ) {
596             Object JavaDoc cont = e.nextElement();
597             if( cont instanceof TKVector ) {
598                 Enumeration f = ((TKVector) cont).elements();
599                 while( f.hasMoreElements() )
600                     ((SiteContent)f.nextElement()).deregisterDoc( this, onlyStructureContents );
601             }
602             else {
603                 ((SiteContent)cont).deregisterDoc( this, onlyStructureContents );
604             }
605         }
606     }
607
608     public void deregisterReferences()
609     {
610         Enumeration e = references.keys();
611         while( e.hasMoreElements() ) {
612             String JavaDoc intName = (String JavaDoc) e.nextElement();
613             SiteReference ref = (SiteReference) references.get(intName);
614
615             Enumeration f = ((TKVector) realRefDocs.get( intName )).elements();
616             while( f.hasMoreElements() ) {
617                 ((GenDocument)f.nextElement()).deregisterContents( ref.getComponentType() == SiteReference.COMPONENT_TYPE_FREE);
618             }
619         }
620     }
621
622     public void cleanupGeneration()
623     {
624         if (!registered) return;
625
626         deregisterContents(false);
627         deregisterReferences();
628     }
629
630     public TKVector sortContents( TKVector contents )
631     {
632         TKVector result = contents.sort();
633         return result;
634     }
635
636     /**
637      * hier für direkten Einsprung aus der Referenzgenerierung
638      * @param domAnchor (?)
639      * @param domData (?)
640      * @param onlyStructureContents (?)
641      */

642     public void generateDOMContents( Element domAnchor, DOMTemplateData domData, boolean onlyStructureContents )
643     {
644         String JavaDoc node = anchor.getPath();
645         // ist der DOMAnchor der richtige Ort für diese Attribute ? -alex
646
domAnchor.setAttribute( "DOCUMENT", node+docName );
647         domAnchor.setAttribute( "NODE_PATH", node );
648         domAnchor.setAttribute( "NODE", anchor.getNodeName() );
649         domAnchor.setAttribute( "LEVEL", Integer.toString(anchor.depth()-1) );
650         Enumeration e = contents.keys();
651         while( e.hasMoreElements() )
652         {
653             String JavaDoc intName = (String JavaDoc) e.nextElement();
654             Object JavaDoc cont = contents.get(intName);
655             boolean isListInt = cont instanceof TKVector;
656
657             SiteContent firstCont = (isListInt
658                 ? (SiteContent)((TKVector) cont).get(0)
659                 : (SiteContent)cont
660             );
661             if (firstCont != null && (firstCont.isStructureContent() || !onlyStructureContents))
662             {
663                 // create only one top element for grouped references; buf request #809 --gck
664
Element currentElement = domData.getContentElement(intName, domAnchor);
665
666                 if( isListInt )
667                 {
668                     domAnchor.setAttribute( intName, Integer.toString(((TKVector)cont).size()) );
669                     TKVector sortContents = sortContents( (TKVector)cont );
670
671                     for (int i = 0; i < sortContents.size(); i++)
672                     {
673                         //Element currentElement = domData.getContentElement(intName, domAnchor); // --gck
674
SiteContent content = ((SiteContent)((TKVector)cont).elementAt(i));
675                         Element contentElement = domData.getContentElement("content", currentElement);
676
677                         /*
678                         if (content != null) // hat das jetzt eine Bedeutung im DOM Fall ? nein ! -alex
679                             content.fillNameIntoTemplate(t, prefix+intName );
680                         */

681                         context.push(); // -gck
682
// context.clearCycle();
683
if ((content != null) && (!onlyStructureContents || content.isStructureContent()))
684                         {
685                             domAnchor.setAttribute( intName+".IDX", String.valueOf(i+1) ); // ?
686
// ??if( i % 2 == 1 ) t.set( currListName+".ODD", Boolean.TRUE );
687
content.fillBasicDOM( domData.getDocument(), contentElement );
688                         }
689                         context.pop(); // -gck
690
}
691                 }
692                 else {
693                     //Element currentElement = domData.getContentElement(intName, domAnchor); // --gck
694
// context.clearCycle();
695
context.push();// -gck
696

697                     if (firstCont != null)
698                     {
699                         //firstCont.fillNameIntoTemplate(t, prefix+intName );
700
if (!onlyStructureContents || firstCont.isStructureContent())
701                         {
702                             String JavaDoc cName = firstCont.isStructureContent() ? "site" :firstCont.getShortName();
703                             if (cName != null && cName.length() != 0)
704                             {
705                                 try
706                                 {
707
708                                     Integer.parseInt(cName.substring(0,1));
709                                     cName = 'A' + cName;
710                                 }
711                                 catch(NumberFormatException JavaDoc nfe)
712                                 {
713                                     cat.debug("Content Kennung ist eine Nummer");
714                                 }
715                             }
716
717                             Element contentElement = domData.getContentElement("content", currentElement);
718                             firstCont.fillBasicDOM( domData.getDocument(), contentElement );
719                         }
720                     }
721                     
722                     context.pop();
723                 }
724             }
725         }
726     }
727
728     /**
729         @param t Template
730         hier wird getrennt nach alten Templates und DOM Implementierung
731         Grund ist die unterschiedliche Behandlung der Datenstrukturen
732     */

733     public void generateContents( TemplateBasic t, String JavaDoc prefix, boolean onlyStructureContents )
734     {
735         if (t.getType().equals(JSP_TEMPLATE))
736         {
737             generateDOMContents(t.getDOMData().getRoot(), t.getDOMData(), onlyStructureContents);
738         }
739         else
740         {
741             String JavaDoc node = anchor.getPath();
742
743             // das raus hier ?
744
t.set( prefix+"DOCUMENT", node+docName );
745             t.set( prefix+"NODE_PATH", node );
746             t.set( prefix+"NODE", anchor.getNodeName() );
747             t.set( prefix+"LEVEL", Integer.toString(anchor.depth()-1) );
748
749             Enumeration e = contents.keys();
750             while( e.hasMoreElements() )
751             {
752                 String JavaDoc intName = (String JavaDoc) e.nextElement();
753                 Object JavaDoc cont = contents.get(intName);
754                 boolean isListInt = cont instanceof TKVector;
755                 SiteContent firstCont = (isListInt
756                     ? (SiteContent)((TKVector) cont).get(0)
757                     : (SiteContent)cont
758                 );
759
760                 if( isListInt )
761                 {
762                     t.set( prefix+intName, Integer.toString(((TKVector)cont).size()) );
763                     TKVector sortContents = sortContents( (TKVector)cont );
764                     TKHTMLTemplate htmlTemplate = (TKHTMLTemplate)t;
765                     htmlTemplate.setListIterator(
766                         new GenContentIterator(
767                             context, sortContents, prefix+intName,
768                             htmlTemplate.getListIterator(),
769                             onlyStructureContents, false, false
770                         )
771                     );
772                 }
773                 else {
774                     if (firstCont != null)
775                     {
776                         //context.clearCycle();
777
context.push();
778                         firstCont.fillNameIntoTemplate(t, prefix+intName );
779                         
780                         if (!onlyStructureContents || firstCont.isStructureContent())
781                             firstCont.fillIntoTemplate( t, prefix+intName );
782                         context.pop();
783                     }
784                 }
785             }
786         }
787     }
788
789     public void generateReferences( TemplateBasic t, DOMTemplateData domData, String JavaDoc tmplBase,
790         boolean isPreview, String JavaDoc ownUrl, String JavaDoc rootDir )
791     {
792         Enumeration e = references.keys();
793         while( e.hasMoreElements() ) {
794             String JavaDoc intName = (String JavaDoc) e.nextElement();
795             SiteReference ref = (SiteReference) references.get(intName);
796             TKVector refUrls = (TKVector) realRefUrls.get( intName );
797             TKVector refDocs = (TKVector) realRefDocs.get( intName );
798             TKVector refNodes = (TKVector) realRefNodes.get( intName );
799
800             if( ref.getIntegrationType() == SiteReference.INTEGRATION_TYPE_SINGLE ) {
801                 if( refUrls.size() > 0 ) {
802                     context.genDocuments.fillReferenceIntoTemplate(
803                         t,
804                         domData,
805                         (String JavaDoc) refUrls.get(0),
806                         (refNodes == null ? null : (GenNode) refNodes.get(0)),
807                         anchor,
808                         (refDocs == null ? null : (GenDocument) refDocs.get(0)),
809                         intName, ref, tmplBase, ownUrl, rootDir, isPreview
810                     );
811                 }
812             }
813             else {
814                 if (t.getType().equals(JSP_TEMPLATE))
815                 {
816                     for (int i = 0; i < refUrls.size(); i++)
817                     {
818                         context.genDocuments.fillReferenceIntoTemplate(
819                             t, domData,
820                             (String JavaDoc) refUrls.get(i),
821                             (refNodes == null ? null : (GenNode) refNodes.get(i)),
822                             anchor,
823                             (refDocs == null ? null : (GenDocument) refDocs.get(i)),
824                             intName, ref, tmplBase, ownUrl, rootDir, isPreview
825                         );
826                     }
827                 }
828                 else
829                 {
830                     if (refUrls != null)
831                     {
832                         t.set( intName, Integer.toString(refUrls.size()) );
833                         ((TKHTMLTemplate)t).setListIterator(
834                             new GenReferenceIterator(
835                                 context, anchor, ref, refUrls, refNodes, refDocs,
836                                 tmplBase, intName, ownUrl, rootDir, isPreview,
837                                 ((TKHTMLTemplate)t).getListIterator()
838                             )
839                         );
840                     }
841                 }
842             }
843         }
844     }
845
846     public void generateToPrintStream ( PrintStream out, String JavaDoc tmplBase,
847             TKHashtable data, boolean isPreview, String JavaDoc ownUrl, String JavaDoc rootDir)
848         throws Exception JavaDoc
849     {
850         TemplateBasic t = TemplateUtils.getTemplate(templateName, tmplBase, false);
851
852         DOMTemplateData domData = t.getDOMData();
853         Date timestamp = new Date();
854
855
856         t.set( "SECONDS", String.valueOf( timestamp.getTime() ) ); // muss das sein ?
857
t.set( "DATE", DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp));
858         t.set( "TIME", DateFormat.getTimeInstance(DateFormat.SHORT).format(timestamp));
859
860         if (data != null) t.set (data);
861
862         synchronized (context)
863         {
864             generateContents(t, "", false);
865             generateReferences(t, domData, tmplBase, isPreview, ownUrl, rootDir);
866         }
867         if (t.getType() != JSP_TEMPLATE)
868         {
869             if (isPreview) t.set( "DISPLACEMENT","");
870             else t.set( "DISPLACEMENT","<TKK_DISPLACEMENT[ANSI]>");
871         }
872         t.doTagSubstitution();
873         t.printTemplate( out );
874
875         cleanupGeneration();
876     }
877
878     /**
879     Generates the document by creating a file with name
880     <code>docName</code> in the directory <code>baseDir</code>.
881     @param baseDir the parent directory path of this node
882     @param rootDir the path to the document root
883     @param tmplBase the absolute path to the template directory
884     @param genFileLogStream a logstream to which the generated files are written
885     */

886     public void generate( File baseDir, String JavaDoc rootDir, String JavaDoc tmplBase, PrintStream genFileLogStream )
887         throws Exception JavaDoc
888     {
889         //System.gc();
890
// heapAPI.save("MyHeapDumpbefore");
891
cat.info(new Date().toString() + ": start doc generation : " + baseDir + " / " + docName, WebmanCategory.ALL);
892         // Dokument eintragen
893
GeneratorContext.setCurrentDocument(this);
894
895         /*System.gc();
896         cat.debug("Memory available before:" + Runtime.getRuntime().freeMemory());*/

897         File outFile = new File( baseDir, docName );
898         String JavaDoc path = outFile.getAbsolutePath();
899
900         Boolean JavaDoc expanded = (Boolean JavaDoc) context.genDocuments.getDocumentExpanded(path);
901
902         if (expanded != null && expanded.booleanValue())
903         {
904             if (genFileLogStream != null)
905                 genFileLogStream.println (path);
906             return;
907         }
908
909         if (expanded != null && !expanded.booleanValue())
910         {
911             context.genDocuments.putExpandedGenDoc (path,new Boolean JavaDoc (true));
912             return;
913         }
914
915         context.genDocuments.putExpandedGenDoc (path,new Boolean JavaDoc (false));
916
917         FileOutputStream f = new FileOutputStream( outFile );
918         PrintStream out = null;
919         try
920         {
921             out = new PrintStream(f);
922
923             generateToPrintStream (out,tmplBase,null,false,null,rootDir);
924         }
925         finally
926         {
927             out.close();
928         }
929
930         context.genDocuments.putExpandedGenDoc(path,new Boolean JavaDoc (true));
931         if (genFileLogStream != null)
932             genFileLogStream.println (path);
933         // wieder da !
934

935         // System.gc();
936
// heapAPI.save("MyHeapDumpAfter");
937
/*S
938         cat.debug("Memory available after:" + Runtime.getRuntime().freeMemory());
939         */

940     }
941
942     public void generatePreview(PrintStream out, String JavaDoc rootDir, String JavaDoc tmplBase, String JavaDoc ownUrl )
943         throws Exception JavaDoc
944     {
945         TKHashtable data = new TKHashtable();
946         data.put ("PREVIEW",Boolean.TRUE);
947
948         File sourceDir = new File( rootDir );
949         if( anchor.getSiteNode().getParent() != null )
950             sourceDir = new File( sourceDir, anchor.getSubPathDir());
951
952         File outFile = new File( sourceDir, docName );
953         String JavaDoc path = outFile.getAbsolutePath();
954
955         Object JavaDoc cached = context.genDocuments.getDocumentExpanded(path);
956         if (cached != null) {
957             if (cached instanceof Boolean JavaDoc) {
958                 if (!((Boolean JavaDoc) cached).booleanValue())
959                 {
960                     context.genDocuments.putExpandedGenDoc (path,new Boolean JavaDoc (true));
961                 }
962             } else out.print ((String JavaDoc) cached);
963
964         } else {
965             context.genDocuments.putExpandedGenDoc(path,new Boolean JavaDoc (false));
966             generateToPrintStream (out,tmplBase,data,true, ownUrl,rootDir);
967         }
968     }
969 }
970
Popular Tags