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 18 public class GenDocumentHandle 19 { 20 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 ownUrl; 27 private String rootDir; 28 29 private GenNode genNode; 30 private GenNode srcNode; 31 private String shortName; 32 private String tmplBase; 33 34 public GenDocumentHandle ( 35 GeneratorContext context, boolean isPreview, boolean displace, String ownUrl, 36 GenNode genNode, GenNode srcNode, 37 String shortName, String tmplBase, String 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 digest (String halfDigested, String 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 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 th ) 73 { 74 cat.warn("GenDocumentHandle.digest says: ", th); 75 out.close(); 76 return ""; 77 } 78 return buffer.toString(); 79 } 80 81 public String 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 ( "existing file "+sourceDir.getAbsolutePath() 94 +" prevents creation of new directory" ); 95 } 96 } 97 else 98 { 99 if( !sourceDir.mkdirs() ) 100 { 101 throw new Error ( "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 path = outFile.getAbsolutePath(); 119 120 Object cached = context.genDocuments.getDocumentExpanded (path); 121 if (cached != null) 122 { 123 if (cached instanceof Boolean ) 124 { 125 126 if (!((Boolean ) cached).booleanValue()) 127 { 128 129 cat.warn ("GenDocumentHandle.toString: Infinite loop, do "+ 130 path+" from '"+srcNode.getPath()+"' ***"); 131 context.genDocuments.putExpandedGenDoc(path,new Boolean (true)); 132 if (!isPreview) TKLib.spitfile (path,""); 133 return ""; 134 } else if (!isPreview) 135 { 136 String d = digest (TKLib.slurpfile (path),path); 137 return d; 138 139 } else return ""; 140 141 } else if (isPreview) return (String ) cached; 142 else return ""; 143 } 144 145 context.genDocuments.putExpandedGenDoc(path,new Boolean (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 th ) 170 { 171 cat.warn ("GenDocumentHandle.toString says: ", th); 172 } 173 174 String 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 (true)); 184 185 String d = digest (fdata,path); 186 return d; 187 } 188 } 189 | Popular Tags |