Permalink
Browse files

Merge pull request #76 from ApplePedlar/TFJ-734

TFJ-734 add getDescriptionURLEntities() to User interface
  • Loading branch information...
2 parents 9d045ac + 372dd4d commit 1a87b363305207505270fc83d08ffd8132d98dad @yusuke committed Dec 10, 2012
View
@@ -47,6 +47,7 @@ marr-masaaki <marr fiveflavors at gmail.com> @marr
matsumo <matsumo at ce.ns0.it>
Max Penet <m at qbits.cc> @mpenet
Mocel <docel77 at gmail.com> @Mocel
+Naoya Hatayama <applepedlar at gmail.com> @ApplePedlar
Ngoc Dao <ngocdaothanh@gmail.com> @ngocdaothanh
Nobutoshi Ogata <n-ogata at cnt.biglobe.co.jp> @nobu666
Nicolas Bouillon <nicolas at bouil.org>
@@ -101,7 +101,17 @@ public String getLocation() {
public String getDescription() {
return getTarget().getDescription();
}
-
+
+ /**
+ * Returns URL entities for user description.
+ *
+ * @return URL entities for user description
+ * @since Twitter4J 3.0.3
+ */
+ @Override
+ public URLEntity[] getDescriptionURLEntities() {
+ return getTarget().getDescriptionURLEntities();
+ }
/**
* Tests if the user is enabling contributors
@@ -225,4 +225,13 @@
* @since Twitter4J 2.1.4
*/
boolean isFollowRequestSent();
+
+ /**
+ * Returns URL entities for user description.
+ *
+ * @return URL entities for user description
+ * @since Twitter4J 3.0.3
+ */
+ URLEntity[] getDescriptionURLEntities();
+
}
@@ -84,16 +84,33 @@ static void unescape(StringBuilder original) {
static String unescapeAndSlideEntityIncdices(String text, UserMentionEntity[] userMentionEntities,
URLEntity[] urlEntities, HashtagEntity[] hashtagEntities,
MediaEntity[] mediaEntities) {
- EntityIndex[] entityIndexes = new EntityIndex[userMentionEntities.length + urlEntities.length +
- hashtagEntities.length + mediaEntities.length];
+
+ int entityIndexesLength = 0;
+ entityIndexesLength += userMentionEntities == null ? 0 : userMentionEntities.length;
+ entityIndexesLength += urlEntities == null ? 0 : urlEntities.length;
+ entityIndexesLength += hashtagEntities == null ? 0 : hashtagEntities.length;
+ entityIndexesLength += mediaEntities == null ? 0 : mediaEntities.length;
+
+ EntityIndex[] entityIndexes = new EntityIndex[entityIndexesLength];
int copyStartIndex = 0;
- System.arraycopy(userMentionEntities, 0, entityIndexes, copyStartIndex, userMentionEntities.length);
- copyStartIndex += userMentionEntities.length;
- System.arraycopy(urlEntities, 0, entityIndexes, copyStartIndex, urlEntities.length);
- copyStartIndex += urlEntities.length;
- System.arraycopy(hashtagEntities, 0, entityIndexes, copyStartIndex, hashtagEntities.length);
- copyStartIndex += hashtagEntities.length;
- System.arraycopy(mediaEntities, 0, entityIndexes, copyStartIndex, mediaEntities.length);
+ if (userMentionEntities != null) {
+ System.arraycopy(userMentionEntities, 0, entityIndexes, copyStartIndex, userMentionEntities.length);
+ copyStartIndex += userMentionEntities.length;
+ }
+
+ if (urlEntities != null) {
+ System.arraycopy(urlEntities, 0, entityIndexes, copyStartIndex, urlEntities.length);
+ copyStartIndex += urlEntities.length;
+ }
+
+ if (hashtagEntities != null) {
+ System.arraycopy(hashtagEntities, 0, entityIndexes, copyStartIndex, hashtagEntities.length);
+ copyStartIndex += hashtagEntities.length;
+ }
+
+ if (mediaEntities != null) {
+ System.arraycopy(mediaEntities, 0, entityIndexes, copyStartIndex, mediaEntities.length);
+ }
Arrays.sort(entityIndexes);
boolean handlingStart = true;
@@ -41,6 +41,7 @@
private String screenName;
private String location;
private String description;
+ private URLEntity[] descriptionURLEntities;
private boolean isContributorsEnabled;
private String profileImageUrl;
private String profileImageUrlHttps;
@@ -103,7 +104,31 @@ private void init(JSONObject json) throws TwitterException {
name = getRawString("name", json);
screenName = getRawString("screen_name", json);
location = getRawString("location", json);
+
+ // descriptionUrlEntities <=> entities/descriptions/urls[]
+ if (!json.isNull("entities")) {
+ JSONObject entitiesJSON = json.getJSONObject("entities");
+ if (!entitiesJSON.isNull("description")) {
+ JSONObject descriptionEntitiesJSON = entitiesJSON.getJSONObject("description");
+ if (!descriptionEntitiesJSON.isNull("urls")) {
+ JSONArray urlsArray = descriptionEntitiesJSON.getJSONArray("urls");
+ int len = urlsArray.length();
+ descriptionURLEntities = new URLEntity[len];
+ for (int i = 0; i < len; i++) {
+ descriptionURLEntities[i] = new URLEntityJSONImpl(urlsArray.getJSONObject(i));
+ }
+ }
+ }
+ }
+
+ descriptionURLEntities = descriptionURLEntities == null ? new URLEntity[0] : descriptionURLEntities;
+
description = getRawString("description", json);
+ if (description != null) {
+ description = HTMLEntity.unescapeAndSlideEntityIncdices(description,
+ null, descriptionURLEntities, null, null);
+ }
+
isContributorsEnabled = getBoolean("contributors_enabled", json);
profileImageUrl = getRawString("profile_image_url", json);
profileImageUrlHttps = getRawString("profile_image_url_https", json);
@@ -508,6 +533,14 @@ public boolean isFollowRequestSent() {
return isFollowRequestSent;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public URLEntity[] getDescriptionURLEntities() {
+ return descriptionURLEntities;
+ }
+
/*package*/
static PagableResponseList<User> createPagableUserList(HttpResponse res, Configuration conf) throws TwitterException {
try {
@@ -626,4 +659,5 @@ public String toString() {
", isFollowRequestSent=" + isFollowRequestSent +
'}';
}
+
}
@@ -63,6 +63,28 @@ public void testUnescapeAndSlideEntityIncdices() throws Exception {
assertEquals("http://t.co/d4G7MQ62", escaped.substring(media.getStart(), media.getEnd()));
}
+
+ public void testUnescapeAndSlideEntityIncdicesWithNullParameters() throws Exception {
+ String rawJSON = "{\"text\":\"@null &lt; #test &gt; &amp;\\u307b\\u3052\\u307b\\u3052 @t4j_news %&amp; http:\\/\\/t.co\\/HwbSpYFr http:\\/\\/t.co\\/d4G7MQ62\"}";
+ JSONObject json = new JSONObject(rawJSON);
+ String escaped = HTMLEntity.unescapeAndSlideEntityIncdices(json.getString("text"),
+ null, null, null, null);
+ assertEquals("@null < #test > &ほげほげ @t4j_news %& http://t.co/HwbSpYFr http://t.co/d4G7MQ62"
+ , escaped);
+ }
+
+ public void testUnescapeAndSlideEntityIncdicesWithURLEntitiesOnly() throws Exception {
+ URLEntityJSONImpl t4jURL = new URLEntityJSONImpl(49, 69, "http://t.co/HwbSpYFr"
+ , "http://twitter4j.org/en/index.html#download", "twitter4j.org/en/index.html#\u2026");
+
+ String rawJSON = "{\"text\":\"@null &lt; #test &gt; &amp;\\u307b\\u3052\\u307b\\u3052 @t4j_news %&amp; http:\\/\\/t.co\\/HwbSpYFr http:\\/\\/t.co\\/d4G7MQ62\"}";
+ JSONObject json = new JSONObject(rawJSON);
+ String escaped = HTMLEntity.unescapeAndSlideEntityIncdices(json.getString("text"),
+ null, new URLEntity[]{t4jURL}, null, null);
+ assertEquals("@null < #test > &ほげほげ @t4j_news %& http://t.co/HwbSpYFr http://t.co/d4G7MQ62"
+ , escaped);
+ assertEquals("http://t.co/HwbSpYFr", escaped.substring(t4jURL.getStart(), t4jURL.getEnd()));
+ }
public void testEscape() {
String original = "<=% !>";
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2007 Yusuke Yamamoto
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @author Naoya Hatayama - applepedlar at gmail.com
+ */
+
+package twitter4j.internal.json;
+
+import twitter4j.TwitterException;
+import twitter4j.URLEntity;
+import twitter4j.internal.org.json.JSONException;
+import twitter4j.internal.org.json.JSONObject;
+import junit.framework.TestCase;
+
+public class UserJSONImplTest extends TestCase {
+
+ public void testGetDescriptionURLEntities1() throws JSONException, TwitterException {
+ String rawJson = "{\"id\":219570417,\"id_str\":\"219570417\",\"name\":\"\\u3066\\u3059\\u3068\",\"screen_name\":\"gjmp9\",\"location\":\"\\u65e5\\u672c\",\"description\":\"&lt;test&gt; url: http:\\/\\/t.co\\/UcHD19ZC url2: http:\\/\\/t.co\\/dRuJ7wCm subaccount: @gjmp10 hashtag: #test\",\"url\":\"http:\\/\\/fdghj.com\",\"entities\":{\"url\":{\"urls\":[{\"url\":\"http:\\/\\/fdghj.com\",\"expanded_url\":null,\"indices\":[0,16]}]},\"description\":{\"urls\":[{\"url\":\"http:\\/\\/t.co\\/UcHD19ZC\",\"expanded_url\":\"http:\\/\\/test.com\\/\",\"display_url\":\"test.com\",\"indices\":[18,38]},{\"url\":\"http:\\/\\/t.co\\/dRuJ7wCm\",\"expanded_url\":\"http:\\/\\/longurl.com\\/abcdefghijklmnopqrstuvwxyz\",\"display_url\":\"longurl.com\\/abcdefghijklmn\\u2026\",\"indices\":[45,65]}]}},\"protected\":false,\"followers_count\":8,\"friends_count\":11,\"listed_count\":0,\"created_at\":\"Thu Nov 25 06:47:37 +0000 2010\",\"favourites_count\":1,\"utc_offset\":-36000,\"time_zone\":\"Hawaii\",\"geo_enabled\":false,\"verified\":false,\"statuses_count\":280,\"lang\":\"ja\",\"status\":{\"created_at\":\"Wed Dec 05 01:53:33 +0000 2012\",\"id\":276142234003468288,\"id_str\":\"276142234003468288\",\"text\":\"\\u307b\\u3052\\u307b\\u3052\",\"source\":\"\\u003ca href=\\\"http:\\/\\/jigtwi.jp\\/?p=1001\\\" rel=\\\"nofollow\\\"\\u003ejigtwi for Android\\u003c\\/a\\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweet_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"favorited\":false,\"retweeted\":false},\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"CBC1E5\",\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/722705453\\/321f2aba5dcbbc97ec0dd0bf9969cb48.jpeg\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/722705453\\/321f2aba5dcbbc97ec0dd0bf9969cb48.jpeg\",\"profile_background_tile\":true,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2607446059\\/bip392byfu5669roobg5_normal.png\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2607446059\\/bip392byfu5669roobg5_normal.png\",\"profile_link_color\":\"B40B43\",\"profile_sidebar_border_color\":\"FFFFFF\",\"profile_sidebar_fill_color\":\"E5507E\",\"profile_text_color\":\"362720\",\"profile_use_background_image\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":true,\"follow_request_sent\":false,\"notifications\":false}";
+ JSONObject json = new JSONObject(rawJson);
+ UserJSONImpl user = new UserJSONImpl(json);
+
+ URLEntity[] descriptionUrlEntities = user.getDescriptionURLEntities();
+ assertNotNull(descriptionUrlEntities);
+ assertEquals(2, descriptionUrlEntities.length);
+
+ assertEquals("http://test.com/", descriptionUrlEntities[0].getExpandedURL());
+ assertEquals("test.com", descriptionUrlEntities[0].getDisplayURL());
+ assertEquals("http://t.co/UcHD19ZC", descriptionUrlEntities[0].getURL());
+ assertEquals(12, descriptionUrlEntities[0].getStart());
+ assertEquals(32, descriptionUrlEntities[0].getEnd());
+
+ assertEquals("http://longurl.com/abcdefghijklmnopqrstuvwxyz", descriptionUrlEntities[1].getExpandedURL());
+ assertEquals("longurl.com/abcdefghijklmn…", descriptionUrlEntities[1].getDisplayURL());
+ assertEquals("http://t.co/dRuJ7wCm", descriptionUrlEntities[1].getURL());
+ assertEquals(39, descriptionUrlEntities[1].getStart());
+ assertEquals(59, descriptionUrlEntities[1].getEnd());
+
+ assertEquals("<test> url: http://t.co/UcHD19ZC url2: http://t.co/dRuJ7wCm subaccount: @gjmp10 hashtag: #test", user.getDescription());
+ }
+
+ public void testGetDescriptionURLEntities2() throws JSONException, TwitterException {
+ String rawJsonWithEmptyDescription = "{\"id\":219570417,\"id_str\":\"219570417\",\"name\":\"\\u3066\\u3059\\u3068\",\"screen_name\":\"gjmp9\",\"location\":\"\\u65e5\\u672c\",\"description\":\"\",\"url\":\"http:\\/\\/fdghj.com\",\"entities\":{\"url\":{\"urls\":[{\"url\":\"http:\\/\\/fdghj.com\",\"expanded_url\":null,\"indices\":[0,16]}]},\"description\":{\"urls\":[]}},\"protected\":false,\"followers_count\":8,\"friends_count\":11,\"listed_count\":0,\"created_at\":\"Thu Nov 25 06:47:37 +0000 2010\",\"favourites_count\":1,\"utc_offset\":-36000,\"time_zone\":\"Hawaii\",\"geo_enabled\":false,\"verified\":false,\"statuses_count\":280,\"lang\":\"ja\",\"status\":{\"created_at\":\"Wed Dec 05 01:53:33 +0000 2012\",\"id\":276142234003468288,\"id_str\":\"276142234003468288\",\"text\":\"\\u307b\\u3052\\u307b\\u3052\",\"source\":\"\\u003ca href=\\\"http:\\/\\/jigtwi.jp\\/?p=1001\\\" rel=\\\"nofollow\\\"\\u003ejigtwi for Android\\u003c\\/a\\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweet_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"favorited\":false,\"retweeted\":false},\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"CBC1E5\",\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/722705453\\/321f2aba5dcbbc97ec0dd0bf9969cb48.jpeg\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/722705453\\/321f2aba5dcbbc97ec0dd0bf9969cb48.jpeg\",\"profile_background_tile\":true,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2607446059\\/bip392byfu5669roobg5_normal.png\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2607446059\\/bip392byfu5669roobg5_normal.png\",\"profile_link_color\":\"B40B43\",\"profile_sidebar_border_color\":\"FFFFFF\",\"profile_sidebar_fill_color\":\"E5507E\",\"profile_text_color\":\"362720\",\"profile_use_background_image\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":true,\"follow_request_sent\":false,\"notifications\":false}";
+ JSONObject jsonWithEmptyDescription = new JSONObject(rawJsonWithEmptyDescription);
+ UserJSONImpl userWithEmptyDescription = new UserJSONImpl(jsonWithEmptyDescription);
+ assertEquals("", userWithEmptyDescription.getDescription());
+ assertEquals(0, userWithEmptyDescription.getDescriptionURLEntities().length);
+
+ String rawJsonWithoutDescription = "{\"id\":219570417,\"id_str\":\"219570417\",\"name\":\"\\u3066\\u3059\\u3068\",\"screen_name\":\"gjmp9\",\"location\":\"\\u65e5\\u672c\",\"url\":\"http:\\/\\/fdghj.com\",\"entities\":{\"url\":{\"urls\":[{\"url\":\"http:\\/\\/fdghj.com\",\"expanded_url\":null,\"indices\":[0,16]}]}},\"protected\":false,\"followers_count\":8,\"friends_count\":11,\"listed_count\":0,\"created_at\":\"Thu Nov 25 06:47:37 +0000 2010\",\"favourites_count\":1,\"utc_offset\":-36000,\"time_zone\":\"Hawaii\",\"geo_enabled\":false,\"verified\":false,\"statuses_count\":280,\"lang\":\"ja\",\"status\":{\"created_at\":\"Wed Dec 05 01:53:33 +0000 2012\",\"id\":276142234003468288,\"id_str\":\"276142234003468288\",\"text\":\"\\u307b\\u3052\\u307b\\u3052\",\"source\":\"\\u003ca href=\\\"http:\\/\\/jigtwi.jp\\/?p=1001\\\" rel=\\\"nofollow\\\"\\u003ejigtwi for Android\\u003c\\/a\\u003e\",\"truncated\":false,\"in_reply_to_status_id\":null,\"in_reply_to_status_id_str\":null,\"in_reply_to_user_id\":null,\"in_reply_to_user_id_str\":null,\"in_reply_to_screen_name\":null,\"geo\":null,\"coordinates\":null,\"place\":null,\"contributors\":null,\"retweet_count\":0,\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"favorited\":false,\"retweeted\":false},\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"CBC1E5\",\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/722705453\\/321f2aba5dcbbc97ec0dd0bf9969cb48.jpeg\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/722705453\\/321f2aba5dcbbc97ec0dd0bf9969cb48.jpeg\",\"profile_background_tile\":true,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2607446059\\/bip392byfu5669roobg5_normal.png\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2607446059\\/bip392byfu5669roobg5_normal.png\",\"profile_link_color\":\"B40B43\",\"profile_sidebar_border_color\":\"FFFFFF\",\"profile_sidebar_fill_color\":\"E5507E\",\"profile_text_color\":\"362720\",\"profile_use_background_image\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":true,\"follow_request_sent\":false,\"notifications\":false}";
+ JSONObject jsonWithoutDescription = new JSONObject(rawJsonWithoutDescription);
+ UserJSONImpl userWithoutDescription = new UserJSONImpl(jsonWithoutDescription);
+ assertEquals(null, userWithoutDescription.getDescription());
+ assertEquals(0, userWithoutDescription.getDescriptionURLEntities().length);
+ }
+
+}

0 comments on commit 1a87b36

Please sign in to comment.