1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.rss; |
21 |
|
|
22 |
|
import java.io.StringReader; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.xwiki.bridge.SkinAccessBridge; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.rendering.block.Block; |
35 |
|
import org.xwiki.rendering.block.GroupBlock; |
36 |
|
import org.xwiki.rendering.block.ImageBlock; |
37 |
|
import org.xwiki.rendering.block.LinkBlock; |
38 |
|
import org.xwiki.rendering.block.ParagraphBlock; |
39 |
|
import org.xwiki.rendering.block.RawBlock; |
40 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
41 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
42 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
43 |
|
import org.xwiki.rendering.macro.Macro; |
44 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
45 |
|
import org.xwiki.rendering.macro.box.BoxMacroParameters; |
46 |
|
import org.xwiki.rendering.macro.rss.RssMacroParameters; |
47 |
|
import org.xwiki.rendering.parser.ParseException; |
48 |
|
import org.xwiki.rendering.parser.Parser; |
49 |
|
import org.xwiki.rendering.syntax.Syntax; |
50 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
51 |
|
|
52 |
|
import com.sun.syndication.feed.synd.SyndEntry; |
53 |
|
import com.sun.syndication.feed.synd.SyndFeed; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named("rss") |
63 |
|
@Singleton |
|
|
| 85.1% |
Uncovered Elements: 11 (74) |
Complexity: 17 |
Complexity Density: 0.33 |
|
64 |
|
public class RssMacro extends AbstractMacro<RssMacroParameters> |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final String CLASS_ATTRIBUTE = "class"; |
70 |
|
|
71 |
|
private static final String FEED_CLASS_VALUE = "rssfeed"; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private static final String DESCRIPTION = "Output latest feed entries from a RSS feed."; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private static final String FEED_ICON_RESOURCE_PATH = "icons/silk/feed.png"; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
@Named("box") |
88 |
|
protected Macro<BoxMacroParameters> boxMacro; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
private SkinAccessBridge skinAccessBridge; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@Inject |
100 |
|
@Named("plain/1.0") |
101 |
|
private Parser plainTextParser; |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
private RomeFeedFactory romeFeedFactory = new DefaultRomeFeedFactory(); |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
111 |
8 |
public RssMacro()... |
112 |
|
{ |
113 |
8 |
super("RSS", DESCRIPTION, RssMacroParameters.class); |
114 |
8 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
117 |
0 |
@Override... |
118 |
|
public boolean supportsInlineMode() |
119 |
|
{ |
120 |
0 |
return false; |
121 |
|
} |
122 |
|
|
|
|
| 87% |
Uncovered Elements: 3 (23) |
Complexity: 5 |
Complexity Density: 0.33 |
|
123 |
8 |
@Override... |
124 |
|
public List<Block> execute(RssMacroParameters parameters, String content, MacroTransformationContext context) |
125 |
|
throws MacroExecutionException |
126 |
|
{ |
127 |
8 |
List<Block> result; |
128 |
8 |
SyndFeed feed = this.romeFeedFactory.createFeed(parameters); |
129 |
|
|
130 |
6 |
if (parameters.isDecoration()) { |
131 |
4 |
BoxMacroParameters boxParameters = new BoxMacroParameters(); |
132 |
4 |
boolean hasImage = parameters.isImage() && (feed.getImage() != null); |
133 |
4 |
boxParameters.setCssClass(FEED_CLASS_VALUE); |
134 |
|
|
135 |
4 |
if (StringUtils.isNotEmpty(parameters.getWidth())) { |
136 |
0 |
boxParameters.setWidth(parameters.getWidth()); |
137 |
|
} |
138 |
|
|
139 |
4 |
boxParameters.setBlockTitle(generateBoxTitle("rsschanneltitle", feed)); |
140 |
|
|
141 |
4 |
if (hasImage) { |
142 |
2 |
boxParameters.setImage(new ResourceReference(feed.getImage().getUrl(), ResourceType.URL)); |
143 |
|
} |
144 |
|
|
145 |
4 |
result = this.boxMacro.execute(boxParameters, content == null ? StringUtils.EMPTY : content, context); |
146 |
|
} else { |
147 |
2 |
result = Arrays.<Block>asList(new GroupBlock(Collections.singletonMap(CLASS_ATTRIBUTE, FEED_CLASS_VALUE))); |
148 |
|
} |
149 |
|
|
150 |
6 |
generateEntries(result.get(0), feed, parameters); |
151 |
|
|
152 |
6 |
return result; |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@param |
159 |
|
@param |
160 |
|
@return |
161 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 2 |
Complexity Density: 0.17 |
|
162 |
4 |
private List< ? extends Block> generateBoxTitle(String cssClass, SyndFeed feed)... |
163 |
|
{ |
164 |
4 |
List<Block> titleBlocks; |
165 |
|
|
166 |
4 |
if (feed.getLink() == null) { |
167 |
0 |
titleBlocks = parsePlainText(feed.getTitle()); |
168 |
|
} else { |
169 |
|
|
170 |
4 |
ResourceReference titleResourceReference = new ResourceReference(feed.getLink(), ResourceType.URL); |
171 |
|
|
172 |
|
|
173 |
4 |
Block titleTextLinkBlock = new LinkBlock(parsePlainText(feed.getTitle()), titleResourceReference, true); |
174 |
|
|
175 |
|
|
176 |
4 |
String imagePath = this.skinAccessBridge.getSkinFile(FEED_ICON_RESOURCE_PATH); |
177 |
4 |
ImageBlock imageBlock = new ImageBlock(new ResourceReference(imagePath, ResourceType.URL), false); |
178 |
|
|
179 |
|
|
180 |
4 |
Block titleImageLinkBlock = new LinkBlock(Arrays.<Block> asList(imageBlock), titleResourceReference, true); |
181 |
|
|
182 |
4 |
titleBlocks = Arrays.<Block> asList(titleTextLinkBlock, titleImageLinkBlock); |
183 |
|
} |
184 |
4 |
ParagraphBlock titleBlock = new ParagraphBlock(titleBlocks); |
185 |
4 |
titleBlock.setParameter(CLASS_ATTRIBUTE, cssClass); |
186 |
|
|
187 |
4 |
return Collections.singletonList(titleBlock); |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
@param |
194 |
|
@param |
195 |
|
@param |
196 |
|
@throws |
197 |
|
|
|
|
| 94.7% |
Uncovered Elements: 1 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
198 |
6 |
private void generateEntries(Block parentBlock, SyndFeed feed, RssMacroParameters parameters)... |
199 |
|
throws MacroExecutionException |
200 |
|
{ |
201 |
6 |
int maxElements = parameters.getCount(); |
202 |
6 |
int count = 0; |
203 |
|
|
204 |
6 |
for (Object item : feed.getEntries()) { |
205 |
14 |
++count; |
206 |
14 |
if (count > maxElements) { |
207 |
4 |
break; |
208 |
|
} |
209 |
10 |
SyndEntry entry = (SyndEntry) item; |
210 |
|
|
211 |
10 |
ResourceReference titleResourceReference = new ResourceReference(entry.getLink(), ResourceType.URL); |
212 |
10 |
Block titleBlock = new LinkBlock(parsePlainText(entry.getTitle()), titleResourceReference, true); |
213 |
10 |
ParagraphBlock paragraphTitleBlock = new ParagraphBlock(Collections.singletonList(titleBlock)); |
214 |
10 |
paragraphTitleBlock.setParameter(CLASS_ATTRIBUTE, "rssitemtitle"); |
215 |
10 |
parentBlock.addChild(paragraphTitleBlock); |
216 |
|
|
217 |
10 |
if (parameters.isContent() && entry.getDescription() != null) { |
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
10 |
Block html = new RawBlock(entry.getDescription().getValue(), Syntax.XHTML_1_0); |
225 |
10 |
parentBlock.addChild(new GroupBlock(Arrays.asList(html), Collections.singletonMap(CLASS_ATTRIBUTE, |
226 |
|
"rssitemdescription"))); |
227 |
|
} |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
@param |
233 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
234 |
1 |
protected void setFeedFactory(RomeFeedFactory romeFeedFactory)... |
235 |
|
{ |
236 |
1 |
this.romeFeedFactory = romeFeedFactory; |
237 |
|
} |
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
@param |
243 |
|
@return |
244 |
|
@since |
245 |
|
|
|
|
| 57.1% |
Uncovered Elements: 3 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
246 |
14 |
private List<Block> parsePlainText(String content)... |
247 |
|
{ |
248 |
14 |
if (StringUtils.isEmpty(content)) { |
249 |
0 |
return Collections.emptyList(); |
250 |
|
} |
251 |
|
|
252 |
14 |
try { |
253 |
14 |
return this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren(); |
254 |
|
} catch (ParseException e) { |
255 |
|
|
256 |
|
|
257 |
0 |
throw new RuntimeException("Failed to parse [" + content + "] as plain text", e); |
258 |
|
} |
259 |
|
} |
260 |
|
} |