License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *   * Neither the name of Oracle Corporation nor the names of its contributors 
 *     may be used to endorse or promote products derived from this software 
 *     without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

    

package tweeter.parser;

import java.io.InputStream;
import javafx.data.pull.Event;
import javafx.data.pull.PullParser;
import tweeter.model.Status;
import tweeter.model.User;

// @author Rakesh Menon

public class StatusParser {
    
    // Error Message (if any)
    public var errorMessage = "";

    // Information about all interesting photos
    var statuses: Status[];
    var status : Status;
    
    // Completion callback that also delivers parsed status
    public var updateStatus:function(statuses:Status[]) = null;

    public function parse(input: InputStream) {
        
        // Parse the input data (Photo Metadata) and construct Photo instance
        def parser = PullParser {
            input:input
            onEvent:function(event: Event) {
                if (event.type == PullParser.TEXT) {

                    if(event.level == 2) {
                        
                        if(event.qname.name == "created_at") {
                            status = Status {
                                createdAt: "{event.text}"
                            };
                        } else if(event.qname.name == "id") {
                            status.id = "{event.text}";
                        } else if(event.qname.name == "text") {
                            status.text = "{event.text}";
                        } else if(event.qname.name == "source") {
                            status.source = "{event.text}";
                        } else if(event.qname.name == "truncated") {
                            status.truncated = "{event.text}";
                        } else if(event.qname.name == "in_reply_to_status_id") {
                            status.inReplyToStatusId = "{event.text}";
                        } else if(event.qname.name == "in_reply_to_user_id") {
                            status.inReplyToUserId = "{event.text}";
                        } else if(event.qname.name == "favorited") {
                            status.favorited = "{event.text}";
                        } else if(event.qname.name == "in_reply_to_screen_name") {
                            status.inReplyToScreenName = "{event.text}";
                        } else if(event.qname.name == "user") {
                            status.user = User { };
                        }
                        
                    } else if(event.level == 3) {
                        
                        if(event.qname.name == "id") {
                            status.user.id = "{event.text}";
                        } else if(event.qname.name == "name") {
                            status.user.name = "{event.text}";
                        } else if(event.qname.name == "screen_name") {
                            status.user.screenName = "{event.text}";
                        } else if(event.qname.name == "location") {
                            status.user.location = "{event.text}";
                        } else if(event.qname.name == "description") {
                            status.user.description = "{event.text}";
                        } else if(event.qname.name == "profile_image_url") {
                            status.user.profileImageURL = "{event.text}";
                        } else if(event.qname.name == "url") {
                            status.user.url = "{event.text}";
                        } else if(event.qname.name == "protected") {
                            status.user.protectedProfile = "{event.text}";
                        } else if(event.qname.name == "followers_count") {
                            status.user.followersCount = "{event.text}";
                        } else if(event.qname.name == "profile_background_color") {
                            status.user.profileBackgroundColor = "{event.text}";
                        } else if(event.qname.name == "profile_text_color") {
                            status.user.profileTextColor = "{event.text}";
                        } else if(event.qname.name == "profile_link_color") {
                            status.user.profileLinkColor = "{event.text}";
                        } else if(event.qname.name == "profile_sidebar_fill_color") {
                            status.user.profileSidebarFillColor = "{event.text}";
                        } else if(event.qname.name == "profile_sidebar_border_color") {
                            status.user.profileSidebarBorderColor = "{event.text}";
                        } else if(event.qname.name == "friends_count") {
                            status.user.friendsCount = "{event.text}";
                        } else if(event.qname.name == "created_at") {
                            status.user.createdAt = "{event.text}";
                        } else if(event.qname.name == "favourites_count") {
                            status.user.favouritesCount = "{event.text}";
                        } else if(event.qname.name == "utc_offset") {
                            status.user.utcOffset = "{event.text}";
                        } else if(event.qname.name == "time_zone") {
                            status.user.timeZone = "{event.text}";
                        } else if(event.qname.name == "profile_background_image_url") {
                            status.user.profileBackgroundImageURL = "{event.text}";
                        } else if(event.qname.name == "profile_background_tile") {
                            status.user.profileBackgroundTile = "{event.text}";
                        } else if(event.qname.name == "statuses_count") {
                            status.user.statusesCount = "{event.text}";
                        } else if(event.qname.name == "notifications") {
                            status.user.notifications = "{event.text}";
                        } else if(event.qname.name == "following") {
                            status.user.following = "{event.text}";
                        }
                    }
                    
                } else if (event.type == PullParser.END_ELEMENT) {
                    if(event.qname.name == "status" and event.level == 1) {
                        insert status into statuses;
                    } else if(event.qname.name == "statuses" and event.level == 0) {
                        if(updateStatus != null) {
                            updateStatus(statuses);
                        }
                    }
                }
            }
        }
        
        parser.parse();
    }
}