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.
144 lines
4.4 KiB
144 lines
4.4 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(NewsItemGroup group);
|
|
}
|
|
|
|
void registerCallback(ParserCallback callback) {
|
|
this.callback = callback;
|
|
}
|
|
|
|
FeedParser() {}
|
|
|
|
|
|
void parseXML(String inputXML) {
|
|
try {
|
|
this.inputXML = inputXML;
|
|
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 String getImageLink(String Line){
|
|
String a = "<img src=\"";
|
|
String b = "\" border=\"0\"";
|
|
Line = Line.substring(Line.indexOf(a)+ a.length());
|
|
Line = Line.substring(0, Line.indexOf(b));
|
|
return Line;
|
|
}
|
|
|
|
public String getDescriptionShort(String Line){
|
|
String a = "<div class=\"K2FeedIntroText\">";
|
|
String b = "</div>";
|
|
Line = Line.substring(Line.indexOf(a)+ a.length());
|
|
Line = Line.substring(0, Line.indexOf(b));
|
|
return deleteHtmlTag(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;
|
|
String imageLink = getImageLink(descriptionStr);
|
|
String shortDescriptionStr = getDescriptionShort(descriptionStr);
|
|
NewsItemGroup group = new NewsItemGroup(titleStr, shortDescriptionStr, imageLink);
|
|
group.setFullText(deleteHtmlTag(descriptionStr, "<", ">"));
|
|
callback.contentDispatcher(group);
|
|
refreshStrings();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void endDocument() throws SAXException {
|
|
callback.onSuccessParsing();
|
|
}
|
|
}
|
|
|
|
}
|
|
|