DocumentParser.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  39. * Wrapper for {@link #parseFile(File)} method.
  40. * @param path the relative path to the datapack root of the XML file to parse.
  41. */
  42. protected void parseDatapackFile(String path)
  43. {
  44. parseFile(new File(Config.DATAPACK_ROOT, path));
  45. }
  46. /**
  47. * Parses a single XML file.<br>
  48. * If the file was successfully parsed, call {@link #parseDocument(Document)} for the parsed document.
  49. * Validation is enforced.
  50. * @param f the XML file to parse.
  51. */
  52. protected void parseFile(File f)
  53. {
  54. if (!xmlFilter.accept(f))
  55. {
  56. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " is not a file or it doesn't exist!");
  57. return;
  58. }
  59. final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  60. dbf.setNamespaceAware(true);
  61. dbf.setValidating(true);
  62. dbf.setIgnoringComments(true);
  63. Document doc = null;
  64. try
  65. {
  66. dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
  67. final DocumentBuilder db = dbf.newDocumentBuilder();
  68. db.setErrorHandler(new XMLErrorHandler());
  69. doc = db.parse(f);
  70. }
  71. catch (Exception e)
  72. {
  73. _log.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " file: " + e.getMessage());
  74. return;
  75. }
  76. parseDocument(doc);
  77. }
  78. /**
  79. * Wrapper for {@link #parseDirectory(File)}.
  80. * @param path the path to the directory where the XML files are.
  81. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  82. */
  83. protected boolean parseDirectory(String path)
  84. {
  85. return parseDirectory(new File(path));
  86. }
  87. /**
  88. * Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
  89. * @param dir the directory object to scan.
  90. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  91. */
  92. protected boolean parseDirectory(File dir)
  93. {
  94. if (!dir.exists())
  95. {
  96. _log.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  97. return false;
  98. }
  99. final File[] listOfFiles = dir.listFiles(xmlFilter);
  100. for (File f : listOfFiles)
  101. {
  102. parseFile(f);
  103. }
  104. return true;
  105. }
  106. /**
  107. * Abstract method that when implemented will parse a document.<br>
  108. * Is expected to be call from {@link #parseFile(File)}.
  109. * @param doc the document to parse.
  110. */
  111. protected abstract void parseDocument(Document doc);
  112. /**
  113. * @param n the named node map.
  114. * @param name the attribute name.
  115. * @return a parsed integer.
  116. */
  117. protected static int parseInt(NamedNodeMap n, String name)
  118. {
  119. return Integer.parseInt(n.getNamedItem(name).getNodeValue());
  120. }
  121. /**
  122. * @param n the named node map.
  123. * @param name the attribute name.
  124. * @return a parsed integer object.
  125. */
  126. protected static Integer parseInteger(NamedNodeMap n, String name)
  127. {
  128. return Integer.valueOf(n.getNamedItem(name).getNodeValue());
  129. }
  130. /**
  131. * @param n the node to parse.
  132. * @return the parsed integer.
  133. */
  134. protected static int parseInt(Node n)
  135. {
  136. return Integer.parseInt(n.getNodeValue());
  137. }
  138. /**
  139. * @param n the node to parse.
  140. * @return the parsed integer object.
  141. */
  142. protected static Integer parseInteger(Node n)
  143. {
  144. return Integer.valueOf(n.getNodeValue());
  145. }
  146. /**
  147. * @param n the named node map.
  148. * @param name the attribute name.
  149. * @return a parsed integer.
  150. */
  151. protected static Long parseLong(NamedNodeMap n, String name)
  152. {
  153. return Long.valueOf(n.getNamedItem(name).getNodeValue());
  154. }
  155. /**
  156. * @param n the named node map.
  157. * @param name the attribute name.
  158. * @return {@code true} if the attribute exists and it's value is {@code true}, {@code false} otherwise.
  159. */
  160. protected static boolean parseBoolean(NamedNodeMap n, String name)
  161. {
  162. final Node b = n.getNamedItem(name);
  163. return (b != null) && Boolean.parseBoolean(b.getNodeValue());
  164. }
  165. /**
  166. * Simple XML error handler.
  167. * @author Zoey76
  168. */
  169. protected class XMLErrorHandler implements ErrorHandler
  170. {
  171. @Override
  172. public void warning(SAXParseException e) throws SAXParseException
  173. {
  174. throw e;
  175. }
  176. @Override
  177. public void error(SAXParseException e) throws SAXParseException
  178. {
  179. throw e;
  180. }
  181. @Override
  182. public void fatalError(SAXParseException e) throws SAXParseException
  183. {
  184. throw e;
  185. }
  186. }
  187. }