PostsParser.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.Calendar;
import java.util.Vector;
import org.xml.sax.Attributes;
/**
* Parses the post server response, creates an array of Post objects.
*/
public class PostsParser extends Parser {
/**
* Result vector of Posts.
*/
public Vector posts = new Vector();
/**
* Current Post being parsed.
*/
private Post post = null;
/**
* For keeping track of nested <struct> eleements. To keep the parser
* as simple as possible, those can be ignored.
*/
private int nestedStruct = 0;
/**
* Reference to a Blog that can be plased inside the Post objects for convenience.
*/
private Blog blog;
public PostsParser(Blog blog) {
this.blog = blog;
}
public void startElement(String uri, String localName, String qName, Attributes attributes) {
super.startElement(uri, localName, qName, attributes);
// Keep track of <struct> elements, ignore all child structs
if (qName.equals("struct")) {
nestedStruct++;
if (post == null && nestedStruct == 1) {
post = new Post(blog);
} else {
}
}
}
public void endElement(String uri, String localName, String qName) {
String chars = getChars();
if (qName.equals("struct")) {
nestedStruct--;
if (nestedStruct == 0 && posts != null) {
posts.addElement(post);
post = null;
}
} else if (qName.equals("name")) {
if (chars.equals("postid") || chars.equals("description") || chars.equals("title") || chars.equals("dateCreated")) {
current = chars;
}
} else if (qName.equals("string")) {
if (current.equals("postid")) {
post.postId = chars;
current = "";
} else if (current.equals("title")) {
post.title = chars;
current = "";
} else if (current.equals("description")) {
post.description = chars;
current = "";
}
} else if (qName.equals("dateTime.iso8601") && current.equals("dateCreated")) {
post.date = convertDate(chars);
current = "";
}
}
}
/* Server response:
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<array><data>
<value><struct>
<member><name>dateCreated</name><value><dateTime.iso8601>20100520T11:59:08</dateTime.iso8601></value></member>
<member><name>userid</name><value><string>14180154</string></value></member>
<member><name>postid</name><value><string>3</string></value></member>
<member><name>description</name><value><string>Testing, blog text</string></value></member>
<member><name>title</name><value><string>Testing, title</string></value></member>
<member><name>link</name><value><string>http://blogusername.wordpress.com/2010/05/20/testing-title/</string></value></member>
<member><name>permaLink</name><value><string>http://blogusername.wordpress.com/2010/05/20/testing-title/</string></value></member>
<member><name>categories</name><value><array><data>
<value><string>Uncategorized</string></value>
</data></array></value></member>
<member><name>mt_excerpt</name><value><string></string></value></member>
<member><name>mt_text_more</name><value><string></string></value></member>
<member><name>mt_allow_comments</name><value
><int>1</int></value></member>
<member><name>mt_allow_pings</name><value><int>1</int></value></member>
<member><name>mt_keywords</name><value><string></string></value></member>
<member><name>wp_slug</name><value><string>testing-title</string></value></member>
<member><name>wp_password</name><value><string></string></value></member>
<member><name>wp_author_id</name><value><string>14180154</string></value></member>
<member><name>wp_author_display_name</name><value><string>blogusername</string></value></member>
<member><name>date_created_gmt</name><value><dateTime.iso8601>20100520T11:59:08</dateTime.iso8601></value></member>
<member><name>post_status</name><value><string>publish</string></value></member>
<member><name>custom_fields</name><value><array><data>
<value><struct>
<member><name>id</name><value><string>2</string></value></member>
<member><name>key</name><value><string>_edit_last</string></value></member>
<member><name>value</name><value><string>14180154</string></value></member
>
</struct></value>
<value><struct>
<member><name>id</name><value><string>3</string></value></member>
<member><name>key</name><value><string>_edit_lock</string></value></member>
<member><name>value</name><value><string>1274356754</string></value></member>
</struct></value>
</data></array></value></member>
</struct></value>
<value><struct>
<member><name>dateCreated</name><value><dateTime.iso8601>20100520T11:53:54</dateTime.iso8601></value></member>
<member><name>userid</name><value><string>14180154</string></value></member>
<member><name>postid</name><value><string>1</string></value></member>
<member><name>description</name><value><string>Welcome to <a href="http://wordpress.com/">WordPress.com</a>. This is your first post. Edit or delete it and start blogging!</string></value></member>
<member><name>title</name><value><string>Hello world!</string></value></member>
<member><name>link</name><value><string>http://blogusername.wordpress.com/2010/05/20/hello-world/</stri
ng></value></member>
<member><name>permaLink</name><value><string>http://blogusername.wordpress.com/2010/05/20/hello-world/</string></value></member>
<member><name>categories</name><value><array><data>
<value><string>Uncategorized</string></value>
</data></array></value></member>
<member><name>mt_excerpt</name><value><string></string></value></member>
<member><name>mt_text_more</name><value><string></string></value></member>
<member><name>mt_allow_comments</name><value><int>1</int></value></member>
<member><name>mt_allow_pings</name><value><int>1</int></value></member>
<member><name>mt_keywords</name><value><string></string></value></member>
<member><name>wp_slug</name><value><string>hello-world</string></value></member>
<member><name>wp_password</name><value><string></string></value></member>
<member><name>wp_author_id</name><value><string>14180154</string></value></member>
<member><name>wp_author_display_name</name><value><string>blogusername</string></value></member>
<member><name>
date_created_gmt</name><value><dateTime.iso8601>20100520T11:53:54</dateTime.iso8601></value></member>
<member><name>post_status</name><value><string>publish</string></value></member>
<member><name>custom_fields</name><value><array><data>
</data></array></value></member>
</struct></value>
</data></array>
</value>
</param>
</params>
</methodResponse>
*/
/*
<params>
<param>
<value>
<array><data>
<value><struct>
<member><name>postid</name><value><string>1</string></value></member>
<member><name>description</name><value><string>Testing, blog text</string></value></member>
<member><name>title</name><value><string>Testing, title</string></value></member>
*/