DocumentParser.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.engines;
  20. import java.io.File;
  21. import java.io.FileFilter;
  22. import java.util.logging.Logger;
  23. import javax.xml.parsers.DocumentBuilder;
  24. import javax.xml.parsers.DocumentBuilderFactory;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.NamedNodeMap;
  27. import org.w3c.dom.Node;
  28. import org.xml.sax.ErrorHandler;
  29. import org.xml.sax.SAXParseException;
  30. import com.l2jserver.Config;
  31. import com.l2jserver.util.file.filter.XMLFilter;
  32. /**
  33. * Abstract class for XML parsers.<br>
  34. * It's in <i>beta</i> state, so it's expected to change over time.
  35. * @author Zoey76
  36. */
  37. public abstract class DocumentParser
  38. {
  39. protected final Logger _log = Logger.getLogger(getClass().getName());
  40. private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  41. private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  42. private static final XMLFilter xmlFilter = new XMLFilter();
  43. private File _currentFile;
  44. private Document _currentDocument;
  45. private FileFilter _currentFilter = null;
  46. /**
  47. * This method can be used to load/reload the data.<br>
  48. * It's highly recommended to clear the data storage, either the list or map.
  49. */
  50. public abstract void load();
  51. /**
  52. * Wrapper for {@link #parseFile(File)} method.
  53. * @param path the relative path to the datapack root of the XML file to parse.
  54. */
  55. protected void parseDatapackFile(String path)
  56. {
  57. parseFile(new File(Config.DATAPACK_ROOT, path));
  58. }
  59. /**
  60. * Parses a single XML file.<br>
  61. * If the file was successfully parsed, call {@link #parseDocument(Document)} for the parsed document.<br>
  62. * <b>Validation is enforced.</b>
  63. * @param f the XML file to parse.
  64. */
  65. protected void parseFile(File f)
  66. {
  67. if (!getCurrentFileFilter().accept(f))
  68. {
  69. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " is not a file or it doesn't exist!");
  70. return;
  71. }
  72. final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  73. dbf.setNamespaceAware(true);
  74. dbf.setValidating(true);
  75. dbf.setIgnoringComments(true);
  76. _currentDocument = null;
  77. _currentFile = f;
  78. try
  79. {
  80. dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
  81. final DocumentBuilder db = dbf.newDocumentBuilder();
  82. db.setErrorHandler(new XMLErrorHandler());
  83. _currentDocument = db.parse(f);
  84. }
  85. catch (Exception e)
  86. {
  87. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " file: " + e.getMessage());
  88. return;
  89. }
  90. parseDocument();
  91. }
  92. /**
  93. * Gets the current file.
  94. * @return the current file
  95. */
  96. public File getCurrentFile()
  97. {
  98. return _currentFile;
  99. }
  100. /**
  101. * Gets the current document.
  102. * @return the current document
  103. */
  104. protected Document getCurrentDocument()
  105. {
  106. return _currentDocument;
  107. }
  108. /**
  109. * Wrapper for {@link #parseDirectory(File, boolean)}.
  110. * @param file the path to the directory where the XML files are.
  111. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  112. */
  113. protected boolean parseDirectory(File file)
  114. {
  115. return parseDirectory(file, false);
  116. }
  117. /**
  118. * Wrapper for {@link #parseDirectory(File, boolean)}.
  119. * @param path the path to the directory where the XML files are.
  120. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  121. */
  122. protected boolean parseDirectory(String path)
  123. {
  124. return parseDirectory(new File(path), false);
  125. }
  126. /**
  127. * Wrapper for {@link #parseDirectory(File, boolean)}.
  128. * @param path the path to the directory where the XML files are.
  129. * @param recursive parses all sub folders if there is.
  130. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  131. */
  132. protected boolean parseDirectory(String path, boolean recursive)
  133. {
  134. return parseDirectory(new File(path), recursive);
  135. }
  136. /**
  137. * Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
  138. * @param dir the directory object to scan.
  139. * @param recursive parses all sub folders if there is.
  140. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  141. */
  142. protected boolean parseDirectory(File dir, boolean recursive)
  143. {
  144. if (!dir.exists())
  145. {
  146. _log.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  147. return false;
  148. }
  149. final File[] listOfFiles = dir.listFiles();
  150. for (File f : listOfFiles)
  151. {
  152. if (recursive && f.isDirectory())
  153. {
  154. parseDirectory(f, recursive);
  155. }
  156. else if (getCurrentFileFilter().accept(f))
  157. {
  158. parseFile(f);
  159. }
  160. }
  161. return true;
  162. }
  163. /**
  164. * Overridable method that could parse a custom document.<br>
  165. * @param doc the document to parse.
  166. */
  167. protected void parseDocument(Document doc)
  168. {
  169. // Do nothing, to be overridden in sub-classes.
  170. }
  171. /**
  172. * Abstract method that when implemented will parse the current document.<br>
  173. * Is expected to be call from {@link #parseFile(File)}.
  174. */
  175. protected abstract void parseDocument();
  176. /**
  177. * Parses the int.
  178. * @param n the named node map.
  179. * @param name the attribute name.
  180. * @return a parsed integer.
  181. */
  182. protected static int parseInt(NamedNodeMap n, String name)
  183. {
  184. return Integer.parseInt(n.getNamedItem(name).getNodeValue());
  185. }
  186. /**
  187. * Parses the integer.
  188. * @param n the named node map.
  189. * @param name the attribute name.
  190. * @return a parsed integer object.
  191. */
  192. protected static Integer parseInteger(NamedNodeMap n, String name)
  193. {
  194. return Integer.valueOf(n.getNamedItem(name).getNodeValue());
  195. }
  196. /**
  197. * Parses the int.
  198. * @param n the node to parse.
  199. * @return the parsed integer.
  200. */
  201. protected static int parseInt(Node n)
  202. {
  203. return Integer.parseInt(n.getNodeValue());
  204. }
  205. /**
  206. * Parses the integer.
  207. * @param n the node to parse.
  208. * @return the parsed integer object.
  209. */
  210. protected static Integer parseInteger(Node n)
  211. {
  212. return Integer.valueOf(n.getNodeValue());
  213. }
  214. /**
  215. * Parses the long.
  216. * @param n the named node map.
  217. * @param name the attribute name.
  218. * @return a parsed integer.
  219. */
  220. protected static Long parseLong(NamedNodeMap n, String name)
  221. {
  222. return Long.valueOf(n.getNamedItem(name).getNodeValue());
  223. }
  224. /**
  225. * Parses the float.
  226. * @param n the named node map.
  227. * @param name the attribute name.
  228. * @return a parsed float.
  229. */
  230. protected static float parseFloat(NamedNodeMap n, String name)
  231. {
  232. return Float.parseFloat(n.getNamedItem(name).getNodeValue());
  233. }
  234. /**
  235. * Parses the double.
  236. * @param n the named node map.
  237. * @param name the attribute name.
  238. * @return a parsed double.
  239. */
  240. protected static Double parseDouble(NamedNodeMap n, String name)
  241. {
  242. return Double.valueOf(n.getNamedItem(name).getNodeValue());
  243. }
  244. /**
  245. * Parses the boolean.
  246. * @param n the named node map.
  247. * @param name the attribute name.
  248. * @return {@code true} if the attribute exists and it's value is {@code true}, {@code false} otherwise.
  249. */
  250. protected static boolean parseBoolean(NamedNodeMap n, String name)
  251. {
  252. final Node b = n.getNamedItem(name);
  253. return (b != null) && Boolean.parseBoolean(b.getNodeValue());
  254. }
  255. /**
  256. * @param n the named node map
  257. * @param name the attribute name
  258. * @return the node string value for the given node name and named node map if exist, otherwise an empty string
  259. */
  260. protected static String parseString(NamedNodeMap n, String name)
  261. {
  262. final Node b = n.getNamedItem(name);
  263. return (b == null) ? "" : b.getNodeValue();
  264. }
  265. public void setCurrentFileFilter(FileFilter filter)
  266. {
  267. _currentFilter = filter;
  268. }
  269. public FileFilter getCurrentFileFilter()
  270. {
  271. return _currentFilter != null ? _currentFilter : xmlFilter;
  272. }
  273. /**
  274. * Simple XML error handler.
  275. * @author Zoey76
  276. */
  277. protected class XMLErrorHandler implements ErrorHandler
  278. {
  279. @Override
  280. public void warning(SAXParseException e) throws SAXParseException
  281. {
  282. throw e;
  283. }
  284. @Override
  285. public void error(SAXParseException e) throws SAXParseException
  286. {
  287. throw e;
  288. }
  289. @Override
  290. public void fatalError(SAXParseException e) throws SAXParseException
  291. {
  292. throw e;
  293. }
  294. }
  295. }