Parser.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 org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
/**
* Parser abstract class to be extended by specific parsers.
* Based on the default SAX parser event handler.
*/
public abstract class Parser extends DefaultHandler {
/**
* Name of the tag inside which the parser is currently.
*/
protected String current = "";
/**
* Buffer for accumulating string content.
*/
private StringBuffer chars = new StringBuffer();
/**
* Event handler for the start of an XML element. Clears the content string buffer
* so that a new string can be stored.
* @param uri
* @param localName
* @param qName
* @param attributes
*/
public void startElement(String uri, String localName, String qName, Attributes attributes) {
chars = new StringBuffer();
}
/**
* Character callback for content characters. Data is appended to the
* string buffer.
* @param ch
* @param start
* @param length
*/
public void characters(char[] ch, int start, int length) {
chars.append(ch, start, length);
}
/**
* Retrieves the last content string and clears the string buffer.
* @return
*/
protected String getChars() {
// For some reason there is some extra whitespace around the characters sometimes. Trim those out.
String c = chars.toString().trim();
chars = new StringBuffer();
return c;
}
/**
* Convert the ISO8601 to a more readable date representation.
* @param text Date string in ISO8601
* @return
*/
protected String convertDate(String text) {
//01234567890123456
//20100520T11:53:54
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.valueOf(text.substring(0, 4)).intValue());
cal.set(Calendar.MONTH, Integer.valueOf(text.substring(4, 6)).intValue() - 1); // note -1
cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(text.substring(6, 8)).intValue());
cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(text.substring(9, 11)).intValue());
cal.set(Calendar.MINUTE, Integer.valueOf(text.substring(12, 14)).intValue());
cal.set(Calendar.SECOND, Integer.valueOf(text.substring(15, 17)).intValue());
// "dow mon dd hh:mm:ss zzz yyy"
String s = cal.getTime().toString();
// "dow " "hh:mm" "dd"
return s.substring(0, 4) + s.substring(11, 16) + ", " + s.substring(8, 10) + ". " + s.substring(4, 7);
}
}