Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package skuska_sax;
  7.  
  8. import org.xml.sax.Attributes;
  9. import org.xml.sax.SAXException;
  10. import org.xml.sax.helpers.DefaultHandler;
  11.  
  12. /**
  13. *
  14. * @author MartinŠtekláč
  15. */
  16. public class Skuska_handler extends DefaultHandler {
  17.  
  18.  
  19.  
  20. boolean bKniha = false;
  21. boolean bMenoKnihy = false;
  22. boolean bMenoKnihkupectva = false;
  23. boolean bCena = false;
  24. boolean bKhihkupectvo = false;
  25. boolean bAutor = false;
  26.  
  27. String edicia;
  28. String cena_knihy;
  29. String meno_knihy;
  30. Double cena_double = 0d;
  31. Double najdrahsia_kniha = 0d;
  32. String nazov_najdrahsia_kniha;
  33. boolean kniha_v_edicii_stopy = false;
  34.  
  35.  
  36.  
  37. boolean edicia_stopy_je_v_dokumente = false;
  38.  
  39. @Override
  40. public void characters(char[] ch, int start, int length) throws SAXException {
  41. if (bCena){
  42. cena_knihy = new String(ch, start, length);
  43. // bud pouzivat tuto podmienku alebo nulovat dole v end elemente knihy, aby sa nestalo, ze
  44. // sa necha ulozena hodnota zo starej knihy, nasledujuca kniha je z edicie stopy ale nema cenu
  45. // a tak vypise ulozenu hodnotu zo zlej knihy
  46. // if ("Stopy".equalsIgnoreCase(edicia))
  47. cena_double = Double.parseDouble(cena_knihy);
  48. }
  49.  
  50. if (bMenoKnihy){
  51. meno_knihy = new String(ch, start, length);
  52.  
  53. }
  54. }
  55.  
  56.  
  57. @Override
  58. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  59. if (localName.equalsIgnoreCase("khihkupectvo")){
  60. bKhihkupectvo = true;
  61. }
  62. if (localName.equalsIgnoreCase("kniha")){
  63. edicia = attributes.getValue("edicia");
  64. bKniha = true;
  65. }
  66. if (localName.equalsIgnoreCase("meno")){
  67. if (bKniha) bMenoKnihy = true;
  68. else bMenoKnihkupectva = true;
  69. }
  70. if (localName.equalsIgnoreCase("autor")){
  71. bAutor = true;
  72. }
  73. if (localName.equalsIgnoreCase("cena")){
  74. bCena = true;
  75. }
  76.  
  77. }
  78.  
  79.  
  80. @Override
  81. public void endElement(String uri, String localName, String qName) throws SAXException {
  82. if (localName.equalsIgnoreCase("knihkupectvo")){
  83. if (kniha_v_edicii_stopy) System.out.println(najdrahsia_kniha + " " + nazov_najdrahsia_kniha );
  84. else System.out.println("neexistuje");
  85. bKhihkupectvo = false;
  86. }
  87. if (localName.equalsIgnoreCase("kniha")){
  88. if("Stopy".equalsIgnoreCase(edicia)){
  89. if (najdrahsia_kniha < cena_double){
  90. najdrahsia_kniha = cena_double;
  91. nazov_najdrahsia_kniha = meno_knihy;
  92. kniha_v_edicii_stopy = true;
  93. }
  94. }
  95. bKniha = false;
  96. // bud tuna vynulovat alebo hore dat podmienku ze pridavat cenu len ak sme v edicii stopy
  97. cena_double = 0d;
  98.  
  99. }
  100. if (localName.equalsIgnoreCase("meno")){
  101. if (bKniha) bMenoKnihy = false;
  102. else bMenoKnihkupectva = false;
  103. }
  104. if (localName.equalsIgnoreCase("autor")){
  105. bAutor = false;
  106. }
  107. if (localName.equalsIgnoreCase("cena")){
  108. bCena = false;
  109. }
  110. }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement