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: package org.apache.cocoon.generation;
018:
019: import java.io.IOException;
020: import java.util.Arrays;
021: import java.util.Comparator;
022: import java.util.Date;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.parameters.Parameters;
026: import org.apache.avalon.framework.service.ServiceSelector;
027: import org.apache.cocoon.ProcessingException;
028: import org.apache.cocoon.environment.ObjectModelHelper;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.cocoon.environment.SourceResolver;
031: import org.apache.lenya.cms.publication.Document;
032: import org.apache.lenya.cms.publication.DocumentException;
033: import org.apache.lenya.cms.publication.DocumentFactory;
034: import org.apache.lenya.cms.publication.DocumentUtil;
035: import org.apache.lenya.cms.publication.Publication;
036: import org.apache.lenya.cms.publication.PublicationUtil;
037: import org.apache.lenya.cms.repository.RepositoryUtil;
038: import org.apache.lenya.cms.repository.Session;
039: import org.apache.lenya.cms.site.SiteManager;
040: import org.xml.sax.SAXException;
041: import org.xml.sax.helpers.AttributesImpl;
042:
043: /**
044: * Blog entry generator
045: */
046: public class BlogGenerator extends ServiceableGenerator {
047:
048: /** The URI of the namespace of this generator. */
049: protected static final String URI = "http://apache.org/cocoon/blog/1.0";
050:
051: /** The namespace prefix for this namespace. */
052: protected static final String PREFIX = "blog";
053:
054: /** Node and attribute names */
055: protected static final String BLOG_NODE_NAME = "blog";
056:
057: protected static final String ENTRY_NODE_NAME = "entry";
058:
059: protected static final String PATH_ATTR_NAME = "path";
060:
061: protected static final String LASTMOD_ATTR_NAME = "lastModified";
062:
063: /**
064: * Convenience object, so we don't need to create an AttributesImpl for every element.
065: */
066: protected AttributesImpl attributes;
067:
068: /**
069: * The Lenya-Area where the generator should work on
070: */
071: protected String area;
072:
073: protected boolean recent;
074:
075: /**
076: * Only generate the #numrecent entries
077: */
078: protected int numrecent;
079:
080: /**
081: * Set the request parameters. Must be called before the generate method.
082: *
083: * @param resolver the SourceResolver object
084: * @param objectModel a <code>Map</code> containing model object
085: * @param src the source URI (ignored)
086: * @param par configuration parameters
087: */
088: public void setup(SourceResolver resolver, Map objectModel,
089: String src, Parameters par) throws ProcessingException,
090: SAXException, IOException {
091: super .setup(resolver, objectModel, src, par);
092:
093: area = par.getParameter("area", null);
094: if (area == null)
095: throw new ProcessingException("no area specified");
096: if (area.equals(Publication.LIVE_AREA))
097: numrecent = 16;
098: else
099: numrecent = 0;
100:
101: this .attributes = new AttributesImpl();
102: }
103:
104: /**
105: * Generate XML data.
106: *
107: * @throws SAXException if an error occurs while outputting the document
108: */
109: public void generate() throws SAXException, ProcessingException {
110:
111: this .contentHandler.startDocument();
112: this .contentHandler.startPrefixMapping(PREFIX, URI);
113: attributes.clear();
114:
115: this .contentHandler.startElement(URI, BLOG_NODE_NAME, PREFIX
116: + ':' + BLOG_NODE_NAME, attributes);
117:
118: ServiceSelector selector = null;
119: SiteManager siteManager = null;
120: try {
121: Request request = ObjectModelHelper
122: .getRequest(this .objectModel);
123: Session session = RepositoryUtil.getSession(this .manager,
124: request);
125: DocumentFactory map = DocumentUtil.createDocumentFactory(
126: this .manager, session);
127: Publication publication = PublicationUtil.getPublication(
128: this .manager, request);
129:
130: selector = (ServiceSelector) this .manager
131: .lookup(SiteManager.ROLE + "Selector");
132: siteManager = (SiteManager) selector.select(publication
133: .getSiteManagerHint());
134:
135: Document[] docs = siteManager.getDocuments(map,
136: publication, area);
137: Arrays.sort((Object[]) docs, new Comparator() {
138: public int compare(Object o1, Object o2) {
139: try {
140: Date d1 = new Date(((Document) o2)
141: .getLastModified());
142: Date d2 = new Date(((Document) o1)
143: .getLastModified());
144: return d2.compareTo(d1);
145: } catch (DocumentException e) {
146: throw new RuntimeException(e);
147: }
148: }
149: });
150:
151: for (int i = 0; i < docs.length; i++) {
152: if (numrecent > 0 && numrecent <= i)
153: break;
154: String path = docs[i].getPath();
155: if (path.startsWith("/entries/")) {
156: attributes.clear();
157: attributes.addAttribute("", PATH_ATTR_NAME,
158: PATH_ATTR_NAME, "CDATA", path);
159: attributes
160: .addAttribute("", LASTMOD_ATTR_NAME,
161: LASTMOD_ATTR_NAME, "CDATA", String
162: .valueOf(docs[i]
163: .getLastModified()));
164:
165: this .contentHandler.startElement(URI,
166: ENTRY_NODE_NAME, PREFIX + ':'
167: + ENTRY_NODE_NAME, attributes);
168: this .contentHandler.endElement(URI,
169: ENTRY_NODE_NAME, PREFIX + ':'
170: + ENTRY_NODE_NAME);
171: }
172: }
173:
174: } catch (Exception e) {
175: throw new RuntimeException(e);
176: } finally {
177: if (selector != null) {
178: if (siteManager != null) {
179: selector.release(siteManager);
180: }
181: this .manager.release(selector);
182: }
183: }
184:
185: this .contentHandler.endElement(URI, BLOG_NODE_NAME, PREFIX
186: + ':' + BLOG_NODE_NAME);
187:
188: this.contentHandler.endPrefixMapping(PREFIX);
189: this.contentHandler.endDocument();
190: }
191: }
|