001: /*
002: * File : $Source: /usr/local/cvs/alkacon/com.alkacon.opencms.feeder/src/com/alkacon/opencms/feeder/CmsFeedGenerator.java,v $
003: * Date : $Date: 2007-12-13 15:48:47 $
004: * Version: $Revision: 1.1 $
005: *
006: * This file is part of the Alkacon OpenCms Add-On Module Package
007: *
008: * Copyright (c) 2007 Alkacon Software GmbH (http://www.alkacon.com)
009: *
010: * The Alkacon OpenCms Add-On Module Package is free software:
011: * you can redistribute it and/or modify
012: * it under the terms of the GNU General Public License as published by
013: * the Free Software Foundation, either version 3 of the License, or
014: * (at your option) any later version.
015: *
016: * The Alkacon OpenCms Add-On Module Package is distributed
017: * in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with the Alkacon OpenCms Add-On Module Package.
024: * If not, see http://www.gnu.org/licenses/.
025: *
026: * For further information about Alkacon Software GmbH, please see the
027: * company website: http://www.alkacon.com.
028: *
029: * For further information about OpenCms, please see the
030: * project website: http://www.opencms.org.
031: */
032:
033: package com.alkacon.opencms.feeder;
034:
035: import org.opencms.file.CmsFile;
036: import org.opencms.file.CmsObject;
037: import org.opencms.file.CmsResource;
038: import org.opencms.i18n.CmsEncoder;
039: import org.opencms.main.CmsException;
040: import org.opencms.main.OpenCms;
041: import org.opencms.util.CmsStringUtil;
042: import org.opencms.xml.content.CmsXmlContent;
043: import org.opencms.xml.content.CmsXmlContentFactory;
044: import org.opencms.xml.content.I_CmsXmlContentHandler;
045:
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.io.OutputStreamWriter;
049: import java.io.Writer;
050: import java.util.ArrayList;
051: import java.util.List;
052: import java.util.Locale;
053:
054: import com.sun.syndication.feed.synd.SyndEntry;
055: import com.sun.syndication.feed.synd.SyndFeed;
056: import com.sun.syndication.feed.synd.SyndFeedImpl;
057: import com.sun.syndication.feed.synd.SyndImage;
058: import com.sun.syndication.io.FeedException;
059: import com.sun.syndication.io.SyndFeedOutput;
060:
061: /**
062: * Creates a syndication feed from a List of XML content resources.<p>
063: *
064: * @author Alexander Kandzior
065: *
066: * @version $Revision: 1.1 $
067: */
068: public class CmsFeedGenerator {
069:
070: /** The list of XML content entries that make up the feed. */
071: private List m_contentEntries;
072:
073: /** The feed copyright message. */
074: private String m_feedCopyright;
075:
076: /** The feed description. */
077: private String m_feedDescription;
078:
079: /** The encoding to use for the feed creation. */
080: private String m_feedEncoding;
081:
082: /** The optional image for the feed. */
083: private SyndImage m_feedImage;
084:
085: /** The base link to the feed source. */
086: private String m_feedLink;
087:
088: /** The title of the feed. */
089: private String m_feedTitle;
090:
091: /** The type of the feed. */
092: private String m_feedType;
093:
094: /** The default XML content mappings that apply in case the XML content does not use a special feed handler. */
095: private CmsFeedContentMapping m_defaultMapping;
096:
097: /**
098: * Creates a new, empty feed generator.<p>
099: */
100: public CmsFeedGenerator() {
101:
102: m_contentEntries = new ArrayList();
103: }
104:
105: /**
106: * Creates a new feed generator with a default feed mapping.<p>
107: *
108: * @param defaultMapping the default feed mapping to use
109: */
110: public CmsFeedGenerator(CmsFeedContentMapping defaultMapping) {
111:
112: m_defaultMapping = defaultMapping;
113: }
114:
115: /**
116: * Returns the list of XML content entries that make up the feed.<p>
117: *
118: * @return the list of XML content entries that make up the feed
119: */
120: public List getContentEntries() {
121:
122: return m_contentEntries;
123: }
124:
125: /**
126: * Creates a feed using the current settings.<p>
127: *
128: * @param cms the OpenCms user context to generate the feed with
129: * @param locale the currently selected locale to use for the feed
130: *
131: * @return a feed created using the current settings
132: *
133: * @throws CmsException in case of errors accessing the OpenCms VFS
134: */
135: public SyndFeed getFeed(CmsObject cms, Locale locale)
136: throws CmsException {
137:
138: // create the feed instance
139: SyndFeed feed = new SyndFeedImpl();
140: // set the main feed parameters
141: if (CmsStringUtil.isNotEmpty(m_feedType)) {
142: feed.setFeedType(m_feedType);
143: }
144: if (CmsStringUtil.isNotEmpty(m_feedTitle)) {
145: feed.setTitle(m_feedTitle);
146: }
147: if (CmsStringUtil.isNotEmpty(m_feedLink)) {
148: feed.setLink(m_feedLink);
149: }
150: if (CmsStringUtil.isNotEmpty(m_feedDescription)) {
151: feed.setDescription(m_feedDescription);
152: }
153: if (m_feedImage != null) {
154: feed.setImage(m_feedImage);
155: }
156: if (CmsStringUtil.isNotEmpty(m_feedEncoding)) {
157: feed.setEncoding(CmsEncoder.lookupEncoding(m_feedEncoding,
158: OpenCms.getSystemInfo().getDefaultEncoding()));
159: } else {
160: // use OpenCms default if nothing is set
161: feed.setEncoding(OpenCms.getSystemInfo()
162: .getDefaultEncoding());
163: }
164: if (CmsStringUtil.isNotEmpty(locale.getLanguage())) {
165: feed.setLanguage(locale.getLanguage());
166: }
167: if (CmsStringUtil.isNotEmpty(m_feedCopyright)) {
168: feed.setCopyright(m_feedCopyright);
169: }
170:
171: // now add the entries
172: List entries = new ArrayList(m_contentEntries.size());
173: for (int i = 0; i < m_contentEntries.size(); i++) {
174: // iterate over all content entries
175: Object obj = m_contentEntries.get(i);
176: CmsXmlContent content;
177: if (obj instanceof CmsXmlContent) {
178: content = (CmsXmlContent) obj;
179: } else {
180: CmsResource res = (CmsResource) obj;
181: CmsFile file = cms.readFile(res);
182: content = CmsXmlContentFactory.unmarshal(cms, file);
183: }
184: I_CmsXmlContentHandler handler = content
185: .getContentDefinition().getContentHandler();
186: CmsFeedContentMapping mapping = null;
187: if (handler instanceof CmsFeedXmlContentHandler) {
188: // this content has a special feed handler
189: mapping = ((CmsFeedXmlContentHandler) handler)
190: .getFeedMapping();
191: } else {
192: // check if default handler applies to the content
193: mapping = m_defaultMapping;
194: }
195: if (mapping != null) {
196: SyndEntry entry = mapping.getEntryFromXmlContent(cms,
197: content, locale);
198: if (entry != null) {
199: entries.add(entry);
200: }
201: }
202: }
203: // set the feed entries
204: feed.setEntries(entries);
205:
206: return feed;
207: }
208:
209: /**
210: * Returns the feed copyright message.<p>
211: *
212: * @return the feed copyright message
213: */
214: public String getFeedCopyright() {
215:
216: return m_feedCopyright;
217: }
218:
219: /**
220: * Returns the feed description.<p>
221: *
222: * @return the feed description
223: */
224: public String getFeedDescription() {
225:
226: return m_feedDescription;
227: }
228:
229: /**
230: * Returns the encoding to use for the feed creation.<p>
231: *
232: * @return the encoding to use for the feed creation
233: */
234: public String getFeedEncoding() {
235:
236: return m_feedEncoding;
237: }
238:
239: /**
240: * Returns the optional image for the feed.<p>
241: *
242: * @return the optional image for the feed
243: */
244: public SyndImage getFeedImage() {
245:
246: return m_feedImage;
247: }
248:
249: /**
250: * Returns the base link to the feed source.<p>
251: *
252: * @return the base link to the feed source
253: */
254: public String getFeedLink() {
255:
256: return m_feedLink;
257: }
258:
259: /**
260: * Returns the title of the feed.<p>
261: *
262: * @return the title of the feed
263: */
264: public String getFeedTitle() {
265:
266: return m_feedTitle;
267: }
268:
269: /**
270: * Returns the type of the feed.<p>
271: *
272: * @return the type of the feed
273: */
274: public String getFeedType() {
275:
276: return m_feedType;
277: }
278:
279: /**
280: * Sets the list of XML content entries that make up the feed.<p>
281: *
282: * @param contentEntries the list of XML content entries that make up the feed to set
283: */
284: public void setContentEntries(List contentEntries) {
285:
286: m_contentEntries = contentEntries;
287: }
288:
289: /**
290: * Sets the feed copyright message.<p>
291: *
292: * @param feedCopyright the feed copyright message to set
293: */
294: public void setFeedCopyright(String feedCopyright) {
295:
296: m_feedCopyright = feedCopyright;
297: }
298:
299: /**
300: * Sets the feed description.<p>
301: *
302: * @param feedDescription the feed description to set
303: */
304: public void setFeedDescription(String feedDescription) {
305:
306: m_feedDescription = feedDescription;
307: }
308:
309: /**
310: * Sets the encoding to use for the feed creation.<p>
311: *
312: * @param feedEncoding the encoding to use for the feed creation to set
313: */
314: public void setFeedEncoding(String feedEncoding) {
315:
316: m_feedEncoding = feedEncoding;
317: }
318:
319: /**
320: * Sets the optional image for the feed.<p>
321: *
322: * @param feedImage the optional image for the feed to set
323: */
324: public void setFeedImage(SyndImage feedImage) {
325:
326: m_feedImage = feedImage;
327: }
328:
329: /**
330: * Sets the base link to the feed source.<p>
331: *
332: * @param feedLink the base link to the feed source to set
333: */
334: public void setFeedLink(String feedLink) {
335:
336: m_feedLink = feedLink;
337: }
338:
339: /**
340: * Sets the title of the feed.<p>
341: *
342: * @param feedTitle the title of the feed to set
343: */
344: public void setFeedTitle(String feedTitle) {
345:
346: m_feedTitle = feedTitle;
347: }
348:
349: /**
350: * Sets the type of the feed.<p>
351: *
352: * @param feedType the type of the feed to set
353: */
354: public void setFeedType(String feedType) {
355:
356: m_feedType = feedType;
357: }
358:
359: /**
360: * Write the feed result to the provided output stream.<p>
361: *
362: * @param cms the current users OpenCms context
363: * @param locale the locale to use
364: * @param out the output stream to write the feed to
365: *
366: * @throws IOException in case of errors writing to the stream
367: * @throws FeedException in case of errors generating the feed
368: * @throws CmsException in case of errors accessing the OpenCms VFS
369: */
370: public void write(CmsObject cms, Locale locale, OutputStream out)
371: throws IOException, FeedException, CmsException {
372:
373: Writer writer = new OutputStreamWriter(out);
374: write(cms, locale, writer);
375: }
376:
377: /**
378: * Write the feed result to the provided writer.<p>
379: *
380: * @param cms the current users OpenCms context
381: * @param locale the locale to use
382: * @param writer the writer to write the feed to
383: *
384: * @throws IOException in case of errors writing to the stream
385: * @throws FeedException in case of errors generating the feed
386: * @throws CmsException in case of errors accessing the OpenCms VFS
387: */
388: public void write(CmsObject cms, Locale locale, Writer writer)
389: throws IOException, FeedException, CmsException {
390:
391: SyndFeed feed = getFeed(cms, locale);
392: SyndFeedOutput out = new SyndFeedOutput();
393: out.output(feed, writer);
394: }
395: }
|