|
|
@ -1,19 +1,12 @@ |
|
|
|
package ru.defend.defdevteam.tstu; |
|
|
|
|
|
|
|
import org.w3c.dom.Document; |
|
|
|
import org.w3c.dom.Node; |
|
|
|
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 java.io.StringReader; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Collection; |
|
|
|
|
|
|
|
import javax.xml.parsers.DocumentBuilder; |
|
|
|
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
|
import javax.xml.parsers.SAXParser; |
|
|
|
import javax.xml.parsers.SAXParserFactory; |
|
|
|
|
|
|
@ -59,79 +52,79 @@ public class FeedParser { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
boolean title = false; |
|
|
|
boolean description = false; |
|
|
|
boolean link = false; |
|
|
|
boolean date = false; |
|
|
|
String titleStr; |
|
|
|
String descriptionStr; |
|
|
|
String linkStr; |
|
|
|
String dateStr; |
|
|
|
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; |
|
|
|
} |
|
|
|
if (item) { |
|
|
|
String tagName = qName.toLowerCase(); |
|
|
|
switch (tagName) { |
|
|
|
case "title": title = true; |
|
|
|
break; |
|
|
|
case "description": description = true; |
|
|
|
break; |
|
|
|
case "link": link = true; |
|
|
|
break; |
|
|
|
case "date": date = true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void characters(char ch[], int start, int length) throws SAXException { |
|
|
|
if(item) { |
|
|
|
if (title) { |
|
|
|
titleStr = new String(ch, start, length); |
|
|
|
title = false; |
|
|
|
} else if (description) { |
|
|
|
descriptionStr = new String(ch, start, length); |
|
|
|
description = false; |
|
|
|
} else if (link) { |
|
|
|
linkStr = new String(ch, start, length); |
|
|
|
link = false; |
|
|
|
} else if (date) { |
|
|
|
dateStr = new String(ch, start, length); |
|
|
|
date = false; |
|
|
|
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 (title) { |
|
|
|
title = false; |
|
|
|
return; |
|
|
|
} else if (description) { |
|
|
|
description = false; |
|
|
|
return; |
|
|
|
} else if (link) { |
|
|
|
link = false; |
|
|
|
return; |
|
|
|
} else if (date) { |
|
|
|
date = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
if (item) { |
|
|
|
if (qName.equalsIgnoreCase("item") && item) { |
|
|
|
item = false; |
|
|
|
descriptionStr = deleteHtmlTag(descriptionStr, "<", ">"); |
|
|
|
callback.contentDispatcher(titleStr, descriptionStr, ""); |
|
|
|
refreshStrings(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void endDocument() throws SAXException { |
|
|
|
callback.onSuccessParsing(); |
|
|
|
callback.onSuccessParsing(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|