001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: PageEnvelopeModule.java 589768 2007-10-29 17:45:43Z andreas $ */
020:
021: package org.apache.lenya.cms.cocoon.components.modules.input;
022:
023: import java.text.SimpleDateFormat;
024: import java.util.Arrays;
025: import java.util.Date;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.configuration.ConfigurationException;
031: import org.apache.cocoon.environment.ObjectModelHelper;
032: import org.apache.cocoon.environment.Request;
033: import org.apache.commons.lang.StringUtils;
034: import org.apache.lenya.cms.publication.Document;
035: import org.apache.lenya.cms.publication.DocumentBuildException;
036: import org.apache.lenya.cms.publication.DocumentLocator;
037: import org.apache.lenya.cms.publication.PageEnvelope;
038: import org.apache.lenya.cms.publication.Publication;
039: import org.apache.lenya.cms.publication.ResourceType;
040: import org.apache.lenya.cms.site.SiteException;
041: import org.apache.lenya.util.ServletHelper;
042:
043: /**
044: * Input module wrapping the page envelope. This module provides publication
045: * related information such as document-uuid, area, publication-id.
046: *
047: * @see org.apache.lenya.cms.publication.PageEnvelope
048: * @deprecated use DocumentInfoModule instead.
049: */
050: public class PageEnvelopeModule extends AbstractPageEnvelopeModule {
051:
052: protected static final String URI_PARAMETER_DOCTYPE = "doctype";
053:
054: /**
055: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
056: * org.apache.avalon.framework.configuration.Configuration,
057: * java.util.Map)
058: */
059: public Object getAttribute(final String attributeName,
060: Configuration modeConf, Map objectModel)
061: throws ConfigurationException {
062:
063: final String name = getAttributeName(attributeName);
064:
065: if (!Arrays.asList(PageEnvelope.PARAMETER_NAMES).contains(name)) {
066: throw new ConfigurationException("The attribute [" + name
067: + "] is not supported!");
068: }
069:
070: PageEnvelope envelope = getEnvelope(objectModel, attributeName);
071: Object value = null;
072:
073: try {
074: if (name.equals(PageEnvelope.AREA)) {
075: value = envelope.getArea();
076: } else if (name.equals(PageEnvelope.CONTEXT)) {
077: value = envelope.getContext();
078: } else if (name.equals(PageEnvelope.IS_PUBLICATION)) {
079: value = Boolean
080: .toString(envelope.getPublication() != null);
081: } else if (name.equals(PageEnvelope.PUBLICATION_ID)) {
082: Publication pub = envelope.getPublication();
083: value = pub != null ? pub.getId() : "";
084: } else if (name.equals(PageEnvelope.PUBLICATION)) {
085: value = envelope.getPublication();
086: } else if (name
087: .equals(PageEnvelope.PUBLICATION_LANGUAGES_CSV)) {
088: value = StringUtils.join(envelope.getPublication()
089: .getLanguages(), ',');
090: } else if (name.equals(PageEnvelope.DEFAULT_LANGUAGE)) {
091: value = envelope.getPublication().getDefaultLanguage();
092: } else if (name.equals(PageEnvelope.LANGUAGE)) {
093: value = envelope.getLanguage();
094: } else if (name.equals(PageEnvelope.BREADCRUMB_PREFIX)) {
095: value = envelope.getPublication().getBreadcrumbPrefix();
096: } else if (name.equals(PageEnvelope.DOCUMENT_PATH)) {
097: value = getPath(envelope, objectModel);
098: } else {
099: Document document = envelope.getDocument();
100: if (document != null) {
101: if (name.equals(PageEnvelope.DOCUMENT)) {
102: value = document;
103: } else if (name.equals(PageEnvelope.DOCUMENT_ID)) {
104: getLogger()
105: .warn(
106: "This attribute [ "
107: + name
108: + " ] is deprecated."
109: + " Use document-path or document-uuid instead!");
110: value = document.getUUID();
111: } else if (name
112: .equals(PageEnvelope.DOCUMENT_PARENT)) {
113: value = document.getLocator().getParent()
114: .getPath();
115: } else if (name.equals(PageEnvelope.DOCUMENT_NAME)) {
116: value = document.getName();
117: } else if (name.equals(PageEnvelope.DOCUMENT_LABEL)) {
118: value = document.getLink().getLabel();
119: } else if (name.equals(PageEnvelope.DOCUMENT_URL)) {
120: value = document.getCanonicalDocumentURL();
121: } else if (name
122: .equals(PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE)) {
123: value = document.getCanonicalWebappURL();
124: } else if (name.equals(PageEnvelope.DOCUMENT_FILE)) {
125: value = document.getFile();
126: } else if (name
127: .equals(PageEnvelope.DOCUMENT_EXTENSION)) {
128: value = document.getExtension();
129: } else if (name
130: .equals(PageEnvelope.DOCUMENT_SOURCE_EXTENSION)) {
131: value = document.getSourceExtension();
132: } else if (name.equals(PageEnvelope.DOCUMENT_UUID)) {
133: value = document.getUUID();
134: } else if (name
135: .equals(PageEnvelope.DOCUMENT_LANGUAGE)) {
136: value = document.getLanguage();
137: } else if (name
138: .equals(PageEnvelope.DOCUMENT_LANGUAGES)) {
139: value = document.getLanguages();
140: } else if (name
141: .equals(PageEnvelope.DOCUMENT_LANGUAGES_CSV)) {
142: value = StringUtils.join(document
143: .getLanguages(), ',');
144: } else if (name
145: .equals(PageEnvelope.DOCUMENT_LASTMODIFIED)) {
146: Date date = new Date(document.getLastModified());
147: value = new SimpleDateFormat(DATE_FORMAT)
148: .format(date);
149: } else if (name.equals(PageEnvelope.DOCUMENT_TYPE)) {
150: ResourceType resourceType = document
151: .getResourceType();
152: if (resourceType == null) {
153: value = null;
154: } else {
155: value = resourceType.getName();
156: }
157: }
158: }
159: }
160: } catch (final Exception e) {
161: throw new ConfigurationException(
162: "Getting attribute for name [" + name
163: + "] failed: ", e);
164: }
165:
166: if (getLogger().isDebugEnabled()) {
167: getLogger().debug(
168: "Returning [" + name + "] = [" + value + "]");
169: }
170:
171: return value;
172: }
173:
174: protected String getPath(PageEnvelope envelope, Map objectModel)
175: throws SiteException {
176: String path;
177: Document doc = envelope.getDocument();
178: if (doc == null) {
179: Publication pub = envelope.getPublication();
180: Request request = ObjectModelHelper.getRequest(objectModel);
181: String url = ServletHelper.getWebappURI(request);
182: DocumentLocator loc;
183: try {
184: loc = pub.getDocumentBuilder().getLocator(
185: pub.getFactory(), url);
186: } catch (DocumentBuildException e) {
187: throw new SiteException(e);
188: }
189: path = loc.getPath();
190: } else {
191: path = doc.getLocator().getPath();
192: }
193: return path;
194: }
195:
196: /**
197: * <code>DATE_FORMAT</code> The date format
198: */
199: public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss Z";
200:
201: /**
202: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
203: * java.util.Map)
204: */
205: public Iterator getAttributeNames(Configuration modeConf,
206: Map objectModel) throws ConfigurationException {
207: return Arrays.asList(PageEnvelope.PARAMETER_NAMES).iterator();
208: }
209:
210: /**
211: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
212: * org.apache.avalon.framework.configuration.Configuration,
213: * java.util.Map)
214: */
215: public Object[] getAttributeValues(String name,
216: Configuration modeConf, Map objectModel)
217: throws ConfigurationException {
218: Object[] objects = { getAttribute(name, modeConf, objectModel) };
219:
220: return objects;
221: }
222:
223: }
|