KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.generator;
2
3 import com.teamkonzept.lib.*;
4 import com.teamkonzept.web.*;
5 import com.teamkonzept.web.templates.*;
6 import com.teamkonzept.webman.*;
7     
8 import com.oroinc.text.regex.*;
9
10 import java.io.*;
11 import java.util.*;
12 import org.apache.log4j.Category;
13
14 /**
15     * @author $Author: alex $
16     * @version $Revision: 1.2 $
17 */

18 public class GenDocumentHandle
19 {
20     /** Log4J Category */
21     private static Category cat = Category.getInstance(GenDocumentHandle.class.getName());
22     
23     private GeneratorContext context;
24     private boolean isPreview;
25     private boolean displace;
26     private String JavaDoc ownUrl;
27     private String JavaDoc rootDir;
28
29     private GenNode genNode;
30     private GenNode srcNode;
31     private String JavaDoc shortName;
32     private String JavaDoc tmplBase;
33
34     public GenDocumentHandle (
35         GeneratorContext context, boolean isPreview, boolean displace, String JavaDoc ownUrl,
36         GenNode genNode, GenNode srcNode,
37         String JavaDoc shortName, String JavaDoc tmplBase, String JavaDoc rootDir)
38     {
39         this.context = context;
40         this.isPreview = isPreview;
41         this.displace = displace;
42         this.ownUrl = ownUrl;
43         this.rootDir = rootDir;
44         
45         this.genNode = genNode;
46         this.srcNode = srcNode;
47         this.shortName = shortName;
48         this.tmplBase = tmplBase;
49     }
50     
51     public String JavaDoc digest (String JavaDoc halfDigested, String JavaDoc path)
52     {
53         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
54         PrintStream out = new PrintStream(buffer);
55
56         try
57         {
58             TKHTMLTemplateSyntax ts = new TKHTMLTemplateSyntax
59                 (halfDigested,"file:"+path,GenDocumentStatics.patPostCompileTKTag);
60             TKHTMLTemplate t = new TKHTMLTemplate (ts);
61
62             String JavaDoc displacement = srcNode == null ? "" : srcNode.makePathRelativeTo (genNode);
63             
64             if (displace) t.set( "DISPLACEMENT","<TKK_DISPLACEMENT[ANSI]>"+displacement);
65             else t.set( "DISPLACEMENT",displacement);
66
67             t.doTagSubstitution();
68             t.printTemplate( out );
69             out.close();
70             buffer.close();
71             
72         } catch ( Throwable JavaDoc th )
73         {
74             cat.warn("GenDocumentHandle.digest says: ", th);
75             out.close();
76             return "";
77         }
78         return buffer.toString();
79     }
80
81     public String JavaDoc toString ()
82     {
83         File sourceDir = new File( rootDir );
84         if( genNode.getSiteNode().getParent() != null )
85         {
86             sourceDir = new File( sourceDir, genNode.getSubPathDir() );
87             if (!isPreview)
88             {
89                 if( sourceDir.exists() )
90                 {
91                     if( !sourceDir.isDirectory() )
92                     {
93                         throw new Error JavaDoc( "existing file "+sourceDir.getAbsolutePath()
94                             +" prevents creation of new directory" );
95                     }
96                 }
97                 else
98                 {
99                     if( !sourceDir.mkdirs() )
100                     {
101                         throw new Error JavaDoc( "could not create file "+sourceDir.getAbsolutePath() );
102                     }
103                 }
104             }
105         };
106
107         SiteDocument sd = genNode.getSiteNode().getDocument (shortName);
108         if (sd == null) {
109             cat.warn("GenDocumentHandle.toString: "+
110                       "could not find document '"+shortName+"' in node "+genNode.getPath());
111             return "";
112         }
113
114         if (context.siteNodes.isReducedBuild()) sd.complete();
115         GenDocument genDoc = sd.getGenDocument( genNode.getPrimaryContent(), genNode );
116
117         File outFile = new File( sourceDir, genDoc.getDocumentName() );
118         String JavaDoc path = outFile.getAbsolutePath();
119         
120         Object JavaDoc cached = context.genDocuments.getDocumentExpanded (path);
121         if (cached != null)
122         {
123             if (cached instanceof Boolean JavaDoc)
124             {
125
126                 if (!((Boolean JavaDoc) cached).booleanValue())
127                 {
128
129                     cat.warn ("GenDocumentHandle.toString: Infinite loop, do "+
130                        path+" from '"+srcNode.getPath()+"' ***");
131                     context.genDocuments.putExpandedGenDoc(path,new Boolean JavaDoc (true));
132                     if (!isPreview) TKLib.spitfile (path,"");
133                     return "";
134                 } else if (!isPreview)
135                 {
136                     String JavaDoc d = digest (TKLib.slurpfile (path),path);
137                     return d;
138
139                 } else return "";
140
141             } else if (isPreview) return (String JavaDoc) cached;
142             else return "";
143         }
144
145         context.genDocuments.putExpandedGenDoc(path,new Boolean JavaDoc (false));
146
147         TKHashtable data = null;
148         if (isPreview)
149         {
150             data = new TKHashtable();
151             data.put ("PREVIEW",Boolean.TRUE);
152             genDoc.makeReferences (ownUrl,genNode.getPrimaryContent());
153
154         } else
155         {
156             genDoc.registerContents(false);
157             genDoc.registerReferences();
158         }
159
160         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
161         PrintStream out = new PrintStream(buffer);
162
163         try
164         {
165             genDoc.generateToPrintStream (out,tmplBase,data,isPreview,ownUrl,rootDir);
166             out.close();
167             buffer.close();
168             
169         } catch ( Throwable JavaDoc th )
170         {
171             cat.warn ("GenDocumentHandle.toString says: ", th);
172         }
173
174         String JavaDoc fdata = buffer.toString();
175
176         if (isPreview)
177         {
178             context.genDocuments.putExpandedGenDoc(path,fdata);
179             return fdata;
180         }
181
182         TKLib.spitfile (path,fdata);
183         context.genDocuments.putExpandedGenDoc(path,new Boolean JavaDoc (true));
184         
185         String JavaDoc d = digest (fdata,path);
186         return d;
187     }
188 }
189
Popular Tags