| 1 | |
package net.spy.digg.parsers; |
| 2 | |
|
| 3 | |
import java.io.Serializable; |
| 4 | |
|
| 5 | |
import org.w3c.dom.Node; |
| 6 | |
import org.w3c.dom.NodeList; |
| 7 | |
|
| 8 | |
import net.spy.digg.Story; |
| 9 | |
import net.spy.digg.Thumbnail; |
| 10 | |
import net.spy.digg.Topic; |
| 11 | |
import net.spy.digg.TopicContainer; |
| 12 | |
import net.spy.digg.User; |
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | 3 | public class StoryImpl implements Story, Serializable { |
| 18 | |
|
| 19 | |
private final User user; |
| 20 | |
private final TopicContainer container; |
| 21 | |
private final Topic topic; |
| 22 | |
|
| 23 | |
private final Thumbnail thumbnail; |
| 24 | |
|
| 25 | |
private final int id; |
| 26 | |
private final String link; |
| 27 | |
private final String diggLink; |
| 28 | |
private final String status; |
| 29 | |
private final String media; |
| 30 | |
private final long promoteDate; |
| 31 | |
private final String title; |
| 32 | |
private final String description; |
| 33 | |
private final long submitDate; |
| 34 | |
private final int diggs; |
| 35 | |
private final int comments; |
| 36 | |
|
| 37 | |
StoryImpl(Node n) { |
| 38 | 267 | super(); |
| 39 | |
|
| 40 | 267 | id=Integer.parseInt(BaseParser.getAttr(n, "id")); |
| 41 | 267 | link=BaseParser.getAttr(n, "link"); |
| 42 | 267 | submitDate=1000*Long.parseLong(BaseParser.getAttr(n, "submit_date")); |
| 43 | 267 | diggs=Integer.parseInt(BaseParser.getAttr(n, "diggs")); |
| 44 | 267 | comments=Integer.parseInt(BaseParser.getAttr(n, "comments")); |
| 45 | 267 | diggLink=BaseParser.getAttr(n, "href"); |
| 46 | 267 | status=BaseParser.getAttr(n, "status"); |
| 47 | 267 | media=BaseParser.getAttr(n, "media"); |
| 48 | |
|
| 49 | 267 | String pdStr=BaseParser.getAttr(n, "promote_date"); |
| 50 | 267 | promoteDate=pdStr == null ? -1 : 1000*Long.parseLong(pdStr); |
| 51 | |
|
| 52 | 267 | String t=null; |
| 53 | 267 | String d=null; |
| 54 | 267 | User u=null; |
| 55 | 267 | Topic top=null; |
| 56 | 267 | TopicContainer tc=null; |
| 57 | 267 | Thumbnail tn=null; |
| 58 | |
|
| 59 | 267 | final NodeList nl=n.getChildNodes(); |
| 60 | 3468 | for(int i=0; i<nl.getLength(); i++) { |
| 61 | 3201 | final Node cn=nl.item(i); |
| 62 | 3201 | final String nm=cn.getNodeName(); |
| 63 | 3201 | if(nm.equals("title")) { |
| 64 | 267 | t=cn.getFirstChild().getNodeValue(); |
| 65 | 2934 | } else if(nm.equals("description")) { |
| 66 | 267 | Node node = cn.getFirstChild(); |
| 67 | 267 | if(node != null){ |
| 68 | 258 | d=node.getNodeValue(); |
| 69 | |
}else{ |
| 70 | 9 | d=null; |
| 71 | |
} |
| 72 | 267 | } else if(nm.equals("user")) { |
| 73 | 267 | u=new UserImpl(cn); |
| 74 | 2400 | } else if(nm.equals("topic")) { |
| 75 | 267 | top=new TopicImpl(BaseParser.getAttr(cn, "name"), |
| 76 | |
BaseParser.getAttr(cn, "short_name")); |
| 77 | 2133 | } else if(nm.equals("container")) { |
| 78 | 267 | tc=new TopicContainerImpl(BaseParser.getAttr(cn, "name"), |
| 79 | |
BaseParser.getAttr(cn, "short_name")); |
| 80 | 1866 | } else if(nm.equals("thumbnail")) { |
| 81 | 132 | tn=new ThumbnailImpl(BaseParser.getAttr(cn, "src"), |
| 82 | |
BaseParser.getAttr(cn, "contentType"), |
| 83 | |
Integer.parseInt(BaseParser.getAttr(cn, "originalwidth")), |
| 84 | |
Integer.parseInt(BaseParser.getAttr(cn, "originalheight")), |
| 85 | |
Integer.parseInt(BaseParser.getAttr(cn, "width")), |
| 86 | |
Integer.parseInt(BaseParser.getAttr(cn, "height"))); |
| 87 | 1734 | } else if(cn.getNodeType() == Node.TEXT_NODE) { |
| 88 | |
|
| 89 | |
} else { |
| 90 | 0 | assert false : "Unexpected node: " + cn; |
| 91 | |
} |
| 92 | |
} |
| 93 | |
|
| 94 | |
|
| 95 | 267 | title=t; |
| 96 | 267 | description=d; |
| 97 | 267 | user=u; |
| 98 | 267 | topic=top; |
| 99 | 267 | container=tc; |
| 100 | 267 | thumbnail=tn; |
| 101 | |
|
| 102 | |
|
| 103 | 267 | container.add(topic); |
| 104 | 267 | ((TopicImpl)topic).setContainerName(container.getShortName()); |
| 105 | 267 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public int getComments() { |
| 111 | 3 | return comments; |
| 112 | |
} |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public TopicContainer getContainer() { |
| 118 | 3 | return container; |
| 119 | |
} |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
public String getDescription() { |
| 125 | 3 | return description; |
| 126 | |
} |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public String getDiggLink() { |
| 132 | 3 | return diggLink; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
public int getDiggs() { |
| 139 | 3 | return diggs; |
| 140 | |
} |
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public int getId() { |
| 146 | 3 | return id; |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public String getLink() { |
| 153 | 3 | return link; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
public String getStatus() { |
| 160 | 3 | return status; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
public long getSubmittedTimestamp() { |
| 167 | 3 | return submitDate; |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public String getTitle() { |
| 174 | 3 | return title; |
| 175 | |
} |
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public Topic getTopic() { |
| 181 | 3 | return topic; |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
public User getUser() { |
| 188 | 3 | return user; |
| 189 | |
} |
| 190 | |
|
| 191 | |
@Override |
| 192 | |
public String toString() { |
| 193 | 12 | return "{Story id=" + id + " ``" + title + "''}"; |
| 194 | |
} |
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
public Thumbnail getThumbnail() { |
| 200 | 18 | return thumbnail; |
| 201 | |
} |
| 202 | |
|
| 203 | |
public String getMedia() { |
| 204 | 0 | return media; |
| 205 | |
} |
| 206 | |
|
| 207 | |
public long getPromoteTimestamp() { |
| 208 | 0 | return promoteDate; |
| 209 | |
} |
| 210 | |
|
| 211 | |
} |