author | dadubrow |
Fri, 13 Aug 2010 08:37:37 -0500 | |
changeset 1822 | 8fdfd1446ad7 |
parent 1814 | 837f2f0d0b21 |
child 1824 | 7e34f628583a |
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 |
||
29 |
import org.eclipse.jface.action.Action; |
|
30 |
import org.eclipse.jface.action.IAction; |
|
31 |
||
32 |
import com.nokia.carbide.discovery.ui.Activator; |
|
33 |
import com.nokia.carbide.discovery.ui.Messages; |
|
34 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader; |
|
35 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Channel; |
|
36 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Item; |
|
37 |
import com.nokia.carbide.internal.discovery.ui.editor.SimpleRSSReader.Rss; |
|
38 |
||
39 |
public abstract class AbstractRSSPortalPageLayer extends AbstractBrowserPortalPageLayer { |
|
40 |
||
1759 | 41 |
private static final List<Pattern> EXCLUDE_TAG_PATTERNS = new ArrayList<Pattern>(); |
42 |
||
1721 | 43 |
private static final String[] EXCLUDE_TAGS = { |
44 |
"object", //$NON-NLS-1$ |
|
45 |
"param", //$NON-NLS-1$ |
|
46 |
"embed", //$NON-NLS-1$ |
|
47 |
"script", //$NON-NLS-1$ |
|
48 |
"img" //$NON-NLS-1$ |
|
49 |
}; |
|
50 |
||
1759 | 51 |
private static final String[] EXCLUDE_FORMAT_TAGS = { |
52 |
"b", //$NON-NLS-1$ |
|
53 |
"i", //$NON-NLS-1$ |
|
54 |
"em", //$NON-NLS-1$ |
|
55 |
"strong", //$NON-NLS-1$ |
|
56 |
"tt", //$NON-NLS-1$ |
|
57 |
"s", //$NON-NLS-1$ |
|
58 |
"strike", //$NON-NLS-1$ |
|
59 |
"p" //$NON-NLS-1$ |
|
60 |
}; |
|
1721 | 61 |
|
62 |
static { |
|
63 |
for (String tagString : EXCLUDE_TAGS) { |
|
64 |
StringBuilder sb = new StringBuilder(); |
|
65 |
sb.append('<'); |
|
66 |
sb.append(tagString); |
|
67 |
sb.append(" .*</"); //$NON-NLS-1$ |
|
68 |
sb.append(tagString); |
|
69 |
sb.append('>'); |
|
1759 | 70 |
EXCLUDE_TAG_PATTERNS.add(Pattern.compile(sb.toString(), |
71 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)); |
|
1721 | 72 |
} |
73 |
for (String tagString : EXCLUDE_TAGS) { |
|
74 |
StringBuilder sb = new StringBuilder(); |
|
75 |
sb.append('<'); |
|
76 |
sb.append(tagString); |
|
77 |
sb.append(" .*/>"); //$NON-NLS-1$ |
|
1759 | 78 |
EXCLUDE_TAG_PATTERNS.add(Pattern.compile(sb.toString(), |
79 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)); |
|
80 |
} |
|
81 |
for (String formatTag : EXCLUDE_FORMAT_TAGS) { |
|
82 |
StringBuilder sb = new StringBuilder(); |
|
83 |
sb.append("</?"); //$NON-NLS-1$ |
|
84 |
sb.append(formatTag); |
|
85 |
sb.append('>'); |
|
86 |
EXCLUDE_TAG_PATTERNS.add(Pattern.compile(sb.toString(), |
|
87 |
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE)); |
|
88 |
||
1721 | 89 |
} |
90 |
} |
|
91 |
||
92 |
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$ |
|
93 |
private static final String HTML_BODY_FOOTER = "</body></html>"; //$NON-NLS-1$ |
|
94 |
private static final int MAX_ELEM_LEN = 256; |
|
95 |
||
96 |
private Rss rss; |
|
97 |
||
98 |
@Override |
|
99 |
public void init() { |
|
1734 | 100 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() { |
101 |
@Override |
|
102 |
public void run() { |
|
1814 | 103 |
actionBar.hookBrowser(); |
104 |
readRSS(); |
|
105 |
actionBar.update(); |
|
1822 | 106 |
browser.setFocus(); |
1721 | 107 |
} |
1734 | 108 |
}); |
1721 | 109 |
} |
110 |
||
1814 | 111 |
protected void readRSS() { |
112 |
URL url = getURL(); |
|
113 |
if (url != null) { |
|
114 |
try { |
|
115 |
rss = SimpleRSSReader.readRSS(url); |
|
116 |
displayRSS(); |
|
117 |
} catch (Exception e) { |
|
118 |
Activator.logError(MessageFormat.format(Messages.AbstractRSSPortalPageLayer_RSSReadError, url), e); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
protected void displayRSS() { |
|
1721 | 124 |
StringBuffer buf = new StringBuffer(); |
125 |
buf.append(HTML_BODY_HEADER); |
|
126 |
for (Channel channel : rss.getChannels()) { |
|
127 |
buf.append("<div class=\"channel\"><a class=\"channelName\" href=\""); //$NON-NLS-1$ |
|
128 |
buf.append(channel.getLink().toString()); |
|
129 |
buf.append("\">"); //$NON-NLS-1$ |
|
130 |
buf.append(clean(channel.getTitle())); |
|
131 |
buf.append("</a>"); //$NON-NLS-1$ |
|
132 |
buf.append("<div class=\"channelBody\">"); //$NON-NLS-1$ |
|
133 |
buf.append(clean(channel.getDescription())); |
|
134 |
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
|
135 |
buf.append("<ul>"); //$NON-NLS-1$ |
1721 | 136 |
for (Item item : channel.getItems()) { |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
137 |
buf.append("<li><div class=\"item\"><a class=\"itemTitle\" href=\""); //$NON-NLS-1$ |
1721 | 138 |
buf.append(item.getLink().toString()); |
139 |
buf.append("\">"); //$NON-NLS-1$ |
|
140 |
buf.append(clean(item.getTitle())); |
|
141 |
buf.append("</a>"); //$NON-NLS-1$ |
|
142 |
buf.append("<div class=\"itemBody\">"); //$NON-NLS-1$ |
|
1750 | 143 |
Date date = item.getPubDate(); |
1759 | 144 |
if (date != null) { |
145 |
String dateString = DateFormat.getDateTimeInstance().format(date); |
|
146 |
buf.append(dateString); |
|
147 |
buf.append("<br>"); |
|
148 |
} |
|
1721 | 149 |
buf.append(clean(item.getDescription())); |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
150 |
buf.append("</div></li>"); //$NON-NLS-1$ |
1721 | 151 |
} |
1727
743cc54fdffe
more portal changes - add kits page + pref page links to customize
dadubrow
parents:
1721
diff
changeset
|
152 |
buf.append("</ul>"); //$NON-NLS-1$ |
1721 | 153 |
} |
154 |
buf.append(HTML_BODY_FOOTER); |
|
155 |
browser.setText(buf.toString()); |
|
156 |
} |
|
157 |
||
158 |
private String clean(String s) { |
|
1759 | 159 |
String output = s; |
1721 | 160 |
for (Pattern pattern : EXCLUDE_TAG_PATTERNS) { |
1759 | 161 |
output = pattern.matcher(output).replaceAll(""); //$NON-NLS-1$ |
1721 | 162 |
} |
1759 | 163 |
output = output.length() > MAX_ELEM_LEN ? output.substring(0, MAX_ELEM_LEN) : output; |
164 |
return output; |
|
1721 | 165 |
} |
166 |
||
167 |
@Override |
|
168 |
public void dispose() { |
|
169 |
} |
|
170 |
||
171 |
@Override |
|
172 |
protected Set<IAction> makeActions() { |
|
173 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
174 |
for (IAction action : super.makeActions()) { |
|
175 |
if (action.getText().equals(Messages.AbstractBrowserPortalPage_RefreshLabel)) { |
|
176 |
IAction a = new Action(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
177 |
@Override |
|
178 |
public void run() { |
|
179 |
if (browser != null) { |
|
180 |
if (browserHasURL()) |
|
181 |
browser.refresh(); |
|
182 |
else |
|
1814 | 183 |
readRSS(); |
1721 | 184 |
actionBar.update(); |
185 |
} |
|
186 |
} |
|
187 |
||
188 |
@Override |
|
189 |
public boolean isEnabled() { |
|
190 |
return browser != null ? !actionBar.isLoading() : false; |
|
191 |
} |
|
192 |
}; |
|
193 |
actions.add(a); |
|
194 |
} |
|
195 |
else |
|
196 |
actions.add(action); |
|
197 |
} |
|
198 |
||
199 |
actions.add(new Action(Messages.AbstractRSSPortalPageLayer_ReturnToFeedLabel) { |
|
200 |
@Override |
|
201 |
public void run() { |
|
202 |
displayRSS(); |
|
203 |
actionBar.update(); |
|
204 |
} |
|
205 |
||
206 |
@Override |
|
207 |
public boolean isEnabled() { |
|
208 |
return browserHasURL(); |
|
209 |
} |
|
210 |
}); |
|
211 |
||
212 |
return actions; |
|
213 |
} |
|
214 |
||
215 |
} |