DocumentParser.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * Copyright (C) 2004-2014 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 (SAXParseException e)
  86. {
  87. _log.warning(getClass().getSimpleName() + ": Could not parse file " + f.getName() + " at line " + e.getLineNumber() + ", column " + e.getColumnNumber() + ": " + e.getMessage());
  88. return;
  89. }
  90. catch (Exception e)
  91. {
  92. _log.warning(getClass().getSimpleName() + ": Could not parse file " + f.getName() + ": " + e.getMessage());
  93. return;
  94. }
  95. parseDocument();
  96. }
  97. /**
  98. * Gets the current file.
  99. * @return the current file
  100. */
  101. public File getCurrentFile()
  102. {
  103. return _currentFile;
  104. }
  105. /**
  106. * Gets the current document.
  107. * @return the current document
  108. */
  109. protected Document getCurrentDocument()
  110. {
  111. return _currentDocument;
  112. }
  113. /**
  114. * Wrapper for {@link #parseDirectory(File, boolean)}.
  115. * @param file the path to the directory where the XML files are.
  116. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  117. */
  118. protected boolean parseDirectory(File file)
  119. {
  120. return parseDirectory(file, false);
  121. }
  122. /**
  123. * Wrapper for {@link #parseDirectory(File, boolean)}.
  124. * @param path the path to the directory where the XML files are.
  125. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  126. */
  127. protected boolean parseDirectory(String path)
  128. {
  129. return parseDirectory(new File(path), false);
  130. }
  131. /**
  132. * Wrapper for {@link #parseDirectory(File, boolean)}.
  133. * @param path the path to the directory where the XML files are.
  134. * @param recursive parses all sub folders if there is.
  135. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  136. */
  137. protected boolean parseDirectory(String path, boolean recursive)
  138. {
  139. return parseDirectory(new File(path), recursive);
  140. }
  141. /**
  142. * Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
  143. * @param dir the directory object to scan.
  144. * @param recursive parses all sub folders if there is.
  145. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  146. */
  147. protected boolean parseDirectory(File dir, boolean recursive)
  148. {
  149. if (!dir.exists())
  150. {
  151. _log.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  152. return false;
  153. }
  154. final File[] listOfFiles = dir.listFiles();
  155. for (File f : listOfFiles)
  156. {
  157. if (recursive && f.isDirectory())
  158. {
  159. parseDirectory(f, recursive);
  160. }
  161. else if (getCurrentFileFilter().accept(f))
  162. {
  163. parseFile(f);
  164. }
  165. }
  166. return true;
  167. }
  168. /**
  169. * Wrapper for {@link #parseDirectory(File, boolean)}.
  170. * @param path the path to the directory where the XML files are
  171. * @param recursive parses all sub folders if there is
  172. * @return {@code false} if it fails to find the directory, {@code true} otherwise
  173. */
  174. protected boolean parseDatapackDirectory(String path, boolean recursive)
  175. {
  176. return parseDirectory(new File(Config.DATAPACK_ROOT, path), recursive);
  177. }
  178. /**
  179. * Overridable method that could parse a custom document.<br>
  180. * @param doc the document to parse.
  181. */
  182. protected void parseDocument(Document doc)
  183. {
  184. // Do nothing, to be overridden in sub-classes.
  185. }
  186. /**
  187. * Abstract method that when implemented will parse the current document.<br>
  188. * Is expected to be call from {@link #parseFile(File)}.
  189. */
  190. protected abstract void parseDocument();
  191. /**
  192. * Parses a boolean value.
  193. * @param node the node to parse
  194. * @param defaultValue the default value
  195. * @return if the node is not null, the value of the parsed node, otherwise the default value
  196. */
  197. protected Boolean parseBoolean(Node node, Boolean defaultValue)
  198. {
  199. return node != null ? Boolean.valueOf(node.getNodeValue()) : defaultValue;
  200. }
  201. /**
  202. * Parses a boolean value.
  203. * @param node the node to parse
  204. * @return if the node is not null, the value of the parsed node, otherwise null
  205. */
  206. protected Boolean parseBoolean(Node node)
  207. {
  208. return parseBoolean(node, null);
  209. }
  210. /**
  211. * Parses a boolean value.
  212. * @param attrs the attributes
  213. * @param name the name of the attribute to parse
  214. * @return if the node is not null, the value of the parsed node, otherwise null
  215. */
  216. protected Boolean parseBoolean(NamedNodeMap attrs, String name)
  217. {
  218. return parseBoolean(attrs.getNamedItem(name));
  219. }
  220. /**
  221. * Parses a boolean value.
  222. * @param attrs the attributes
  223. * @param name the name of the attribute to parse
  224. * @param defaultValue the default value
  225. * @return if the node is not null, the value of the parsed node, otherwise the default value
  226. */
  227. protected Boolean parseBoolean(NamedNodeMap attrs, String name, Boolean defaultValue)
  228. {
  229. return parseBoolean(attrs.getNamedItem(name), defaultValue);
  230. }
  231. /**
  232. * Parses a byte value.
  233. * @param node the node to parse
  234. * @param defaultValue the default value
  235. * @return if the node is not null, the value of the parsed node, otherwise the default value
  236. */
  237. protected Byte parseByte(Node node, Byte defaultValue)
  238. {
  239. return node != null ? Byte.valueOf(node.getNodeValue()) : defaultValue;
  240. }
  241. /**
  242. * Parses a byte value.
  243. * @param node the node to parse
  244. * @return if the node is not null, the value of the parsed node, otherwise null
  245. */
  246. protected Byte parseByte(Node node)
  247. {
  248. return parseByte(node, null);
  249. }
  250. /**
  251. * Parses a byte value.
  252. * @param attrs the attributes
  253. * @param name the name of the attribute to parse
  254. * @return if the node is not null, the value of the parsed node, otherwise null
  255. */
  256. protected Byte parseByte(NamedNodeMap attrs, String name)
  257. {
  258. return parseByte(attrs.getNamedItem(name));
  259. }
  260. /**
  261. * Parses a byte value.
  262. * @param attrs the attributes
  263. * @param name the name of the attribute to parse
  264. * @param defaultValue the default value
  265. * @return if the node is not null, the value of the parsed node, otherwise the default value
  266. */
  267. protected Byte parseByte(NamedNodeMap attrs, String name, Byte defaultValue)
  268. {
  269. return parseByte(attrs.getNamedItem(name), defaultValue);
  270. }
  271. /**
  272. * Parses a short value.
  273. * @param node the node to parse
  274. * @param defaultValue the default value
  275. * @return if the node is not null, the value of the parsed node, otherwise the default value
  276. */
  277. protected Short parseShort(Node node, Short defaultValue)
  278. {
  279. return node != null ? Short.valueOf(node.getNodeValue()) : defaultValue;
  280. }
  281. /**
  282. * Parses a short value.
  283. * @param node the node to parse
  284. * @return if the node is not null, the value of the parsed node, otherwise null
  285. */
  286. protected Short parseShort(Node node)
  287. {
  288. return parseShort(node, null);
  289. }
  290. /**
  291. * Parses a short value.
  292. * @param attrs the attributes
  293. * @param name the name of the attribute to parse
  294. * @return if the node is not null, the value of the parsed node, otherwise null
  295. */
  296. protected Short parseShort(NamedNodeMap attrs, String name)
  297. {
  298. return parseShort(attrs.getNamedItem(name));
  299. }
  300. /**
  301. * Parses a short value.
  302. * @param attrs the attributes
  303. * @param name the name of the attribute to parse
  304. * @param defaultValue the default value
  305. * @return if the node is not null, the value of the parsed node, otherwise the default value
  306. */
  307. protected Short parseShort(NamedNodeMap attrs, String name, Short defaultValue)
  308. {
  309. return parseShort(attrs.getNamedItem(name), defaultValue);
  310. }
  311. /**
  312. * Parses an integer value.
  313. * @param node the node to parse
  314. * @param defaultValue the default value
  315. * @return if the node is not null, the value of the parsed node, otherwise the default value
  316. */
  317. protected Integer parseInteger(Node node, Integer defaultValue)
  318. {
  319. return node != null ? Integer.valueOf(node.getNodeValue()) : defaultValue;
  320. }
  321. /**
  322. * Parses an integer value.
  323. * @param node the node to parse
  324. * @return if the node is not null, the value of the parsed node, otherwise null
  325. */
  326. protected Integer parseInteger(Node node)
  327. {
  328. return parseInteger(node, null);
  329. }
  330. /**
  331. * Parses an integer value.
  332. * @param attrs the attributes
  333. * @param name the name of the attribute to parse
  334. * @return if the node is not null, the value of the parsed node, otherwise null
  335. */
  336. protected Integer parseInteger(NamedNodeMap attrs, String name)
  337. {
  338. return parseInteger(attrs.getNamedItem(name));
  339. }
  340. /**
  341. * Parses an integer value.
  342. * @param attrs the attributes
  343. * @param name the name of the attribute to parse
  344. * @param defaultValue the default value
  345. * @return if the node is not null, the value of the parsed node, otherwise the default value
  346. */
  347. protected Integer parseInteger(NamedNodeMap attrs, String name, Integer defaultValue)
  348. {
  349. return parseInteger(attrs.getNamedItem(name), defaultValue);
  350. }
  351. /**
  352. * Parses a long value.
  353. * @param node the node to parse
  354. * @param defaultValue the default value
  355. * @return if the node is not null, the value of the parsed node, otherwise the default value
  356. */
  357. protected Long parseLong(Node node, Long defaultValue)
  358. {
  359. return node != null ? Long.valueOf(node.getNodeValue()) : defaultValue;
  360. }
  361. /**
  362. * Parses a long value.
  363. * @param node the node to parse
  364. * @return if the node is not null, the value of the parsed node, otherwise null
  365. */
  366. protected Long parseLong(Node node)
  367. {
  368. return parseLong(node, null);
  369. }
  370. /**
  371. * Parses a long value.
  372. * @param attrs the attributes
  373. * @param name the name of the attribute to parse
  374. * @return if the node is not null, the value of the parsed node, otherwise null
  375. */
  376. protected Long parseLong(NamedNodeMap attrs, String name)
  377. {
  378. return parseLong(attrs.getNamedItem(name));
  379. }
  380. /**
  381. * Parses a long value.
  382. * @param attrs the attributes
  383. * @param name the name of the attribute to parse
  384. * @param defaultValue the default value
  385. * @return if the node is not null, the value of the parsed node, otherwise the default value
  386. */
  387. protected Long parseLong(NamedNodeMap attrs, String name, Long defaultValue)
  388. {
  389. return parseLong(attrs.getNamedItem(name), defaultValue);
  390. }
  391. /**
  392. * Parses a float value.
  393. * @param node the node to parse
  394. * @param defaultValue the default value
  395. * @return if the node is not null, the value of the parsed node, otherwise the default value
  396. */
  397. protected Float parseFloat(Node node, Float defaultValue)
  398. {
  399. return node != null ? Float.valueOf(node.getNodeValue()) : defaultValue;
  400. }
  401. /**
  402. * Parses a float value.
  403. * @param node the node to parse
  404. * @return if the node is not null, the value of the parsed node, otherwise null
  405. */
  406. protected Float parseFloat(Node node)
  407. {
  408. return parseFloat(node, null);
  409. }
  410. /**
  411. * Parses a float value.
  412. * @param attrs the attributes
  413. * @param name the name of the attribute to parse
  414. * @return if the node is not null, the value of the parsed node, otherwise null
  415. */
  416. protected Float parseFloat(NamedNodeMap attrs, String name)
  417. {
  418. return parseFloat(attrs.getNamedItem(name));
  419. }
  420. /**
  421. * Parses a float value.
  422. * @param attrs the attributes
  423. * @param name the name of the attribute to parse
  424. * @param defaultValue the default value
  425. * @return if the node is not null, the value of the parsed node, otherwise the default value
  426. */
  427. protected Float parseFloat(NamedNodeMap attrs, String name, Float defaultValue)
  428. {
  429. return parseFloat(attrs.getNamedItem(name), defaultValue);
  430. }
  431. /**
  432. * Parses a double value.
  433. * @param node the node to parse
  434. * @param defaultValue the default value
  435. * @return if the node is not null, the value of the parsed node, otherwise the default value
  436. */
  437. protected Double parseDouble(Node node, Double defaultValue)
  438. {
  439. return node != null ? Double.valueOf(node.getNodeValue()) : defaultValue;
  440. }
  441. /**
  442. * Parses a double value.
  443. * @param node the node to parse
  444. * @return if the node is not null, the value of the parsed node, otherwise null
  445. */
  446. protected Double parseDouble(Node node)
  447. {
  448. return parseDouble(node, null);
  449. }
  450. /**
  451. * Parses a double value.
  452. * @param attrs the attributes
  453. * @param name the name of the attribute to parse
  454. * @return if the node is not null, the value of the parsed node, otherwise null
  455. */
  456. protected Double parseDouble(NamedNodeMap attrs, String name)
  457. {
  458. return parseDouble(attrs.getNamedItem(name));
  459. }
  460. /**
  461. * Parses a double value.
  462. * @param attrs the attributes
  463. * @param name the name of the attribute to parse
  464. * @param defaultValue the default value
  465. * @return if the node is not null, the value of the parsed node, otherwise the default value
  466. */
  467. protected Double parseDouble(NamedNodeMap attrs, String name, Double defaultValue)
  468. {
  469. return parseDouble(attrs.getNamedItem(name), defaultValue);
  470. }
  471. /**
  472. * Parses a string value.
  473. * @param node the node to parse
  474. * @param defaultValue the default value
  475. * @return if the node is not null, the value of the parsed node, otherwise the default value
  476. */
  477. protected String parseString(Node node, String defaultValue)
  478. {
  479. return node != null ? node.getNodeValue() : defaultValue;
  480. }
  481. /**
  482. * Parses a string value.
  483. * @param node the node to parse
  484. * @return if the node is not null, the value of the parsed node, otherwise null
  485. */
  486. protected String parseString(Node node)
  487. {
  488. return parseString(node, null);
  489. }
  490. /**
  491. * Parses a string value.
  492. * @param attrs the attributes
  493. * @param name the name of the attribute to parse
  494. * @return if the node is not null, the value of the parsed node, otherwise null
  495. */
  496. protected String parseString(NamedNodeMap attrs, String name)
  497. {
  498. return parseString(attrs.getNamedItem(name));
  499. }
  500. /**
  501. * Parses a string value.
  502. * @param attrs the attributes
  503. * @param name the name of the attribute to parse
  504. * @param defaultValue the default value
  505. * @return if the node is not null, the value of the parsed node, otherwise the default value
  506. */
  507. protected String parseString(NamedNodeMap attrs, String name, String defaultValue)
  508. {
  509. return parseString(attrs.getNamedItem(name), defaultValue);
  510. }
  511. /**
  512. * Parses an enumerated value.
  513. * @param <T> the enumerated type
  514. * @param node the node to parse
  515. * @param clazz the class of the enumerated
  516. * @param defaultValue the default value
  517. * @return if the node is not null and the node value is valid the parsed value, otherwise the default value
  518. */
  519. protected <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz, T defaultValue)
  520. {
  521. if (node == null)
  522. {
  523. return defaultValue;
  524. }
  525. try
  526. {
  527. return Enum.valueOf(clazz, node.getNodeValue());
  528. }
  529. catch (IllegalArgumentException e)
  530. {
  531. _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);
  532. return defaultValue;
  533. }
  534. }
  535. /**
  536. * Parses an enumerated value.
  537. * @param <T> the enumerated type
  538. * @param node the node to parse
  539. * @param clazz the class of the enumerated
  540. * @return if the node is not null and the node value is valid the parsed value, otherwise null
  541. */
  542. protected <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz)
  543. {
  544. return parseEnum(node, clazz, null);
  545. }
  546. /**
  547. * Parses an enumerated value.
  548. * @param <T> the enumerated type
  549. * @param attrs the attributes
  550. * @param clazz the class of the enumerated
  551. * @param name the name of the attribute to parse
  552. * @return if the node is not null and the node value is valid the parsed value, otherwise null
  553. */
  554. protected <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name)
  555. {
  556. return parseEnum(attrs.getNamedItem(name), clazz);
  557. }
  558. /**
  559. * Parses an enumerated value.
  560. * @param <T> the enumerated type
  561. * @param attrs the attributes
  562. * @param clazz the class of the enumerated
  563. * @param name the name of the attribute to parse
  564. * @param defaultValue the default value
  565. * @return if the node is not null and the node value is valid the parsed value, otherwise the default value
  566. */
  567. protected <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name, T defaultValue)
  568. {
  569. return parseEnum(attrs.getNamedItem(name), clazz, defaultValue);
  570. }
  571. /**
  572. * Sets the current file filter.
  573. * @param filter the file filter
  574. */
  575. public void setCurrentFileFilter(FileFilter filter)
  576. {
  577. _currentFilter = filter;
  578. }
  579. /**
  580. * Gets the current file filter.
  581. * @return the current file filter
  582. */
  583. public FileFilter getCurrentFileFilter()
  584. {
  585. return _currentFilter != null ? _currentFilter : XML_FILTER;
  586. }
  587. /**
  588. * Simple XML error handler.
  589. * @author Zoey76
  590. */
  591. protected class XMLErrorHandler implements ErrorHandler
  592. {
  593. @Override
  594. public void warning(SAXParseException e) throws SAXParseException
  595. {
  596. throw e;
  597. }
  598. @Override
  599. public void error(SAXParseException e) throws SAXParseException
  600. {
  601. throw e;
  602. }
  603. @Override
  604. public void fatalError(SAXParseException e) throws SAXParseException
  605. {
  606. throw e;
  607. }
  608. }
  609. }