XMLParser.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.util;
  16. import java.io.File;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javax.xml.parsers.DocumentBuilderFactory;
  20. import org.w3c.dom.Document;
  21. /**
  22. * @author UnAfraid
  23. *
  24. */
  25. public abstract class XMLParser
  26. {
  27. private static final Logger _log = Logger.getLogger(XMLParser.class.getName());
  28. private final File _file;
  29. public XMLParser(File f)
  30. {
  31. _file = f;
  32. doParse();
  33. }
  34. public boolean isIgnoringComments()
  35. {
  36. return false;
  37. }
  38. public boolean isValidating()
  39. {
  40. return false;
  41. }
  42. public void doParse()
  43. {
  44. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  45. factory.setValidating(isValidating());
  46. factory.setIgnoringComments(isIgnoringComments());
  47. Document doc = null;
  48. if (getXML().exists())
  49. {
  50. try
  51. {
  52. doc = factory.newDocumentBuilder().parse(getXML());
  53. }
  54. catch (Exception e)
  55. {
  56. _log.log(Level.WARNING, "Could not parse " + getXML().getName() + " file: " + e.getMessage(), e);
  57. return;
  58. }
  59. try
  60. {
  61. parseDoc(doc);
  62. }
  63. catch (Exception e)
  64. {
  65. _log.log(Level.WARNING, "Error while parsing doc: " + e.getMessage(), e);
  66. }
  67. }
  68. else
  69. {
  70. _log.log(Level.WARNING, "Could not found " + getXML().getName() + " file!");
  71. }
  72. }
  73. public File getXML()
  74. {
  75. return _file;
  76. }
  77. public abstract void parseDoc(Document doc);
  78. }