KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > generation > SiteTreeGenerator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  */

17
18 /* $Id:$ */
19
20 package org.apache.lenya.cms.cocoon.generation;
21
22 import java.io.IOException JavaDoc;
23 import java.io.Serializable JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.parameters.ParameterException;
27 import org.apache.avalon.framework.parameters.Parameterizable;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.cocoon.ProcessingException;
30 import org.apache.cocoon.caching.CacheableProcessingComponent;
31 import org.apache.cocoon.environment.SourceResolver;
32 import org.apache.cocoon.generation.ServiceableGenerator;
33 import org.apache.excalibur.source.SourceValidity;
34 import org.apache.excalibur.source.impl.validity.TimeStampValidity;
35 import org.apache.lenya.cms.publication.Label;
36 import org.apache.lenya.cms.publication.LastModified;
37 import org.apache.lenya.cms.publication.Publication;
38 import org.apache.lenya.cms.publication.PublicationException;
39 import org.apache.lenya.cms.publication.PublicationFactory;
40 import org.apache.lenya.cms.publication.SiteTree;
41 import org.apache.lenya.cms.publication.SiteTreeException;
42 import org.apache.lenya.cms.publication.SiteTreeNode;
43 import org.apache.log4j.Logger;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.helpers.AttributesImpl JavaDoc;
46 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
47
48 /**
49  * Site tree generator.
50  *
51  * @version $Id:$
52  */

53 public class SiteTreeGenerator extends ServiceableGenerator implements Parameterizable,
54         CacheableProcessingComponent {
55     private static Logger log = Logger.getLogger(SiteTreeGenerator.class);
56
57     protected final static String JavaDoc I18N_PX = "i18n";
58     protected final static String JavaDoc I18N_NS = "http://apache.org/cocoon/i18n/2.1";
59
60     protected final static String JavaDoc CDATA = "CDATA";
61
62     protected final static String JavaDoc SITE_ELEMENT = "site";
63     protected final static String JavaDoc NODE_ELEMENT = "node";
64     protected final static String JavaDoc LABEL_ELEMENT = "label";
65
66     protected final static String JavaDoc LABEL_ATTRIBUTE = "label";
67     protected final static String JavaDoc ATTR_ATTRIBUTE = "attr";
68     protected final static String JavaDoc Q_ATTR_ATTRIBUTE = I18N_PX + ":" + ATTR_ATTRIBUTE;
69     protected final static String JavaDoc ID_ATTRIBUTE = "id";
70     protected final static String JavaDoc LANG_ATTRIBUTE = "lang";
71     protected final static String JavaDoc HREF_ATTRIBUTE = "href";
72     protected final static String JavaDoc LINK_ATTRIBUTE = "link";
73     protected final static String JavaDoc VISIBLEINNAV_ATTRIBUTE = "visibleinnav";
74     protected final static String JavaDoc SUFFIX_ATTRIBUTE = "suffix";
75
76     protected final static String JavaDoc Q_LANG_ATTRIBUTE = "xml:lang";
77
78     /**
79      * The area of the site tree.
80      */

81     public final static String JavaDoc AREA_PARAMETER = "area";
82
83     private final AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
84
85     SiteTree sitetree = null;
86     String JavaDoc area = null;
87
88     /**
89      * No parameters implemented.
90      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
91      */

92     public void parameterize(Parameters parameters) throws ParameterException {
93     }
94
95     /**
96      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
97      * java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
98      */

99     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
100             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
101         log.debug("setup");
102         try {
103             Publication publication = PublicationFactory.getPublication(objectModel);
104             area = par.getParameter(AREA_PARAMETER);
105             sitetree = publication.getTree(area);
106         } catch (PublicationException e) {
107             throw new ProcessingException("Unable to get sitetree: publication exception.", e);
108         } catch (ParameterException e) {
109             throw new ProcessingException("Unable to get sitetree: parameter 'area' not found.", e);
110         } catch (SiteTreeException e) {
111             throw new ProcessingException("Unable to get sitetree.", e);
112         }
113     }
114
115     /**
116      * @see org.apache.cocoon.generation.Generator#generate()
117      */

118     public void generate() throws SAXException JavaDoc {
119         log.debug("generate");
120         // Start the document and set the namespace.
121
this.contentHandler.startDocument();
122         // Default namespace.
123
this.contentHandler.startPrefixMapping("", SiteTree.NAMESPACE_URI);
124         this.contentHandler.startPrefixMapping(I18N_PX, I18N_NS);
125
126         generateSiteTree(sitetree);
127
128         // End the document.
129
this.contentHandler.endPrefixMapping("");
130         this.contentHandler.endDocument();
131     }
132
133     private void generateSiteTree(SiteTree tree) throws SAXException JavaDoc {
134         atts.clear();
135         // TODO: Do not hardcode "Authoring" label!!!
136
atts.addAttribute("", LABEL_ATTRIBUTE, LABEL_ATTRIBUTE, CDATA, "Authoring");
137         atts.addAttribute(I18N_NS, ATTR_ATTRIBUTE, Q_ATTR_ATTRIBUTE, CDATA, "label");
138
139         this.contentHandler.startElement(SiteTree.NAMESPACE_URI, SITE_ELEMENT, SITE_ELEMENT, atts);
140
141         SiteTreeNode[] topNodes = tree.getTopNodes();
142         for (int i = 0; i < topNodes.length; i++) {
143             generateNodes(topNodes[i]);
144         }
145
146         this.contentHandler.endElement(SiteTree.NAMESPACE_URI, SITE_ELEMENT, SITE_ELEMENT);
147     }
148
149     private void generateNodes(SiteTreeNode node) throws SAXException JavaDoc {
150         atts.clear();
151         atts.addAttribute("", ID_ATTRIBUTE, ID_ATTRIBUTE, CDATA, node.getId());
152         if (node.getHref() != null)
153             atts.addAttribute("", HREF_ATTRIBUTE, HREF_ATTRIBUTE, CDATA, node.getHref());
154         if (node.getSuffix() != null)
155             atts.addAttribute("", SUFFIX_ATTRIBUTE, SUFFIX_ATTRIBUTE, CDATA, node.getSuffix());
156         atts.addAttribute("", LINK_ATTRIBUTE, LINK_ATTRIBUTE, CDATA, Boolean.toString(node
157                 .hasLink()));
158         atts.addAttribute("", VISIBLEINNAV_ATTRIBUTE, VISIBLEINNAV_ATTRIBUTE, CDATA, Boolean
159                 .toString(node.visibleInNav()));
160
161         this.contentHandler.startElement(SiteTree.NAMESPACE_URI, NODE_ELEMENT, NODE_ELEMENT, atts);
162
163         Label[] labels = node.getLabels();
164         for (int i = 0; i < labels.length; i++)
165             generateLabels(labels[i]);
166         SiteTreeNode[] children = node.getChildren();
167         for (int i = 0; i < children.length; i++)
168             generateNodes(children[i]);
169
170         this.contentHandler.endElement(SiteTree.NAMESPACE_URI, NODE_ELEMENT, NODE_ELEMENT);
171     }
172
173     private void generateLabels(Label label) throws SAXException JavaDoc {
174         atts.clear();
175         atts.addAttribute(NamespaceSupport.XMLNS, LANG_ATTRIBUTE, Q_LANG_ATTRIBUTE, CDATA, label
176                 .getLanguage());
177
178         this.contentHandler
179                 .startElement(SiteTree.NAMESPACE_URI, LABEL_ELEMENT, LABEL_ELEMENT, atts);
180         char[] labelA = label.getLabel().toCharArray();
181         this.contentHandler.characters(labelA, 0, labelA.length);
182         this.contentHandler.endElement(SiteTree.NAMESPACE_URI, LABEL_ELEMENT, LABEL_ELEMENT);
183     }
184
185     /**
186      * Recycle the generator
187      */

188     public void recycle() {
189         log.debug("recycle");
190         super.recycle();
191         sitetree = null;
192         area = null;
193     }
194
195     /**
196      * @see org.apache.cocoon.caching.CacheableProcessingComponent#getKey()
197      */

198     public Serializable JavaDoc getKey() {
199         return area;
200     }
201
202     /**
203      * @see org.apache.cocoon.caching.CacheableProcessingComponent#getValidity()
204      */

205     public SourceValidity getValidity() {
206         // Check if sitetree implementation supports last modified
207
if (!(sitetree instanceof LastModified)) {
208             return null;
209         } else {
210             return new TimeStampValidity(((LastModified) sitetree).getLastModified());
211         }
212     }
213 }
Popular Tags