1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
52
53
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
66
67
68
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
80
81
82
83
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
93 formattedName = StringUtils.substringAfter(formattedName, BLOGGER_NOREPLY_NAME);
94 if (StringUtils.endsWith(formattedName, ")")) {
95 formattedName = StringUtils.substringBeforeLast(formattedName, ")");
96 }
97 }
98
99 if (StringUtils.equalsIgnoreCase(formattedName, "rah003")) {
100 formattedName = "Jan Haderka";
101 }
102 }
103
104 return formattedName;
105 }
106
107
108
109
110
111
112
113
114 public static String formatDescription(String origDescription, Integer abbreviation) {
115
116
117
118 if (abbreviation != null && abbreviation > 0) {
119 origDescription = StringUtils.abbreviate(origDescription, abbreviation);
120 }
121
122 Whitelist whitelist = Whitelist.basicWithImages();
123 whitelist.addTags("h1", "h2", "h3", "h4", "h5", "h6", "div");
124
125 whitelist.addTags("table", "tbody", "td", "tfoot", "th", "thead", "tr");
126
127 return Jsoup.clean(origDescription, whitelist);
128 }
129
130
131
132
133
134
135
136
137
138
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 }