Created
August 1, 2013 19:58
-
-
Save KamilLelonek/6134686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pwr.rss.reader.xml | |
import java.io.InputStream | |
import org.xmlpull.v1.XmlPullParser | |
import android.util.Xml | |
import AbstractXMLParser._ | |
import scala.collection.mutable.ListBuffer | |
import android.util.Log | |
import pwr.rss.reader.database.dao.Feed | |
object AbstractXMLParser { | |
lazy val UTF_ENCODING = "UTF-8" | |
} | |
abstract class AbstractXMLParser(channel: Long) { | |
protected lazy val feedsList = new ListBuffer[Feed] | |
protected lazy val xmlPullParser = Xml.newPullParser | |
def parseStream(inputStream: InputStream) = { | |
configureParser(inputStream) | |
xmlPullParser.nextTag | |
parseFeeds | |
} | |
def configureParser(inputStream: InputStream) = { | |
xmlPullParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false) | |
xmlPullParser.setInput(inputStream, UTF_ENCODING) | |
} | |
protected def parseFeeds | |
protected def readFeed: Feed | |
protected def skipToTag(tag: String) = while (!tagFound(tag) && !isEndOfDocument) xmlPullParser.next | |
protected def tagFound(tag: String) = tag.equals(xmlPullParser.getName) | |
protected def isEndTag = equalsTag(XmlPullParser.END_TAG) | |
protected def isStartTag = equalsTag(XmlPullParser.START_TAG) | |
protected def isEndOfDocument = equalsTag(XmlPullParser.END_DOCUMENT) | |
private def equalsTag(TAG: Int) = xmlPullParser.getEventType == TAG | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pwr.rss.reader.xml | |
import org.xmlpull.v1.XmlPullParser | |
import XMLRssParser._ | |
import scala.collection.mutable.ListBuffer | |
import java.text.SimpleDateFormat | |
import android.text.Html | |
import pwr.rss.reader.database.dao.Feed | |
object XMLRssParser { | |
lazy val ROOT = "item" | |
lazy val TITLE = "title" | |
lazy val LINK = "link" | |
lazy val DATE = "pubDate" | |
lazy val DESCRIPTION = "description" | |
lazy val IMAGE = "napwr:plakatMiniatura" | |
lazy val dateFormat = "EEE, d MMM yyyy HH:mm:ss Z" | |
lazy val dateFormatter = new SimpleDateFormat(dateFormat) | |
} | |
class XMLRssParser(channel: Long) extends AbstractXMLParser(channel) { | |
var title: String = _ | |
var date: Long = _ | |
var link: String = _ | |
var description: String = _ | |
var image: String = _ | |
override def parseFeeds = { | |
skipToTag(ROOT) | |
while (isInMainTree) { | |
feedsList += readFeed | |
xmlPullParser.next | |
} | |
} | |
private def isInMainTree = !isEndTag && !isEndOfDocument | |
override protected def readFeed = { | |
xmlPullParser.nextTag | |
while (isInRoot) { | |
if (isStartTag) parseParticularTag | |
xmlPullParser.next | |
} | |
new Feed(0L, title, image, link, description, channel, 0L, date) | |
} | |
private def isInRoot = !isEndTag || !tagFound(ROOT) | |
private def parseParticularTag = { | |
val tagName = xmlPullParser.getName | |
tagName match { | |
case TITLE => title = readNextText | |
case LINK => link = readNextText | |
case DATE => date = readDate | |
case DESCRIPTION => description = readDescription | |
case IMAGE => image = readImage | |
case _ => | |
} | |
} | |
private def readDate = { | |
val dateString = xmlPullParser.nextText | |
val date = dateFormatter.parse(dateString) | |
date.getTime | |
} | |
private def readNextText = xmlPullParser.nextText | |
private def readDescription = { | |
val htmlDescription = readNextText | |
parseImage(htmlDescription) | |
val parsedDescription = Html.fromHtml(htmlDescription) | |
parsedDescription.toString | |
} | |
private def parseImage(htmlDescription: String) = { | |
} | |
private def readImage = { | |
val downloadLink = readNextText | |
// TODO download bitmap and save to file, then pass file id | |
"" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment