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 return Jsoup.clean(origDescription,whitelist);
126 }
127
128 public static Node findAuthorNode(Node searchNode, String authorName, int count) {
129 boolean found = false;
130 Node an = null;
131 try {
132 NodeIterator nit = searchNode.getNodes();
133 while (nit.hasNext()) {
134 an = nit.nextNode();
135 String author = an.getProperty("author").getString();
136
137 if (StringUtils.equals(authorName, author)) {
138 found = true;
139 break;
140 }
141 }
142
143 if (!found) {
144 an = NodeUtil.createPath(searchNode, "author-" + count, DataConsts.MODULE_DATA_CONTENT_NODE_TYPE, true);
145 }
146 } catch (RepositoryException e) {
147 log.error("Problem while searching for existing author: " + e.getMessage());
148 }
149 return an;
150 }
151 }