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