diff options
author | Yana Stamcheva <yana@jitsi.org> | 2010-09-15 15:57:40 +0000 |
---|---|---|
committer | Yana Stamcheva <yana@jitsi.org> | 2010-09-15 15:57:40 +0000 |
commit | e36f654bb3ed43fdad82905a1cd30f0fa09293e1 (patch) | |
tree | 688d69decd4ad160fb8d7e00582b3af1d1e8dee9 /src/net/java/sip/communicator/impl | |
parent | 8f01ecde7408b0d13d5e6f7eee2e8632354a9353 (diff) | |
download | jitsi-e36f654bb3ed43fdad82905a1cd30f0fa09293e1.zip jitsi-e36f654bb3ed43fdad82905a1cd30f0fa09293e1.tar.gz jitsi-e36f654bb3ed43fdad82905a1cd30f0fa09293e1.tar.bz2 |
Patch provided by Purvesh Sahoo, fixing redundant code in replacement implementations + removed replacement blue borders
Diffstat (limited to 'src/net/java/sip/communicator/impl')
13 files changed, 481 insertions, 672 deletions
diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java b/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java index 26f4d7d..aa26527 100755 --- a/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/ChatConversationPanel.java @@ -26,6 +26,7 @@ import net.java.sip.communicator.impl.gui.main.chat.menus.*; import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.service.gui.*; import net.java.sip.communicator.service.replacement.*; +import net.java.sip.communicator.service.replacement.smilies.*; import net.java.sip.communicator.util.*; import net.java.sip.communicator.util.swing.*; import net.java.sip.communicator.util.swing.SwingWorker; @@ -617,31 +618,82 @@ public class ChatConversationPanel ReplacementService source = entry.getValue(); + boolean isSmiley + = source instanceof SmiliesReplacementService; + if (!(GuiActivator.getConfigurationService().getBoolean( ReplacementProperty.getPropertyName(source - .getSourceName()), true) && (isEnabled || entry.getKey() - .equals("SMILEY")))) + .getSourceName()), true) && (isEnabled || isSmiley))) continue; - temp = source.getReplacedMessage(msgStore); + String sourcePattern = source.getPattern(); + Pattern p = Pattern.compile(sourcePattern, + Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + + Matcher m = p.matcher(msgStore); + + String startPlainTextTag = START_PLAINTEXT_TAG; + String endPlainTextTag = END_PLAINTEXT_TAG; + + int count = 0, startPos = 0; + StringBuffer msgBuff = new StringBuffer(); + + while (m.find()) + { + count++; + msgBuff.append(msgStore.substring(startPos, m.start())); + startPos = m.end(); + + temp = source.getReplacement(m.group()); + + if(!temp.equals(m.group(0)) || source.getSourceName() + .equals("DIRECTIMAGE")) + { + if(isSmiley) + { + msgBuff.append(endPlainTextTag); + msgBuff.append("<IMG SRC=\""); + } + else + { + msgBuff.append( + "<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); + } + + msgBuff.append(temp); + msgBuff.append("\" BORDER=\"0\" ALT=\""); + msgBuff.append(m.group(0)); + msgBuff.append("\"></IMG>"); + + if(isSmiley) + msgBuff.append(startPlainTextTag); + } + else + { + msgBuff.append( + msgStore.substring(m.start(), m.end())); + } + } + + msgBuff.append(msgStore.substring(startPos)); /* * replace the msgStore variable with the current replaced * message before next iteration */ - if (!temp.equals(msgStore)) + if (!msgBuff.toString().equals(msgStore)) { - msgStore = temp; + msgStore = msgBuff.toString(); } } - if (!temp.equals(chatFinal)) + if (!msgStore.equals(chatFinal)) { synchronized (scrollToBottomRunnable) { scrollToBottomIsPending = true; - document.setOuterHTML(elem, temp.toString().substring( - temp.indexOf("<DIV"))); + document.setOuterHTML(elem, msgStore.toString() + .substring(msgStore.indexOf("<DIV"))); } } return ""; diff --git a/src/net/java/sip/communicator/impl/replacement/bliptv/ReplacementServiceBliptvImpl.java b/src/net/java/sip/communicator/impl/replacement/bliptv/ReplacementServiceBliptvImpl.java index d51bd36..fcd885b 100644 --- a/src/net/java/sip/communicator/impl/replacement/bliptv/ReplacementServiceBliptvImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/bliptv/ReplacementServiceBliptvImpl.java @@ -33,7 +33,8 @@ public class ReplacementServiceBliptvImpl * The regex used to match the link in the message. */ public static final String BLIPTV_PATTERN = - "(?:[\\>])(http:\\/\\/(?:www\\.)?blip\\.tv\\/file\\/(\\d+).*(?=<))"; + "(?<=>)(http:\\/\\/(?:www\\.)?blip\\.tv" + + "\\/file\\/(\\d+)([?&\\?]\\w+=[\\w-]+)*)(?=</A>)"; /** * Configuration label shown in the config form. @@ -44,7 +45,7 @@ public class ReplacementServiceBliptvImpl * Source name; also used as property label. */ public static final String SOURCE_NAME = "BLIPTV"; - + /** * Constructor for <tt>ReplacementServiceBliptvImpl</tt>. */ @@ -54,81 +55,47 @@ public class ReplacementServiceBliptvImpl } /** - * Replaces the Blip.tv video links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Replaces the Blip.tv video links with their corresponding thumbnails. + * + * @param sourceString the original chat message. + * @return replaced thumbnail image link; the original video link in case of + * no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { - final Pattern p = - Pattern.compile(BLIPTV_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); - - while (m.find()) + try { - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); - - try - { - String url = "http://oohembed.com/oohembed/?url=" + m.group(1); + String url = "http://oohembed.com/oohembed/?url=" + sourceString; - URL sourceURL = new URL(url); - URLConnection conn = sourceURL.openConnection(); + URL sourceURL = new URL(url); + URLConnection conn = sourceURL.openConnection(); - BufferedReader in = - new BufferedReader(new InputStreamReader(conn - .getInputStream())); + BufferedReader in = + new BufferedReader(new InputStreamReader(conn.getInputStream())); - String inputLine, holder = ""; + String inputLine, holder = ""; - while ((inputLine = in.readLine()) != null) - holder += inputLine; - in.close(); + while ((inputLine = in.readLine()) != null) + holder += inputLine; + in.close(); - JSONObject wrapper = new JSONObject(holder); + JSONObject wrapper = new JSONObject(holder); - String thumbUrl = wrapper.getString("thumbnail_url"); + String thumbUrl = wrapper.getString("thumbnail_url"); - if (thumbUrl != null) - { - msgBuff.append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff.append(thumbUrl); - msgBuff.append("\"></IMG>"); - - } - else - { - startPos = 0; - msgBuff = new StringBuffer(); - } - - } - catch (Exception e) + if (thumbUrl != null) { - startPos = 0; - msgBuff = new StringBuffer(); - e.printStackTrace(); + return thumbUrl; } - + } + catch (Exception e) + { + e.printStackTrace(); } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return sourceString; } - + /** * Returns the source name * @@ -138,4 +105,14 @@ public class ReplacementServiceBliptvImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return BLIPTV_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/dailymotion/ReplacementServiceDailymotionImpl.java b/src/net/java/sip/communicator/impl/replacement/dailymotion/ReplacementServiceDailymotionImpl.java index d2cfc6b..d6aa92c 100644 --- a/src/net/java/sip/communicator/impl/replacement/dailymotion/ReplacementServiceDailymotionImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/dailymotion/ReplacementServiceDailymotionImpl.java @@ -13,7 +13,7 @@ import net.java.sip.communicator.util.*; /** * Implements the {@link ReplacementService} to provide previews for Dailymotion * links. - * + * * @author Purvesh Sahoo */ public class ReplacementServiceDailymotionImpl @@ -29,13 +29,14 @@ public class ReplacementServiceDailymotionImpl * The regex used to match the link in the message. */ public static final String DAILYMOTION_PATTERN = - "(http.*?(www\\.)*?dailymotion\\.com\\/video\\/([a-zA-Z0-9_\\-]+))([?#]([a-zA-Z0-9_\\-]+))*"; + "(?<=>)(https?\\:\\/\\/(www\\.)*?dailymotion\\.com" + + "\\/video\\/([a-zA-Z0-9_\\-]+))([?#]([a-zA-Z0-9_\\-]+))*(?=</A>)"; /** * Configuration label shown in the config form. */ public static final String DAILYMOTION_CONFIG_LABEL = "DailyMotion"; - + /** * Source name; also used as property label. */ @@ -50,52 +51,29 @@ public class ReplacementServiceDailymotionImpl } /** - * Replaces the dailymotion video links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Returns the thumbnail URL of the video link provided. + * + * @param sourceString the original video link. + * @return the thumbnail image link; the original link in case of no match. */ - public String getReplacedMessage(final String chatString) + public String getReplacement(String sourceString) { final Pattern p = - Pattern.compile(DAILYMOTION_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + Pattern.compile( + "(.+\\/video\\/([a-zA-Z0-9_\\-]+))([?#]([a-zA-Z0-9_\\-]+))*", + Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + Matcher m = p.matcher(sourceString); + + String thumbUrl = sourceString; while (m.find()) - { - - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); - - if (count % 2 == 0) - { - msgBuff.append("<IMG HEIGHT=\"120\" WIDTH=\"160\" SRC=\""); - msgBuff - .append("http://www.dailymotion.com/thumbnail/160x120/video/"); - msgBuff.append(m.group(3)); - msgBuff.append("\"></IMG>"); - } - else - { - msgBuff.append(chatString.substring(m.start(), m.end())); - } - } - - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); + thumbUrl = + "http://www.dailymotion.com/thumbnail/160x120/video/" + + m.group(2); - return chatString; + return thumbUrl; } - + /** * Returns the source name * @@ -105,4 +83,14 @@ public class ReplacementServiceDailymotionImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return DAILYMOTION_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/directimage/ReplacementServiceDirectImageImpl.java b/src/net/java/sip/communicator/impl/replacement/directimage/ReplacementServiceDirectImageImpl.java index 85fe938..c8339a0 100644 --- a/src/net/java/sip/communicator/impl/replacement/directimage/ReplacementServiceDirectImageImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/directimage/ReplacementServiceDirectImageImpl.java @@ -5,8 +5,6 @@ */ package net.java.sip.communicator.impl.replacement.directimage; -import java.util.regex.*; - import net.java.sip.communicator.service.replacement.*; import net.java.sip.communicator.util.*; @@ -40,7 +38,7 @@ public class ReplacementServiceDirectImageImpl * Source name; also used as property label. */ public static final String SOURCE_NAME = "DIRECTIMAGE"; - + /** * Constructor for <tt>ReplacementServiceDirectImageImpl</tt>. */ @@ -50,45 +48,16 @@ public class ReplacementServiceDirectImageImpl } /** - * Replaces the direct image links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of exception. + * Returns the thumbnail URL of the image link provided. + * + * @param sourceString the original image link. + * @return the thumbnail image link; the original link in case of no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { - final Pattern p = - Pattern.compile(URL_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); - - while (m.find()) - { - - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); - - String url = - "<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\"" + m.group(0) - + "\"></IMG>"; - msgBuff.append(url); - - } - - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return sourceString; } - + /** * Returns the source name * @@ -98,4 +67,14 @@ public class ReplacementServiceDirectImageImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return URL_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/flickr/ReplacementServiceFlickrImpl.java b/src/net/java/sip/communicator/impl/replacement/flickr/ReplacementServiceFlickrImpl.java index ad5c63a..1e37de6 100644 --- a/src/net/java/sip/communicator/impl/replacement/flickr/ReplacementServiceFlickrImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/flickr/ReplacementServiceFlickrImpl.java @@ -33,7 +33,8 @@ public class ReplacementServiceFlickrImpl * The regex used to match the link in the message. */ public static final String FLICKR_PATTERN = - "(http.*?(www\\.)*?flickr\\.com\\/photos\\/[0-9a-zA-Z_\\-\\@]+\\/([0-9]+)(\\/[^\"\\<]*)*)"; + "(?<=>)(https?\\:\\/\\/(www\\.)*?flickr\\.com" + + "\\/photos\\/[0-9a-zA-Z_\\-\\@]+\\/([0-9]+)(\\/[^\"\\<]*)*)(?=</A>)"; /** * API Key required to access the Flickr api. @@ -59,104 +60,72 @@ public class ReplacementServiceFlickrImpl } /** - * Replaces the Flickr image links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Replaces the Flickr image links with their corresponding thumbnails. + * + * @param sourceString the original flickr image link. + * @return replaced thumbnail image link; the original image link in case of + * no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { final Pattern p = - Pattern.compile(FLICKR_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + Pattern.compile( + "\\/photos\\/[0-9a-zA-Z_\\-\\@]+\\/([0-9]+)(\\/[^\"\\<]*)*", + Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + Matcher m = p.matcher(sourceString); + String thumbUrl = sourceString; while (m.find()) { - - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); - - if (count % 2 == 0) + try { - try - { - // API URL - String url = - "http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=" - + API_KEY + "&photo_id=" + m.group(3) - + "&format=json&nojsoncallback=1"; + // API URL + String url = + "http://api.flickr.com/services/rest/" + + "?method=flickr.photos.getInfo&api_key=" + + API_KEY + "&photo_id=" + m.group(1) + + "&format=json&nojsoncallback=1"; - URL flickrURL = new URL(url); - URLConnection conn = flickrURL.openConnection(); + URL flickrURL = new URL(url); + URLConnection conn = flickrURL.openConnection(); - BufferedReader in = - new BufferedReader(new InputStreamReader(conn - .getInputStream())); + BufferedReader in = + new BufferedReader(new InputStreamReader(conn + .getInputStream())); - String inputLine, holder = ""; + String inputLine, holder = ""; - while ((inputLine = in.readLine()) != null) - holder = inputLine; - in.close(); + while ((inputLine = in.readLine()) != null) + holder = inputLine; + in.close(); - JSONObject wrapper = new JSONObject(holder); + JSONObject wrapper = new JSONObject(holder); - if (wrapper.getString("stat").equals("ok")) + if (wrapper.getString("stat").equals("ok")) + { + JSONObject result = wrapper.getJSONObject("photo"); + if (!(result.length() == 0)) { - - JSONObject result = wrapper.getJSONObject("photo"); - String farmID = result.getString("farm"); String serverID = result.getString("server"); String secret = result.getString("secret"); - String thumbURL = + thumbUrl = "http://farm" + farmID + ".static.flickr.com/" - + serverID + "/" + m.group(3) + "_" + secret + + serverID + "/" + m.group(1) + "_" + secret + "_t.jpg"; - - if (!(result.length() == 0)) - { - msgBuff - .append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff.append(thumbURL); - msgBuff.append("\"></IMG>"); - } - } - else - { - startPos = 0; - msgBuff = new StringBuffer(); } } - catch (Exception e) - { - startPos = 0; - msgBuff = new StringBuffer(); - e.printStackTrace(); - } } - else + catch(Exception e) { - msgBuff.append(chatString.substring(m.start(), m.end())); + e.printStackTrace(); } } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return thumbUrl; } - + /** * Returns the source name * @@ -166,4 +135,14 @@ public class ReplacementServiceFlickrImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return FLICKR_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/hulu/ReplacementServiceHuluImpl.java b/src/net/java/sip/communicator/impl/replacement/hulu/ReplacementServiceHuluImpl.java index 5630737..3dfa6fc 100644 --- a/src/net/java/sip/communicator/impl/replacement/hulu/ReplacementServiceHuluImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/hulu/ReplacementServiceHuluImpl.java @@ -7,7 +7,6 @@ package net.java.sip.communicator.impl.replacement.hulu; import java.io.*; import java.net.*; -import java.util.regex.*; import org.json.*; @@ -32,7 +31,8 @@ public class ReplacementServiceHuluImpl * The regex used to match the link in the message. */ public static final String HULU_PATTERN = - "(http.*?(www\\.)*?hulu\\.com\\/watch\\/([a-zA-Z0-9_\\-]+))(\\/([^\\\"\\<]*)*)"; + "(?<=>)(https?\\:\\/\\/(www\\.)*?hulu\\.com" + + "\\/watch\\/([a-zA-Z0-9_\\-]+))(\\/([^\\\"\\<]*)*)(?=<\\/A>)"; /** * Configuration label shown in the config form. @@ -53,90 +53,46 @@ public class ReplacementServiceHuluImpl } /** - * Replaces the Hulu video links in the chat message with their - * corresponding thumbnails. + * Replaces the Hulu video links with their corresponding thumbnails. * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * @param sourceString the original video link. + * @return thumbnail image link; the original video link in case of no + * match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { - final Pattern p = - Pattern.compile(HULU_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); + try + { + String url = "http://oohembed.com/oohembed/?url=" + sourceString; + URL sourceURL = new URL(url); + URLConnection conn = sourceURL.openConnection(); - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + BufferedReader in = + new BufferedReader(new InputStreamReader(conn.getInputStream())); - while (m.find()) - { + String inputLine, holder = ""; - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); + while ((inputLine = in.readLine()) != null) + holder = inputLine; + in.close(); - if (count % 2 == 0) - { - try - { - String url = - "http://oohembed.com/oohembed/?url=" + m.group(0); - - URL sourceURL = new URL(url); - URLConnection conn = sourceURL.openConnection(); - - BufferedReader in = - new BufferedReader(new InputStreamReader(conn - .getInputStream())); - - String inputLine, holder = ""; - - while ((inputLine = in.readLine()) != null) - holder = inputLine; - in.close(); - - JSONObject wrapper = new JSONObject(holder); - - String thumbUrl = wrapper.getString("thumbnail_url"); - - if (thumbUrl != null) - { - msgBuff - .append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff.append(thumbUrl); - msgBuff.append("\"></IMG>"); - - } - else - { - startPos = 0; - msgBuff = new StringBuffer(); - } - - } - catch (Exception e) - { - startPos = 0; - msgBuff = new StringBuffer(); - e.printStackTrace(); - } - } - else + JSONObject wrapper = new JSONObject(holder); + + String thumbUrl = wrapper.getString("thumbnail_url"); + + if (thumbUrl != null) { - msgBuff.append(chatString.substring(m.start(), m.end())); + return thumbUrl; } } + catch (Exception e) + { + e.printStackTrace(); + } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return sourceString; } - + /** * Returns the source name * @@ -146,4 +102,14 @@ public class ReplacementServiceHuluImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return HULU_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/metacafe/ReplacementServiceMetacafeImpl.java b/src/net/java/sip/communicator/impl/replacement/metacafe/ReplacementServiceMetacafeImpl.java index ea8b86a..324fc82 100644 --- a/src/net/java/sip/communicator/impl/replacement/metacafe/ReplacementServiceMetacafeImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/metacafe/ReplacementServiceMetacafeImpl.java @@ -29,7 +29,8 @@ public class ReplacementServiceMetacafeImpl * The regex used to match the link in the message. */ public static final String METACAFE_PATTERN = - "(http.*?(www\\.)*?metacafe\\.com\\/watch\\/([a-zA-Z0-9_\\-]+))(\\/[a-zA-Z0-9_\\-\\/]+)*"; + "(?<=>)(https?\\:\\/\\/(www\\.)*?metacafe\\.com" + + "\\/watch\\/([a-zA-Z0-9_\\-]+))(\\/[a-zA-Z0-9_\\-\\/]+)*(?=</A>)"; /** * Configuration label shown in the config form. @@ -50,50 +51,28 @@ public class ReplacementServiceMetacafeImpl } /** - * Replaces the metacafe video links in the chat message with their - * corresponding thumbnails. + * Replaces the metacafe video links with their corresponding thumbnails. * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * @param sourceString the original video link. + * @return replaced thumbnail image; the original video link in case of no + * match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { final Pattern p = - Pattern.compile(METACAFE_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); + Pattern.compile( + "\\/watch\\/([a-zA-Z0-9_\\-]+)(\\/[a-zA-Z0-9_\\-\\/]+)*", + Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + Matcher m = p.matcher(sourceString); - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + String thumbUrl = sourceString; while (m.find()) - { - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); + thumbUrl = "http://www.metacafe.com/thumb/" + m.group(1) + ".jpg"; - if (count % 2 == 0) - { - msgBuff.append("<IMG HEIGHT=\"81\" WIDTH=\"136\" SRC=\""); - msgBuff.append("http://www.metacafe.com/thumb/"); - msgBuff.append(m.group(3)); - msgBuff.append(".jpg\"></IMG>"); - } - else - { - msgBuff.append(chatString.substring(m.start(), m.end())); - } - } - - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return thumbUrl; } - + /** * Returns the source name * @@ -103,4 +82,14 @@ public class ReplacementServiceMetacafeImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return METACAFE_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/smiley/ReplacementServiceSmileyImpl.java b/src/net/java/sip/communicator/impl/replacement/smiley/ReplacementServiceSmileyImpl.java index 6863954..d954f2c 100644 --- a/src/net/java/sip/communicator/impl/replacement/smiley/ReplacementServiceSmileyImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/smiley/ReplacementServiceSmileyImpl.java @@ -6,7 +6,6 @@ package net.java.sip.communicator.impl.replacement.smiley; import java.util.*; -import java.util.regex.*; import net.java.sip.communicator.service.replacement.*; import net.java.sip.communicator.service.replacement.smilies.*; @@ -23,97 +22,60 @@ public class ReplacementServiceSmileyImpl implements SmiliesReplacementService { /** - * The compiled <tt>Pattern</tt> which matches {@link #smileyStrings}. - */ - private static Pattern smileyPattern; - - /** * The <tt>List</tt> of smiley strings which are matched by - * {@link #smileyPattern}. + * {@link #smileyRegex}. */ private static final java.util.List<String> smileyStrings = new ArrayList<String>(); /** - * The closing tag of the <code>PLAINTEXT</code> HTML element. - */ - private static final String END_PLAINTEXT_TAG = "</PLAINTEXT>"; - - /** - * The opening tag of the <code>PLAINTEXT</code> HTML element. + * Configuration label shown in the config form. */ - private static final String START_PLAINTEXT_TAG = "<PLAINTEXT>"; + public static final String SMILEY_SOURCE = "SMILEY"; /** - * Configuration label shown in the config form. + * The regex used to match the smileys in the message. */ - public static final String SMILEY_SOURCE = "SMILEY"; + public static String smileyRegex; /** - * Replaces the smiley strings in the chat message with their - * corresponding smiley image. - * - * @param chatString the original chat message. - * @return replaced chat message with the smiley images; the original - * message in case of no match. + * Replaces the smiley strings with their corresponding smiley image. + * + * @param sourceString the original smiley string. + * @return the smiley image replaced for the smiley string; the original + * smiley string in case of no match. */ - public String getReplacedMessage(final String chatString) + public String getReplacement(final String sourceString) { - String startPlainTextTag = START_PLAINTEXT_TAG; - String endPlainTextTag = END_PLAINTEXT_TAG; - Collection<Smiley> smilies = Resources.getDefaultSmileyPack(); - - Matcher m = getSmileyPattern(smilies).matcher(chatString); - StringBuffer msgBuffer = new StringBuffer(); - - int prevEnd = 0; - - while (m.find()) + try { - msgBuffer.append(chatString.substring(prevEnd, m.start())); - prevEnd = m.end(); - - String smileyString = m.group().trim(); - - msgBuffer.append(endPlainTextTag); - msgBuffer.append("<IMG SRC=\""); - try - { - msgBuffer.append( - Resources.getSmiley(smileyString).getImagePath()); - } - catch (Exception e) - { - e.printStackTrace(); - } - msgBuffer.append("\" ALT=\""); - msgBuffer.append(smileyString); - msgBuffer.append("\"></IMG>"); - msgBuffer.append(startPlainTextTag); - + return Resources.getSmiley(sourceString.trim()).getImagePath(); + } + catch (Exception e) + { + e.printStackTrace(); } - msgBuffer.append(chatString.substring(prevEnd)); - return msgBuffer.toString(); + return sourceString; } /** - * Gets a compiled <tt>Pattern</tt> which matches the smiley strings of the - * specified <tt>Collection</tt> of <tt>Smiley</tt>s. + * Gets a regex string which matches the smiley strings of the specified + * <tt>Collection</tt> of <tt>Smiley</tt>s. * * @param smileys the <tt>Collection</tt> of <tt>Smiley</tt>s for which to * get a compiled <tt>Pattern</tt> which matches its smiley * strings - * @return a compiled <tt>Pattern</tt> which matches the smiley strings of - * the specified <tt>Collection</tt> of <tt>Smiley</tt>s + * @return a regex string which matches the smiley strings of the specified + * <tt>Collection</tt> of <tt>Smiley</tt>s */ - private static Pattern getSmileyPattern(Collection<Smiley> smileys) + private static String getSmileyPattern(Collection<Smiley> smileys) { synchronized (smileyStrings) { boolean smileyStringsIsEqual; - if (smileyPattern == null) + if (smileyRegex == null) smileyStringsIsEqual = false; else { @@ -157,15 +119,15 @@ public class ReplacementServiceSmileyImpl regex = regex.deleteCharAt(regex.length() - 1); regex.append(')'); - smileyPattern = Pattern.compile(regex.toString()); + smileyRegex = regex.toString(); } - return smileyPattern; + return smileyRegex; } } /** * Returns the source name - * + * * @return the source name */ public String getSourceName() @@ -174,7 +136,19 @@ public class ReplacementServiceSmileyImpl } /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + Collection<Smiley> smileys = Resources.getDefaultSmileyPack(); + return getSmileyPattern(smileys); + } + + /** * Returns the smileys pack to use in the user interface. + * * @return a collection of all smileys available */ public Collection<Smiley> getSmiliesPack() diff --git a/src/net/java/sip/communicator/impl/replacement/twitpic/ReplacementServiceTwitpicImpl.java b/src/net/java/sip/communicator/impl/replacement/twitpic/ReplacementServiceTwitpicImpl.java index 4e9d419..42e12da 100644 --- a/src/net/java/sip/communicator/impl/replacement/twitpic/ReplacementServiceTwitpicImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/twitpic/ReplacementServiceTwitpicImpl.java @@ -29,7 +29,7 @@ public class ReplacementServiceTwitpicImpl * The regex used to match the link in the message. */ public static final String TWITPIC_PATTERN = - "http:\\/\\/(?:www\\.)?twitpic\\.com\\/([^\\/<]*)(?=<)"; + "(?<=>)http:\\/\\/(?:www\\.)?twitpic\\.com\\/([^\\/<]*)(?=</A>)"; /** * Configuration label shown in the config form. @@ -50,44 +50,29 @@ public class ReplacementServiceTwitpicImpl } /** - * Replaces the twitpic image links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Replaces the twitpic image links with their corresponding thumbnails. + * + * @param sourceString the original twitpic link. + * @return thumbnail image link for the source string; the original + * image link in case of no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { final Pattern p = - Pattern.compile(TWITPIC_PATTERN, Pattern.CASE_INSENSITIVE + Pattern.compile("\\.com\\/([^\\/<]*)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + Matcher m = p.matcher(sourceString); + String thumbUrl = sourceString; while (m.find()) { - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); - - msgBuff.append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff.append("http://twitpic.com/show/thumb/"); - msgBuff.append(m.group(1)); - msgBuff.append("\"></IMG>"); - + thumbUrl = "http://twitpic.com/show/thumb/" + m.group(1); } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return thumbUrl; } - + /** * Returns the source name * @@ -97,4 +82,14 @@ public class ReplacementServiceTwitpicImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return TWITPIC_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/vbox7/ReplacementServiceVbox7Impl.java b/src/net/java/sip/communicator/impl/replacement/vbox7/ReplacementServiceVbox7Impl.java index 0ae75de..40a065e 100644 --- a/src/net/java/sip/communicator/impl/replacement/vbox7/ReplacementServiceVbox7Impl.java +++ b/src/net/java/sip/communicator/impl/replacement/vbox7/ReplacementServiceVbox7Impl.java @@ -29,7 +29,8 @@ public class ReplacementServiceVbox7Impl * The regex used to match the link in the message. */ public static final String VBOX7_PATTERN = - "(http.*?(www\\.)*?vbox7\\.com\\/play\\:([a-zA-Z0-9_\\-]+))([?&]\\w+=[\\w-]*)*"; + "(?<=>)(https?\\:\\/\\/(www\\.)*?vbox7\\.com" + + "\\/play\\:([a-zA-Z0-9_\\-]+))([?&]\\w+=[\\w-]*)*(?=</A>)"; /** * Configuration label shown in the config form. @@ -50,51 +51,27 @@ public class ReplacementServiceVbox7Impl } /** - * Replaces the vbox7 video links in the chat message with their - * corresponding thumbnails. + * Returns the thumbnail URL of the video link provided. * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * @param sourceString the original video link. + * @return the thumbnail image link; the original link in case of no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { final Pattern p = - Pattern.compile(VBOX7_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + Pattern.compile("\\/play\\:([a-zA-Z0-9_\\-]+)([?&]\\w+=[\\w-]*)*", + Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + Matcher m = p.matcher(sourceString); + String thumbUrl = sourceString; while (m.find()) { - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); - - if (count % 2 == 0) - { - msgBuff.append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff - .append("http://i.vbox7.com/p/"); - msgBuff.append(m.group(3)); - msgBuff.append("3.jpg\"></IMG>"); - } - else - { - msgBuff.append(chatString.substring(m.start(), m.end())); - } + thumbUrl = "http://i.vbox7.com/p/" + m.group(1) + "3.jpg"; } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return thumbUrl; } - + /** * Returns the source name * @@ -104,4 +81,14 @@ public class ReplacementServiceVbox7Impl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return VBOX7_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/viddler/ReplacementServiceViddlerImpl.java b/src/net/java/sip/communicator/impl/replacement/viddler/ReplacementServiceViddlerImpl.java index cda7ffb..09178a5 100644 --- a/src/net/java/sip/communicator/impl/replacement/viddler/ReplacementServiceViddlerImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/viddler/ReplacementServiceViddlerImpl.java @@ -7,7 +7,6 @@ package net.java.sip.communicator.impl.replacement.viddler; import java.io.*; import java.net.*; -import java.util.regex.*; import net.java.sip.communicator.service.replacement.*; import net.java.sip.communicator.util.*; @@ -31,7 +30,8 @@ public class ReplacementServiceViddlerImpl * The regex used to match the link in the message. */ public static final String VIDDLER_PATTERN = - "(?:[\\>])(http:\\/\\/(?:www\\.)?viddler\\.com\\/explore\\/(\\w+)\\/videos\\/\\d+.*(?=<\\/A>))"; + "(?<=>)(http:\\/\\/(?:www\\.)?viddler\\.com" + + "\\/explore\\/(\\w+)\\/videos\\/\\d+.*(?=<\\/A>))"; /** * API Key required to access the viddler api. @@ -42,19 +42,20 @@ public class ReplacementServiceViddlerImpl * Viddler API url. */ private static final String sourceURL = - "http://api.viddler.com/rest/v1/?method=viddler.videos.getDetailsByUrl&api_key=" - + API_KEY; + "http://api.viddler.com/rest/v1/" + + "?method=viddler.videos.getDetailsByUrl&api_key=" + + API_KEY; /** * Configuration label shown in the config form. */ public static final String VIDDLER_CONFIG_LABEL = "Viddler"; - + /** * Source name; also used as property label. */ public static final String SOURCE_NAME = "VIDDLER"; - + /** * Constructor for <tt>ReplacementServiceViddlerImpl</tt>. */ @@ -64,84 +65,51 @@ public class ReplacementServiceViddlerImpl } /** - * Replaces the viddler video links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Returns the thumbnail URL of the video link provided. + * + * @param sourceString the original video link. + * @return the thumbnail image link; the original link in case of no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { - final Pattern p = - Pattern.compile(VIDDLER_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); - - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); - - while (m.find()) + try { - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); + String url = sourceURL + "&url=" + sourceString + "/"; - try - { - String url = sourceURL + "&url=" + m.group(1) + "/"; - - URL sourceURL = new URL(url); - URLConnection conn = sourceURL.openConnection(); - - BufferedReader in = - new BufferedReader(new InputStreamReader(conn - .getInputStream())); + URL sourceURL = new URL(url); + URLConnection conn = sourceURL.openConnection(); - String inputLine; - StringBuffer holder = new StringBuffer(); + BufferedReader in = + new BufferedReader(new InputStreamReader(conn + .getInputStream())); - while ((inputLine = in.readLine()) != null) - holder.append(inputLine); - in.close(); + String inputLine; + StringBuffer holder = new StringBuffer(); - String startTag = "<thumbnail_url>"; - String endTag = "</thumbnail_url>"; + while ((inputLine = in.readLine()) != null) + holder.append(inputLine); + in.close(); - String response = holder.toString(); + String startTag = "<thumbnail_url>"; + String endTag = "</thumbnail_url>"; - int start = response.indexOf(startTag) + startTag.length(); - int end = response.toString().indexOf(endTag); - String thumbUrl = response.substring(start, end); + String response = holder.toString(); - if (thumbUrl != null) - { - msgBuff.append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff.append(thumbUrl); - msgBuff.append("\"></IMG>"); + int start = response.indexOf(startTag) + startTag.length(); + int end = response.toString().indexOf(endTag); + String thumbUrl = response.substring(start, end); - } - else - { - startPos = 0; - msgBuff = new StringBuffer(); - } - - } - catch (Exception e) + if (thumbUrl != null) { - startPos = 0; - msgBuff = new StringBuffer(); - e.printStackTrace(); + return thumbUrl; } } + catch (Exception e) + { + e.printStackTrace(); + } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return sourceString; } /** @@ -153,4 +121,14 @@ public class ReplacementServiceViddlerImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return VIDDLER_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/vimeo/ReplacementServiceVimeoImpl.java b/src/net/java/sip/communicator/impl/replacement/vimeo/ReplacementServiceVimeoImpl.java index 169a88d..caf3508 100644 --- a/src/net/java/sip/communicator/impl/replacement/vimeo/ReplacementServiceVimeoImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/vimeo/ReplacementServiceVimeoImpl.java @@ -34,7 +34,8 @@ public class ReplacementServiceVimeoImpl * The regex used to match the link in the message. */ public static final String VIMEO_PATTERN = - "(http.*?(www\\.)*?vimeo\\.com\\/([a-zA-Z0-9_\\-]+))"; + "(?<=>)(https?\\:\\/\\/(www\\.)*?vimeo\\.com" + + "\\/([a-zA-Z0-9_\\-]+))(?=</A>)"; /** * Configuration label shown in the config form. @@ -55,87 +56,56 @@ public class ReplacementServiceVimeoImpl } /** - * Replaces the vimeo video links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Returns the thumbnail URL of the video link provided. + * + * @param sourceString the original video link. + * @return the thumbnail image link; the original link in case of no match. */ - public String getReplacedMessage(String chatString) + public String getReplacement(String sourceString) { final Pattern p = - Pattern.compile(VIMEO_PATTERN, Pattern.CASE_INSENSITIVE - | Pattern.DOTALL); - Matcher m = p.matcher(chatString); + Pattern.compile(".+\\.com\\/([a-zA-Z0-9_\\-]+)", + Pattern.CASE_INSENSITIVE | Pattern.DOTALL); + Matcher m = p.matcher(sourceString); - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + String thumbUrl = sourceString; while (m.find()) { + try + { + String url = + "http://vimeo.com/api/v2/video/" + m.group(1) + ".json"; + URL vimeoURL = new URL(url); + URLConnection conn = vimeoURL.openConnection(); - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); + BufferedReader in = + new BufferedReader(new InputStreamReader(conn + .getInputStream())); - if (count % 2 == 0) - { + String inputLine, holder = ""; - try - { - String url = - "http://vimeo.com/api/v2/video/" + m.group(3) + ".json"; - URL vimeoURL = new URL(url); - URLConnection conn = vimeoURL.openConnection(); - - BufferedReader in = - new BufferedReader(new InputStreamReader(conn - .getInputStream())); - - String inputLine, holder = ""; - - while ((inputLine = in.readLine()) != null) - holder = inputLine; - in.close(); - - JSONArray result = new JSONArray(holder); - - if (!(result.length() == 0)) - { - msgBuff - .append("<IMG HEIGHT=\"150\" WIDTH=\"200\" SRC=\""); - msgBuff.append(result.getJSONObject(0).getString( - "thumbnail_medium")); - msgBuff.append("\"></IMG>"); - } - else - { - startPos = 0; - msgBuff = new StringBuffer(); - } - } - catch (Exception e) + while ((inputLine = in.readLine()) != null) + holder = inputLine; + in.close(); + + JSONArray result = new JSONArray(holder); + + if (!(result.length() == 0)) { - startPos = 0; - msgBuff = new StringBuffer(); - e.printStackTrace(); + thumbUrl + = result.getJSONObject(0).getString("thumbnail_medium"); } } - else + catch (Exception e) { - msgBuff.append(chatString.substring(m.start(), m.end())); + e.printStackTrace(); } } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return thumbUrl; } - + /** * Returns the source name * @@ -145,4 +115,14 @@ public class ReplacementServiceVimeoImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return VIMEO_PATTERN; + } }
\ No newline at end of file diff --git a/src/net/java/sip/communicator/impl/replacement/youtube/ReplacementServiceYoutubeImpl.java b/src/net/java/sip/communicator/impl/replacement/youtube/ReplacementServiceYoutubeImpl.java index faec282..4811aff 100644 --- a/src/net/java/sip/communicator/impl/replacement/youtube/ReplacementServiceYoutubeImpl.java +++ b/src/net/java/sip/communicator/impl/replacement/youtube/ReplacementServiceYoutubeImpl.java @@ -7,7 +7,6 @@ package net.java.sip.communicator.impl.replacement.youtube; import java.io.*; import java.net.*; -import java.util.regex.*; import net.java.sip.communicator.service.replacement.*; import net.java.sip.communicator.util.*; @@ -17,7 +16,7 @@ import org.json.*; /** * Implements the {@link ReplacementService} to provide previews for Youtube * links. - * + * * @author Purvesh Sahoo */ public class ReplacementServiceYoutubeImpl @@ -32,8 +31,9 @@ public class ReplacementServiceYoutubeImpl /** * The regex used to match the link in the message. */ - public static String re1 = - "(http.*?(www\\.)*?youtube\\.com\\/watch\\?v=([a-zA-Z0-9_\\-]+))([?&]\\w+=[\\w-]+)*"; + public static final String YOUTUBE_PATTERN = + "(?<=>)(https?\\:\\/\\/(www\\.)*?youtube\\.com" + + "\\/watch\\?v=([a-zA-Z0-9_\\-]+))([?&]\\w+=[\\w-]+)*(?=</A>)"; /** * Configuration label shown in the config form. @@ -44,7 +44,7 @@ public class ReplacementServiceYoutubeImpl * Source name; also used as property label. */ public static final String SOURCE_NAME = "YOUTUBE"; - + /** * Constructor for <tt>ReplacementServiceYoutubeImpl</tt>. */ @@ -54,88 +54,43 @@ public class ReplacementServiceYoutubeImpl } /** - * Replaces the youtube video links in the chat message with their - * corresponding thumbnails. - * - * @param chatString the original chat message. - * @return replaced chat message with the thumbnail image; the original - * message in case of no match. + * Returns the thumbnail URL of the video link provided. + * + * @param sourceString the original video link. + * @return the thumbnail image link; the original link in case of no match. */ - public String getReplacedMessage(final String chatString) + public String getReplacement(String sourceString) { - final Pattern p = - Pattern.compile(re1, Pattern.CASE_INSENSITIVE | Pattern.DOTALL); - Matcher m = p.matcher(chatString); + try + { + String url = "http://youtube.com/oembed/?url=" + sourceString; + URL sourceURL = new URL(url); + URLConnection conn = sourceURL.openConnection(); - int count = 0, startPos = 0; - StringBuffer msgBuff = new StringBuffer(); + BufferedReader in = + new BufferedReader(new InputStreamReader(conn.getInputStream())); - while (m.find()) - { - count++; - msgBuff.append(chatString.substring(startPos, m.start())); - startPos = m.end(); + String inputLine, holder = ""; - // We only want to replace the inner link text and not the link src. - // All even matches are the text we want to replace. - if (count % 2 == 0) - { - try - { - String url = - "http://youtube.com/oembed/?url=" + m.group(0); - - URL sourceURL = new URL(url); - URLConnection conn = sourceURL.openConnection(); - - BufferedReader in = - new BufferedReader(new InputStreamReader(conn - .getInputStream())); - - String inputLine, holder = ""; - - while ((inputLine = in.readLine()) != null) - holder = inputLine; - in.close(); - - JSONObject wrapper = new JSONObject(holder); - - String thumbUrl = wrapper.getString("thumbnail_url"); - - if (thumbUrl != null) - { - msgBuff - .append("<IMG HEIGHT=\"90\" WIDTH=\"120\" SRC=\""); - msgBuff.append(thumbUrl); - msgBuff.append("\"></IMG>"); - - } - else - { - startPos = 0; - msgBuff = new StringBuffer(); - } - - } - catch (Exception e) - { - startPos = 0; - msgBuff = new StringBuffer(); - e.printStackTrace(); - } - } - else + while ((inputLine = in.readLine()) != null) + holder = inputLine; + in.close(); + + JSONObject wrapper = new JSONObject(holder); + + String thumbUrl = wrapper.getString("thumbnail_url"); + + if (thumbUrl != null) { - msgBuff.append(chatString.substring(m.start(), m.end())); + return thumbUrl; } } + catch (Exception e) + { + e.printStackTrace(); + } - msgBuff.append(chatString.substring(startPos)); - - if (!msgBuff.toString().equals(chatString)) - return msgBuff.toString(); - - return chatString; + return sourceString; } /** @@ -147,4 +102,14 @@ public class ReplacementServiceYoutubeImpl { return SOURCE_NAME; } + + /** + * Returns the pattern of the source + * + * @return the source pattern + */ + public String getPattern() + { + return YOUTUBE_PATTERN; + } }
\ No newline at end of file |