DocumentParser.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.gameserver.engines;
  16. import java.io.File;
  17. import java.util.logging.Logger;
  18. import javax.xml.parsers.DocumentBuilder;
  19. import javax.xml.parsers.DocumentBuilderFactory;
  20. import org.w3c.dom.Document;
  21. import org.w3c.dom.NamedNodeMap;
  22. import org.w3c.dom.Node;
  23. import org.xml.sax.ErrorHandler;
  24. import org.xml.sax.SAXParseException;
  25. import com.l2jserver.util.file.filter.XMLFilter;
  26. /**
  27. * Abstract class for XML parsers.<br>
  28. * It's in <i>beta</i> state, so it's expected to change over time.
  29. * @author Zoey76
  30. */
  31. public abstract class DocumentParser
  32. {
  33. protected final Logger _log = Logger.getLogger(getClass().getName());
  34. private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  35. private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  36. private static final XMLFilter xmlFilter = new XMLFilter();
  37. /**
  38. * Parses a single XML file.<br>
  39. * If the file was successfully parsed, call {@link #parseDocument(Document)} for the parsed document.
  40. * Validation is enforced.
  41. * @param f the XML file to parse.
  42. */
  43. protected void parseFile(File f)
  44. {
  45. if (!xmlFilter.accept(f))
  46. {
  47. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " is not a file or it doesn't exist!");
  48. return;
  49. }
  50. final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  51. dbf.setNamespaceAware(true);
  52. dbf.setValidating(true);
  53. dbf.setIgnoringComments(true);
  54. Document doc = null;
  55. try
  56. {
  57. dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
  58. final DocumentBuilder db = dbf.newDocumentBuilder();
  59. db.setErrorHandler(new XMLErrorHandler());
  60. doc = db.parse(f);
  61. }
  62. catch (Exception e)
  63. {
  64. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " file: " + e.getMessage());
  65. return;
  66. }
  67. parseDocument(doc);
  68. }
  69. /**
  70. * Wrapper for {@link #parseDirectory(File)}.
  71. * @param path the path to the directory where the XML files are.
  72. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  73. */
  74. protected boolean parseDirectory(String path)
  75. {
  76. return parseDirectory(new File(path));
  77. }
  78. /**
  79. * Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
  80. * @param dir the directory object to scan.
  81. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  82. */
  83. protected boolean parseDirectory(File dir)
  84. {
  85. if (!dir.exists())
  86. {
  87. _log.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  88. return false;
  89. }
  90. final File[] listOfFiles = dir.listFiles(xmlFilter);
  91. for (File f : listOfFiles)
  92. {
  93. parseFile(f);
  94. }
  95. return true;
  96. }
  97. /**
  98. * Abstract method that when implemented will parse a document.<br>
  99. * Is expected to be call from {@link #parseFile(File)}.
  100. * @param doc the document to parse.
  101. */
  102. protected abstract void parseDocument(Document doc);
  103. /**
  104. * @param n the named node map.
  105. * @param name the attribute name.
  106. * @return a parsed integer.
  107. */
  108. protected static int parseInt(NamedNodeMap n, String name)
  109. {
  110. return Integer.parseInt(n.getNamedItem(name).getNodeValue());
  111. }
  112. /**
  113. * @param n the named node map.
  114. * @param name the attribute name.
  115. * @return a parsed integer object.
  116. */
  117. protected static Integer parseInteger(NamedNodeMap n, String name)
  118. {
  119. return Integer.valueOf(n.getNamedItem(name).getNodeValue());
  120. }
  121. /**
  122. * @param n the node to parse.
  123. * @return the parsed integer.
  124. */
  125. protected static int parseInt(Node n)
  126. {
  127. return Integer.parseInt(n.getNodeValue());
  128. }
  129. /**
  130. * @param n the node to parse.
  131. * @return the parsed integer object.
  132. */
  133. protected static Integer parseInteger(Node n)
  134. {
  135. return Integer.valueOf(n.getNodeValue());
  136. }
  137. /**
  138. * @param n the named node map.
  139. * @param name the attribute name.
  140. * @return a parsed integer.
  141. */
  142. protected static Long parseLong(NamedNodeMap n, String name)
  143. {
  144. return Long.valueOf(n.getNamedItem(name).getNodeValue());
  145. }
  146. /**
  147. * Simple XML error handler.
  148. * @author Zoey76
  149. */
  150. protected class XMLErrorHandler implements ErrorHandler
  151. {
  152. @Override
  153. public void warning(SAXParseException e) throws SAXParseException
  154. {
  155. throw e;
  156. }
  157. @Override
  158. public void error(SAXParseException e) throws SAXParseException
  159. {
  160. throw e;
  161. }
  162. @Override
  163. public void fatalError(SAXParseException e) throws SAXParseException
  164. {
  165. throw e;
  166. }
  167. }
  168. }