DocumentParser.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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.
  34. * @author Zoey76
  35. */
  36. public abstract class DocumentParser
  37. {
  38. protected final Logger _log = Logger.getLogger(getClass().getName());
  39. private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  40. private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  41. /** The default file filter, ".xml" files only. */
  42. private static final XMLFilter XML_FILTER = 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. * Wrapper for {@link #parseDirectory(File, boolean)}.
  165. * @param path the path to the directory where the XML files are
  166. * @param recursive parses all sub folders if there is
  167. * @return {@code false} if it fails to find the directory, {@code true} otherwise
  168. */
  169. protected boolean parseDatapackDirectory(String path, boolean recursive)
  170. {
  171. return parseDirectory(new File(Config.DATAPACK_ROOT, path), recursive);
  172. }
  173. /**
  174. * Overridable method that could parse a custom document.<br>
  175. * @param doc the document to parse.
  176. */
  177. protected void parseDocument(Document doc)
  178. {
  179. // Do nothing, to be overridden in sub-classes.
  180. }
  181. /**
  182. * Abstract method that when implemented will parse the current document.<br>
  183. * Is expected to be call from {@link #parseFile(File)}.
  184. */
  185. protected abstract void parseDocument();
  186. /**
  187. * Parses a boolean value.
  188. * @param node the node to parse
  189. * @param defaultValue the default value
  190. * @return if the node is not null, the value of the parsed node, otherwise the default value
  191. */
  192. protected Boolean parseBoolean(Node node, Boolean defaultValue)
  193. {
  194. return node != null ? Boolean.valueOf(node.getNodeValue()) : defaultValue;
  195. }
  196. /**
  197. * Parses a boolean value.
  198. * @param node the node to parse
  199. * @return if the node is not null, the value of the parsed node, otherwise null
  200. */
  201. protected Boolean parseBoolean(Node node)
  202. {
  203. return parseBoolean(node, null);
  204. }
  205. /**
  206. * Parses a boolean value.
  207. * @param attrs the attributes
  208. * @param name the name of the attribute to parse
  209. * @return if the node is not null, the value of the parsed node, otherwise null
  210. */
  211. protected Boolean parseBoolean(NamedNodeMap attrs, String name)
  212. {
  213. return parseBoolean(attrs.getNamedItem(name));
  214. }
  215. /**
  216. * Parses a boolean value.
  217. * @param attrs the attributes
  218. * @param name the name of the attribute to parse
  219. * @param defaultValue the default value
  220. * @return if the node is not null, the value of the parsed node, otherwise the default value
  221. */
  222. protected Boolean parseBoolean(NamedNodeMap attrs, String name, Boolean defaultValue)
  223. {
  224. return parseBoolean(attrs.getNamedItem(name), defaultValue);
  225. }
  226. /**
  227. * Parses a byte value.
  228. * @param node the node to parse
  229. * @param defaultValue the default value
  230. * @return if the node is not null, the value of the parsed node, otherwise the default value
  231. */
  232. protected Byte parseByte(Node node, Byte defaultValue)
  233. {
  234. return node != null ? Byte.valueOf(node.getNodeValue()) : defaultValue;
  235. }
  236. /**
  237. * Parses a byte value.
  238. * @param node the node to parse
  239. * @return if the node is not null, the value of the parsed node, otherwise null
  240. */
  241. protected Byte parseByte(Node node)
  242. {
  243. return parseByte(node, null);
  244. }
  245. /**
  246. * Parses a byte value.
  247. * @param attrs the attributes
  248. * @param name the name of the attribute to parse
  249. * @return if the node is not null, the value of the parsed node, otherwise null
  250. */
  251. protected Byte parseByte(NamedNodeMap attrs, String name)
  252. {
  253. return parseByte(attrs.getNamedItem(name));
  254. }
  255. /**
  256. * Parses a byte value.
  257. * @param attrs the attributes
  258. * @param name the name of the attribute to parse
  259. * @param defaultValue the default value
  260. * @return if the node is not null, the value of the parsed node, otherwise the default value
  261. */
  262. protected Byte parseByte(NamedNodeMap attrs, String name, Byte defaultValue)
  263. {
  264. return parseByte(attrs.getNamedItem(name), defaultValue);
  265. }
  266. /**
  267. * Parses a short value.
  268. * @param node the node to parse
  269. * @param defaultValue the default value
  270. * @return if the node is not null, the value of the parsed node, otherwise the default value
  271. */
  272. protected Short parseShort(Node node, Short defaultValue)
  273. {
  274. return node != null ? Short.valueOf(node.getNodeValue()) : defaultValue;
  275. }
  276. /**
  277. * Parses a short value.
  278. * @param node the node to parse
  279. * @return if the node is not null, the value of the parsed node, otherwise null
  280. */
  281. protected Short parseShort(Node node)
  282. {
  283. return parseShort(node, null);
  284. }
  285. /**
  286. * Parses a short value.
  287. * @param attrs the attributes
  288. * @param name the name of the attribute to parse
  289. * @return if the node is not null, the value of the parsed node, otherwise null
  290. */
  291. protected Short parseShort(NamedNodeMap attrs, String name)
  292. {
  293. return parseShort(attrs.getNamedItem(name));
  294. }
  295. /**
  296. * Parses a short value.
  297. * @param attrs the attributes
  298. * @param name the name of the attribute to parse
  299. * @param defaultValue the default value
  300. * @return if the node is not null, the value of the parsed node, otherwise the default value
  301. */
  302. protected Short parseShort(NamedNodeMap attrs, String name, Short defaultValue)
  303. {
  304. return parseShort(attrs.getNamedItem(name), defaultValue);
  305. }
  306. /**
  307. * Parses an integer value.
  308. * @param node the node to parse
  309. * @param defaultValue the default value
  310. * @return if the node is not null, the value of the parsed node, otherwise the default value
  311. */
  312. protected Integer parseInteger(Node node, Integer defaultValue)
  313. {
  314. return node != null ? Integer.valueOf(node.getNodeValue()) : defaultValue;
  315. }
  316. /**
  317. * Parses an integer value.
  318. * @param node the node to parse
  319. * @return if the node is not null, the value of the parsed node, otherwise null
  320. */
  321. protected Integer parseInteger(Node node)
  322. {
  323. return parseInteger(node, null);
  324. }
  325. /**
  326. * Parses an integer value.
  327. * @param attrs the attributes
  328. * @param name the name of the attribute to parse
  329. * @return if the node is not null, the value of the parsed node, otherwise null
  330. */
  331. protected Integer parseInteger(NamedNodeMap attrs, String name)
  332. {
  333. return parseInteger(attrs.getNamedItem(name));
  334. }
  335. /**
  336. * Parses an integer value.
  337. * @param attrs the attributes
  338. * @param name the name of the attribute to parse
  339. * @param defaultValue the default value
  340. * @return if the node is not null, the value of the parsed node, otherwise the default value
  341. */
  342. protected Integer parseInteger(NamedNodeMap attrs, String name, Integer defaultValue)
  343. {
  344. return parseInteger(attrs.getNamedItem(name), defaultValue);
  345. }
  346. /**
  347. * Parses a long value.
  348. * @param node the node to parse
  349. * @param defaultValue the default value
  350. * @return if the node is not null, the value of the parsed node, otherwise the default value
  351. */
  352. protected Long parseLong(Node node, Long defaultValue)
  353. {
  354. return node != null ? Long.valueOf(node.getNodeValue()) : defaultValue;
  355. }
  356. /**
  357. * Parses a long value.
  358. * @param node the node to parse
  359. * @return if the node is not null, the value of the parsed node, otherwise null
  360. */
  361. protected Long parseLong(Node node)
  362. {
  363. return parseLong(node, null);
  364. }
  365. /**
  366. * Parses a long value.
  367. * @param attrs the attributes
  368. * @param name the name of the attribute to parse
  369. * @return if the node is not null, the value of the parsed node, otherwise null
  370. */
  371. protected Long parseLong(NamedNodeMap attrs, String name)
  372. {
  373. return parseLong(attrs.getNamedItem(name));
  374. }
  375. /**
  376. * Parses a long value.
  377. * @param attrs the attributes
  378. * @param name the name of the attribute to parse
  379. * @param defaultValue the default value
  380. * @return if the node is not null, the value of the parsed node, otherwise the default value
  381. */
  382. protected Long parseLong(NamedNodeMap attrs, String name, Long defaultValue)
  383. {
  384. return parseLong(attrs.getNamedItem(name), defaultValue);
  385. }
  386. /**
  387. * Parses a float value.
  388. * @param node the node to parse
  389. * @param defaultValue the default value
  390. * @return if the node is not null, the value of the parsed node, otherwise the default value
  391. */
  392. protected Float parseFloat(Node node, Float defaultValue)
  393. {
  394. return node != null ? Float.valueOf(node.getNodeValue()) : defaultValue;
  395. }
  396. /**
  397. * Parses a float value.
  398. * @param node the node to parse
  399. * @return if the node is not null, the value of the parsed node, otherwise null
  400. */
  401. protected Float parseFloat(Node node)
  402. {
  403. return parseFloat(node, null);
  404. }
  405. /**
  406. * Parses a float value.
  407. * @param attrs the attributes
  408. * @param name the name of the attribute to parse
  409. * @return if the node is not null, the value of the parsed node, otherwise null
  410. */
  411. protected Float parseFloat(NamedNodeMap attrs, String name)
  412. {
  413. return parseFloat(attrs.getNamedItem(name));
  414. }
  415. /**
  416. * Parses a float value.
  417. * @param attrs the attributes
  418. * @param name the name of the attribute to parse
  419. * @param defaultValue the default value
  420. * @return if the node is not null, the value of the parsed node, otherwise the default value
  421. */
  422. protected Float parseFloat(NamedNodeMap attrs, String name, Float defaultValue)
  423. {
  424. return parseFloat(attrs.getNamedItem(name), defaultValue);
  425. }
  426. /**
  427. * Parses a double value.
  428. * @param node the node to parse
  429. * @param defaultValue the default value
  430. * @return if the node is not null, the value of the parsed node, otherwise the default value
  431. */
  432. protected Double parseDouble(Node node, Double defaultValue)
  433. {
  434. return node != null ? Double.valueOf(node.getNodeValue()) : defaultValue;
  435. }
  436. /**
  437. * Parses a double value.
  438. * @param node the node to parse
  439. * @return if the node is not null, the value of the parsed node, otherwise null
  440. */
  441. protected Double parseDouble(Node node)
  442. {
  443. return parseDouble(node, null);
  444. }
  445. /**
  446. * Parses a double value.
  447. * @param attrs the attributes
  448. * @param name the name of the attribute to parse
  449. * @return if the node is not null, the value of the parsed node, otherwise null
  450. */
  451. protected Double parseDouble(NamedNodeMap attrs, String name)
  452. {
  453. return parseDouble(attrs.getNamedItem(name));
  454. }
  455. /**
  456. * Parses a double value.
  457. * @param attrs the attributes
  458. * @param name the name of the attribute to parse
  459. * @param defaultValue the default value
  460. * @return if the node is not null, the value of the parsed node, otherwise the default value
  461. */
  462. protected Double parseDouble(NamedNodeMap attrs, String name, Double defaultValue)
  463. {
  464. return parseDouble(attrs.getNamedItem(name), defaultValue);
  465. }
  466. /**
  467. * Parses a string value.
  468. * @param node the node to parse
  469. * @param defaultValue the default value
  470. * @return if the node is not null, the value of the parsed node, otherwise the default value
  471. */
  472. protected String parseString(Node node, String defaultValue)
  473. {
  474. return node != null ? node.getNodeValue() : defaultValue;
  475. }
  476. /**
  477. * Parses a string value.
  478. * @param node the node to parse
  479. * @return if the node is not null, the value of the parsed node, otherwise null
  480. */
  481. protected String parseString(Node node)
  482. {
  483. return parseString(node, null);
  484. }
  485. /**
  486. * Parses a string value.
  487. * @param attrs the attributes
  488. * @param name the name of the attribute to parse
  489. * @return if the node is not null, the value of the parsed node, otherwise null
  490. */
  491. protected String parseString(NamedNodeMap attrs, String name)
  492. {
  493. return parseString(attrs.getNamedItem(name));
  494. }
  495. /**
  496. * Parses a string value.
  497. * @param attrs the attributes
  498. * @param name the name of the attribute to parse
  499. * @param defaultValue the default value
  500. * @return if the node is not null, the value of the parsed node, otherwise the default value
  501. */
  502. protected String parseString(NamedNodeMap attrs, String name, String defaultValue)
  503. {
  504. return parseString(attrs.getNamedItem(name), defaultValue);
  505. }
  506. /**
  507. * Parses an enumerated value.
  508. * @param <T> the enumerated type
  509. * @param node the node to parse
  510. * @param clazz the class of the enumerated
  511. * @param defaultValue the default value
  512. * @return if the node is not null and the node value is valid the parsed value, otherwise the default value
  513. */
  514. protected <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz, T defaultValue)
  515. {
  516. if (node == null)
  517. {
  518. return defaultValue;
  519. }
  520. try
  521. {
  522. return Enum.valueOf(clazz, node.getNodeValue());
  523. }
  524. catch (IllegalArgumentException e)
  525. {
  526. _log.warning("[" + getCurrentFile().getName() + "] Invalid value specified for node: " + node.getNodeName() + " specified value: " + node.getNodeValue() + " should be enum value of \"" + clazz.getSimpleName() + "\" using default value: " + defaultValue);
  527. return defaultValue;
  528. }
  529. }
  530. /**
  531. * Parses an enumerated value.
  532. * @param <T> the enumerated type
  533. * @param node the node to parse
  534. * @param clazz the class of the enumerated
  535. * @return if the node is not null and the node value is valid the parsed value, otherwise null
  536. */
  537. protected <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz)
  538. {
  539. return parseEnum(node, clazz, null);
  540. }
  541. /**
  542. * Parses an enumerated value.
  543. * @param <T> the enumerated type
  544. * @param attrs the attributes
  545. * @param clazz the class of the enumerated
  546. * @param name the name of the attribute to parse
  547. * @return if the node is not null and the node value is valid the parsed value, otherwise null
  548. */
  549. protected <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name)
  550. {
  551. return parseEnum(attrs.getNamedItem(name), clazz);
  552. }
  553. /**
  554. * Parses an enumerated value.
  555. * @param <T> the enumerated type
  556. * @param attrs the attributes
  557. * @param clazz the class of the enumerated
  558. * @param name the name of the attribute to parse
  559. * @param defaultValue the default value
  560. * @return if the node is not null and the node value is valid the parsed value, otherwise the default value
  561. */
  562. protected <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name, T defaultValue)
  563. {
  564. return parseEnum(attrs.getNamedItem(name), clazz, defaultValue);
  565. }
  566. /**
  567. * Sets the current file filter.
  568. * @param filter the file filter
  569. */
  570. public void setCurrentFileFilter(FileFilter filter)
  571. {
  572. _currentFilter = filter;
  573. }
  574. /**
  575. * Gets the current file filter.
  576. * @return the current file filter
  577. */
  578. public FileFilter getCurrentFileFilter()
  579. {
  580. return _currentFilter != null ? _currentFilter : XML_FILTER;
  581. }
  582. /**
  583. * Simple XML error handler.
  584. * @author Zoey76
  585. */
  586. protected class XMLErrorHandler implements ErrorHandler
  587. {
  588. @Override
  589. public void warning(SAXParseException e) throws SAXParseException
  590. {
  591. throw e;
  592. }
  593. @Override
  594. public void error(SAXParseException e) throws SAXParseException
  595. {
  596. throw e;
  597. }
  598. @Override
  599. public void fatalError(SAXParseException e) throws SAXParseException
  600. {
  601. throw e;
  602. }
  603. }
  604. }