author | dadubrow |
Mon, 16 Aug 2010 10:18:18 -0500 | |
changeset 1832 | aaf2f7bc0609 |
parent 1831 | 3319dd953212 |
child 1849 | 40d70835dc4f |
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 int MAX_ELEM_LEN = 256; |
|
98 |
||
99 |
private Rss rss; |
|
100 |
||
101 |
@Override |
|
102 |
public void init() { |
|
1734 | 103 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() { |
104 |
@Override |
|
105 |
public void run() { |
|
1814 | 106 |
actionBar.hookBrowser(); |
107 |
readRSS(); |
|
108 |
actionBar.update(); |
|
1822 | 109 |
browser.setFocus(); |
1721 | 110 |
} |
1734 | 111 |
}); |
1721 | 112 |
} |
113 |
||
1814 | 114 |
protected void readRSS() { |
1831 | 115 |
final URL url = getURL(); |
1814 | 116 |
if (url != null) { |
1831 | 117 |
Job j = new Job("Getting RSS feed") { |
118 |
||
119 |
@Override |
|
120 |
protected IStatus run(IProgressMonitor monitor) { |
|
121 |
try { |
|
122 |
rss = SimpleRSSReader.readRSS(url); |
|
123 |
displayRSS(); |
|
124 |
} catch (Exception e) { |
|
125 |
Activator.logError(MessageFormat.format(Messages.AbstractRSSPortalPageLayer_RSSReadError, url), e); |
|
126 |
} |
|
127 |
return Status.OK_STATUS; |
|
128 |
} |
|
129 |
||
130 |
}; |
|
131 |
j.setUser(true); |
|
132 |
j.schedule(); |
|
1814 | 133 |
} |
134 |
} |
|
135 |
||
136 |
protected void displayRSS() { |
|
1721 | 137 |
StringBuffer buf = new StringBuffer(); |
138 |
buf.append(HTML_BODY_HEADER); |
|
139 |
for (Channel channel : rss.getChannels()) { |
|
140 |
buf.append("<div class=\"channel\"><a class=\"channelName\" href=\""); //$NON-NLS-1$ |
|
141 |
buf.append(channel.getLink().toString()); |
|
142 |
buf.append("\">"); //$NON-NLS-1$ |
|
143 |
buf.append(clean(channel.getTitle())); |
|
144 |
buf.append("</a>"); //$NON-NLS-1$ |
|
145 |
buf.append("<div class=\"channelBody\">"); //$NON-NLS-1$ |
|
146 |
buf.append(clean(channel.getDescription())); |
|
147 |
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
|
148 |
buf.append("<ul>"); //$NON-NLS-1$ |
1721 | 149 |
for (Item item : channel.getItems()) { |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
150 |
buf.append("<li><div class=\"item\"><a class=\"itemTitle\" href=\""); //$NON-NLS-1$ |
1721 | 151 |
buf.append(item.getLink().toString()); |
152 |
buf.append("\">"); //$NON-NLS-1$ |
|
153 |
buf.append(clean(item.getTitle())); |
|
154 |
buf.append("</a>"); //$NON-NLS-1$ |
|
155 |
buf.append("<div class=\"itemBody\">"); //$NON-NLS-1$ |
|
1750 | 156 |
Date date = item.getPubDate(); |
1759 | 157 |
if (date != null) { |
158 |
String dateString = DateFormat.getDateTimeInstance().format(date); |
|
159 |
buf.append(dateString); |
|
1824 | 160 |
buf.append("<br>"); //$NON-NLS-1$ |
1759 | 161 |
} |
1721 | 162 |
buf.append(clean(item.getDescription())); |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
163 |
buf.append("</div></li>"); //$NON-NLS-1$ |
1721 | 164 |
} |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
165 |
buf.append("</ul>"); //$NON-NLS-1$ |
1721 | 166 |
} |
167 |
buf.append(HTML_BODY_FOOTER); |
|
1831 | 168 |
final String s = buf.toString(); |
169 |
Display.getDefault().syncExec(new Runnable() { |
|
170 |
@Override |
|
171 |
public void run() { |
|
172 |
browser.setText(s); |
|
173 |
} |
|
174 |
}); |
|
1721 | 175 |
} |
176 |
||
177 |
private String clean(String s) { |
|
1759 | 178 |
String output = s; |
1721 | 179 |
for (Pattern pattern : EXCLUDE_TAG_PATTERNS) { |
1759 | 180 |
output = pattern.matcher(output).replaceAll(""); //$NON-NLS-1$ |
1721 | 181 |
} |
1759 | 182 |
output = output.length() > MAX_ELEM_LEN ? output.substring(0, MAX_ELEM_LEN) : output; |
183 |
return output; |
|
1721 | 184 |
} |
185 |
||
186 |
@Override |
|
187 |
public void dispose() { |
|
188 |
} |
|
189 |
||
190 |
@Override |
|
191 |
protected Set<IAction> makeActions() { |
|
192 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
193 |
for (IAction action : super.makeActions()) { |
|
194 |
if (action.getText().equals(Messages.AbstractBrowserPortalPage_RefreshLabel)) { |
|
195 |
IAction a = new Action(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
196 |
@Override |
|
197 |
public void run() { |
|
198 |
if (browser != null) { |
|
199 |
if (browserHasURL()) |
|
200 |
browser.refresh(); |
|
201 |
else |
|
1814 | 202 |
readRSS(); |
1721 | 203 |
actionBar.update(); |
204 |
} |
|
205 |
} |
|
206 |
||
207 |
@Override |
|
208 |
public boolean isEnabled() { |
|
209 |
return browser != null ? !actionBar.isLoading() : false; |
|
210 |
} |
|
211 |
}; |
|
212 |
actions.add(a); |
|
213 |
} |
|
214 |
else |
|
215 |
actions.add(action); |
|
216 |
} |
|
217 |
||
218 |
actions.add(new Action(Messages.AbstractRSSPortalPageLayer_ReturnToFeedLabel) { |
|
219 |
@Override |
|
220 |
public void run() { |
|
221 |
displayRSS(); |
|
222 |
actionBar.update(); |
|
223 |
} |
|
224 |
||
225 |
@Override |
|
226 |
public boolean isEnabled() { |
|
227 |
return browserHasURL(); |
|
228 |
} |
|
229 |
}); |
|
230 |
||
231 |
return actions; |
|
232 |
} |
|
233 |
||
234 |
} |