2 Copyright (c) 2007-2009, Yusuke Yamamoto
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Yusuke Yamamoto nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 package weibo4android
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Date
;
31 import java
.util
.List
;
33 import org
.w3c
.dom
.Document
;
34 import org
.w3c
.dom
.Element
;
35 import org
.w3c
.dom
.NodeList
;
37 import weibo4android
.http
.Response
;
38 import weibo4android
.org
.json
.JSONArray
;
39 import weibo4android
.org
.json
.JSONException
;
40 import weibo4android
.org
.json
.JSONObject
;
43 * A data class representing sent/received direct message.
44 * @author Yusuke Yamamoto - yusuke at mac.com
46 public class DirectMessage
extends WeiboResponse
implements java
.io
.Serializable
{
49 private int sender_id
;
50 private int recipient_id
;
51 private Date created_at
;
52 private String sender_screen_name
;
53 private String recipient_screen_name
;
54 private static final long serialVersionUID
= -3253021825891789737L;
56 /*package*/DirectMessage(Response res
, Weibo weibo
) throws WeiboException
{
58 init(res
, res
.asDocument().getDocumentElement(), weibo
);
60 /*package*/DirectMessage(Response res
, Element elem
, Weibo weibo
) throws WeiboException
{
62 init(res
, elem
, weibo
);
64 /*modify by sycheng add json call*/
65 /*package*/DirectMessage(JSONObject json
) throws WeiboException
{
67 id
= json
.getLong("id");
68 text
= json
.getString("text");
69 sender_id
= json
.getInt("sender_id");
70 recipient_id
= json
.getInt("recipient_id");
71 created_at
= parseDate(json
.getString("created_at"), "EEE MMM dd HH:mm:ss z yyyy");
72 sender_screen_name
= json
.getString("sender_screen_name");
73 recipient_screen_name
= json
.getString("recipient_screen_name");
75 if(!json
.isNull("sender"))
76 sender
= new User(json
.getJSONObject("sender"));
77 } catch (JSONException jsone
) {
78 throw new WeiboException(jsone
.getMessage() + ":" + json
.toString(), jsone
);
83 private void init(Response res
, Element elem
, Weibo weibo
) throws WeiboException
{
84 ensureRootNodeNameIs("direct_message", elem
);
85 sender
= new User(res
, (Element
) elem
.getElementsByTagName("sender").item(0),
87 recipient
= new User(res
, (Element
) elem
.getElementsByTagName("recipient").item(0),
89 id
= getChildLong("id", elem
);
90 text
= getChildText("text", elem
);
91 sender_id
= getChildInt("sender_id", elem
);
92 recipient_id
= getChildInt("recipient_id", elem
);
93 created_at
= getChildDate("created_at", elem
);
94 sender_screen_name
= getChildText("sender_screen_name", elem
);
95 recipient_screen_name
= getChildText("recipient_screen_name", elem
);
102 public String
getText() {
106 public int getSenderId() {
110 public int getRecipientId() {
116 * @since Weibo4J 1.1.0
118 public Date
getCreatedAt() {
122 public String
getSenderScreenName() {
123 return sender_screen_name
;
126 public String
getRecipientScreenName() {
127 return recipient_screen_name
;
132 public User
getSender() {
136 private User recipient
;
138 public User
getRecipient() {
143 static List
<DirectMessage
> constructDirectMessages(Response res
,
144 Weibo weibo
) throws WeiboException
{
145 Document doc
= res
.asDocument();
146 if (isRootNodeNilClasses(doc
)) {
147 return new ArrayList
<DirectMessage
>(0);
150 ensureRootNodeNameIs("direct-messages", doc
);
151 NodeList list
= doc
.getDocumentElement().getElementsByTagName(
153 int size
= list
.getLength();
154 List
<DirectMessage
> messages
= new ArrayList
<DirectMessage
>(size
);
155 for (int i
= 0; i
< size
; i
++) {
156 Element status
= (Element
) list
.item(i
);
157 messages
.add(new DirectMessage(res
, status
, weibo
));
160 } catch (WeiboException te
) {
161 if (isRootNodeNilClasses(doc
)) {
162 return new ArrayList
<DirectMessage
>(0);
171 static List
<DirectMessage
> constructDirectMessages(Response res
172 ) throws WeiboException
{
173 JSONArray list
= res
.asJSONArray();
176 int size
= list
.length();
177 List
<DirectMessage
> messages
= new ArrayList
<DirectMessage
>(size
);
178 for (int i
= 0; i
< size
; i
++) {
180 messages
.add(new DirectMessage(list
.getJSONObject(i
)));
183 } catch (JSONException jsone
) {
184 throw new WeiboException(jsone
);
189 public int hashCode() {
190 return id
.hashCode();
194 public boolean equals(Object obj
) {
201 return obj
instanceof DirectMessage
&& ((DirectMessage
) obj
).id
== this.id
;
205 public String
toString() {
206 return "DirectMessage{" +
208 ", text='" + text
+ '\'' +
209 ", sender_id=" + sender_id
+
210 ", recipient_id=" + recipient_id
+
211 ", created_at=" + created_at
+
212 ", sender_screen_name='" + sender_screen_name
+ '\'' +
213 ", recipient_screen_name='" + recipient_screen_name
+ '\'' +
214 ", sender=" + sender
+
215 ", recipient=" + recipient
+