1721
|
1 |
package com.nokia.carbide.internal.discovery.ui.extension;
|
|
2 |
|
|
3 |
import java.net.URL;
|
|
4 |
import java.text.MessageFormat;
|
|
5 |
import java.util.LinkedHashSet;
|
|
6 |
import java.util.Set;
|
|
7 |
import java.util.regex.Pattern;
|
|
8 |
|
|
9 |
import org.eclipse.jface.action.Action;
|
|
10 |
import org.eclipse.jface.action.IAction;
|
|
11 |
|
|
12 |
import com.nokia.carbide.discovery.ui.Activator;
|
|
13 |
import com.nokia.carbide.discovery.ui.Messages;
|
|
14 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader;
|
|
15 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Channel;
|
|
16 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Item;
|
|
17 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Rss;
|
|
18 |
|
|
19 |
public abstract class AbstractRSSPortalPageLayer extends AbstractBrowserPortalPageLayer {
|
|
20 |
|
|
21 |
private static final String[] EXCLUDE_TAGS = {
|
|
22 |
"object", //$NON-NLS-1$
|
|
23 |
"param", //$NON-NLS-1$
|
|
24 |
"embed", //$NON-NLS-1$
|
|
25 |
"script", //$NON-NLS-1$
|
|
26 |
"img" //$NON-NLS-1$
|
|
27 |
};
|
|
28 |
|
|
29 |
private static final Pattern[] EXCLUDE_TAG_PATTERNS = new Pattern[EXCLUDE_TAGS.length * 2];
|
|
30 |
|
|
31 |
static {
|
|
32 |
int i = 0;
|
|
33 |
for (String tagString : EXCLUDE_TAGS) {
|
|
34 |
StringBuilder sb = new StringBuilder();
|
|
35 |
sb.append('<');
|
|
36 |
sb.append(tagString);
|
|
37 |
sb.append(" .*</"); //$NON-NLS-1$
|
|
38 |
sb.append(tagString);
|
|
39 |
sb.append('>');
|
|
40 |
EXCLUDE_TAG_PATTERNS[i++] =
|
|
41 |
Pattern.compile(sb.toString(),
|
|
42 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
|
|
43 |
}
|
|
44 |
for (String tagString : EXCLUDE_TAGS) {
|
|
45 |
StringBuilder sb = new StringBuilder();
|
|
46 |
sb.append('<');
|
|
47 |
sb.append(tagString);
|
|
48 |
sb.append(" .*/>"); //$NON-NLS-1$
|
|
49 |
EXCLUDE_TAG_PATTERNS[i++] =
|
|
50 |
Pattern.compile(sb.toString(),
|
|
51 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
|
|
52 |
}
|
|
53 |
}
|
|
54 |
|
|
55 |
private static final String HTML_BODY_HEADER = "<html><head><title></title><style type=\"text/css\">div.item {font-family : sans-serif; font-size : 12px; margin-bottom : 16px;} div.itemBody {padding-top : 3px; padding-bottom : 3px;} div.itemInfo {background-color : #EEEEEE; color : #333333;} div.feedflare {display: none;} a.itemTitle {font-size : 12px; font-weight : bold;} a.markItemRead {font-size : 10px; color : #333333;}</style></head><body>"; //$NON-NLS-1$
|
|
56 |
private static final String HTML_BODY_FOOTER = "</body></html>"; //$NON-NLS-1$
|
|
57 |
private static final int MAX_ELEM_LEN = 256;
|
|
58 |
|
|
59 |
private Rss rss;
|
|
60 |
|
|
61 |
@Override
|
|
62 |
public void init() {
|
|
63 |
URL url = getURL();
|
|
64 |
if (url != null) {
|
|
65 |
try {
|
|
66 |
rss = SimpleRSSReader.readRSS(url);
|
|
67 |
displayRSS();
|
|
68 |
actionBar.hookBrowser();
|
|
69 |
} catch (Exception e) {
|
|
70 |
Activator.logError(MessageFormat.format(Messages.AbstractRSSPortalPageLayer_RSSReadError, url), e);
|
|
71 |
}
|
|
72 |
actionBar.update();
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
private void displayRSS() {
|
|
77 |
StringBuffer buf = new StringBuffer();
|
|
78 |
buf.append(HTML_BODY_HEADER);
|
|
79 |
for (Channel channel : rss.getChannels()) {
|
|
80 |
buf.append("<div class=\"channel\"><a class=\"channelName\" href=\""); //$NON-NLS-1$
|
|
81 |
buf.append(channel.getLink().toString());
|
|
82 |
buf.append("\">"); //$NON-NLS-1$
|
|
83 |
buf.append(clean(channel.getTitle()));
|
|
84 |
buf.append("</a>"); //$NON-NLS-1$
|
|
85 |
buf.append("<div class=\"channelBody\">"); //$NON-NLS-1$
|
|
86 |
buf.append(clean(channel.getDescription()));
|
|
87 |
buf.append("</div><br>"); //$NON-NLS-1$
|
|
88 |
for (Item item : channel.getItems()) {
|
|
89 |
buf.append("<div class=\"item\"><a class=\"itemTitle\" href=\""); //$NON-NLS-1$
|
|
90 |
buf.append(item.getLink().toString());
|
|
91 |
buf.append("\">"); //$NON-NLS-1$
|
|
92 |
buf.append(clean(item.getTitle()));
|
|
93 |
buf.append("</a>"); //$NON-NLS-1$
|
|
94 |
buf.append("<div class=\"itemBody\">"); //$NON-NLS-1$
|
|
95 |
buf.append(clean(item.getDescription()));
|
|
96 |
buf.append("</div>"); //$NON-NLS-1$
|
|
97 |
}
|
|
98 |
}
|
|
99 |
buf.append(HTML_BODY_FOOTER);
|
|
100 |
System.out.println(buf.toString());
|
|
101 |
browser.setText(buf.toString());
|
|
102 |
}
|
|
103 |
|
|
104 |
private String clean(String s) {
|
|
105 |
for (Pattern pattern : EXCLUDE_TAG_PATTERNS) {
|
|
106 |
s = pattern.matcher(s).replaceAll(""); //$NON-NLS-1$
|
|
107 |
}
|
|
108 |
return s.length() > MAX_ELEM_LEN ? s.substring(0, MAX_ELEM_LEN) : s;
|
|
109 |
}
|
|
110 |
|
|
111 |
@Override
|
|
112 |
public void dispose() {
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
protected Set<IAction> makeActions() {
|
|
117 |
Set<IAction> actions = new LinkedHashSet<IAction>();
|
|
118 |
for (IAction action : super.makeActions()) {
|
|
119 |
if (action.getText().equals(Messages.AbstractBrowserPortalPage_RefreshLabel)) {
|
|
120 |
IAction a = new Action(Messages.AbstractBrowserPortalPage_RefreshLabel) {
|
|
121 |
@Override
|
|
122 |
public void run() {
|
|
123 |
if (browser != null) {
|
|
124 |
if (browserHasURL())
|
|
125 |
browser.refresh();
|
|
126 |
else
|
|
127 |
displayRSS();
|
|
128 |
actionBar.update();
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
|
132 |
@Override
|
|
133 |
public boolean isEnabled() {
|
|
134 |
return browser != null ? !actionBar.isLoading() : false;
|
|
135 |
}
|
|
136 |
};
|
|
137 |
actions.add(a);
|
|
138 |
}
|
|
139 |
else
|
|
140 |
actions.add(action);
|
|
141 |
}
|
|
142 |
|
|
143 |
actions.add(new Action(Messages.AbstractRSSPortalPageLayer_ReturnToFeedLabel) {
|
|
144 |
@Override
|
|
145 |
public void run() {
|
|
146 |
displayRSS();
|
|
147 |
actionBar.update();
|
|
148 |
}
|
|
149 |
|
|
150 |
@Override
|
|
151 |
public boolean isEnabled() {
|
|
152 |
return browserHasURL();
|
|
153 |
}
|
|
154 |
});
|
|
155 |
|
|
156 |
return actions;
|
|
157 |
}
|
|
158 |
|
|
159 |
}
|