summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/util/News.java
blob: 38afe0470147c036428a9aae26bc0ded6e9bfbb1 (plain) (tree)
1
2
3
4
5
6
7





                                      
                              














                                                      









                                                              






                                                





                                                     











                                                    





                                                    










                                               





                                                         










                                            





                                         


                                                   





                                        


                                                  





                                     


                                             
















                                                                
























                                                                                                                        


                                                            



























































































                                                                                                                                                        
package util;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Date;

import javax.swing.JOptionPane;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import models.SessionData;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *	Class representing the news given by the satellite
 *	Makes use of the News-API of the webservice slx-admin:
 *		http://satellite.ip/slx-admin/api.php?do=news
 *
 *	Access the data through the public getters:
 *		String newsHeadline = News.getHeadline();
 *		String newsContent = News.getContent();
 *		Date newsLatestDate = News.getDate();
 */

public class News {

	// Private members representing the news
	private static String headline = null;
	private static String content = null;
	private static Date date = null;

	/**
	 * Gets the headline as String.
	 *
	 * @return String Contains the headline text.
	 */
	public static String getHeadline() {
		if (headline != null)
			return headline;
		else {
			init();
			// check if it is still null
			if (headline != null)
				return headline;
			else
				return null;
		}
	}

	/**
	 * Gets the content as String.
	 *
	 * @return String Contains the content text.
	 */
	public static String getContent() {
		if (content != null)
			return content;
		else {
			init();
			if (content != null)
				return content;
			else
				return null;
		}
	}

	/**
	 * Gets the date of the latest news as Date.
	 *
	 * @return Date Represent last modification date.
	 */
	public static Date getDate() {
		if (date != null)
			return date;
		else {
			init();
			if (date != null)
				return date;
			else
				return null;
		}
	}

	/**
	 * Sets the headline of the news.
	 *
	 * @param s headline as String
	 */
	private static void setHeadline(String s) {
		if (s != null) headline = s;
	}

	/**
	 * Sets the content of the news.
	 *
	 * @param s content as String
	 */
	private static void setContent(String s) {
		if (s != null) content = s;
	}

	/**
	 * Sets the date of the news.
	 *
	 * @param d date as Date
	 */
	private static void setDate(Date d) {
		if (d != null) date = d;
	}

	/**
	 *	Private 'init' function doing the main work.
	 *	It is called by getters if their object is null.
	 *	Follows these steps:
	 *	- Opens URL as stream
	 *	- Parse stream as (XML-)Document
	 *	- Check validity of the news format:
	 *		<news>
	 *			<headline>...</headline>
	 *			<content>...</content>
	 *			<date>...</date>
	 *		</news>
	 *
	 *	- Sets private members
	 */

	private static void init(){
		// Document representing the XML
		Document doc = null;
		
		// URL to news api
		URL url = null;
		try {
			url = new URL("http://" + SessionData.session.getServerAdress() + "/slx-admin/api.php?do=news");
		} catch (MalformedURLException e2) {
			e2.printStackTrace();
			// This shouldn't happen, but in case it does, alert user.
			JOptionPane.showMessageDialog(null,
					"News-URL falsch formatiert!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
		}
		
		// check for argument
		if (url == null) {
			// no URL, alert user.
			JOptionPane.showMessageDialog(null,
					"Keine News-URL angegben!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
			return;
		}
				
		// set timeout for URLConnection
		URLConnection urlCon = url.openConnection();

		InputStream is = null;
		try {
			is = url.openStream();
		} catch (IOException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"Konnte den News-URL-Stream nicht öffnen!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
		}
		
		// use java's DocumentBuilder engine to parse the XML
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"XML-Parser falsch konfiguriert!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
		}
		
		// try the parsing
		try {
			doc = builder.parse(is);
		} catch (SAXException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					"Konnte die News-XML nicht parsen!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					"IO-Fehler!",
					"Fehler", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}

		// now that we have parsed the XML, check if it is valid
		Element docRoot = doc.getDocumentElement();
		
		// first element has to be name "news"
		if (docRoot.getNodeName().equals("news")) {
			Node headlineNode = null;
			Node contentNode = null;
			Node dateNode = null;
			
			// get list of every child
			NodeList nList = docRoot.getElementsByTagName("*");		
			
			// fail if the list is empty
			if (nList == null) return;
			
			// now we go through the list looking for our elements
			// TODO: get definition of tags externally/through static api?
			for (int i = 0; i < nList.getLength(); i++) {
				Node current = nList.item(i);
				if (current.getNodeType() == Node.ELEMENT_NODE) {
					if (current.getNodeName().equals("headline") && 
							current.getFirstChild().getNodeType() == Node.TEXT_NODE) headlineNode = current.getFirstChild();
					if (current.getNodeName().equals("info") &&
							current.getFirstChild().getNodeType() == Node.TEXT_NODE) contentNode = current.getFirstChild();
					if (current.getNodeName().equals("date") &&
							current.getFirstChild().getNodeType() == Node.TEXT_NODE) dateNode = current.getFirstChild();
				}
			}
			
			// set what the elements we found
			if (headlineNode != null) setHeadline(headlineNode.getNodeValue().trim());
			if (contentNode != null)  setContent(contentNode.getNodeValue().trim());
			if (dateNode != null) {
				// for the date we need a bit more stuff
				Date tmpDate = null;
				try {
					tmpDate = new Date(Long.parseLong(dateNode.getNodeValue().trim()));
				} catch (NumberFormatException nfe) {
					nfe.printStackTrace();
					JOptionPane.showMessageDialog(null,
							"Zeitstempel aus der XML is invalid!",
							"Fehler", JOptionPane.ERROR_MESSAGE);
				}
				// Date creation worked, save it
				if (tmpDate != null) setDate(tmpDate);
			}
		} else {
			JOptionPane.showMessageDialog(null,
					"Invalides XML! Kein 'news' Wurzelelement. News wird leer gelassen.",
					"Fehler", JOptionPane.ERROR_MESSAGE);
			return;
		}
	}	
}