CommentsParser.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.networking;

import java.util.Vector;
import org.xml.sax.Attributes;


/**
 * Parses the server comments response.
 */
public class CommentsParser extends Parser {

    /**
     * Vector of result comments.
     */
    public Vector comments = new Vector();
    /**
     * Current comment being parsed.
     */
    private Comment comment = null;

    public CommentsParser() {
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) {
        super.startElement(uri, localName, qName, attributes);

        if (qName.equals("struct")) {
            comment = new Comment();
        }
    }

    public void endElement(String uri, String localName, String qName) {
        String chars = getChars();

        if (qName.equals("struct")) {
            comments.addElement(comment);
            comment = null;

        } else if (qName.equals("name")) {
            if (chars.equals("comment_id") || chars.equals("post_id") || chars.equals("content") || chars.equals("author") ||
                    chars.equals("author_email") || chars.equals("dateCreated") || chars.equals("date_created_gmt")) {
                current = chars;
            }
        } else if (qName.equals("string")) {
            if (current.equals("comment_id")) {
                comment.commentId = chars;
                current = "";
            }
            if (current.equals("post_id")) {
                comment.postId = chars;
                current = "";
            } else if (current.equals("content")) {
                comment.content = chars;
                current = "";
            } else if (current.equals("author")) {
                comment.author = chars;
                current = "";
            } else if (current.equals("author_email")) {
                comment.author_email = chars;
                current = "";
            }
        } else if (qName.equals("dateTime.iso8601") &&
                (current.equals("date_created_gmt") || current.equals("dateCreated"))) {
            comment.date = convertDate(chars);
            current = "";
        }
    }
}

/* Server response:
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array><data>
<value><struct>
<member><name>date_created_gmt</name><value><dateTime.iso8601>20100520T11:53:54</dateTime.iso8601></value></member>
<member><name>user_id</name><value><string>0</string></value></member>
<member><name>comment_id</name><value><string>1</string></value></member>
<member><name>parent</name><value><string>0</string></value></member>
<member><name>status</name><value><string>approve</string></value></member>
<member><name>content</name><value><string>Hi, this is a comment.<br />To delete a comment, just log in, and view the posts' comments, there you will have the option to edit or delete them.</string></value></member>
<member><name>link</name><value><string>http://blogusername.wordpress.com/2010/05/20/hello-world/#comment-1</string></value></member>
<member><name>post_id</name><value><string>1</string></value></member>
<member><name>post_title</name><value><string>Hello&nbsp;world!</string></value></member>
<member><name>author</name><value><string>Mr WordPress</string></value></member>
<member><name>author_url</name><value><string>http://WordPress.com/</string></value></member>
<member><name>author_email</name><value><string></string></value></member>
<member><name>author_ip</name><value><string></string></value></member>
<member><name>type</name><value><string></string></value></member>
</struct></value>
</data></array>
</value>
</param>
</params>
</methodResponse>
 */