DocumentParser.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Validation is enforced.
  40. * @param f the XML file to parse.
  41. * @return the document with the parsed data.
  42. */
  43. public Document 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 null;
  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 null;
  66. }
  67. return 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. public 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.<br>
  80. * If the file was successfully parsed, call {@link #parseDocument(Document)} for the parsed document.
  81. * @param dir the directory object to scan.
  82. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  83. */
  84. public boolean parseDirectory(File dir)
  85. {
  86. if (!dir.exists())
  87. {
  88. _log.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  89. return false;
  90. }
  91. final File[] listOfFiles = dir.listFiles(xmlFilter);
  92. for (File f : listOfFiles)
  93. {
  94. final Document doc = parseFile(f);
  95. if (doc != null)
  96. {
  97. parseDocument(doc);
  98. }
  99. }
  100. return true;
  101. }
  102. /**
  103. * Abstract method that when implemented will parse a document.<br>
  104. * Is expected to be used along with {@link #parseFile(File)}.
  105. * @param doc the document to parse.
  106. */
  107. protected abstract void parseDocument(Document doc);
  108. /**
  109. * @param n the named node map.
  110. * @param name the attribute name.
  111. * @return a parsed integer.
  112. */
  113. protected static int parseInt(NamedNodeMap n, String name)
  114. {
  115. return Integer.parseInt(n.getNamedItem(name).getNodeValue());
  116. }
  117. /**
  118. * @param n the named node map.
  119. * @param name the attribute name.
  120. * @return a parsed integer object.
  121. */
  122. protected static Integer parseInteger(NamedNodeMap n, String name)
  123. {
  124. return Integer.valueOf(n.getNamedItem(name).getNodeValue());
  125. }
  126. /**
  127. * @param n the node to parse.
  128. * @return the parsed integer.
  129. */
  130. protected static int parseInt(Node n)
  131. {
  132. return Integer.parseInt(n.getNodeValue());
  133. }
  134. /**
  135. * @param n the node to parse.
  136. * @return the parsed integer object.
  137. */
  138. protected static Integer parseInteger(Node n)
  139. {
  140. return Integer.valueOf(n.getNodeValue());
  141. }
  142. /**
  143. * Simple XML error handler.
  144. * @author Zoey76
  145. */
  146. public class XMLErrorHandler implements ErrorHandler
  147. {
  148. @Override
  149. public void warning(SAXParseException e) throws SAXParseException
  150. {
  151. throw e;
  152. }
  153. @Override
  154. public void error(SAXParseException e) throws SAXParseException
  155. {
  156. throw e;
  157. }
  158. @Override
  159. public void fatalError(SAXParseException e) throws SAXParseException
  160. {
  161. throw e;
  162. }
  163. }
  164. }