001    /* 
002        Licensed to the Apache Software Foundation (ASF) under one
003        or more contributor license agreements.  See the NOTICE file
004        distributed with this work for additional information
005        regarding copyright ownership.  The ASF licenses this file
006        to you under the Apache License, Version 2.0 (the
007        "License"); you may not use this file except in compliance
008        with the License.  You may obtain a copy of the License at
009    
010           http://www.apache.org/licenses/LICENSE-2.0
011    
012        Unless required by applicable law or agreed to in writing,
013        software distributed under the License is distributed on an
014        "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015        KIND, either express or implied.  See the License for the
016        specific language governing permissions and limitations
017        under the License.  
018     */
019    package org.apache.wiki.rss;
020    
021    import java.util.ArrayList;
022    import java.util.List;
023    
024    import javax.servlet.ServletContext;
025    
026    import org.apache.commons.lang.StringEscapeUtils;
027    import org.apache.wiki.WikiContext;
028    import org.apache.wiki.WikiEngine;
029    import org.apache.wiki.api.exceptions.NoSuchVariableException;
030    
031    /**
032     * Represents an abstract feed.
033     */
034    public abstract class Feed {
035        protected List<Entry> m_entries = new ArrayList<Entry>();
036    
037        protected String m_feedURL;
038        protected String m_channelTitle;
039        protected String m_channelDescription;
040        protected String m_channelLanguage;
041    
042        protected WikiContext m_wikiContext;
043    
044        protected String m_mode = RSSGenerator.MODE_WIKI;
045    
046        /**
047         * Wiki variable storing the blog's name.
048         */
049        public static final String VAR_BLOGNAME = "blogname";
050    
051        /**
052         * Figure out a site name for a feed.
053         *
054         * @param context the wiki context
055         * @return the site name
056         */
057        public static String getSiteName(WikiContext context) {
058            WikiEngine engine = context.getEngine();
059    
060            String blogname = null;
061    
062            try {
063                blogname = engine.getVariableManager().getValue(context, VAR_BLOGNAME);
064            } catch (NoSuchVariableException e) {
065            }
066    
067            if (blogname == null) {
068                blogname = engine.getApplicationName() + ": " + context.getPage().getName();
069            }
070    
071            return blogname;
072        }
073    
074        /**
075         * Create a new Feed for a particular WikiContext.
076         *
077         * @param context The WikiContext.
078         */
079        public Feed(WikiContext context) {
080            m_wikiContext = context;
081        }
082    
083        /**
084         * Set the mode of the Feed.  It can be any of the following:
085         * <ul>
086         * <li>{@link RSSGenerator#MODE_WIKI} - to create a wiki diff list per page.</li>
087         * <li>{@link RSSGenerator#MODE_BLOG} - to assume that the Entries are blog entries.</li>
088         * <li>{@link RSSGenerator#MODE_FULL} - to create a wiki diff list for the entire blog.</li>
089         * </ul>
090         * As the Entry list itself is generated elsewhere, this mostly just affects the way
091         * that the layout and metadata for each entry is generated.
092         *
093         * @param mode As defined in RSSGenerator.
094         */
095        public void setMode(String mode) {
096            m_mode = mode;
097        }
098    
099        /**
100         * Adds a new Entry to the Feed, at the end of the list.
101         *
102         * @param e The Entry to add.
103         */
104        public void addEntry(Entry e) {
105            m_entries.add(e);
106        }
107    
108        /**
109         * Returns the XML for the feed contents in a String format.  All subclasses must implement.
110         *
111         * @return valid XML, ready to be shoved out.
112         */
113        public abstract String getString();
114    
115        /**
116         * @return Returns the m_channelDescription.
117         */
118        public String getChannelDescription() {
119            return m_channelDescription;
120        }
121    
122        /**
123         * @param description The m_channelDescription to set.
124         */
125        public void setChannelDescription(String description) {
126            m_channelDescription = description;
127        }
128    
129        /**
130         * @return Returns the m_channelLanguage.
131         */
132        public String getChannelLanguage() {
133            return m_channelLanguage;
134        }
135    
136        /**
137         * @param language The m_channelLanguage to set.
138         */
139        public void setChannelLanguage(String language) {
140            m_channelLanguage = language;
141        }
142    
143        /**
144         * @return Returns the m_channelTitle.
145         */
146        public String getChannelTitle() {
147            return m_channelTitle;
148        }
149    
150        /**
151         * @param title The m_channelTitle to set.
152         */
153        public void setChannelTitle(String title) {
154            m_channelTitle = title;
155        }
156    
157        /**
158         * @return Returns the m_feedURL.
159         */
160        public String getFeedURL() {
161            return m_feedURL;
162        }
163    
164        /**
165         * @param feedurl The m_feedURL to set.
166         */
167        public void setFeedURL(String feedurl) {
168            m_feedURL = feedurl;
169        }
170    
171        /**
172         * A helper method for figuring out the MIME type for an enclosure.
173         *
174         * @param c    A ServletContext
175         * @param name The filename
176         * @return Something sane for a MIME type.
177         */
178        protected String getMimeType(ServletContext c, String name) {
179            String type = c.getMimeType(name);
180    
181            if (type == null) {
182                type = "application/octet-stream";
183            }
184    
185            return type;
186        }
187    
188        /**
189         * Does the required formatting and entity replacement for XML.
190         *
191         * @param s The String to format. Null is safe.
192         * @return A formatted string.
193         */
194        public static String format( String s ) {
195            return StringEscapeUtils.escapeXml( s );
196        }
197    }