author | dadubrow |
Thu, 29 Jul 2010 15:30:00 -0500 | |
changeset 1727 | 743cc54fdffe |
parent 1721 | 18638adda547 |
child 1734 | bf670d38123a |
permissions | -rw-r--r-- |
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$ |
|
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
88 |
buf.append("<ul>"); //$NON-NLS-1$ |
1721 | 89 |
for (Item item : channel.getItems()) { |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
90 |
buf.append("<li><div class=\"item\"><a class=\"itemTitle\" href=\""); //$NON-NLS-1$ |
1721 | 91 |
buf.append(item.getLink().toString()); |
92 |
buf.append("\">"); //$NON-NLS-1$ |
|
93 |
buf.append(clean(item.getTitle())); |
|
94 |
buf.append("</a>"); //$NON-NLS-1$ |
|
95 |
buf.append("<div class=\"itemBody\">"); //$NON-NLS-1$ |
|
96 |
buf.append(clean(item.getDescription())); |
|
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
97 |
buf.append("</div></li>"); //$NON-NLS-1$ |
1721 | 98 |
} |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
99 |
buf.append("</ul>"); //$NON-NLS-1$ |
1721 | 100 |
} |
101 |
buf.append(HTML_BODY_FOOTER); |
|
102 |
System.out.println(buf.toString()); |
|
103 |
browser.setText(buf.toString()); |
|
104 |
} |
|
105 |
||
106 |
private String clean(String s) { |
|
107 |
for (Pattern pattern : EXCLUDE_TAG_PATTERNS) { |
|
108 |
s = pattern.matcher(s).replaceAll(""); //$NON-NLS-1$ |
|
109 |
} |
|
110 |
return s.length() > MAX_ELEM_LEN ? s.substring(0, MAX_ELEM_LEN) : s; |
|
111 |
} |
|
112 |
||
113 |
@Override |
|
114 |
public void dispose() { |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
protected Set<IAction> makeActions() { |
|
119 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
120 |
for (IAction action : super.makeActions()) { |
|
121 |
if (action.getText().equals(Messages.AbstractBrowserPortalPage_RefreshLabel)) { |
|
122 |
IAction a = new Action(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
123 |
@Override |
|
124 |
public void run() { |
|
125 |
if (browser != null) { |
|
126 |
if (browserHasURL()) |
|
127 |
browser.refresh(); |
|
128 |
else |
|
129 |
displayRSS(); |
|
130 |
actionBar.update(); |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public boolean isEnabled() { |
|
136 |
return browser != null ? !actionBar.isLoading() : false; |
|
137 |
} |
|
138 |
}; |
|
139 |
actions.add(a); |
|
140 |
} |
|
141 |
else |
|
142 |
actions.add(action); |
|
143 |
} |
|
144 |
||
145 |
actions.add(new Action(Messages.AbstractRSSPortalPageLayer_ReturnToFeedLabel) { |
|
146 |
@Override |
|
147 |
public void run() { |
|
148 |
displayRSS(); |
|
149 |
actionBar.update(); |
|
150 |
} |
|
151 |
||
152 |
@Override |
|
153 |
public boolean isEnabled() { |
|
154 |
return browserHasURL(); |
|
155 |
} |
|
156 |
}); |
|
157 |
||
158 |
return actions; |
|
159 |
} |
|
160 |
||
161 |
} |