KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > PageContext


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 package org.outerj.daisy.frontend;
17
18 import org.outerj.daisy.repository.Repository;
19 import org.outerj.daisy.repository.RepositoryException;
20 import org.outerj.daisy.repository.variant.VariantManager;
21 import org.outerj.daisy.frontend.components.siteconf.SiteConf;
22 import org.outerj.daisy.util.VersionHelper;
23 import org.apache.excalibur.xml.sax.XMLizable;
24 import org.apache.cocoon.xml.AttributesImpl;
25 import org.apache.cocoon.xml.SaxBuffer;
26 import org.apache.cocoon.environment.Request;
27 import org.apache.cocoon.components.ContextHelper;
28 import org.apache.avalon.framework.context.Context;
29 import org.xml.sax.ContentHandler JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.Attributes JavaDoc;
32
33 import java.util.Properties JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37  * An object holding useful contextual information to be passed to the view layer
38  * for each page. Since it implements XMLizable, it is directly streamable as XML in templates.
39  */

40 public class PageContext implements XMLizable {
41     private String JavaDoc mountPoint;
42     private SiteConf siteConf;
43     private Repository repository;
44     private String JavaDoc layoutType;
45     private String JavaDoc skin;
46     private SaxBuffer skinConf;
47     private Context context;
48     private String JavaDoc versionMode;
49     private static final Attributes JavaDoc EMPTY_ATTRIBUTES = new AttributesImpl();
50     private static final Attributes JavaDoc versionAttributes;
51     static {
52         AttributesImpl attrs = new AttributesImpl();
53
54         Properties JavaDoc versionProps;
55         try {
56             versionProps = VersionHelper.getVersionProperties(SetVersionHeaderAction.class.getClassLoader(),
57                     "org/outerj/daisy/frontend/versioninfo.properties");
58         } catch (IOException JavaDoc e) {
59             throw new RuntimeException JavaDoc("Could not load Daisy Wiki version information.");
60         }
61
62         attrs.addAttribute("", "version", "version", "CDATA", versionProps.getProperty("artifact.version"));
63         attrs.addAttribute("", "buildHostName", "buildHostName", "CDATA", versionProps.getProperty("build.hostname"));
64         attrs.addAttribute("", "buildDateTime", "buildDateTime", "CDATA", versionProps.getProperty("build.datetime"));
65
66         versionAttributes = attrs;
67     }
68     /**
69      * Constructor to use when outside the context of a site.
70      */

71     public PageContext(String JavaDoc mountPoint, Repository repository, String JavaDoc layoutType, String JavaDoc skin, SaxBuffer skinConf, Context context) {
72         init(mountPoint, null, repository, layoutType, skin, skinConf, context);
73     }
74
75     /**
76      * Constructor to use when in the context of a site.
77      */

78     public PageContext(String JavaDoc mountPoint, SiteConf siteConf, Repository repository, String JavaDoc layoutType, String JavaDoc skin, Context context) {
79         init(mountPoint, siteConf, repository, layoutType, skin, null, context);
80     }
81
82     private void init(String JavaDoc mountPoint, SiteConf siteConf, Repository repository, String JavaDoc layoutType, String JavaDoc skin,
83             SaxBuffer skinConf, Context context) {
84         this.mountPoint = mountPoint;
85         this.siteConf = siteConf;
86         this.repository = repository;
87         this.layoutType = layoutType == null ? "default" : layoutType;
88         this.skin = skin;
89         this.skinConf = skinConf;
90         this.context = context;
91     }
92
93     public String JavaDoc getMountPoint() {
94         return mountPoint;
95     }
96
97     public SiteConf getSiteConf() {
98         return siteConf;
99     }
100
101     public Repository getRepository() {
102         return repository;
103     }
104
105     public String JavaDoc getSkin() {
106         return skin;
107     }
108
109     public String JavaDoc getLayoutType() {
110         return layoutType;
111     }
112
113     public String JavaDoc getVersionMode() {
114         if (versionMode == null) {
115             Request request = getRequest();
116             if (request != null)
117                 versionMode = WikiHelper.getVersionMode(request).toString();
118         }
119         return versionMode;
120     }
121
122     public void toSAX(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
123         // Note: this code is written such that it doesn't fail when any of the data
124
// (repository, siteConf, ...) is null, which can be the case in
125
// the error-handling pipeline. In addition, siteConf will be null for all pages
126
// outside the context of site.
127
try {
128             contentHandler.startElement("", "context", "context", EMPTY_ATTRIBUTES);
129
130             // version
131
contentHandler.startElement("", "versionInfo", "versionInfo", versionAttributes);
132             contentHandler.endElement("", "versionInfo", "versionInfo");
133
134             // mountPoint
135
if (mountPoint != null)
136                 generateStringElement("mountPoint", mountPoint, contentHandler);
137
138             // version mode
139
if (getVersionMode() != null) {
140                 generateStringElement("versionMode", getVersionMode(), contentHandler);
141             }
142
143             // site
144
if (siteConf != null && repository != null) {
145                 AttributesImpl siteAttrs = new AttributesImpl();
146                 siteAttrs.addCDATAAttribute("name", siteConf.getName());
147                 siteAttrs.addCDATAAttribute("title", siteConf.getTitle());
148                 siteAttrs.addCDATAAttribute("description", siteConf.getDescription());
149                 siteAttrs.addCDATAAttribute("navigationDocId", String.valueOf(siteConf.getNavigationDocId()));
150                 siteAttrs.addCDATAAttribute("collectionId", String.valueOf(siteConf.getCollectionId()));
151                 siteAttrs.addCDATAAttribute("collection", repository.getCollectionManager().getCollection(siteConf.getCollectionId(), false).getName());
152                 siteAttrs.addCDATAAttribute("branchId", String.valueOf(siteConf.getBranchId()));
153                 VariantManager variantManager = repository.getVariantManager();
154                 siteAttrs.addCDATAAttribute("branch", variantManager.getBranch(siteConf.getBranchId(), false).getName());
155                 siteAttrs.addCDATAAttribute("languageId", String.valueOf(siteConf.getLanguageId()));
156                 siteAttrs.addCDATAAttribute("language", variantManager.getLanguage(siteConf.getLanguageId(), false).getName());
157
158                 contentHandler.startElement("", "site", "site", siteAttrs);
159                 contentHandler.endElement("", "site", "site");
160             }
161
162             SaxBuffer skinConf;
163             if (siteConf != null)
164                 skinConf = siteConf.getSkinConf();
165             else
166                 skinConf = this.skinConf;
167             if (skinConf != null)
168                 skinConf.toSAX(contentHandler);
169
170             // user
171
if (repository != null) {
172                 UserInfoStreamer.streamUserInfo(repository, contentHandler);
173             }
174
175             generateStringElement("layoutType", layoutType, contentHandler);
176
177             Request request = getRequest();
178             if (request != null) {
179                 String JavaDoc requestURI = request.getRequestURI();
180                 String JavaDoc queryString = request.getQueryString();
181                 if (queryString != null) {
182                     requestURI = requestURI + "?" + queryString;
183                 }
184                 String JavaDoc method = request.getMethod();
185
186                 AttributesImpl requestAttrs = new AttributesImpl();
187                 requestAttrs.addAttribute("", "uri", "uri", "CDATA", requestURI);
188                 requestAttrs.addAttribute("", "method", "method", "CDATA", method);
189                 requestAttrs.addAttribute("", "server", "server", "CDATA", RequestUtil.getServer(request));
190                 contentHandler.startElement("", "request", "request", requestAttrs);
191                 contentHandler.endElement("", "request", "request");
192             }
193
194             if (skin != null)
195                 generateStringElement("skin", skin, contentHandler);
196
197             contentHandler.endElement("", "context", "context");
198         } catch (RepositoryException e) {
199             throw new SAXException JavaDoc("Error in PageContext.toSAX", e);
200         }
201     }
202
203     private void generateStringElement(String JavaDoc name, String JavaDoc value, ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
204         contentHandler.startElement("", name, name, EMPTY_ATTRIBUTES);
205         contentHandler.characters(value.toCharArray(), 0, value.length());
206         contentHandler.endElement("", name, name);
207     }
208
209     private Request getRequest() {
210         if (context != null)
211             return ContextHelper.getRequest(context);
212         else
213             return null;
214     }
215
216 }
217
Popular Tags