Siemano! Napisałem czytnik RSS który działał bo działał. Ktoś mi polecił, żebym wszystkie case'y przeniósł do osobnej klasy no i tak zrobiłem. Tylko, że skończyło się tak, że jako wynik drukowania otrzymuje "null" a konsola zostaje cały czas otwarta i mogę po niej pisać do woli. Nie mam pojęcia dlaczego tak się dzieje. Pętla while w klasie *Case * wykonuje się w nieskończoność, a przecież eventy pobierane ze strony się kończą.
public class ReadTest {
private static Scanner scanner = new Scanner(System.in);
private static RSSList rssList = new RSSList();
public static String url;
public static String righturl;
public static void main(String[] args) throws XMLStreamException, IOException, ClassNotFoundException {
MENU();
}
public static void MENU() throws IOException, ClassNotFoundException {
int choice;
System.out.println("MENU:");
System.out.println(" 1 = Print RSS");
System.out.println(" 2 = Show RSS List");
System.out.println(" 3 = Add new RSS URL");
System.out.println(" 4 = Delete RSS");
System.out.println(" 5 = Print History");
System.out.println("5 = EXIT");
choice = scanner.nextInt();
switch (choice) {
case 1:
rssList.readCurrentlyList();
printRSS();
MENU();
break;
case 2:
rssList.readCurrentlyList();
printList();
MENU();
break;
case 3:
rssList.readCurrentlyList();
rssList.readHistory();
feedURL();
checkCurrentyList();
addRSS();
rssList.saveCurrentlyList();
MENU();
break;
case 4:
rssList.readCurrentlyList();
System.out.println("Ktory link chcesz usunac?");
printList();
deleteIndex();
rssList.saveCurrentlyList();
MENU();
break;
case 5:
rssList.readHistory();
readHistory();
MENU();
break;
}
}
public static void printRSS() {
for (int i = 0; i < rssList.RSSList.size(); i++) {
righturl = rssList.RSSList.get(i);
RSSFeedParser parser = new RSSFeedParser(righturl);
Feed feed = parser.readFeed();
System.out.println(feed);
for (FeedMessage message : feed.getMessages()) {
System.out.println("nie");
System.out.println(message);
}
}
}
public static void printList(){
for(int i = 0; i<rssList.RSSList.size(); i++){
System.out.println(i+1 +". "+rssList.RSSList.get(i));
}
}
public static void deleteIndex(){
int index = scanner.nextInt();
rssList.RSSList.remove(index-1);
}
public static void feedURL(){
scanner.nextLine();
url = scanner.nextLine();
}
public static void addToHistory(){
boolean thereAlreadyIs = false;
for(int i = 0; i<rssList.RSSHistory.size(); i++){
if(rssList.RSSHistory.get(i).equals(url)){
thereAlreadyIs = true;
}
}
if(thereAlreadyIs==false){
rssList.RSSHistory.add(url);
}
}
public static void checkCurrentyList() throws IOException, ClassNotFoundException {
boolean thereAlreadyIs = false;
rssList.readCurrentlyList();
for (int i = 0; i < rssList.RSSList.size(); i++) {
if (url.equals(rssList.RSSList.get(i))) {
thereAlreadyIs = true;
}
}
if (thereAlreadyIs == true) {
System.out.println("Juz jest dodany RSS o takim adresie!");
}
else{
addRSS();
}
}
public static void addRSS(){
rssList.RSSList.add(url);
addToHistory();
}
public static void readHistory() throws IOException, ClassNotFoundException {
rssList.readHistory();
for(int i =0; i<rssList.RSSHistory.size(); i++){
System.out.println("1. "+rssList.RSSHistory.get(i));
}
}
}
public class RSSFeedParser {
public static String ITEM = "";
public static String description = "";
public static String title = "";
public static String link = "";
public static String language = "";
public static String copyright = "";
public static String author = "";
public static String pubDate = "";
public static String guid = "";
public static String localPart = "";
public Feed feed;
public InputStream in;
public XMLInputFactory inputFactory = XMLInputFactory.newInstance();
public XMLEventReader eventReader;
public XMLEvent event;
final URL url;
public RSSFeedParser(){
url = null;
in = null;
eventReader = null;
}
public RSSFeedParser(String feedUrl) {
try {
url = new URL(feedUrl);
in = read();
eventReader = inputFactory.createXMLEventReader(in);
read().close();
event = eventReader.nextEvent();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Feed readFeed() {
try {
while (eventReader.hasNext()) {
if (event.isStartElement()) {
localPart = event.asStartElement().getName().getLocalPart();
}
try {
Case.setCases();
}catch (ExceptionInInitializerError e){
System.out.println(e.toString());
System.out.println("______________________________________________");
System.out.println(e.getCause());
}
}
} catch (XMLStreamException e) {
throw new RuntimeException(e);
}
return feed;
}
public Feed setAllElements() throws XMLStreamException {
FeedMessage message = new FeedMessage();
message.setAuthor(author);
message.setDescription(description);
message.setGuid(guid);
message.setLink(link);
message.setTitle(title);
feed.getMessages().add(message);
return feed;
}
private InputStream read() throws XMLStreamException {
try {
return url.openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String getCharacterData(XMLEvent event, XMLEventReader eventReader) throws XMLStreamException {
String results = "";
event = eventReader.nextEvent();
if (event instanceof Characters) {
results = event.asCharacters().getData();
}
return results;
}
public class Case extends RSSFeedParser {
public static ReadTest readTest = new ReadTest();
public static RSSFeedParser rss = new RSSFeedParser(readTest.righturl);
private static final String TITLE = "title";
private static final String DESCRIPTION = "description";
private static final String LANGUAGE = "language";
private static final String COPYRIGHT = "copyright";
private static final String LINK = "link";
private static final String AUTHOR = "author";
private static final String PUB_DATE = "pubDate";
private static final String GUID = "guid";
private static final String ITEM = "item";
public static void setCases() throws XMLStreamException {
rss.event = rss.eventReader.nextEvent();
boolean isFeedHeader = true;
while (rss.eventReader.hasNext()) {
switch (localPart) {
case ITEM:
if (isFeedHeader) {
isFeedHeader = false;
rss.feed = new Feed(title, link, description, language, copyright, pubDate);
}
rss.event = rss.eventReader.nextEvent();
break;
case TITLE:
rss.title = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case DESCRIPTION:
rss.description = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case LINK:
rss.link = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case GUID:
guid = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case LANGUAGE:
language = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case AUTHOR:
author = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case PUB_DATE:
pubDate = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
case COPYRIGHT:
copyright = getCharacterData(rss.event, rss.eventReader);
rss.eventReader.close();
break;
}
rss.eventReader.close();
if (rss.event.isEndElement()) {
if (rss.event.asEndElement().getName().getLocalPart() == (ITEM)) {
rss.setAllElements();
}
}
}
}
A null siedzi tutaj:
Feed feed = parser.readFeed();
System.out.println(feed);
Tylko dlaczego?