author | dadubrow |
Mon, 16 Aug 2010 09:03:23 -0500 | |
changeset 1831 | 3319dd953212 |
parent 1824 | 7e34f628583a |
child 1832 | aaf2f7bc0609 |
permissions | -rw-r--r-- |
1734 | 1 |
/* |
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
1721 | 17 |
package com.nokia.carbide.internal.discovery.ui.extension; |
18 |
||
19 |
import java.net.URL; |
|
1750 | 20 |
import java.text.DateFormat; |
1721 | 21 |
import java.text.MessageFormat; |
1759 | 22 |
import java.util.ArrayList; |
1750 | 23 |
import java.util.Date; |
1721 | 24 |
import java.util.LinkedHashSet; |
1759 | 25 |
import java.util.List; |
1721 | 26 |
import java.util.Set; |
27 |
import java.util.regex.Pattern; |
|
28 |
||
1831 | 29 |
import org.eclipse.core.runtime.IProgressMonitor; |
30 |
import org.eclipse.core.runtime.IStatus; |
|
31 |
import org.eclipse.core.runtime.Status; |
|
32 |
import org.eclipse.core.runtime.jobs.Job; |
|
1721 | 33 |
import org.eclipse.jface.action.Action; |
34 |
import org.eclipse.jface.action.IAction; |
|
1831 | 35 |
import org.eclipse.swt.widgets.Display; |
1721 | 36 |
|
37 |
import com.nokia.carbide.discovery.ui.Activator; |
|
38 |
import com.nokia.carbide.discovery.ui.Messages; |
|
39 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader; |
|
40 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Channel; |
|
41 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Item; |
|
42 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Rss; |
|
43 |
||
44 |
public abstract class AbstractRSSPortalPageLayer extends AbstractBrowserPortalPageLayer { |
|
45 |
||
1759 | 46 |
private static final List<Pattern> EXCLUDE_TAG_PATTERNS = new ArrayList<Pattern>(); |
47 |
||
1721 | 48 |
private static final String[] EXCLUDE_TAGS = { |
49 |
"object", //$NON-NLS-1$ |
|
50 |
"param", //$NON-NLS-1$ |
|
51 |
"embed", //$NON-NLS-1$ |
|
52 |
"script", //$NON-NLS-1$ |
|
53 |
"img" //$NON-NLS-1$ |
|
54 |
}; |
|
55 |
||
1759 | 56 |
private static final String[] EXCLUDE_FORMAT_TAGS = { |
57 |
"b", //$NON-NLS-1$ |
|
58 |
"i", //$NON-NLS-1$ |
|
59 |
"em", //$NON-NLS-1$ |
|
60 |
"strong", //$NON-NLS-1$ |
|
61 |
"tt", //$NON-NLS-1$ |
|
62 |
"s", //$NON-NLS-1$ |
|
63 |
"strike", //$NON-NLS-1$ |
|
64 |
"p" //$NON-NLS-1$ |
|
65 |
}; |
|
1721 | 66 |
|
67 |
static { |
|
68 |
for (String tagString : EXCLUDE_TAGS) { |
|
69 |
StringBuilder sb = new StringBuilder(); |
|
70 |
sb.append('<'); |
|
71 |
sb.append(tagString); |
|
72 |
sb.append(" .*</"); //$NON-NLS-1$ |
|
73 |
sb.append(tagString); |
|
74 |
sb.append('>'); |
|
1759 | 75 |
EXCLUDE_TAG_PATTERNS.add(Pattern.compile(sb.toString(), |
76 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)); |
|
1721 | 77 |
} |
78 |
for (String tagString : EXCLUDE_TAGS) { |
|
79 |
StringBuilder sb = new StringBuilder(); |
|
80 |
sb.append('<'); |
|
81 |
sb.append(tagString); |
|
82 |
sb.append(" .*/>"); //$NON-NLS-1$ |
|
1759 | 83 |
EXCLUDE_TAG_PATTERNS.add(Pattern.compile(sb.toString(), |
84 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)); |
|
85 |
} |
|
86 |
for (String formatTag : EXCLUDE_FORMAT_TAGS) { |
|
87 |
StringBuilder sb = new StringBuilder(); |
|
88 |
sb.append("</?"); //$NON-NLS-1$ |
|
89 |
sb.append(formatTag); |
|
90 |
sb.append('>'); |
|
91 |
EXCLUDE_TAG_PATTERNS.add(Pattern.compile(sb.toString(), |
|
92 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)); |
|
93 |
||
1721 | 94 |
} |
95 |
} |
|
96 |
||
97 |
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$ |
|
98 |
private static final String HTML_BODY_FOOTER = "</body></html>"; //$NON-NLS-1$ |
|
99 |
private static final int MAX_ELEM_LEN = 256; |
|
100 |
||
101 |
private Rss rss; |
|
102 |
||
103 |
@Override |
|
104 |
public void init() { |
|
1734 | 105 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() { |
106 |
@Override |
|
107 |
public void run() { |
|
1814 | 108 |
actionBar.hookBrowser(); |
109 |
readRSS(); |
|
110 |
actionBar.update(); |
|
1822 | 111 |
browser.setFocus(); |
1721 | 112 |
} |
1734 | 113 |
}); |
1721 | 114 |
} |
115 |
||
1814 | 116 |
protected void readRSS() { |
1831 | 117 |
final URL url = getURL(); |
1814 | 118 |
if (url != null) { |
1831 | 119 |
Job j = new Job("Getting RSS feed") { |
120 |
||
121 |
@Override |
|
122 |
protected IStatus run(IProgressMonitor monitor) { |
|
123 |
try { |
|
124 |
rss = SimpleRSSReader.readRSS(url); |
|
125 |
displayRSS(); |
|
126 |
} catch (Exception e) { |
|
127 |
Activator.logError(MessageFormat.format(Messages.AbstractRSSPortalPageLayer_RSSReadError, url), e); |
|
128 |
} |
|
129 |
return Status.OK_STATUS; |
|
130 |
} |
|
131 |
||
132 |
}; |
|
133 |
j.setUser(true); |
|
134 |
j.schedule(); |
|
1814 | 135 |
} |
136 |
} |
|
137 |
||
138 |
protected void displayRSS() { |
|
1721 | 139 |
StringBuffer buf = new StringBuffer(); |
140 |
buf.append(HTML_BODY_HEADER); |
|
141 |
for (Channel channel : rss.getChannels()) { |
|
142 |
buf.append("<div class=\"channel\"><a class=\"channelName\" href=\""); //$NON-NLS-1$ |
|
143 |
buf.append(channel.getLink().toString()); |
|
144 |
buf.append("\">"); //$NON-NLS-1$ |
|
145 |
buf.append(clean(channel.getTitle())); |
|
146 |
buf.append("</a>"); //$NON-NLS-1$ |
|
147 |
buf.append("<div class=\"channelBody\">"); //$NON-NLS-1$ |
|
148 |
buf.append(clean(channel.getDescription())); |
|
149 |
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
|
150 |
buf.append("<ul>"); //$NON-NLS-1$ |
1721 | 151 |
for (Item item : channel.getItems()) { |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
152 |
buf.append("<li><div class=\"item\"><a class=\"itemTitle\" href=\""); //$NON-NLS-1$ |
1721 | 153 |
buf.append(item.getLink().toString()); |
154 |
buf.append("\">"); //$NON-NLS-1$ |
|
155 |
buf.append(clean(item.getTitle())); |
|
156 |
buf.append("</a>"); //$NON-NLS-1$ |
|
157 |
buf.append("<div class=\"itemBody\">"); //$NON-NLS-1$ |
|
1750 | 158 |
Date date = item.getPubDate(); |
1759 | 159 |
if (date != null) { |
160 |
String dateString = DateFormat.getDateTimeInstance().format(date); |
|
161 |
buf.append(dateString); |
|
1824 | 162 |
buf.append("<br>"); //$NON-NLS-1$ |
1759 | 163 |
} |
1721 | 164 |
buf.append(clean(item.getDescription())); |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
165 |
buf.append("</div></li>"); //$NON-NLS-1$ |
1721 | 166 |
} |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
167 |
buf.append("</ul>"); //$NON-NLS-1$ |
1721 | 168 |
} |
169 |
buf.append(HTML_BODY_FOOTER); |
|
1831 | 170 |
final String s = buf.toString(); |
171 |
Display.getDefault().syncExec(new Runnable() { |
|
172 |
@Override |
|
173 |
public void run() { |
|
174 |
browser.setText(s); |
|
175 |
} |
|
176 |
}); |
|
1721 | 177 |
} |
178 |
||
179 |
private String clean(String s) { |
|
1759 | 180 |
String output = s; |
1721 | 181 |
for (Pattern pattern : EXCLUDE_TAG_PATTERNS) { |
1759 | 182 |
output = pattern.matcher(output).replaceAll(""); //$NON-NLS-1$ |
1721 | 183 |
} |
1759 | 184 |
output = output.length() > MAX_ELEM_LEN ? output.substring(0, MAX_ELEM_LEN) : output; |
185 |
return output; |
|
1721 | 186 |
} |
187 |
||
188 |
@Override |
|
189 |
public void dispose() { |
|
190 |
} |
|
191 |
||
192 |
@Override |
|
193 |
protected Set<IAction> makeActions() { |
|
194 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
195 |
for (IAction action : super.makeActions()) { |
|
196 |
if (action.getText().equals(Messages.AbstractBrowserPortalPage_RefreshLabel)) { |
|
197 |
IAction a = new Action(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
198 |
@Override |
|
199 |
public void run() { |
|
200 |
if (browser != null) { |
|
201 |
if (browserHasURL()) |
|
202 |
browser.refresh(); |
|
203 |
else |
|
1814 | 204 |
readRSS(); |
1721 | 205 |
actionBar.update(); |
206 |
} |
|
207 |
} |
|
208 |
||
209 |
@Override |
|
210 |
public boolean isEnabled() { |
|
211 |
return browser != null ? !actionBar.isLoading() : false; |
|
212 |
} |
|
213 |
}; |
|
214 |
actions.add(a); |
|
215 |
} |
|
216 |
else |
|
217 |
actions.add(action); |
|
218 |
} |
|
219 |
||
220 |
actions.add(new Action(Messages.AbstractRSSPortalPageLayer_ReturnToFeedLabel) { |
|
221 |
@Override |
|
222 |
public void run() { |
|
223 |
displayRSS(); |
|
224 |
actionBar.update(); |
|
225 |
} |
|
226 |
||
227 |
@Override |
|
228 |
public boolean isEnabled() { |
|
229 |
return browserHasURL(); |
|
230 |
} |
|
231 |
}); |
|
232 |
||
233 |
return actions; |
|
234 |
} |
|
235 |
||
236 |
} |