KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > transformation > DocumentIndexTransformer


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
18 /* $Id: DocumentIndexTransformer.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.cocoon.transformation;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.avalon.framework.parameters.ParameterException;
29 import org.apache.avalon.framework.parameters.Parameterizable;
30 import org.apache.avalon.framework.parameters.Parameters;
31 import org.apache.cocoon.ProcessingException;
32 import org.apache.cocoon.environment.SourceResolver;
33 import org.apache.cocoon.transformation.AbstractSAXTransformer;
34 import org.apache.lenya.cms.publication.Document;
35 import org.apache.lenya.cms.publication.DocumentBuildException;
36 import org.apache.lenya.cms.publication.DocumentBuilder;
37 import org.apache.lenya.cms.publication.DocumentException;
38 import org.apache.lenya.cms.publication.PageEnvelope;
39 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
40 import org.apache.lenya.cms.publication.Publication;
41 import org.apache.lenya.cms.publication.SiteTree;
42 import org.apache.lenya.cms.publication.SiteTreeNode;
43 import org.xml.sax.Attributes JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.helpers.AttributesImpl JavaDoc;
46
47 /**
48  * This transformer lists the children of a document if the tag <namespaceURI:children>
49  * is present in this document. The list of the children is in the form :
50  * <namespaceURI:children>
51  * <child HREF="....html>
52  * <ci:include SRC="..." element="included"/>
53  * </child>
54  * ...
55  * </namespaceURI:children>
56  * Multiple language : if a child doesn't exist in the parent language, then the version
57  * in the default language will be considered. If it doesn't exist too, any other existent
58  * language will be considered.
59  */

60 public class DocumentIndexTransformer extends AbstractSAXTransformer implements Parameterizable {
61
62     private String JavaDoc namespace;
63     private String JavaDoc cIncludeNamespace;
64
65     public static final String JavaDoc CHILDREN_ELEMENT = "children";
66     public static final String JavaDoc ABSTRACT_ATTRIBUTE = "abstract";
67     
68     public static final String JavaDoc NAMESPACE = "http://apache.org/cocoon/lenya/documentindex/1.0";
69     public static final String JavaDoc PREFIX = "index:";
70
71     /** (non-Javadoc)
72          * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
73          */

74     public void parameterize(Parameters parameters) throws ParameterException {
75         this.namespace = parameters.getParameter("namespace", null);
76         this.cIncludeNamespace = parameters.getParameter("cIncludeNamespace", null);
77     }
78
79     private Document document;
80
81     private Publication publication;
82
83     private String JavaDoc area;
84
85     private DocumentBuilder builder;
86
87     private SiteTree siteTree;
88
89     /** (non-Javadoc)
90      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
91      */

92     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
93         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
94         try {
95
96             super.setup(resolver, objectModel, src, parameters);
97
98             parameterize(parameters);
99
100             PageEnvelope envelope = null;
101             envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
102
103             setDocument(envelope.getDocument());
104             setPublication(document.getPublication());
105             setArea(document.getArea());
106             setBuilder(document.getPublication().getDocumentBuilder());
107             setSiteTree(publication.getTree(area));
108
109         } catch (Exception JavaDoc e) {
110             throw new ProcessingException(e);
111         }
112
113     }
114
115     /** (non-Javadoc)
116      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
117      */

118     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc raw, Attributes JavaDoc attr)
119         throws SAXException JavaDoc {
120
121         if (uri != null
122             && uri.equals(namespace)
123             && cIncludeNamespace != null
124             && localName.equals(CHILDREN_ELEMENT)) {
125                 
126             if (getLogger().isInfoEnabled()) {
127                 getLogger().info("Inserting index");
128             }
129
130             String JavaDoc cIncludePrefix = "";
131             if (!this.cIncludeNamespace.equals("")) {
132                 cIncludePrefix = "ci:";
133             }
134
135             String JavaDoc documentId = document.getId();
136             String JavaDoc language = document.getLanguage();
137             String JavaDoc defaultLanguage = publication.getDefaultLanguage();
138             SiteTreeNode[] children = siteTree.getNode(documentId).getChildren();
139
140             super.startElement(uri, localName, raw, attr);
141
142             for (int i = 0; i < children.length; i++) {
143                 String JavaDoc childId = documentId + "/" + children[i].getId();
144
145                 //get child document with the same language than the parent document
146
String JavaDoc url = builder.buildCanonicalUrl(publication, area, childId, language);
147                 Document doc;
148                 try {
149                     doc = builder.buildDocument(publication, url);
150                 } catch (DocumentBuildException e) {
151                     throw new SAXException JavaDoc(e);
152                 }
153                 File JavaDoc file = doc.getFile();
154
155                 if (!file.exists()) {
156                     //get first the child document in the default language and then in any other existent language
157
getLogger().debug(
158                         "There is no child file "
159                             + file.getAbsolutePath()
160                             + " in the same language as the parent document ["
161                             + language
162                             + "]");
163
164                     //available language
165
String JavaDoc[] availableLanguages = null;
166                     try {
167                         availableLanguages = doc.getLanguages();
168                     } catch (DocumentException e) {
169                         throw new SAXException JavaDoc(e);
170                     }
171
172                     List JavaDoc languages = new ArrayList JavaDoc();
173                     for (int l = 0; l < availableLanguages.length; l++) {
174                         if (availableLanguages[l].equals(language)) {
175                             getLogger().debug(
176                                 "Do nothing because language was already tested: ["
177                                     + availableLanguages[l]
178                                     + "]");
179                         } else if (availableLanguages[l].equals(defaultLanguage)) {
180                             languages.add(0, availableLanguages[l]);
181                         } else {
182                             languages.add(availableLanguages[l]);
183                         }
184                     }
185
186                     int j = 0;
187                     while (!file.exists() && j < languages.size()) {
188                         String JavaDoc newlanguage = (String JavaDoc) languages.get(j);
189                         url = builder.buildCanonicalUrl(publication, area, childId, newlanguage);
190                         try {
191                             doc = builder.buildDocument(publication, url);
192                         } catch (DocumentBuildException e) {
193                             throw new SAXException JavaDoc(e);
194                         }
195                         file = doc.getFile();
196
197                         j++;
198                     }
199                 }
200
201                 if (file.exists()) {
202                     //create the tags for the child
203
String JavaDoc path;
204                     try {
205                         path = file.getCanonicalPath();
206                     } catch (IOException JavaDoc e) {
207                         throw new SAXException JavaDoc(e);
208                     }
209
210                     AttributesImpl JavaDoc attribute = new AttributesImpl JavaDoc();
211                     attribute.addAttribute("", "href", "href", "", url);
212                     super.startElement(NAMESPACE, "child", PREFIX + "child", attribute);
213
214                     AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
215                     attributes.addAttribute("", "src", "src", "", path);
216                     attributes.addAttribute("", "element", "element", "", "included");
217
218                     super.startElement(
219                         this.cIncludeNamespace,
220                         "include",
221                         cIncludePrefix + "include",
222                         attributes);
223                     super.endElement(this.cIncludeNamespace, "include", cIncludePrefix + "include");
224                     super.endElement(NAMESPACE, "child", PREFIX + "child");
225                 } else {
226                     //do nothing for this child
227
getLogger().warn("There are no existing file for the child with id " + childId);
228                 }
229
230             }
231         } else {
232             super.startElement(uri, localName, raw, attr);
233         }
234
235     }
236
237     /**
238      * @return SiteTree The siteTree belonging to the area of the document
239      */

240     public SiteTree getSiteTree() {
241         return siteTree;
242     }
243
244     /**
245      * @param tree The siteTree of the area, which the document belongs.
246      */

247     public void setSiteTree(SiteTree tree) {
248         siteTree = tree;
249     }
250
251     /**
252      * @param string The area, which the document belongs.
253      */

254     public void setArea(String JavaDoc string) {
255         area = string;
256     }
257
258     /**
259      * @param builder The document builder.
260      */

261     public void setBuilder(DocumentBuilder builder) {
262         this.builder = builder;
263     }
264
265     /**
266      * @param document The document.
267      */

268     public void setDocument(Document document) {
269         this.document = document;
270     }
271
272     /**
273      * @param publication The publication, which the document belongs.
274      */

275     public void setPublication(Publication publication) {
276         this.publication = publication;
277     }
278
279 }
280
Popular Tags