You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.0 KiB
131 lines
4.0 KiB
package ru.defend.defdevteam.tstu;
|
|
|
|
import org.xml.sax.Attributes;
|
|
import org.xml.sax.InputSource;
|
|
import org.xml.sax.SAXException;
|
|
import org.xml.sax.helpers.DefaultHandler;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import javax.xml.parsers.SAXParser;
|
|
import javax.xml.parsers.SAXParserFactory;
|
|
|
|
/**
|
|
* Created by thedefend on 29.11.16.
|
|
*/
|
|
|
|
public class FeedParser {
|
|
|
|
ParserCallback callback;
|
|
String inputXML;
|
|
|
|
interface ParserCallback {
|
|
void onSuccessParsing();
|
|
void onFailureParsing();
|
|
void contentDispatcher(String title, String text, String link);
|
|
}
|
|
|
|
void registerCallback(ParserCallback callback) {
|
|
this.callback = callback;
|
|
}
|
|
|
|
FeedParser(String inputXML) {
|
|
this.inputXML = inputXML;
|
|
}
|
|
|
|
// static NewsItemList.Group parsedItem;
|
|
// static void getParsedItem(String title, String description, String link, String date) {
|
|
// parsedItem = new NewsItemList().new Group(title, description, date);
|
|
// this.callback.contentDispatcher();
|
|
// }
|
|
|
|
void parseXML() {
|
|
try {
|
|
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
SAXParser parser = factory.newSAXParser();
|
|
ExtendedHandler handler = new ExtendedHandler();
|
|
InputSource inputSource = new InputSource(new StringReader(inputXML));
|
|
parser.parse(inputSource, handler);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
public String deleteHtmlTag(String Line, String a, String b){
|
|
while(true) {
|
|
if (Line.contains(a) && Line.contains(b)) {
|
|
Line = Line.substring(0, Line.indexOf(a)) +
|
|
Line.substring(Line.indexOf(b) + 1, Line.length());
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
return Line;
|
|
}
|
|
|
|
|
|
public class ExtendedHandler extends DefaultHandler {
|
|
boolean item = false;
|
|
final String title = "title";
|
|
final String description = "description";
|
|
final String link = "link";
|
|
final String date = "date";
|
|
String titleStr = "";
|
|
String descriptionStr = "";
|
|
String linkStr = "";
|
|
String dateStr = "";
|
|
String currentItem = "";
|
|
|
|
void refreshStrings() {
|
|
this.titleStr = "";
|
|
this.descriptionStr = "";
|
|
this.linkStr = "";
|
|
this.dateStr = "";
|
|
}
|
|
|
|
@Override
|
|
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
|
|
currentItem = qName.toLowerCase();
|
|
if (qName.equalsIgnoreCase("item")) {
|
|
item = true;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void characters(char ch[], int start, int length) throws SAXException {
|
|
if(item) {
|
|
switch (currentItem) {
|
|
case title:
|
|
titleStr = titleStr.concat(new String(ch, start, length));
|
|
break;
|
|
case description:
|
|
descriptionStr = descriptionStr.concat(new String(ch, start, length));
|
|
break;
|
|
case link:
|
|
linkStr = linkStr.concat(new String(ch, start, length));
|
|
break;
|
|
case date:
|
|
dateStr = dateStr.concat(new String(ch, start, length));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void endElement(String uri, String localName, String qName) throws SAXException {
|
|
if (qName.equalsIgnoreCase("item") && item) {
|
|
item = false;
|
|
descriptionStr = deleteHtmlTag(descriptionStr, "<", ">");
|
|
callback.contentDispatcher(titleStr, descriptionStr, "");
|
|
refreshStrings();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void endDocument() throws SAXException {
|
|
callback.onSuccessParsing();
|
|
}
|
|
}
|
|
|
|
}
|
|
|