KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SitemapTask


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.io.File JavaDoc;
18 import java.io.FileWriter JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.xml.transform.TransformerException JavaDoc;
28
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Project;
31 import org.apache.tools.ant.types.FileSet;
32 import org.apache.xpath.XPathAPI;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38 import com.thoughtworks.qdox.ant.AbstractQdoxTask;
39 import com.thoughtworks.qdox.model.DocletTag;
40 import com.thoughtworks.qdox.model.JavaClass;
41 import com.thoughtworks.qdox.parser.ParseException;
42
43 /**
44  * Generate documentation for sitemap components based on javadoc tags
45  * in the source of the component.
46  *
47  * This is the first, experimental version - the code is a little bit
48  * hacky but straight forward :)
49  *
50  * @since 2.1.5
51  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
52  * @version CVS $Id: SitemapTask.java 153376 2005-02-11 08:50:21Z cziegeler $
53  */

54 public final class SitemapTask extends AbstractQdoxTask {
55
56     /** The name of the component in the sitemap (required) */
57     public static final String JavaDoc NAME_TAG = "cocoon.sitemap.component.name";
58     /** The logger category (optional) */
59     public static final String JavaDoc LOGGER_TAG = "cocoon.sitemap.component.logger";
60     /** The label for views (optional) */
61     public static final String JavaDoc LABEL_TAG = "cocoon.sitemap.component.label";
62     /** The mime type for serializers and readers (optional) */
63     public static final String JavaDoc MIMETYPE_TAG = "cocoon.sitemap.component.mimetype";
64     /** If this tag is specified, the component is not added to the sitemap (optional) */
65     public static final String JavaDoc HIDDEN_TAG = "cocoon.sitemap.component.hide";
66     /** If this tag is specified no documentation is generated (optional) */
67     public static final String JavaDoc NO_DOC_TAG = "cocoon.sitemap.component.documentation.disabled";
68     /** The documentation (optional) */
69     public static final String JavaDoc DOC_TAG = "cocoon.sitemap.component.documentation";
70     /** Configuration (optional) */
71     public static final String JavaDoc CONF_TAG = "cocoon.sitemap.component.configuration";
72     /** Caching info (optional) */
73     public static final String JavaDoc CACHING_INFO_TAG = "cocoon.sitemap.component.documentation.caching";
74
75     /** Pooling max (optional) */
76     public static final String JavaDoc POOL_MAX_TAG = "cocoon.sitemap.component.pooling.max";
77     
78     private static final String JavaDoc LINE_SEPARATOR = "\n";
79     
80     /** The sitemap */
81     private File JavaDoc sitemap;
82     
83     /** The doc dir */
84     private File JavaDoc docDir;
85     
86     /** Cache for classes */
87     private static Map JavaDoc cache = new HashMap JavaDoc();
88     
89     /** The directory containing the sources*/
90     private String JavaDoc directory;
91     
92     /** The name of the block */
93     protected String JavaDoc blockName;
94     
95     /** Is this block deprecated? */
96     protected boolean deprecated = false;
97     
98     /** Is this block stable? */
99     protected boolean stable = true;
100     
101     /**
102      * Set the directory containing the source files.
103      * Only .java files will be scanned
104      */

105     public void setSource(File JavaDoc dir) {
106         try {
107             this.directory = dir.toURL().toExternalForm();
108             if ( !dir.isDirectory() ) {
109                 throw new BuildException("Source is not a directory.");
110             }
111         } catch (IOException JavaDoc ioe) {
112             throw new BuildException(ioe);
113         }
114         FileSet set = new FileSet();
115         set.setDir(dir);
116         set.setIncludes("**/*.java");
117         super.addFileset(set);
118     }
119     
120     /**
121      * The location of the sitemap
122      */

123     public void setSitemap( final File JavaDoc sitemap ) {
124         this.sitemap = sitemap;
125     }
126
127     /**
128      * The location of the documentation files
129      */

130     public void setDocDir( final File JavaDoc dir ) {
131         this.docDir = dir;
132     }
133     
134     /**
135      * Set the block name
136      */

137     public void setBlock(String JavaDoc value) {
138         this.blockName = value;
139     }
140     
141     /**
142      * Is the block deprecated?
143      */

144     public void setDeprecated(boolean value) {
145         this.deprecated = value;
146     }
147     
148     /**
149      * Is the block stable?
150      */

151     public void setStable(boolean value) {
152         this.stable = value;
153     }
154
155     /**
156      * Determine the type of the class
157     */

158     private String JavaDoc classType(JavaClass clazz) {
159         if ( clazz.isA(GENERATOR) ) {
160             return "generator";
161         } else if ( clazz.isA(TRANSFORMER) ) {
162             return "transformer";
163         } else if ( clazz.isA(READER) ) {
164             return "reader";
165         } else if ( clazz.isA(SERIALIZER) ) {
166             return "serializer";
167         } else if ( clazz.isA(ACTION) ) {
168             return "action";
169         } else if ( clazz.isA(MATCHER) ) {
170             return "matcher";
171         } else if ( clazz.isA(SELECTOR) ) {
172             return "selector";
173         } else if ( clazz.isA(PIPELINE) ) {
174             return "pipe";
175         // Should qdox resolve recursively? ie: HTMLGenerator isA ServiceableGenerator isA AbstractGenerator isA Generator
176
} else if ( clazz.getPackage().equals("org.apache.cocoon.generation") && (clazz.isA("Generator") || clazz.isA("ServiceableGenerator")) ) {
177             return "generator";
178         } else if ( clazz.isA("org.apache.cocoon.generation.ServiceableGenerator") ) {
179             return "generator";
180         } else if ( clazz.getPackage().equals("org.apache.cocoon.transformation") && clazz.isA("Transformer") ) {
181             return "transformer";
182         } else if ( clazz.getPackage().equals("org.apache.cocoon.reading") && clazz.isA("Reader") ) {
183             return "reader";
184         } else if ( clazz.getPackage().equals("org.apache.cocoon.serialization") && clazz.isA("Serializer") ) {
185             return "serializer";
186         } else if ( clazz.getPackage().equals("org.apache.cocoon.acting") && clazz.isA("Action") ) {
187             return "action";
188         } else if ( clazz.getPackage().equals("org.apache.cocoon.matching") && clazz.isA("Matcher") ) {
189             return "matcher";
190         } else if ( clazz.getPackage().equals("org.apache.cocoon.selection") && clazz.isA("Selector") ) {
191             return "selector";
192         } else if ( clazz.getPackage().equals("org.apache.cocoon.components.pipeline") && clazz.isA("ProcessingPipeline") ) {
193             return "pipe";
194         } else {
195             return null;
196         }
197     }
198     
199     /**
200      * Execute generator task.
201      *
202      * @throws BuildException if there was a problem collecting the info
203      */

204     public void execute()
205     throws BuildException {
206         validate();
207
208         List JavaDoc components = (List JavaDoc)cache.get(this.directory);
209         if ( components == null ) {
210             // this does the hard work :)
211
try {
212                 super.execute();
213             }
214             catch (ParseException pe) {
215               log("ParseException: " + pe);
216             }
217             components = this.collectInfo();
218             cache.put(this.directory, components);
219         }
220
221         try {
222             
223             if ( this.sitemap != null ) {
224                 this.processSitemap(components);
225             }
226             if ( this.docDir != null ) {
227                 this.processDocDir(components);
228             }
229             
230         } catch ( final BuildException e ) {
231             throw e;
232         } catch ( final Exception JavaDoc e ) {
233             throw new BuildException( e.toString(), e );
234         }
235     }
236
237     /**
238      * Validate that the parameters are valid.
239      */

240     private void validate() {
241         if ( this.directory == null ) {
242             throw new BuildException("Source is not specified.");
243         }
244         if ( this.sitemap == null && this.docDir == null ) {
245             throw new BuildException("Sitemap or DocDir is not specified.");
246         }
247         
248         if ( this.sitemap != null && this.sitemap.isDirectory() ) {
249             throw new BuildException( "Sitemap (" + this.sitemap + ") is not a file." );
250         }
251         if ( this.docDir != null && !this.docDir.isDirectory() ) {
252             throw new BuildException( "DocDir (" + this.docDir + ") is not a directory." );
253         }
254     }
255
256     /**
257      * Collect the component information
258      */

259     private List JavaDoc collectInfo() {
260         log("Collecting sitemap components info");
261         final List JavaDoc components = new ArrayList JavaDoc();
262         final List JavaDoc allComponentNames = new ArrayList JavaDoc();
263         
264         final Iterator JavaDoc it = super.allClasses.iterator();
265         
266         while ( it.hasNext() ) {
267             final JavaClass javaClass = (JavaClass) it.next();
268
269             final DocletTag tag = javaClass.getTagByName( NAME_TAG );
270             if (classType(javaClass) != null) {
271                 allComponentNames.add(javaClass.getFullyQualifiedName());
272             }
273
274             if ( null != tag ) {
275                 final SitemapComponent comp = new SitemapComponent( javaClass );
276
277                 log("Found component: " + comp, Project.MSG_DEBUG);
278                 components.add(comp);
279             }
280         }
281
282         // Generate a list of all sitemap components
283
final String JavaDoc fileSeparator = System.getProperty("file.separator", "/");
284         final String JavaDoc matchString = "blocks" + fileSeparator;
285         final String JavaDoc outputFname = "build" + fileSeparator + "all-sitemap-components" +
286             (directory.endsWith(matchString) ? "-blocks" : "") + ".txt";
287         log("Listing all sitemap components to " + outputFname);
288         try {
289             File JavaDoc outputFile = new File JavaDoc(outputFname);
290             FileWriter JavaDoc out = new FileWriter JavaDoc(outputFile);
291             final String JavaDoc lineSeparator = System.getProperty("line.separator", "\n");
292             final Iterator JavaDoc iter = allComponentNames.iterator();
293             while ( iter.hasNext() ) {
294                 out.write((String JavaDoc)iter.next());
295                 out.write(lineSeparator);
296             }
297             out.close();
298         }
299         catch (IOException JavaDoc ioe) {
300             log("IOException: " + ioe);
301         }
302
303         return components;
304     }
305
306     /**
307      * Add components to sitemap
308     */

309     private void processSitemap(List JavaDoc components)
310     throws Exception JavaDoc {
311         log("Adding sitemap components");
312         Document JavaDoc document;
313         
314         document = DocumentCache.getDocument(this.sitemap, this);
315         
316         boolean changed = false;
317
318         Iterator JavaDoc iter = components.iterator();
319         while ( iter.hasNext() ) {
320             SitemapComponent component = (SitemapComponent)iter.next();
321             final String JavaDoc type = component.getType();
322             final String JavaDoc section = type + 's';
323             
324             NodeList JavaDoc nodes = XPathAPI.selectNodeList(document, "/sitemap/components/" + section);
325
326             if (nodes.getLength() != 1 ) {
327                 throw new BuildException("Unable to find section for component type " + type);
328             }
329             // remove old node!
330
NodeList JavaDoc oldNodes = XPathAPI.selectNodeList(document,
331                     "/sitemap/components/" + section + '/' + type + "[@name='" + component.getName() + "']");
332             for(int i=0; i < oldNodes.getLength(); i++ ) {
333                 final Node JavaDoc node = oldNodes.item(i);
334                 node.getParentNode().removeChild(node);
335             }
336             
337             // and add it again
338
if (component.append(nodes.item(0)) ) {
339                 changed = true;
340             }
341             
342         }
343         
344         if ( changed ) {
345             DocumentCache.writeDocument(this.sitemap, document, this);
346         }
347         DocumentCache.storeDocument(this.sitemap, document, this);
348     }
349     
350     /**
351      * Add components to sitemap
352     */

353     private void processDocDir(List JavaDoc components)
354     throws Exception JavaDoc {
355         log("Generating documentation");
356
357         Iterator JavaDoc iter = components.iterator();
358         while ( iter.hasNext() ) {
359             final SitemapComponent component = (SitemapComponent)iter.next();
360             
361             // Read template
362
final File JavaDoc templateFile = this.getProject().resolveFile("src/documentation/templates/sitemap-component.xml");
363             Document JavaDoc template = DocumentCache.getDocument(templateFile, this);
364             
365             // create directory - if required
366
final File JavaDoc componentsDir = new File JavaDoc(this.docDir, component.getType()+'s');
367             componentsDir.mkdir();
368             
369             // get file name
370
String JavaDoc fileName = component.getName() + "-" + component.getType() + ".xml";
371             if ( this.blockName != null ) {
372                 fileName = this.blockName + "-" + fileName;
373             }
374             final File JavaDoc docFile = new File JavaDoc(componentsDir, fileName);
375             
376             // generate the doc
377
component.generateDocs(template, docFile, this.getProject());
378             
379             // generate the index
380
final File JavaDoc indexFile = new File JavaDoc(componentsDir, "book.xml");
381             final Document JavaDoc indexDoc = DocumentCache.getDocument(indexFile, this);
382             final String JavaDoc section;
383             if ( this.blockName == null ) {
384                 section = "Core";
385             } else {
386                 section = this.blockName + " Block";
387             }
388             Node JavaDoc sectionNode = XPathAPI.selectSingleNode(indexDoc, "/book/menu[@label='"+section+"']");
389             if ( sectionNode == null ) {
390                 sectionNode = indexDoc.createElement("menu");
391                 ((Element JavaDoc)sectionNode).setAttribute("label", section);
392                 indexDoc.getDocumentElement().appendChild(sectionNode);
393             }
394             final String JavaDoc htmlName = docFile.getName().substring(0, docFile.getName().length()-3) + "html";
395             Node JavaDoc oldEntry = XPathAPI.selectSingleNode(sectionNode, "menu-item[@href='"+htmlName+"']");
396             Node JavaDoc newEntry = indexDoc.createElement("menu-item");
397             ((Element JavaDoc)newEntry).setAttribute("href", htmlName);
398             final String JavaDoc label = capitalize(component.getName()) + " " + capitalize(component.getType());
399             ((Element JavaDoc)newEntry).setAttribute("label", label);
400             if ( oldEntry != null ) {
401                 oldEntry.getParentNode().replaceChild(newEntry, oldEntry);
402             } else {
403                 Node JavaDoc nextLabel = null;
404                 final NodeList JavaDoc childs = sectionNode.getChildNodes();
405                 int i = 0;
406                 while ( nextLabel == null && i < childs.getLength() ) {
407                     final Node JavaDoc current = childs.item(i);
408                     if ( current instanceof Element JavaDoc ) {
409                         final String JavaDoc currentLabel = ((Element JavaDoc)current).getAttribute("label");
410                         if ( label.compareTo(currentLabel) < 0 ) {
411                             nextLabel = current;
412                         }
413                     }
414                     i++;
415                 }
416                 if ( nextLabel == null ) {
417                     sectionNode.appendChild(newEntry);
418                 } else {
419                     sectionNode.insertBefore(newEntry, nextLabel);
420                 }
421             }
422             DocumentCache.writeDocument(indexFile, indexDoc, this);
423         }
424         
425     }
426
427     /**
428      * Helper method to capitalize a string.
429      * This is taken from commons-lang, but we don't want the dependency
430      * right now!
431      */

432     public static String JavaDoc capitalize(String JavaDoc str) {
433         int strLen;
434         if (str == null || (strLen = str.length()) == 0) {
435             return str;
436         }
437         return new StringBuffer JavaDoc(strLen)
438             .append(Character.toTitleCase(str.charAt(0)))
439             .append(str.substring(1))
440             .toString();
441     }
442
443     final class SitemapComponent {
444         
445         final protected JavaClass javaClass;
446         final String JavaDoc name;
447         final String JavaDoc type;
448         
449         public SitemapComponent(JavaClass javaClass) {
450             this.javaClass = javaClass;
451             
452             this.name = this.javaClass.getTagByName( NAME_TAG ).getValue();
453             // TEST CODE
454
System.out.println("Name: " + this.name);
455             System.out.println("className: " + this.javaClass.getName());
456             System.out.println();
457             
458             
459             JavaClass[] jc = this.javaClass.getImplementedInterfaces();
460             if (jc.length> 0) {
461                 System.out.println("Implemented interfaces:");
462             }
463             for (int i = 0; i < jc.length; i++) {
464                 System.out.println(jc[i].getName() + ". Full name: " + jc[i].getFullyQualifiedName());
465             }
466             System.out.println("==== END of implements ===");
467             // END TEST CODE
468
this.type = classType(this.javaClass);
469         }
470         
471         /* (non-Javadoc)
472          * @see java.lang.Object#toString()
473          */

474         public String JavaDoc toString() {
475             return "Sitemap component: " + this.javaClass.getName();
476         }
477         
478         public String JavaDoc getType() {
479             return this.type;
480         }
481         
482         public String JavaDoc getName() {
483             return this.name;
484         }
485         
486         public boolean append(Node JavaDoc parent) {
487             if ( this.getTagValue(HIDDEN_TAG, null) != null ) {
488                 return false;
489             }
490             Document JavaDoc doc = parent.getOwnerDocument();
491             Node JavaDoc node;
492             
493             // first check: deprecated?
494
if ( SitemapTask.this.deprecated || this.getTagValue("deprecated", null) != null ) {
495                 indent(parent, 3);
496                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("The ");
497                 buffer.append(this.type)
498                 .append(" ")
499                 .append(this.name)
500                 .append(" is deprecated");
501                 node = doc.createComment(buffer.toString());
502                 parent.appendChild(node);
503                 newLine(parent);
504             }
505             // unstable block?
506
if ( !SitemapTask.this.stable ) {
507                 indent(parent, 3);
508                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("The ");
509                 buffer.append(this.type)
510                 .append(" ")
511                 .append(this.name)
512                 .append(" is in an unstable block");
513                 node = doc.createComment(buffer.toString());
514                 parent.appendChild(node);
515                 newLine(parent);
516             }
517             
518             indent(parent, 3);
519             node = doc.createElement("map:" + this.type);
520             ((Element JavaDoc)node).setAttribute("name", this.name);
521             ((Element JavaDoc)node).setAttribute("src", this.javaClass.getFullyQualifiedName());
522             
523             // test for logger
524
// TODO Default logger?
525
if ( this.javaClass.isA(LOG_ENABLED) ) {
526                 this.addAttribute(node, LOGGER_TAG, "logger", null);
527             }
528             
529             // test for label
530
this.addAttribute(node, LABEL_TAG, "label", null);
531
532             // pooling?
533
if ( this.javaClass.isA(POOLABLE) ) {
534                 // TODO - Think about default values
535
this.addAttribute(node, POOL_MAX_TAG, "pool-max", null);
536             }
537             
538             // mime-type
539
if ( this.javaClass.isA(OUTPUT_COMPONENT) ) {
540                 this.addAttribute(node, MIMETYPE_TAG, "mime-type", null);
541             }
542             
543             // append node
544
parent.appendChild(node);
545             newLine(parent);
546             
547             // add configuration
548
String JavaDoc configuration = this.getTagValue(CONF_TAG, null);
549             if ( configuration != null ) {
550                 configuration = "<root>" + configuration + "</root>";
551                 final Document JavaDoc confDoc = DocumentCache.getDocument(configuration, null);
552                 setValue(node, null, confDoc.getDocumentElement().getChildNodes());
553             }
554
555             return true;
556         }
557         
558         private void addAttribute(Node JavaDoc node, String JavaDoc tag, String JavaDoc attributeName, String JavaDoc defaultValue) {
559             final String JavaDoc tagValue = this.getTagValue(tag, defaultValue);
560             if ( tagValue != null ) {
561                 ((Element JavaDoc)node).setAttribute(attributeName, tagValue);
562             }
563         }
564         
565         private void newLine(Node JavaDoc node) {
566             final Node JavaDoc n = node.getOwnerDocument().createTextNode(LINE_SEPARATOR);
567             node.appendChild(n);
568         }
569         
570         private void indent(Node JavaDoc node, int depth) {
571             final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
572             for(int i=0; i < depth*2; i++ ) {
573                 buffer.append(' ');
574             }
575             final Node JavaDoc n = node.getOwnerDocument().createTextNode(buffer.toString());
576             node.appendChild(n);
577         }
578         
579         public void generateDocs(Document JavaDoc template, File JavaDoc docFile, Project project)
580         throws TransformerException JavaDoc {
581
582             final String JavaDoc doc = this.getDocumentation();
583             if ( doc == null ) {
584                 if ( docFile.exists() ) {
585                     docFile.delete();
586                 }
587                 return;
588             }
589             // get body from template
590
final Node JavaDoc body = XPathAPI.selectSingleNode(template, "/document/body");
591             
592             // append root element and surrounding paragraph
593
final String JavaDoc description = "<root><p>" + doc + "</p></root>";
594             String JavaDoc systemURI = null;
595             try {
596                 systemURI = docFile.toURL().toExternalForm();
597             } catch (MalformedURLException JavaDoc mue) {
598                 // we ignore this
599
}
600             final Document JavaDoc descriptionDoc = DocumentCache.getDocument(description, systemURI);
601             
602             // Title
603
setValue(template, "/document/header/title",
604                      "Description of the " + this.name + " " + this.type);
605             
606             // Version
607
setValue(template, "/document/header/version",
608                      project.getProperty("version"));
609             
610             // Description
611
setValue(body, "s1[@title='Description']",
612                      descriptionDoc.getDocumentElement().getChildNodes());
613             
614             // check: deprecated?
615
if ( SitemapTask.this.deprecated || this.getTagValue("deprecated", null) != null ) {
616                 Node JavaDoc node = XPathAPI.selectSingleNode(body, "s1[@title='Description']");
617                 // node is never null - this is ensured by the test above
618
Element JavaDoc e = node.getOwnerDocument().createElement("note");
619                 node.appendChild(e);
620                 e.appendChild(node.getOwnerDocument().createTextNode("This component is deprecated."));
621                 final String JavaDoc info = this.getTagValue("deprecated", null);
622                 if ( info != null ) {
623                     e.appendChild(node.getOwnerDocument().createTextNode(info));
624                 }
625             }
626             // check: stable?
627
if ( !SitemapTask.this.stable ) {
628                 Node JavaDoc node = XPathAPI.selectSingleNode(body, "s1[@title='Description']");
629                 // node is never null - this is ensured by the test above
630
Element JavaDoc e = node.getOwnerDocument().createElement("note");
631                 node.appendChild(e);
632                 e.appendChild(node.getOwnerDocument().createTextNode("This component is in an unstable block."));
633             }
634             
635             // Info Table
636
final Node JavaDoc tableNode = XPathAPI.selectSingleNode(body, "s1[@title='Info']/table");
637             
638             // Info - Name
639
this.addRow(tableNode, "Name", this.name);
640
641             // Info - Block
642
if ( SitemapTask.this.blockName != null ) {
643                 this.addRow(tableNode, "Block", SitemapTask.this.blockName);
644             }
645             
646             // Info - Cacheable
647
if ( this.javaClass.isA(GENERATOR)
648                  || this.javaClass.isA(TRANSFORMER)
649                  || this.javaClass.isA(SERIALIZER)
650                  || this.javaClass.isA(READER)) {
651                 
652                 String JavaDoc cacheInfo;
653                 if ( this.javaClass.isA(CACHEABLE) ) {
654                     cacheInfo = this.getTagValue(CACHING_INFO_TAG, null);
655                     if ( cacheInfo != null ) {
656                         cacheInfo = "Yes - " + cacheInfo;
657                     } else {
658                         cacheInfo = "Yes";
659                     }
660                 } else if ( this.javaClass.isA(DEPRECATED_CACHEABLE) ) {
661                     cacheInfo = this.getTagValue(CACHING_INFO_TAG, null);
662                     if ( cacheInfo != null ) {
663                         cacheInfo = "Yes (2.0 Caching) - " + cacheInfo;
664                     } else {
665                         cacheInfo = "Yes (2.0 Caching)";
666                     }
667                 } else {
668                     cacheInfo = "No";
669                 }
670                 this.addRow(tableNode, "Cacheable", cacheInfo);
671             }
672             
673             // Info - mime-type
674
if ( this.javaClass.isA(OUTPUT_COMPONENT) ) {
675                 final String JavaDoc value = this.getTagValue(MIMETYPE_TAG, "-");
676                 this.addRow(tableNode, "Mime-Type", value);
677             }
678             
679             // Info - Class
680
this.addRow(tableNode, "Class", this.javaClass.getFullyQualifiedName());
681
682             // merge with old doc
683
this.merge(body, docFile);
684             
685             // finally write the doc
686
DocumentCache.writeDocument(docFile, template, SitemapTask.this);
687         }
688         
689         /**
690          * Merge the sections of the old document with the new generated one.
691          * All sections (s1) of the old document are added to the generated one
692          * if not a section with the same title exists.
693          */

694         private void merge(Node JavaDoc body, File JavaDoc docFile) throws TransformerException JavaDoc {
695             final Document JavaDoc mergeDocument;
696             try {
697                 mergeDocument = DocumentCache.getDocument(docFile, SitemapTask.this);
698             } catch (Exception JavaDoc ignore) {
699                 return;
700             }
701             NodeList JavaDoc sections = XPathAPI.selectNodeList(mergeDocument, "/document/body/s1");
702             if ( sections != null ) {
703                 for(int i=0; i<sections.getLength(); i++) {
704                     final Element JavaDoc current = (Element JavaDoc)sections.item(i);
705                     final String JavaDoc title = current.getAttribute("title");
706
707                     // is this section not in the template?
708
if (XPathAPI.selectSingleNode(body, "s1[@title='"+title+"']") == null ) {
709                         body.appendChild(body.getOwnerDocument().importNode(current, true));
710                     }
711                 }
712             }
713         }
714         
715         /**
716          * Return the documentation or null
717          * @return
718          */

719         private String JavaDoc getDocumentation() {
720             if ( this.getTagValue(NO_DOC_TAG, null) != null ) {
721                 return null;
722             }
723             return this.getTagValue(DOC_TAG, null);
724         }
725         
726         private String JavaDoc getTagValue(String JavaDoc tagName, String JavaDoc defaultValue) {
727             final DocletTag tag = this.javaClass.getTagByName( tagName );
728             if ( tag != null ) {
729                 return tag.getValue();
730             }
731             return defaultValue;
732         }
733         
734         private void addRow(Node JavaDoc table, String JavaDoc title, String JavaDoc value) {
735             final Element JavaDoc row = table.getOwnerDocument().createElement("tr");
736             final Element JavaDoc firstColumn = table.getOwnerDocument().createElement("td");
737             firstColumn.appendChild(table.getOwnerDocument().createTextNode(title));
738             final Element JavaDoc secondColumn = table.getOwnerDocument().createElement("td");
739             secondColumn.appendChild(table.getOwnerDocument().createTextNode(value));
740             row.appendChild(firstColumn);
741             row.appendChild(secondColumn);
742             table.appendChild(row);
743         }
744
745         private void setValue(Node JavaDoc node, String JavaDoc xpath, String JavaDoc value) {
746             try {
747                 final Node JavaDoc insertNode = (xpath == null ? node : XPathAPI.selectSingleNode(node, xpath));
748                 if ( insertNode == null ) {
749                     throw new BuildException("Node (" + xpath + ") not found.");
750                 }
751                 Node JavaDoc text = insertNode.getOwnerDocument().createTextNode(value);
752                 while (insertNode.hasChildNodes() ) {
753                     insertNode.removeChild(insertNode.getFirstChild());
754                 }
755                 insertNode.appendChild(text);
756             } catch (TransformerException JavaDoc e) {
757                 throw new BuildException(e);
758             }
759         }
760
761         private void setValue(Node JavaDoc node, String JavaDoc xpath, NodeList JavaDoc nodes) {
762             try {
763                 final Node JavaDoc insertNode = (xpath == null ? node : XPathAPI.selectSingleNode(node, xpath));
764                 if ( insertNode == null ) {
765                     throw new BuildException("Node (" + xpath + ") not found.");
766                 }
767                 while (insertNode.hasChildNodes() ) {
768                     insertNode.removeChild(insertNode.getFirstChild());
769                 }
770                 for(int i=0; i<nodes.getLength(); i++) {
771                     final Node JavaDoc current = nodes.item(i);
772                     insertNode.appendChild(insertNode.getOwnerDocument().importNode(current, true));
773                 }
774             } catch (TransformerException JavaDoc e) {
775                 throw new BuildException(e);
776             }
777         }
778     }
779     
780     // Class Constants
781
private static final String JavaDoc LOG_ENABLED = "org.apache.avalon.framework.logger.LogEnabled";
782     private static final String JavaDoc POOLABLE = "org.apache.avalon.excalibur.pool.Poolable";
783
784     private static final String JavaDoc CACHEABLE = "org.apache.cocoon.caching.CacheableProcessingComponent";
785     private static final String JavaDoc DEPRECATED_CACHEABLE = "org.apache.cocoon.caching.Cacheable";
786     
787     private static final String JavaDoc OUTPUT_COMPONENT = "org.apache.cocoon.sitemap.SitemapOutputComponent";
788
789     private static final String JavaDoc GENERATOR = "org.apache.cocoon.generation.Generator";
790     private static final String JavaDoc TRANSFORMER = "org.apache.cocoon.transformation.Transformer";
791     private static final String JavaDoc SERIALIZER = "org.apache.cocoon.serialization.Serializer";
792     private static final String JavaDoc READER = "org.apache.cocoon.reading.Reader";
793     private static final String JavaDoc MATCHER = "org.apache.cocoon.matching.Matcher";
794     private static final String JavaDoc SELECTOR = "org.apache.cocoon.selection.Selector";
795     private static final String JavaDoc ACTION = "org.apache.cocoon.acting.Action";
796     private static final String JavaDoc PIPELINE = "org.apache.cocoon.components.pipeline.ProcessingPipeline";
797 }
798
Popular Tags