Skip to content

Instantly share code, notes, and snippets.

@alexoro
Created June 19, 2013 16:15
Show Gist options
  • Save alexoro/5815607 to your computer and use it in GitHub Desktop.
Save alexoro/5815607 to your computer and use it in GitHub Desktop.
package com.dengionline.content.faq;
import android.content.Context;
import com.dengionline.R;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
/**
* User: [email protected]
* DateTime: 06.02.13 12:54
*/
public class FaqReader {
private Context mContext;
private List<FaqCategory> mData;
protected FaqReader(Context context) {
mContext = context;
mData = null;
}
public static FaqReader read(Context context) {
return new FaqReader(context.getApplicationContext());
}
public List<FaqCategory> getCategoriesList() {
lazyLoad();
return mData;
}
public FaqCategory getCategoryById(int id) {
lazyLoad();
for (FaqCategory category: mData) {
if (category.getId() == id) {
return category;
}
}
return null;
}
public FaqArticle getArticleById(int id) {
lazyLoad();
for (FaqCategory category: mData) {
for (FaqArticle article: category.getArticles()) {
if (article.getId() == id) {
return article;
}
}
}
return null;
}
protected void lazyLoad() {
if (mData == null) {
try {
readData();
} catch (Exception ex) {
throw new RuntimeException("Unable to parse FAQ", ex);
}
}
}
protected void readData() throws ParserConfigurationException, SAXException, IOException {
mData = new LinkedList<FaqCategory>();
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
FaqXmlHandler handler = new FaqXmlHandler();
xr.setContentHandler(handler);
xr.parse(new InputSource(
mContext.getResources().openRawResource(R.raw.faq)
));
}
class FaqXmlHandler extends DefaultHandler {
private StringBuilder mNodeValue;
private int mCategoryOffset;
private int mArticleOffset;
private FaqCategory mCurrentCategory;
private FaqArticle mCurrentArticle;
private List<FaqArticle> mCurrentArticlesList;
public FaqXmlHandler() {
super();
mNodeValue = new StringBuilder();
mCategoryOffset = 0;
mArticleOffset = 0;
mCurrentArticlesList = new LinkedList<FaqArticle>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("category".equals(localName)) {
mCurrentCategory = new FaqCategory();
mCurrentCategory.setId(mCategoryOffset++);
mCurrentCategory.setTitle(attributes.getValue("title"));
} else if ("article".equals(localName)) {
mCurrentArticle = new FaqArticle();
mCurrentArticle.setId(mArticleOffset++);
mCurrentArticle.setTitle(attributes.getValue("title"));
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (mCurrentArticle != null) {
mCurrentArticle.setContent(mNodeValue.toString());
mCurrentArticlesList.add(mCurrentArticle);
mCurrentArticle = null;
} else if (mCurrentCategory != null) {
mCurrentCategory.setArticles(mCurrentArticlesList);
mData.add(mCurrentCategory);
mCurrentArticlesList = new LinkedList<FaqArticle>();
mCurrentCategory = null;
}
mNodeValue.setLength(0);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (mCurrentCategory != null || mCurrentArticle != null) {
mNodeValue.append(new String(ch, start, length));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment