Coverage Report - net.spy.digg.parsers.StoryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
StoryImpl
95%
61/64
86%
19/22
0
 
 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  
  * A Digg story.
 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  
                                 // skipping random text node
 89  
                         } else {
 90  0
                                 assert false : "Unexpected node: " + cn;
 91  
                         }
 92  
                 }
 93  
 
 94  
                 // Set the final fields.
 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  
                 // Add the topic to the container.
 103  267
                 container.add(topic);
 104  267
                 ((TopicImpl)topic).setContainerName(container.getShortName());
 105  267
         }
 106  
 
 107  
         /* (non-Javadoc)
 108  
          * @see net.spy.digg.Story#getComments()
 109  
          */
 110  
         public int getComments() {
 111  3
                 return comments;
 112  
         }
 113  
 
 114  
         /* (non-Javadoc)
 115  
          * @see net.spy.digg.Story#getContainer()
 116  
          */
 117  
         public TopicContainer getContainer() {
 118  3
                 return container;
 119  
         }
 120  
 
 121  
         /* (non-Javadoc)
 122  
          * @see net.spy.digg.Story#getDescription()
 123  
          */
 124  
         public String getDescription() {
 125  3
                 return description;
 126  
         }
 127  
 
 128  
         /* (non-Javadoc)
 129  
          * @see net.spy.digg.Story#getDiggLink()
 130  
          */
 131  
         public String getDiggLink() {
 132  3
                 return diggLink;
 133  
         }
 134  
 
 135  
         /* (non-Javadoc)
 136  
          * @see net.spy.digg.Story#getDiggs()
 137  
          */
 138  
         public int getDiggs() {
 139  3
                 return diggs;
 140  
         }
 141  
 
 142  
         /* (non-Javadoc)
 143  
          * @see net.spy.digg.Story#getId()
 144  
          */
 145  
         public int getId() {
 146  3
                 return id;
 147  
         }
 148  
 
 149  
         /* (non-Javadoc)
 150  
          * @see net.spy.digg.Story#getLink()
 151  
          */
 152  
         public String getLink() {
 153  3
                 return link;
 154  
         }
 155  
 
 156  
         /* (non-Javadoc)
 157  
          * @see net.spy.digg.Story#getStatus()
 158  
          */
 159  
         public String getStatus() {
 160  3
                 return status;
 161  
         }
 162  
 
 163  
         /* (non-Javadoc)
 164  
          * @see net.spy.digg.Story#getSubmitDate()
 165  
          */
 166  
         public long getSubmittedTimestamp() {
 167  3
                 return submitDate;
 168  
         }
 169  
 
 170  
         /* (non-Javadoc)
 171  
          * @see net.spy.digg.Story#getTitle()
 172  
          */
 173  
         public String getTitle() {
 174  3
                 return title;
 175  
         }
 176  
 
 177  
         /* (non-Javadoc)
 178  
          * @see net.spy.digg.Story#getTopic()
 179  
          */
 180  
         public Topic getTopic() {
 181  3
                 return topic;
 182  
         }
 183  
 
 184  
         /* (non-Javadoc)
 185  
          * @see net.spy.digg.Story#getUser()
 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  
         /* (non-Javadoc)
 197  
          * @see net.spy.digg.Story#getThumbnail()
 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  
 }