Permalink
Browse files

Merge pull request #78 from ApplePedlar/TFJ-739

issue# (TFJ-739)
  • Loading branch information...
2 parents 7aa5c6c + 003f001 commit b9d69adbf39e3499f2b2b4dd826943c61bc2869b @yusuke committed Dec 20, 2012
View
13 twitter4j-appengine/src/main/java/twitter4j/internal/json/LazyUser.java
@@ -112,7 +112,18 @@ public String getDescription() {
public URLEntity[] getDescriptionURLEntities() {
return getTarget().getDescriptionURLEntities();
}
-
+
+ /**
+ * Returns URL entity for user's URL.
+ *
+ * @return URL entity for user's URL.
+ * @since Twitter4J 3.0.3
+ */
+ @Override
+ public URLEntity getURLEntity() {
+ return getTarget().getURLEntity();
+ }
+
/**
* Tests if the user is enabling contributors
*
View
9 twitter4j-core/src/main/java/twitter4j/User.java
@@ -234,4 +234,13 @@
*/
URLEntity[] getDescriptionURLEntities();
+ /**
+ * Returns URL entity for user's URL.
+ *
+ * @return URL entity for user's URL.
+ * @since Twitter4J 3.0.3
+ */
+ URLEntity getURLEntity();
+
+
}
View
65 twitter4j-core/src/main/java/twitter4j/internal/json/UserJSONImpl.java
@@ -42,6 +42,7 @@
private String location;
private String description;
private URLEntity[] descriptionURLEntities;
+ private URLEntity urlEntity;
private boolean isContributorsEnabled;
private String profileImageUrl;
private String profileImageUrlHttps;
@@ -106,23 +107,15 @@ private void init(JSONObject json) throws TwitterException {
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 = getURLEntitiesFromJSON(json, "description");
descriptionURLEntities = descriptionURLEntities == null ? new URLEntity[0] : descriptionURLEntities;
+ // urlEntity <=> entities/url/urls[]
+ URLEntity[] urlEntities = getURLEntitiesFromJSON(json, "url");
+ if (urlEntities != null && urlEntities.length > 0) {
+ urlEntity = urlEntities[0];
+ }
+
description = getRawString("description", json);
if (description != null) {
description = HTMLEntity.unescapeAndSlideEntityIncdices(description,
@@ -167,6 +160,35 @@ private void init(JSONObject json) throws TwitterException {
throw new TwitterException(jsone.getMessage() + ":" + json.toString(), jsone);
}
}
+
+ /**
+ * Get URL Entities from JSON Object.
+ * returns URLEntity array by entities/[category]/urls/url[]
+ *
+ * @param json user json object
+ * @param category entities category. e.g. "description" or "url"
+ * @return URLEntity array by entities/[category]/urls/url[]
+ * @throws JSONException
+ * @throws TwitterException
+ */
+ private static URLEntity[] getURLEntitiesFromJSON(JSONObject json, String category) throws JSONException, TwitterException {
+ if (!json.isNull("entities")) {
+ JSONObject entitiesJSON = json.getJSONObject("entities");
+ if (!entitiesJSON.isNull(category)) {
+ JSONObject descriptionEntitiesJSON = entitiesJSON.getJSONObject(category);
+ if (!descriptionEntitiesJSON.isNull("urls")) {
+ JSONArray urlsArray = descriptionEntitiesJSON.getJSONArray("urls");
+ int len = urlsArray.length();
+ URLEntity[] urlEntities = new URLEntity[len];
+ for (int i = 0; i < len; i++) {
+ urlEntities[i] = new URLEntityJSONImpl(urlsArray.getJSONObject(i));
+ }
+ return urlEntities;
+ }
+ }
+ }
+ return null;
+ }
@Override
public int compareTo(User that) {
@@ -541,6 +563,19 @@ public boolean isFollowRequestSent() {
return descriptionURLEntities;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public URLEntity getURLEntity() {
+ if (urlEntity == null) {
+ String plainURL = url == null ? "" : url;
+ return new URLEntityJSONImpl(0, plainURL.length(), plainURL, plainURL, plainURL);
+ }
+
+ return urlEntity;
+ }
+
/*package*/
static PagableResponseList<User> createPagableUserList(HttpResponse res, Configuration conf) throws TwitterException {
try {
View
50 twitter4j-core/src/test/java/twitter4j/internal/json/UserJSONImplTest.java
@@ -40,12 +40,14 @@ public void testGetDescriptionURLEntities1() throws JSONException, TwitterExcept
assertEquals("http://t.co/UcHD19ZC", descriptionUrlEntities[0].getURL());
assertEquals(12, descriptionUrlEntities[0].getStart());
assertEquals(32, descriptionUrlEntities[0].getEnd());
+ assertEquals("http://t.co/UcHD19ZC", user.getDescription().substring(descriptionUrlEntities[0].getStart(), 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("http://t.co/dRuJ7wCm", user.getDescription().substring(descriptionUrlEntities[1].getStart(), descriptionUrlEntities[1].getEnd()));
assertEquals("<test> url: http://t.co/UcHD19ZC url2: http://t.co/dRuJ7wCm subaccount: @gjmp10 hashtag: #test", user.getDescription());
}
@@ -64,4 +66,52 @@ public void testGetDescriptionURLEntities2() throws JSONException, TwitterExcept
assertEquals(0, userWithoutDescription.getDescriptionURLEntities().length);
}
+ public void testGetURLEntity1() throws JSONException, TwitterException {
+ // full url entity
+ String rawJson = "{\"profile_sidebar_fill_color\":\"DDEEF6\",\"id\":219570539,\"favourites_count\":252,\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/736851794\\/e6f0992a68eda21a2809efbec2f88050.jpeg\",\"screen_name\":\"gjmp10\",\"following\":false,\"location\":\"\\u4eac\\u90fd\\u5e9c\\u4eac\\u90fd\\u5e02\\u6771\\u5c71\\u533a\\u4e09\\u6761\\u901a\\u5357\\u4e8c\\u7b4b\\u76ee\\u767d\\u5ddd\\u7b4b\\u897f\\u5165\\u30eb\\u4e8c\\u4e01\\u76ee\",\"contributors_enabled\":false,\"profile_background_color\":\"FF8F00\",\"time_zone\":\"Osaka\",\"utc_offset\":32400,\"name\":\"\\u9577\\u3044\\u9577\\u3044\\u9577\\u3044\\u9577\\u3044\\u3042\\u3042\\u3042\\u3042\\u3042\\u3042\\u304b\\u3055\\u305f\\u306a\\u306f\\u3046\",\"notifications\":false,\"geo_enabled\":true,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2964940756\\/51e2ad9e42297e2be926ec4794b95267_normal.jpeg\",\"id_str\":\"219570539\",\"default_profile\":false,\"follow_request_sent\":false,\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/736851794\\/e6f0992a68eda21a2809efbec2f88050.jpeg\",\"protected\":false,\"profile_link_color\":\"0084B4\",\"verified\":false,\"entities\":{\"description\":{\"urls\":[{\"expanded_url\":\"http:\\/\\/jigtwi.jp\\/\",\"display_url\":\"jigtwi.jp\",\"url\":\"http:\\/\\/t.co\\/n578Jr6S\",\"indices\":[31,51]},{\"expanded_url\":\"http:\\/\\/twitpic.com\\/7tx0wk\",\"display_url\":\"twitpic.com\\/7tx0wk\",\"url\":\"http:\\/\\/t.co\\/i2cX7pEY\",\"indices\":[52,72]},{\"expanded_url\":\"http:\\/\\/twitter.com\\/ak_12\\/status\\/12813969077051392\",\"display_url\":\"twitter.com\\/ak_12\\/status\\/1\\u2026\",\"url\":\"http:\\/\\/t.co\\/PlItS8jh\",\"indices\":[77,97]}]},\"url\":{\"urls\":[{\"display_url\":\"google.co.jp\\/search?q=adgjg\",\"expanded_url\":\"https:\\/\\/www.google.co.jp\\/search?q=adgjg\",\"indices\":[0,21],\"url\":\"https:\\/\\/t.co\\/gLcBbz6G\"}]}},\"listed_count\":5,\"profile_use_background_image\":true,\"statuses_count\":979,\"profile_text_color\":\"333333\",\"created_at\":\"Thu Nov 25 06:48:26 +0000 2010\",\"lang\":\"ja\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2964940756\\/51e2ad9e42297e2be926ec4794b95267_normal.jpeg\",\"is_translator\":false,\"profile_sidebar_border_color\":\"FFFFFF\",\"friends_count\":12,\"url\":\"https:\\/\\/t.co\\/gLcBbz6G\",\"default_profile_image\":false,\"followers_count\":10,\"description\":\"\\n09012345678\\n@kouda12345 #test http:\\/\\/t.co\\/n578Jr6S http:\\/\\/t.co\\/i2cX7pEY http:\\/\\/t.co\\/PlItS8jh 0123456789\\n\\u3066\\u3059\\u3068 @gjmp9\",\"profile_background_tile\":true}";
+ JSONObject json = new JSONObject(rawJson);
+ UserJSONImpl user = new UserJSONImpl(json);
+
+ URLEntity urlEntity = user.getURLEntity();
+ assertNotNull(urlEntity);
+ assertEquals("https://www.google.co.jp/search?q=adgjg", urlEntity.getExpandedURL());
+ assertEquals("google.co.jp/search?q=adgjg", urlEntity.getDisplayURL());
+ assertEquals("https://t.co/gLcBbz6G", urlEntity.getURL());
+ assertEquals(0, urlEntity.getStart());
+ assertEquals(21, urlEntity.getEnd());
+ }
+
+ public void testGetURLEntity2() throws JSONException, TwitterException {
+ // url, indices only
+ String rawJson = "{\"profile_sidebar_fill_color\":\"F3F3F3\",\"id\":4311171,\"favourites_count\":118,\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_background_images\\/725168446\\/fed71f230b033a13a69d803d464871ec.gif\",\"screen_name\":\"ApplePedlar\",\"following\":false,\"location\":\"Fukui, Japan\",\"contributors_enabled\":false,\"profile_background_color\":\"FDF4D3\",\"time_zone\":\"Tokyo\",\"utc_offset\":32400,\"name\":\"Naoya Hatayama\",\"notifications\":false,\"geo_enabled\":true,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/1197534673\\/100x100_normal.png\",\"id_str\":\"4311171\",\"default_profile\":false,\"follow_request_sent\":false,\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_background_images\\/725168446\\/fed71f230b033a13a69d803d464871ec.gif\",\"protected\":false,\"profile_link_color\":\"990000\",\"verified\":false,\"entities\":{\"description\":{\"urls\":[]},\"url\":{\"urls\":[{\"display_url\":null,\"expanded_url\":null,\"indices\":[0,30],\"url\":\"http:\\/\\/twitter.com\\/ApplePedlar\"}]}},\"listed_count\":74,\"profile_use_background_image\":true,\"statuses_count\":10130,\"profile_text_color\":\"333333\",\"created_at\":\"Thu Apr 12 06:27:44 +0000 2007\",\"lang\":\"ja\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1197534673\\/100x100_normal.png\",\"is_translator\":false,\"profile_sidebar_border_color\":\"FFFFFF\",\"friends_count\":984,\"url\":\"http:\\/\\/twitter.com\\/ApplePedlar\",\"default_profile_image\":false,\"followers_count\":901,\"description\":\"\\u3081\\u304c\\u306d\\u4f1a\\u9928\\u3067\\u50cd\\u304f\\u30e2\\u30d0\\u30a4\\u30eb\\u7cfb\\u958b\\u767a\\u8005\\u3067\\u3059\\n#kosen #fukui #sabae #jigtwi #Twitter4J #DQ10\",\"profile_background_tile\":true}";
+ JSONObject json = new JSONObject(rawJson);
+ UserJSONImpl user = new UserJSONImpl(json);
+
+ URLEntity urlEntity = user.getURLEntity();
+ assertNotNull(urlEntity);
+ assertEquals("http://twitter.com/ApplePedlar", urlEntity.getExpandedURL());
+ assertEquals("http://twitter.com/ApplePedlar", urlEntity.getDisplayURL());
+ assertEquals("http://twitter.com/ApplePedlar", urlEntity.getURL());
+ assertEquals(0, urlEntity.getStart());
+ assertEquals(30, urlEntity.getEnd());
+ }
+
+ public void testGetURLEntity3() throws JSONException, TwitterException {
+ // url is null
+ String rawJson = "{\"id\":78941611,\"id_str\":\"78941611\",\"name\":\"Harsha Bhogle\",\"screen_name\":\"bhogleharsha\",\"location\":\"Mumbai, India\",\"description\":\"once upon a time a chem engr and mgmt grad,now cricket and motivational speaking my calling!\",\"url\":null,\"entities\":{\"description\":{\"urls\":[]}},\"protected\":false,\"followers_count\":540458,\"friends_count\":83,\"listed_count\":4229,\"created_at\":\"Thu Oct 01 16:18:03 +0000 2009\",\"favourites_count\":3,\"utc_offset\":19800,\"time_zone\":\"Chennai\",\"geo_enabled\":false,\"verified\":true,\"statuses_count\":12205,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"profile_background_color\":\"C0DEED\",\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme1\\/bg.png\",\"profile_background_tile\":false,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2769690929\\/61db65334849e8876c538051e885cdab_normal.png\",\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2769690929\\/61db65334849e8876c538051e885cdab_normal.png\",\"profile_link_color\":\"0084B4\",\"profile_sidebar_border_color\":\"C0DEED\",\"profile_sidebar_fill_color\":\"DDEEF6\",\"profile_text_color\":\"333333\",\"profile_use_background_image\":true,\"default_profile\":true,\"default_profile_image\":false,\"following\":null,\"follow_request_sent\":null,\"notifications\":null}";
+ JSONObject json = new JSONObject(rawJson);
+ UserJSONImpl user = new UserJSONImpl(json);
+
+ assertNull(user.getURL());
+
+ URLEntity urlEntity = user.getURLEntity();
+ assertNotNull(urlEntity);
+ assertEquals("", urlEntity.getExpandedURL());
+ assertEquals("", urlEntity.getDisplayURL());
+ assertEquals("", urlEntity.getURL());
+ assertEquals(0, urlEntity.getStart());
+ assertEquals(0, urlEntity.getEnd());
+ }
+
+
}

0 comments on commit b9d69ad

Please sign in to comment.