DocumentParser.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.Config;
  26. import com.l2jserver.util.file.filter.XMLFilter;
  27. /**
  28. * Abstract class for XML parsers.<br>
  29. * It's in <i>beta</i> state, so it's expected to change over time.
  30. * @author Zoey76
  31. */
  32. public abstract class DocumentParser
  33. {
  34. protected final Logger _log = Logger.getLogger(getClass().getName());
  35. private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  36. private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  37. private static final XMLFilter xmlFilter = new XMLFilter();
  38. private File _currentFile;
  39. private Document _currentDocument;
  40. /**
  41. * This method can be used to load/reload the data.<br>
  42. * It's highly recommended to clear the data storage, either the list or map.
  43. */
  44. public abstract void load();
  45. /**
  46. * Wrapper for {@link #parseFile(File)} method.
  47. * @param path the relative path to the datapack root of the XML file to parse.
  48. */
  49. protected void parseDatapackFile(String path)
  50. {
  51. parseFile(new File(Config.DATAPACK_ROOT, path));
  52. }
  53. /**
  54. * Parses a single XML file.<br>
  55. * If the file was successfully parsed, call {@link #parseDocument(Document)} for the parsed document.<br>
  56. * <b>Validation is enforced.</b>
  57. * @param f the XML file to parse.
  58. */
  59. protected void parseFile(File f)
  60. {
  61. if (!xmlFilter.accept(f))
  62. {
  63. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " is not a file or it doesn't exist!");
  64. return;
  65. }
  66. final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  67. dbf.setNamespaceAware(true);
  68. dbf.setValidating(true);
  69. dbf.setIgnoringComments(true);
  70. _currentDocument = null;
  71. _currentFile = f;
  72. try
  73. {
  74. dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
  75. final DocumentBuilder db = dbf.newDocumentBuilder();
  76. db.setErrorHandler(new XMLErrorHandler());
  77. _currentDocument = db.parse(f);
  78. }
  79. catch (Exception e)
  80. {
  81. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " file: " + e.getMessage());
  82. return;
  83. }
  84. parseDocument();
  85. }
  86. /**
  87. * Gets the current file.
  88. * @return the current file
  89. */
  90. public File getCurrentFile()
  91. {
  92. return _currentFile;
  93. }
  94. /**
  95. * Gets the current document.
  96. * @return the current document
  97. */
  98. protected Document getCurrentDocument()
  99. {
  100. return _currentDocument;
  101. }
  102. /**
  103. * Wrapper for {@link #parseDirectory(File)}.
  104. * @param path the path to the directory where the XML files are.
  105. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  106. */
  107. protected boolean parseDirectory(String path)
  108. {
  109. return parseDirectory(new File(path));
  110. }
  111. /**
  112. * Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
  113. * @param dir the directory object to scan.
  114. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  115. */
  116. protected boolean parseDirectory(File dir)
  117. {
  118. if (!dir.exists())
  119. {
  120. _log.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  121. return false;
  122. }
  123. final File[] listOfFiles = dir.listFiles(xmlFilter);
  124. for (File f : listOfFiles)
  125. {
  126. parseFile(f);
  127. }
  128. return true;
  129. }
  130. /**
  131. * Overridable method that could parse a custom document.<br>
  132. * @param doc the document to parse.
  133. */
  134. protected void parseDocument(Document doc)
  135. {
  136. // Do nothing, to be overridden in sub-classes.
  137. }
  138. /**
  139. * Abstract method that when implemented will parse the current document.<br>
  140. * Is expected to be call from {@link #parseFile(File)}.
  141. */
  142. protected abstract void parseDocument();
  143. /**
  144. * Parses the int.
  145. * @param n the named node map.
  146. * @param name the attribute name.
  147. * @return a parsed integer.
  148. */
  149. protected static int parseInt(NamedNodeMap n, String name)
  150. {
  151. return Integer.parseInt(n.getNamedItem(name).getNodeValue());
  152. }
  153. /**
  154. * Parses the integer.
  155. * @param n the named node map.
  156. * @param name the attribute name.
  157. * @return a parsed integer object.
  158. */
  159. protected static Integer parseInteger(NamedNodeMap n, String name)
  160. {
  161. return Integer.valueOf(n.getNamedItem(name).getNodeValue());
  162. }
  163. /**
  164. * Parses the int.
  165. * @param n the node to parse.
  166. * @return the parsed integer.
  167. */
  168. protected static int parseInt(Node n)
  169. {
  170. return Integer.parseInt(n.getNodeValue());
  171. }
  172. /**
  173. * Parses the integer.
  174. * @param n the node to parse.
  175. * @return the parsed integer object.
  176. */
  177. protected static Integer parseInteger(Node n)
  178. {
  179. return Integer.valueOf(n.getNodeValue());
  180. }
  181. /**
  182. * Parses the long.
  183. * @param n the named node map.
  184. * @param name the attribute name.
  185. * @return a parsed integer.
  186. */
  187. protected static Long parseLong(NamedNodeMap n, String name)
  188. {
  189. return Long.valueOf(n.getNamedItem(name).getNodeValue());
  190. }
  191. /**
  192. * Parses the double.
  193. * @param n the named node map.
  194. * @param name the attribute name.
  195. * @return a parsed double.
  196. */
  197. protected static Double parseDouble(NamedNodeMap n, String name)
  198. {
  199. return Double.valueOf(n.getNamedItem(name).getNodeValue());
  200. }
  201. /**
  202. * Parses the boolean.
  203. * @param n the named node map.
  204. * @param name the attribute name.
  205. * @return {@code true} if the attribute exists and it's value is {@code true}, {@code false} otherwise.
  206. */
  207. protected static boolean parseBoolean(NamedNodeMap n, String name)
  208. {
  209. final Node b = n.getNamedItem(name);
  210. return (b != null) && Boolean.parseBoolean(b.getNodeValue());
  211. }
  212. /**
  213. * @param n the named node map
  214. * @param name the attribute name
  215. * @return the node string value for the given node name and named node map if exist, otherwise an empty string
  216. */
  217. protected static String parseString(NamedNodeMap n, String name)
  218. {
  219. final Node b = n.getNamedItem(name);
  220. return (b == null) ? "" : b.getNodeValue();
  221. }
  222. /**
  223. * Simple XML error handler.
  224. * @author Zoey76
  225. */
  226. protected class XMLErrorHandler implements ErrorHandler
  227. {
  228. @Override
  229. public void warning(SAXParseException e) throws SAXParseException
  230. {
  231. throw e;
  232. }
  233. @Override
  234. public void error(SAXParseException e) throws SAXParseException
  235. {
  236. throw e;
  237. }
  238. @Override
  239. public void fatalError(SAXParseException e) throws SAXParseException
  240. {
  241. throw e;
  242. }
  243. }
  244. }