Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.93 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3. import java.net.MalformedURLException;
  4. import java.net.URL;
  5. import java.text.DateFormat;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.time.LocalDateTime;
  9. import java.time.format.DateTimeFormatter;
  10. import java.util.Date;
  11. import java.util.GregorianCalendar;
  12. import java.util.Objects;
  13. import java.util.TimeZone;
  14.  
  15. import javax.imageio.ImageIO;
  16. import javax.swing.ImageIcon;
  17. import javax.xml.parsers.DocumentBuilder;
  18. import javax.xml.parsers.DocumentBuilderFactory;
  19. import javax.xml.parsers.ParserConfigurationException;
  20.  
  21. import org.w3c.dom.Document;
  22. import org.w3c.dom.Element;
  23. import org.w3c.dom.Node;
  24. import org.w3c.dom.NodeList;
  25. import org.xml.sax.SAXException;
  26.  
  27. /**
  28. * MoreInfoPanel
  29. *
  30. * Description:
  31. *
  32. * The purpose of this class is to read from an XML document
  33. * that comes from a URL address that comes from the parameter.
  34. * The class should read through all the pages of the document.
  35. *
  36. * The nodes "scheduledepisode" and "pagination" will be stored in NodeLists.
  37. *
  38. * The information from the most important child nodes will be stored in a 2D-array.
  39. *
  40. * Other nodes will be stored in string variables.
  41. *
  42. *
  43. * @author Johan Hörnblad
  44. * @version 1.0 Date: 2017-08-15
  45. *
  46. */
  47. public class ChannelParse {
  48. NodeList nodes;
  49. NodeList channelNodes;
  50. private Document doc;
  51. private Element root;
  52. private NodeList pageList;
  53. private String urlChannel;
  54. /**
  55. * Parse through a XML document with the parsing method DOM.
  56. * Stores the tags "scheduledepisode" in a NodeList
  57. * and stores the tags "pagination" in a NodeList.
  58. *
  59. * @param urlChannel - a url-address for a program table for a channel.
  60. */
  61. public ChannelParse(String urlChannel) {
  62. this.urlChannel = urlChannel;
  63. URL url;
  64. try {
  65. url = new URL(urlChannel);
  66. // Create a documentBuilder
  67. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  68. DocumentBuilder db;
  69. db = dbf.newDocumentBuilder();
  70. // create a document from stream
  71. doc = db.parse(url.openStream());
  72.  
  73. // extract the root element
  74. root = doc.getDocumentElement();
  75. root.normalize();
  76.  
  77. nodes = doc.getElementsByTagName("scheduledepisode");
  78. pageList = doc.getElementsByTagName("pagination");
  79. channelNodes = doc.getElementsByTagName("Channel");
  80. } catch (SAXException | IOException | ParserConfigurationException e) {
  81. System.out.println("Could not parse the XML document");
  82. }
  83.  
  84. }
  85.  
  86.  
  87. /**
  88. * Stores information in to a 2D-array from the XML document.
  89. *
  90. *Store the title,start time, end time and stores the information,
  91. *if a program has finished or not.
  92. *
  93. * @return a 2D-array.
  94. */
  95. public Object[][] getProgramSchedule() {
  96. int row;
  97. int nuOfPages = numberOfPages();
  98. int arraySize = checkArraySize();
  99. Object[][] images = new Object[arraySize][5];
  100. for (int col = 0; col < 5; col++) {
  101. row = 0;
  102. for (int page = 1; page < nuOfPages + 1; page++) {
  103. for (int index = 0; index < nodes.getLength(); index++) {
  104. Node nNode = nodes.item(index);
  105.  
  106. switch (col) {
  107.  
  108. case 0:
  109. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  110. Element eElement = (Element) nNode;
  111. String program = eElement.getElementsByTagName("title").item(0).getTextContent();
  112. images[row][col] = program;
  113. }
  114. break;
  115.  
  116. case 1:
  117. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  118. Element eElement = (Element) nNode;
  119. String timeString = eElement.getElementsByTagName("starttimeutc").item(0).getTextContent();
  120. LocalDateTime startTime = LocalDateTime.parse(timeString, DateTimeFormatter.ISO_DATE_TIME);
  121. String dateNTime = (startTime.toLocalTime().plusHours(2).toString());
  122. images[row][col] = dateNTime;
  123. }
  124. break;
  125.  
  126. case 2:
  127.  
  128. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  129. Element eElement = (Element) nNode;
  130. String timeString = eElement.getElementsByTagName("endtimeutc").item(0).getTextContent();
  131. LocalDateTime endTime = LocalDateTime.parse(timeString, DateTimeFormatter.ISO_DATE_TIME);
  132. String dateNTime = (endTime.toLocalTime().plusHours(2).toString());
  133. images[row][col] = dateNTime;
  134. }
  135. break;
  136.  
  137. case 3:
  138. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  139. Element eElement = (Element) nNode;
  140. String timeString = eElement.getElementsByTagName("endtimeutc").item(0).getTextContent();
  141. TimeZone utc = TimeZone.getTimeZone("UTC");
  142. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  143. simpleDateFormat.setTimeZone(utc);
  144. GregorianCalendar cal = new GregorianCalendar(utc);
  145. GregorianCalendar cal2 = new GregorianCalendar(utc);
  146.  
  147. try {
  148. cal.setTime(simpleDateFormat.parse(timeString));
  149. cal2.setTime(new Date());
  150. } catch (ParseException e) {
  151. System.out.println("The time format is not correct");
  152. }
  153. if (cal2.after(cal)) {
  154. images[row][col] = "avslutad";
  155. } else {
  156. images[row][col] = " ";
  157. }
  158. }
  159.  
  160. break;
  161. case 4:
  162. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  163. Element eElement = (Element) nNode;
  164. String program = eElement.getElementsByTagName("title").item(0).getTextContent();
  165. images[row][col] = program;
  166. }
  167. break;
  168. }
  169. row++;
  170. }
  171. nextPage();
  172. }
  173. reset();
  174. }
  175. reset();
  176. return images;
  177. }
  178.  
  179.  
  180. /**
  181. * Stores the description of a program with the title from the parameter.
  182. * @param name - the name of a program.
  183. * @return - a string that is the description of the program.
  184. */
  185. public String getDescription(String name) {
  186. int nuOfPages = numberOfPages();
  187. String description = null;
  188. for (int page = 1; page < nuOfPages + 1; page++) {
  189. for (int temp = 0; temp < nodes.getLength(); temp++) {
  190.  
  191. Node nNode = nodes.item(temp);
  192. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  193.  
  194. Element eElement = (Element) nNode;
  195. if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
  196. try {
  197. description = eElement.getElementsByTagName("description").item(0).getTextContent();
  198. } catch (NullPointerException e) {
  199. description = "No Description!";
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. nextPage();
  206. }
  207. reset();
  208. return description;
  209. }
  210.  
  211. /**
  212. * Stores the image URL address of a program with the title from the parameter.
  213. *
  214. * @param name - the name of a program.
  215. * @return - a string that is the image URL address of the program.
  216. */
  217. public String getIm(String name) {
  218. int nuOfPages = numberOfPages();
  219. String imageUrl = null;
  220. for (int page = 1; page < nuOfPages + 1; page++) {
  221. for (int temp = 0; temp < nodes.getLength(); temp++) {
  222.  
  223. Node nNode = nodes.item(temp);
  224. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  225. Element eElement = (Element) nNode;
  226. if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
  227. try {
  228. imageUrl = eElement.getElementsByTagName("imageurl").item(0).getTextContent();
  229. } catch (NullPointerException e) {
  230. imageUrl = "noImage";
  231. }
  232. break;
  233. }
  234. }
  235. }
  236. nextPage();
  237. }
  238. reset();
  239. return imageUrl;
  240. }
  241.  
  242. /**
  243. * Stores the sub title of a program with the title from the parameter.
  244. *
  245. * @param name - the name of a program.
  246. * @return - a string that is the sub title of the program.
  247. */
  248. public String getSubTitle(String name) {
  249. int nuOfPages = numberOfPages();
  250. String subTitle = null;
  251. for (int page = 1; page < nuOfPages + 1; page++) {
  252. for (int temp = 0; temp < nodes.getLength(); temp++) {
  253.  
  254. Node nNode = nodes.item(temp);
  255. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  256. Element eElement = (Element) nNode;
  257. if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
  258. try {
  259. subTitle = eElement.getElementsByTagName("subtitle").item(0).getTextContent();
  260. } catch (NullPointerException e) {
  261. subTitle = " ";
  262. }
  263. break;
  264. }
  265. }
  266. }
  267. nextPage();
  268. }
  269. reset();
  270. return subTitle;
  271. }
  272. /**
  273. * Stores the title of a program with the title from the parameter.
  274. *
  275. * @param name - the name of a program.
  276. * @return - a string that is the title of the program.
  277. */
  278. public String getTitle(String name) {
  279. Node pageNode = pageList.item(0);
  280. String pages = null;
  281. if (pageNode.getNodeType() == Node.ELEMENT_NODE) {
  282. Element eElement = (Element) pageNode;
  283. pages = eElement.getElementsByTagName("totalpages").item(0).getTextContent();
  284. }
  285. int nuOfPages = Integer.parseInt(pages);
  286. String title = null;
  287. for (int page = 1; page < nuOfPages + 1; page++) {
  288. for (int temp = 0; temp < nodes.getLength(); temp++) {
  289.  
  290. Node nNode = nodes.item(temp);
  291. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  292. Element eElement = (Element) nNode;
  293. if (Objects.equals(eElement.getElementsByTagName("title").item(0).getTextContent(), name)) {
  294. title = eElement.getElementsByTagName("title").item(0).getTextContent();
  295. break;
  296. }
  297. }
  298. }
  299. nextPage();
  300. }
  301. reset();
  302. return title;
  303. }
  304. /**
  305. * Parse the next page of the XML document with the parsing method DOM.
  306. * Nodes from the next page stores in the NodeList instead of previous the nodes.
  307. *
  308. */
  309. public void nextPage() {
  310. boolean hasNoPagesLeft = false;
  311. Node nNode = pageList.item(0);
  312. String nextPageUrl = null;
  313. if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  314. Element eElement = (Element) nNode;
  315. try {
  316. nextPageUrl = eElement.getElementsByTagName("nextpage").item(0).getTextContent();
  317. } catch (NullPointerException e) {
  318. hasNoPagesLeft = true;
  319. }
  320. }
  321. if (!hasNoPagesLeft) {
  322. URL url = null;
  323. try {
  324. url = new URL(nextPageUrl);
  325. } catch (MalformedURLException e) {
  326. // TODO Auto-generated catch block
  327. e.printStackTrace();
  328. }
  329. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  330. DocumentBuilder db = null;
  331. try {
  332. db = dbf.newDocumentBuilder();
  333. } catch (ParserConfigurationException e) {
  334. // TODO Auto-generated catch block
  335. e.printStackTrace();
  336. }
  337. // create a document from stream
  338. try {
  339. doc = db.parse(url.openStream());
  340. } catch (SAXException | IOException e) {
  341. // TODO Auto-generated catch block
  342. e.printStackTrace();
  343. }
  344. // extract the root element
  345. root = doc.getDocumentElement();
  346. root.normalize();
  347. nodes = doc.getElementsByTagName("scheduledepisode");
  348. pageList = doc.getElementsByTagName("pagination");
  349. }
  350.  
  351. }
  352.  
  353. /**
  354. * Counting the number of pages of the XML document.
  355. * @return - the number of pages.
  356. */
  357. public int numberOfPages() {
  358. Node pageNode = pageList.item(0);
  359. String pages = null;
  360. if (pageNode.getNodeType() == Node.ELEMENT_NODE) {
  361. Element eElement = (Element) pageNode;
  362. pages = eElement.getElementsByTagName("totalpages").item(0).getTextContent();
  363. }
  364. int nuOfPages = Integer.parseInt(pages);
  365. return nuOfPages;
  366. }
  367.  
  368. /**
  369. * Resets the XMl Document to the first page.
  370. * Nodes from this page stores in the NodeLists
  371. * instead of the previous nodes.
  372. */
  373. public void reset() {
  374. URL url = null;
  375. try {
  376. url = new URL(urlChannel);
  377. } catch (MalformedURLException e) {
  378. // TODO Auto-generated catch block
  379. e.printStackTrace();
  380. }
  381. // Create a documentBuilder
  382. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  383. DocumentBuilder db = null;
  384. try {
  385. db = dbf.newDocumentBuilder();
  386. } catch (ParserConfigurationException e) {
  387. // TODO Auto-generated catch block
  388. e.printStackTrace();
  389. }
  390. // create a document from stream
  391. try {
  392. doc = db.parse(url.openStream());
  393. } catch (SAXException | IOException e) {
  394. // TODO Auto-generated catch block
  395. e.printStackTrace();
  396. }
  397.  
  398. // extract the root element
  399. root = doc.getDocumentElement();
  400. root.normalize();
  401. nodes = doc.getElementsByTagName("scheduledepisode");
  402. pageList = doc.getElementsByTagName("pagination");
  403. }
  404.  
  405. /**
  406. * Checks how many "scheduledepisode" nodes there is in the document.
  407. * @return - the number of nodes.
  408. */
  409. private int checkArraySize() {
  410. int nuOfPages = numberOfPages();
  411. int counter = 0;
  412. for (int page = 1; page < nuOfPages + 1; page++) {
  413. for (int index = 0; index < nodes.getLength(); index++) {
  414. counter++;
  415. }
  416. nextPage();
  417. }
  418. reset();
  419. return counter;
  420. }
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement