View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.util;
35  
36  import info.magnolia.jcr.util.NodeUtil;
37  import info.magnolia.jcr.util.PropertyUtil;
38  import info.magnolia.module.data.DataConsts;
39  
40  import javax.jcr.Node;
41  import javax.jcr.NodeIterator;
42  import javax.jcr.RepositoryException;
43  
44  import org.apache.commons.lang.StringUtils;
45  import org.jsoup.Jsoup;
46  import org.jsoup.safety.Whitelist;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  /**
51   * Utility methods for Planet RSS.
52   *
53   * @author lfischer
54   */
55  public final class PlanetUtil {
56  
57      private static final Logger log = LoggerFactory.getLogger(PlanetUtil.class);
58      private static final String PLANET_FEED_PROPERTY = "planetFeed";
59      private static final String BLOGGER_NOREPLY_NAME = "noreply@blogger.com (";
60  
61      private PlanetUtil() {
62      }
63  
64      /**
65       * Determine if a combined feed has the planet attribute set to true.
66       *
67       * @param feedNode Feed node to check
68       * @return true, if planet is checked, false otherwise
69       */
70      public static boolean isPlanetNode(Node feedNode) throws RepositoryException {
71          boolean result = false;
72          if (feedNode.hasProperty(PLANET_FEED_PROPERTY)) {
73              result = StringUtils.equalsIgnoreCase("true", feedNode.getProperty(PLANET_FEED_PROPERTY).getString());
74          }
75          return result;
76      }
77  
78      /**
79       * Format node content containing author and channel names.
80       *
81       * @param node the node where the name property is stored.
82       * @param propName the name property to be formatted.
83       * @return formatted name property.
84       */
85      public static String formatName(Node node, String propName) throws RepositoryException {
86          String formattedName = "";
87  
88          if (node != null && node.hasProperty(propName)) {
89              formattedName = PropertyUtil.getString(node, propName, "");
90  
91              if (formattedName.startsWith(BLOGGER_NOREPLY_NAME)) {
92                  // extract the author name from a blogger noreply address
93                  formattedName = StringUtils.substringAfter(formattedName, BLOGGER_NOREPLY_NAME);
94                  if (StringUtils.endsWith(formattedName, ")")) {
95                      formattedName = StringUtils.substringBeforeLast(formattedName, ")");
96                  }
97              }
98              // TODO this needs to be handled in a different way
99              if (StringUtils.equalsIgnoreCase(formattedName, "rah003")) {
100                 formattedName = "Jan Haderka";
101             }
102         }
103 
104         return formattedName;
105     }
106 
107     /**
108      * Format and sanitize description input.
109      *
110      * @param origDescription original description as delivered by feed fetcher.
111      * @param abbreviation gives the amount of characters used for abbreviation.
112      * @return sanitized HTML output string.
113      */
114     public static String formatDescription(String origDescription, Integer abbreviation) {
115         // replace Java linebreaks with HTML
116         //origDescription = StringUtils.replace(origDescription, "\n", "<br/>");
117         // check if abbreviation is wanted
118         if (abbreviation != null && abbreviation > 0) {
119             origDescription = StringUtils.abbreviate(origDescription, abbreviation);
120         }
121         // Sanitize HTML input
122         Whitelist whitelist = Whitelist.basicWithImages();
123         whitelist.addTags("h1", "h2", "h3", "h4", "h5", "h6", "div");
124         // table tags
125         whitelist.addTags("table", "tbody", "td", "tfoot", "th", "thead", "tr");
126 
127         return Jsoup.clean(origDescription, whitelist);
128     }
129 
130     /**
131      * Search for an author / feed combination in planet statistics and return the authors node if found.
132      * Both the author name and the link for the feed must be found, because several blogs can have the same author name.
133      *
134      * @param searchNode Starting node for the search.
135      * @param authorName Name of the author to search for.
136      * @param feedLink Link identifying the blog.
137      * @param count Internal counter to create unique node names.
138      * @return A found author's node or a new one if not found.
139      */
140     public static Node findAuthorNode(Node searchNode, String authorName, String feedLink, int count) {
141         boolean found = false;
142         Node an = null;
143         try {
144             NodeIterator nit = searchNode.getNodes();
145             while (nit.hasNext()) {
146                 an = nit.nextNode();
147                 String author = an.getProperty("author").getString();
148                 String feed = an.getProperty("feedLink").getString();
149 
150                 if (StringUtils.equals(authorName, author) && StringUtils.equals(feedLink, feed)) {
151                     found = true;
152                     break;
153                 }
154             }
155             if (!found) {
156                 an = NodeUtil.createPath(searchNode, "author-" + count, DataConsts.MODULE_DATA_CONTENT_NODE_TYPE, true);
157             }
158         } catch (RepositoryException e) {
159             log.error("Problem while searching for existing author: " + e.getMessage());
160         }
161         return an;
162     }
163 
164 }