001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.frontend;
017:
018: import org.outerj.daisy.repository.Repository;
019: import org.outerj.daisy.repository.RepositoryException;
020: import org.outerj.daisy.repository.variant.VariantManager;
021: import org.outerj.daisy.frontend.components.siteconf.SiteConf;
022: import org.outerj.daisy.frontend.util.PublisherRequestHelper;
023: import org.outerj.daisy.frontend.util.XmlObjectXMLizable;
024: import org.outerj.daisy.util.VersionHelper;
025: import org.apache.excalibur.xml.sax.XMLizable;
026: import org.apache.cocoon.xml.AttributesImpl;
027: import org.apache.cocoon.xml.SaxBuffer;
028: import org.apache.cocoon.environment.Request;
029: import org.xml.sax.ContentHandler;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.Attributes;
032:
033: import java.util.Properties;
034: import java.io.IOException;
035:
036: /**
037: * An object holding useful contextual information to be passed to the view layer
038: * for each page. Since it implements XMLizable, it is directly streamable as XML in templates.
039: */
040: public class PageContext implements XMLizable {
041: private String mountPoint;
042: private SiteConf siteConf;
043: private Repository repository;
044: private String layoutType;
045: private String skin;
046: private SaxBuffer skinConf;
047: private String versionMode;
048: private FrontEndContext frontEndContext;
049: private Request request;
050: private String requestURI;
051: private String requestMethod;
052: private String requestServer;
053: private static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
054: private static final Attributes versionAttributes;
055: static {
056: AttributesImpl attrs = new AttributesImpl();
057:
058: Properties versionProps;
059: try {
060: versionProps = VersionHelper.getVersionProperties(
061: SetVersionHeaderAction.class.getClassLoader(),
062: "org/outerj/daisy/frontend/versioninfo.properties");
063: } catch (IOException e) {
064: throw new RuntimeException(
065: "Could not load Daisy Wiki version information.");
066: }
067:
068: attrs.addAttribute("", "version", "version", "CDATA",
069: versionProps.getProperty("artifact.version"));
070: attrs.addAttribute("", "buildHostName", "buildHostName",
071: "CDATA", versionProps.getProperty("build.hostname"));
072: attrs.addAttribute("", "buildDateTime", "buildDateTime",
073: "CDATA", versionProps.getProperty("build.datetime"));
074:
075: versionAttributes = attrs;
076: }
077:
078: public PageContext(Request request) {
079: init(null, null, request);
080: }
081:
082: public PageContext(Repository repository, String layoutType,
083: Request request) {
084: init(repository, layoutType, request);
085: }
086:
087: private void init(Repository repository, String layoutType,
088: Request request) {
089: // Note: this init code is written with the intent that it won't
090: // fail easily, even in case of errors (e.g. can't get repository etc.)
091:
092: frontEndContext = FrontEndContext.get(request);
093:
094: this .mountPoint = frontEndContext.getMountPoint();
095:
096: if (layoutType == null)
097: layoutType = frontEndContext.getLayoutType();
098: this .layoutType = layoutType == null ? "default" : layoutType;
099:
100: if (repository == null) {
101: try {
102: this .repository = frontEndContext.getRepository();
103: } catch (Throwable e) {
104: // continue without repository
105: }
106: } else {
107: this .repository = repository;
108: }
109:
110: if (frontEndContext.inSite())
111: this .siteConf = frontEndContext.getSiteConf();
112:
113: if (frontEndContext.isSkinSet())
114: this .skin = frontEndContext.getSkin();
115:
116: try {
117: this .skinConf = frontEndContext.inSite() ? frontEndContext
118: .getSiteConf().getSkinConf() : frontEndContext
119: .getGlobalSkinConf();
120: } catch (Throwable e) {
121: // continue without skinconf
122: }
123:
124: this .versionMode = frontEndContext.getVersionMode().toString();
125:
126: this .request = request;
127: requestURI = request.getRequestURI();
128: String queryString = request.getQueryString();
129: if (queryString != null) {
130: requestURI = requestURI + "?" + queryString;
131: }
132: requestMethod = request.getMethod();
133: requestServer = RequestUtil.getServer(request);
134: }
135:
136: public String getMountPoint() {
137: return mountPoint;
138: }
139:
140: public SiteConf getSiteConf() {
141: return siteConf;
142: }
143:
144: public Repository getRepository() {
145: return repository;
146: }
147:
148: public String getSkin() {
149: return skin;
150: }
151:
152: public String getLayoutType() {
153: return layoutType;
154: }
155:
156: public String getVersionMode() {
157: return versionMode;
158: }
159:
160: public void toSAX(ContentHandler contentHandler)
161: throws SAXException {
162: // Note: this code is written such that it doesn't fail when any of the data
163: // (repository, siteConf, ...) is null, which can be the case in
164: // the error-handling pipeline. In addition, siteConf will be null for all pages
165: // outside the context of site.
166: try {
167: contentHandler.startElement("", "context", "context",
168: EMPTY_ATTRIBUTES);
169:
170: // version
171: contentHandler.startElement("", "versionInfo",
172: "versionInfo", versionAttributes);
173: contentHandler.endElement("", "versionInfo", "versionInfo");
174:
175: // mountPoint
176: if (mountPoint != null)
177: generateStringElement("mountPoint", mountPoint,
178: contentHandler);
179:
180: // version mode
181: if (versionMode != null) {
182: generateStringElement("versionMode", versionMode,
183: contentHandler);
184: }
185:
186: // site
187: if (siteConf != null && repository != null) {
188: AttributesImpl siteAttrs = new AttributesImpl();
189: siteAttrs.addCDATAAttribute("name", siteConf.getName());
190: siteAttrs.addCDATAAttribute("title", siteConf
191: .getTitle());
192: siteAttrs.addCDATAAttribute("description", siteConf
193: .getDescription());
194: siteAttrs.addCDATAAttribute("navigationDocId", String
195: .valueOf(siteConf.getNavigationDocId()));
196: siteAttrs.addCDATAAttribute("collectionId", String
197: .valueOf(siteConf.getCollectionId()));
198: siteAttrs.addCDATAAttribute("collection", repository
199: .getCollectionManager().getCollection(
200: siteConf.getCollectionId(), false)
201: .getName());
202: siteAttrs.addCDATAAttribute("branchId", String
203: .valueOf(siteConf.getBranchId()));
204: VariantManager variantManager = repository
205: .getVariantManager();
206: siteAttrs.addCDATAAttribute("branch", variantManager
207: .getBranch(siteConf.getBranchId(), false)
208: .getName());
209: siteAttrs.addCDATAAttribute("languageId", String
210: .valueOf(siteConf.getLanguageId()));
211: siteAttrs.addCDATAAttribute("language", variantManager
212: .getLanguage(siteConf.getLanguageId(), false)
213: .getName());
214: siteAttrs.addCDATAAttribute("publisherRequestSet",
215: siteConf.getPublisherRequestSet());
216:
217: contentHandler.startElement("", "site", "site",
218: siteAttrs);
219: contentHandler.endElement("", "site", "site");
220: }
221:
222: SaxBuffer skinConf;
223: if (siteConf != null)
224: skinConf = siteConf.getSkinConf();
225: else
226: skinConf = this .skinConf;
227: if (skinConf != null)
228: skinConf.toSAX(contentHandler);
229:
230: // user
231: if (repository != null) {
232: UserInfoStreamer.streamUserInfo(repository,
233: contentHandler);
234: }
235:
236: generateStringElement("layoutType", layoutType,
237: contentHandler);
238:
239: if (request != null) {
240: AttributesImpl requestAttrs = new AttributesImpl();
241: requestAttrs.addAttribute("", "uri", "uri", "CDATA",
242: requestURI);
243: requestAttrs.addAttribute("", "method", "method",
244: "CDATA", requestMethod);
245: requestAttrs.addAttribute("", "server", "server",
246: "CDATA", requestServer);
247: contentHandler.startElement("", "request", "request",
248: requestAttrs);
249: contentHandler.endElement("", "request", "request");
250: }
251:
252: if (skin != null)
253: generateStringElement("skin", skin, contentHandler);
254:
255: contentHandler.endElement("", "context", "context");
256: } catch (RepositoryException e) {
257: throw new SAXException("Error in PageContext.toSAX", e);
258: }
259: }
260:
261: private void generateStringElement(String name, String value,
262: ContentHandler contentHandler) throws SAXException {
263: contentHandler.startElement("", name, name, EMPTY_ATTRIBUTES);
264: contentHandler.characters(value.toCharArray(), 0, value
265: .length());
266: contentHandler.endElement("", name, name);
267: }
268:
269: public String getRequestURI() {
270: return requestURI;
271: }
272:
273: public String getRequestMethod() {
274: return requestMethod;
275: }
276:
277: public String getRequestServer() {
278: return requestServer;
279: }
280:
281: public XMLizable getPublisherVariablesConfig() {
282: return new XmlObjectXMLizable(PublisherRequestHelper
283: .getVariablesConfig(frontEndContext), true);
284: }
285: }
|