Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Objects;
- import java.util.TimeZone;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- 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;
- /**
- * MoreInfoPanel
- *
- * Description:
- *
- * The purpose of this class is to read from an XML document
- * that comes from a URL address that comes from the parameter.
- * The class should read through all the pages of the document.
- *
- * The nodes "scheduledepisode" and "pagination" will be stored in NodeLists.
- *
- * The information from the most important child nodes will be stored in a 2D-array.
- *
- * Other nodes will be stored in string variables.
- *
- *
- * @author Johan Hörnblad
- * @version 1.0 Date: 2017-08-15
- *
- */
- public class ChannelParse {
- NodeList nodes;
- NodeList channelNodes;
- private Document doc;
- private Element root;
- private NodeList pageList;
- private String urlChannel;
- /**
- * Parse through a XML document with the parsing method DOM.
- * Stores the tags "scheduledepisode" in a NodeList
- * and stores the tags "pagination" in a NodeList.
- *
- * @param urlChannel - a url-address for a program table for a channel.
- */
- public ChannelParse(String urlChannel) {
- this.urlChannel = urlChannel;
- URL url;
- try {
- url = new URL(urlChannel);
- // Create a documentBuilder
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db;
- db = dbf.newDocumentBuilder();
- // create a document from stream
- doc = db.parse(url.openStream());
- // extract the root element
- root = doc.getDocumentElement();
- root.normalize();
- nodes = doc.getElementsByTagName("scheduledepisode");
- pageList = doc.getElementsByTagName("pagination");
- channelNodes = doc.getElementsByTagName("Channel");
- } catch (SAXException | IOException | ParserConfigurationException e) {
- System.out.println("Could not parse the XML document");
- }
- }
- /**
- * Stores information in to a 2D-array from the XML document.
- *
- *Store the title,start time, end time and stores the information,
- *if a program has finished or not.
- *
- * @return a 2D-array.
- */
- public Object[][] getProgramSchedule() {
- int row;
- int nuOfPages = numberOfPages();
- int arraySize = checkArraySize();
- Object[][] images = new Object[arraySize][5];
- for (int col = 0; col < 5; col++) {
- row = 0;
- for (int page = 1; page < nuOfPages + 1; page++) {
- for (int index = 0; index < nodes.getLength(); index++) {
- Node nNode = nodes.item(index);
- switch (col) {
- case 0:
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- String program = eElement.getElementsByTagName("title").item(0).getTextContent();
- images[row][col] = program;
- }
- break;
- case 1:
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- String timeString = eElement.getElementsByTagName("starttimeutc").item(0).getTextContent();
- LocalDateTime startTime = LocalDateTime.parse(timeString, DateTimeFormatter.ISO_DATE_TIME);
- String dateNTime = (startTime.toLocalTime().plusHours(2).toString());
- images[row][col] = dateNTime;
- }
- break;
- case 2:
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- String timeString = eElement.getElementsByTagName("endtimeutc").item(0).getTextContent();
- LocalDateTime endTime = LocalDateTime.parse(timeString, DateTimeFormatter.ISO_DATE_TIME);
- String dateNTime = (endTime.toLocalTime().plusHours(2).toString());
- images[row][col] = dateNTime;
- }
- break;
- case 3:
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- String timeString = eElement.getElementsByTagName("endtimeutc").item(0).getTextContent();
- TimeZone utc = TimeZone.getTimeZone("UTC");
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
- simpleDateFormat.setTimeZone(utc);
- GregorianCalendar cal = new GregorianCalendar(utc);
- GregorianCalendar cal2 = new GregorianCalendar(utc);
- try {
- cal.setTime(simpleDateFormat.parse(timeString));
- cal2.setTime(new Date());
- } catch (ParseException e) {
- System.out.println("The time format is not correct");
- }
- if (cal2.after(cal)) {
- images[row][col] = "avslutad";
- } else {
- images[row][col] = " ";
- }
- }
- break;
- case 4:
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- String program = eElement.getElementsByTagName("title").item(0).getTextContent();
- images[row][col] = program;
- }
- break;
- }
- row++;
- }
- nextPage();
- }
- reset();
- }
- reset();
- return images;
- }
- /**
- * Stores the description of a program with the title from the parameter.
- * @param name - the name of a program.
- * @return - a string that is the description of the program.
- */
- public String getDescription(String name) {
- int nuOfPages = numberOfPages();
- String description = null;
- for (int page = 1; page < nuOfPages + 1; page++) {
- for (int temp = 0; temp < nodes.getLength(); temp++) {
- Node nNode = nodes.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
- try {
- description = eElement.getElementsByTagName("description").item(0).getTextContent();
- } catch (NullPointerException e) {
- description = "No Description!";
- }
- break;
- }
- }
- }
- nextPage();
- }
- reset();
- return description;
- }
- /**
- * Stores the image URL address of a program with the title from the parameter.
- *
- * @param name - the name of a program.
- * @return - a string that is the image URL address of the program.
- */
- public String getIm(String name) {
- int nuOfPages = numberOfPages();
- String imageUrl = null;
- for (int page = 1; page < nuOfPages + 1; page++) {
- for (int temp = 0; temp < nodes.getLength(); temp++) {
- Node nNode = nodes.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
- try {
- imageUrl = eElement.getElementsByTagName("imageurl").item(0).getTextContent();
- } catch (NullPointerException e) {
- imageUrl = "noImage";
- }
- break;
- }
- }
- }
- nextPage();
- }
- reset();
- return imageUrl;
- }
- /**
- * Stores the sub title of a program with the title from the parameter.
- *
- * @param name - the name of a program.
- * @return - a string that is the sub title of the program.
- */
- public String getSubTitle(String name) {
- int nuOfPages = numberOfPages();
- String subTitle = null;
- for (int page = 1; page < nuOfPages + 1; page++) {
- for (int temp = 0; temp < nodes.getLength(); temp++) {
- Node nNode = nodes.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
- try {
- subTitle = eElement.getElementsByTagName("subtitle").item(0).getTextContent();
- } catch (NullPointerException e) {
- subTitle = " ";
- }
- break;
- }
- }
- }
- nextPage();
- }
- reset();
- return subTitle;
- }
- /**
- * Stores the title of a program with the title from the parameter.
- *
- * @param name - the name of a program.
- * @return - a string that is the title of the program.
- */
- public String getTitle(String name) {
- Node pageNode = pageList.item(0);
- String pages = null;
- if (pageNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) pageNode;
- pages = eElement.getElementsByTagName("totalpages").item(0).getTextContent();
- }
- int nuOfPages = Integer.parseInt(pages);
- String title = null;
- for (int page = 1; page < nuOfPages + 1; page++) {
- for (int temp = 0; temp < nodes.getLength(); temp++) {
- Node nNode = nodes.item(temp);
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
- title = eElement.getElementsByTagName("title").item(0).getTextContent();
- break;
- }
- }
- }
- nextPage();
- }
- reset();
- return title;
- }
- /**
- * Parse the next page of the XML document with the parsing method DOM.
- * Nodes from the next page stores in the NodeList instead of previous the nodes.
- *
- */
- public void nextPage() {
- boolean hasNoPagesLeft = false;
- Node nNode = pageList.item(0);
- String nextPageUrl = null;
- if (nNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) nNode;
- try {
- nextPageUrl = eElement.getElementsByTagName("nextpage").item(0).getTextContent();
- } catch (NullPointerException e) {
- hasNoPagesLeft = true;
- }
- }
- if (!hasNoPagesLeft) {
- URL url = null;
- try {
- url = new URL(nextPageUrl);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = null;
- try {
- db = dbf.newDocumentBuilder();
- } catch (ParserConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // create a document from stream
- try {
- doc = db.parse(url.openStream());
- } catch (SAXException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // extract the root element
- root = doc.getDocumentElement();
- root.normalize();
- nodes = doc.getElementsByTagName("scheduledepisode");
- pageList = doc.getElementsByTagName("pagination");
- }
- }
- /**
- * Counting the number of pages of the XML document.
- * @return - the number of pages.
- */
- public int numberOfPages() {
- Node pageNode = pageList.item(0);
- String pages = null;
- if (pageNode.getNodeType() == Node.ELEMENT_NODE) {
- Element eElement = (Element) pageNode;
- pages = eElement.getElementsByTagName("totalpages").item(0).getTextContent();
- }
- int nuOfPages = Integer.parseInt(pages);
- return nuOfPages;
- }
- /**
- * Resets the XMl Document to the first page.
- * Nodes from this page stores in the NodeLists
- * instead of the previous nodes.
- */
- public void reset() {
- URL url = null;
- try {
- url = new URL(urlChannel);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // Create a documentBuilder
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- DocumentBuilder db = null;
- try {
- db = dbf.newDocumentBuilder();
- } catch (ParserConfigurationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // create a document from stream
- try {
- doc = db.parse(url.openStream());
- } catch (SAXException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // extract the root element
- root = doc.getDocumentElement();
- root.normalize();
- nodes = doc.getElementsByTagName("scheduledepisode");
- pageList = doc.getElementsByTagName("pagination");
- }
- /**
- * Checks how many "scheduledepisode" nodes there is in the document.
- * @return - the number of nodes.
- */
- private int checkArraySize() {
- int nuOfPages = numberOfPages();
- int counter = 0;
- for (int page = 1; page < nuOfPages + 1; page++) {
- for (int index = 0; index < nodes.getLength(); index++) {
- counter++;
- }
- nextPage();
- }
- reset();
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement