KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.generator;
2
3 import java.io.*;
4 import java.sql.*;
5
6 import com.teamkonzept.lib.*;
7 import com.teamkonzept.db.*;
8 //import com.teamkonzept.webman.TKWebmanInit;
9
import com.teamkonzept.field.TKUploadField;
10 import com.teamkonzept.webman.db.TKWebmanDBManager;
11 import org.apache.log4j.Category;
12 import de.webman.content.Content;
13 import de.webman.util.log4j.WebmanCategory;
14 /**
15 This class makes a (fast) validation of a webman site
16 and checks if it can be generated.
17 Checks performed at the moment are
18     1. existence of all needed templates
19     2. existence of generatable contents for all single contents.
20 Output is written to three different writers:
21 The standard writer is used for normal messages like "validation starts" etc.
22 The warning writer is used for warnings and the error writer for errors.
23 Of course you can also use _one_ writer for all message channels.
24
25 To use the class, first create an instance of it.
26 Set the Writers for the thrre channels (if no writer is specified, all
27 output is written to stdout).
28 Call initDB() [optional] if database initialization should be done (else an
29 already open connection is used).
30 Call validate() to perform the validation. You can also call
31 apply(SiteTree tree), if you only want to check a given site tree.
32 Call deinitDB() [optional] if database deinitialization should be done.
33  * @author $Author: mischa $
34  * @version $Revision: 1.7 $
35 */

36 public class SiteTreeValidator implements SiteTreeVisitor
37 {
38     
39     /** Log4J Category */
40     private static WebmanCategory cat = (WebmanCategory)WebmanCategory.getInstance(SiteTreeValidator.class);
41     
42     private String JavaDoc docRootPath;
43     private String JavaDoc templatePath;
44     private int numErrors;
45     private int numWarnings;
46         
47     /**
48     Constructor
49     @param docRootPath the (absolute) path to the document root
50     @param templatePath the (absolute) path to the templates
51     */

52     public SiteTreeValidator (String JavaDoc docRootPath, String JavaDoc templatePath)
53     {
54         this.docRootPath = docRootPath;
55         this.templatePath = templatePath;
56     }
57     
58     /**
59         Inits the database
60     */

61     public void initDB() throws TKException
62     {
63         TKWebmanDBManager.initConnection("/webmandb.ini", false);
64         // TKWebmanInit.init(docRootPath); // necessary??
65
TKUploadField.initStaticsForUpload(docRootPath);
66     }
67         
68     /**
69     Deinits the database.
70     Meldet den aktuellen Thread beim DBManager ab. doClose bestimmt, ob
71     alle vom aktuellen Tread gehaltenen Verbindungen geschlossen oder nur
72     freigegeben werden.
73     */

74     public void deinitDB(boolean doClose) throws TKException
75     {
76         try
77         {
78             TKWebmanDBManager.deregister(doClose);
79         }
80         catch (SQLException sqle)
81         {
82             throw new TKSQLError(sqle);
83         }
84     }
85     
86     /**
87     validates the site.
88     Throws an exception, if the validation could not be completed.
89     */

90     public void validate () throws Throwable JavaDoc
91     {
92         GeneratorContext context = GeneratorContext.setup();
93         SiteNode site;
94         try
95         {
96             site = context.siteNodes.buildSiteTree();
97         }
98         catch (Throwable JavaDoc t)
99         {
100             cat.error("Unable to complete validation due to the following error:", t);
101             throw t;
102         }
103         apply(site);
104     }
105     
106     /**
107     validates the site
108     @param node the site node to validate
109     */

110     public void apply(SiteNode node)
111     {
112         if (node == null)
113             return;
114     
115         numErrors = 0;
116         numWarnings = 0;
117         
118         try
119         {
120             node.accept(this);
121         }
122         catch (Exception JavaDoc e)
123         {
124             cat.warn("SiteTreeValidator.apply ", e);
125         }
126         cat.info("...validation finished with " + numErrors
127             + " errors and " + numWarnings + " warnings");
128     }
129     
130     public void visitSiteNode(SiteNode node) throws Exception JavaDoc
131     {
132         // JTest zwing mich zu etwas !!
133
node.getLevel();
134     }
135         
136     public void visitSiteDocument(SiteDocument doc) throws Exception JavaDoc
137     {
138         checkTemplate(doc);
139         
140         for (int i = 0; i < doc.getContents().size(); ++i)
141         {
142             checkContent((SiteContentIntegration) doc.getContents().elementAt(i));
143         }
144     }
145     
146     private void checkTemplate(SiteDocument doc)
147     {
148         String JavaDoc templateName = templatePath + File.separator + doc.getTemplateName();
149        
150         try
151         {
152             FileReader fis = new FileReader(templateName);
153             if (fis.ready() == false)
154                 throw new IOException();
155             fis.close();
156         }
157         catch (FileNotFoundException e)
158         {
159             cat.error("Error: Unable to find template "
160                 + templateName + " used by document " + doc.getPath());
161         }
162         catch (IOException e)
163         {
164             cat.error("Error: Unable to read from file "
165                 + templateName + " used by document " + doc.getPath());
166         }
167     }
168     
169     private void checkContent(SiteContentIntegration contentInt)
170     {
171         if (contentInt == null)
172         {
173             return;
174         }
175             
176         SiteContentNode contentNode = contentInt.getContentNode();
177         if (contentNode == null)
178         {
179             return;
180         }
181         // Content singleContent = null;
182
for (int i = 0; i < contentNode.getContents().size(); ++i)
183         {
184             SiteContent content = (SiteContent) contentNode.getContents().elementAt(i);
185             if (content.isGeneratableVersion()) {
186                 return; // We found at least one generatable content...
187
}
188         }
189         
190         if (contentNode.getType() == SiteContentNode.NODE_TYPE_GROUP)
191         {
192             cat.warn("Warnung: Group Content " + contentNode.getShortName() + " attached to presentation component "
193             + contentInt.getShortName() + " of site document " + contentInt.getSourceDocument().path()
194             + " does not contain any released or valid version", WebmanCategory.ALL);
195         }
196         else
197         {
198             cat.error("Error: Single Content " + contentNode.getShortName() + " attached to presentation component "
199             + contentInt.getShortName() + " of site document " + contentInt.getSourceDocument().path()
200             + " is not a released version", WebmanCategory.ALL);
201         }
202     }
203     
204 }
205
Popular Tags