IXmlReader.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Copyright (C) 2004-2015 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.util.data.xml;
  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. * Interface for XML parsers.
  34. * @author Zoey76
  35. */
  36. public interface IXmlReader
  37. {
  38. static final Logger LOGGER = Logger.getLogger(IXmlReader.class.getName());
  39. static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  40. static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
  41. /** The default file filter, ".xml" files only. */
  42. static final XMLFilter XML_FILTER = new XMLFilter();
  43. /**
  44. * This method can be used to load/reload the data.<br>
  45. * It's highly recommended to clear the data storage, either the list or map.
  46. */
  47. public void load();
  48. /**
  49. * Wrapper for {@link #parseFile(File)} method.
  50. * @param path the relative path to the datapack root of the XML file to parse.
  51. */
  52. default void parseDatapackFile(String path)
  53. {
  54. parseFile(new File(Config.DATAPACK_ROOT, path));
  55. }
  56. /**
  57. * Parses a single XML file.<br>
  58. * If the file was successfully parsed, call {@link #parseDocument(Document, File)} for the parsed document.<br>
  59. * <b>Validation is enforced.</b>
  60. * @param f the XML file to parse.
  61. */
  62. default void parseFile(File f)
  63. {
  64. if (!getCurrentFileFilter().accept(f))
  65. {
  66. LOGGER.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " is not a file or it doesn't exist!");
  67. return;
  68. }
  69. final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  70. dbf.setNamespaceAware(true);
  71. dbf.setValidating(true);
  72. dbf.setIgnoringComments(true);
  73. try
  74. {
  75. dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
  76. final DocumentBuilder db = dbf.newDocumentBuilder();
  77. db.setErrorHandler(new XMLErrorHandler());
  78. parseDocument(db.parse(f), f);
  79. }
  80. catch (SAXParseException e)
  81. {
  82. LOGGER.warning(getClass().getSimpleName() + ": Could not parse file " + f.getName() + " at line " + e.getLineNumber() + ", column " + e.getColumnNumber() + ": " + e.getMessage());
  83. return;
  84. }
  85. catch (Exception e)
  86. {
  87. LOGGER.warning(getClass().getSimpleName() + ": Could not parse file " + f.getName() + ": " + e.getMessage());
  88. return;
  89. }
  90. }
  91. /**
  92. * Wrapper for {@link #parseDirectory(File, boolean)}.
  93. * @param file the path to the directory where the XML files are.
  94. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  95. */
  96. default boolean parseDirectory(File file)
  97. {
  98. return parseDirectory(file, false);
  99. }
  100. /**
  101. * Wrapper for {@link #parseDirectory(File, boolean)}.
  102. * @param path the path to the directory where the XML files are.
  103. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  104. */
  105. default boolean parseDirectory(String path)
  106. {
  107. return parseDirectory(new File(path), false);
  108. }
  109. /**
  110. * Wrapper for {@link #parseDirectory(File, boolean)}.
  111. * @param path the path to the directory where the XML files are.
  112. * @param recursive parses all sub folders if there is.
  113. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  114. */
  115. default boolean parseDirectory(String path, boolean recursive)
  116. {
  117. return parseDirectory(new File(path), recursive);
  118. }
  119. /**
  120. * Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
  121. * @param dir the directory object to scan.
  122. * @param recursive parses all sub folders if there is.
  123. * @return {@code false} if it fails to find the directory, {@code true} otherwise.
  124. */
  125. default boolean parseDirectory(File dir, boolean recursive)
  126. {
  127. if (!dir.exists())
  128. {
  129. LOGGER.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
  130. return false;
  131. }
  132. final File[] listOfFiles = dir.listFiles();
  133. for (File f : listOfFiles)
  134. {
  135. if (recursive && f.isDirectory())
  136. {
  137. parseDirectory(f, recursive);
  138. }
  139. else if (getCurrentFileFilter().accept(f))
  140. {
  141. parseFile(f);
  142. }
  143. }
  144. return true;
  145. }
  146. /**
  147. * Wrapper for {@link #parseDirectory(File, boolean)}.
  148. * @param path the path to the directory where the XML files are
  149. * @param recursive parses all sub folders if there is
  150. * @return {@code false} if it fails to find the directory, {@code true} otherwise
  151. */
  152. default boolean parseDatapackDirectory(String path, boolean recursive)
  153. {
  154. return parseDirectory(new File(Config.DATAPACK_ROOT, path), recursive);
  155. }
  156. /**
  157. * Abstract method that when implemented will parse the current document.<br>
  158. * Is expected to be call from {@link #parseFile(File)}.
  159. * @param doc the current document to parse
  160. * @param f the current file
  161. */
  162. default void parseDocument(Document doc, File f)
  163. {
  164. parseDocument(doc);
  165. }
  166. /**
  167. * Abstract method that when implemented will parse the current document.<br>
  168. * Is expected to be call from {@link #parseFile(File)}.
  169. * @param doc the current document to parse
  170. */
  171. default void parseDocument(Document doc)
  172. {
  173. LOGGER.severe("Parser not implemented!");
  174. }
  175. /**
  176. * Parses a boolean value.
  177. * @param node the node to parse
  178. * @param defaultValue the default value
  179. * @return if the node is not null, the value of the parsed node, otherwise the default value
  180. */
  181. default Boolean parseBoolean(Node node, Boolean defaultValue)
  182. {
  183. return node != null ? Boolean.valueOf(node.getNodeValue()) : defaultValue;
  184. }
  185. /**
  186. * Parses a boolean value.
  187. * @param node the node to parse
  188. * @return if the node is not null, the value of the parsed node, otherwise null
  189. */
  190. default Boolean parseBoolean(Node node)
  191. {
  192. return parseBoolean(node, null);
  193. }
  194. /**
  195. * Parses a boolean value.
  196. * @param attrs the attributes
  197. * @param name the name of the attribute to parse
  198. * @return if the node is not null, the value of the parsed node, otherwise null
  199. */
  200. default Boolean parseBoolean(NamedNodeMap attrs, String name)
  201. {
  202. return parseBoolean(attrs.getNamedItem(name));
  203. }
  204. /**
  205. * Parses a boolean value.
  206. * @param attrs the attributes
  207. * @param name the name of the attribute to parse
  208. * @param defaultValue the default value
  209. * @return if the node is not null, the value of the parsed node, otherwise the default value
  210. */
  211. default Boolean parseBoolean(NamedNodeMap attrs, String name, Boolean defaultValue)
  212. {
  213. return parseBoolean(attrs.getNamedItem(name), defaultValue);
  214. }
  215. /**
  216. * Parses a byte value.
  217. * @param node the node to parse
  218. * @param defaultValue the default value
  219. * @return if the node is not null, the value of the parsed node, otherwise the default value
  220. */
  221. default Byte parseByte(Node node, Byte defaultValue)
  222. {
  223. return node != null ? Byte.valueOf(node.getNodeValue()) : defaultValue;
  224. }
  225. /**
  226. * Parses a byte value.
  227. * @param node the node to parse
  228. * @return if the node is not null, the value of the parsed node, otherwise null
  229. */
  230. default Byte parseByte(Node node)
  231. {
  232. return parseByte(node, null);
  233. }
  234. /**
  235. * Parses a byte value.
  236. * @param attrs the attributes
  237. * @param name the name of the attribute to parse
  238. * @return if the node is not null, the value of the parsed node, otherwise null
  239. */
  240. default Byte parseByte(NamedNodeMap attrs, String name)
  241. {
  242. return parseByte(attrs.getNamedItem(name));
  243. }
  244. /**
  245. * Parses a byte value.
  246. * @param attrs the attributes
  247. * @param name the name of the attribute to parse
  248. * @param defaultValue the default value
  249. * @return if the node is not null, the value of the parsed node, otherwise the default value
  250. */
  251. default Byte parseByte(NamedNodeMap attrs, String name, Byte defaultValue)
  252. {
  253. return parseByte(attrs.getNamedItem(name), defaultValue);
  254. }
  255. /**
  256. * Parses a short value.
  257. * @param node the node to parse
  258. * @param defaultValue the default value
  259. * @return if the node is not null, the value of the parsed node, otherwise the default value
  260. */
  261. default Short parseShort(Node node, Short defaultValue)
  262. {
  263. return node != null ? Short.valueOf(node.getNodeValue()) : defaultValue;
  264. }
  265. /**
  266. * Parses a short value.
  267. * @param node the node to parse
  268. * @return if the node is not null, the value of the parsed node, otherwise null
  269. */
  270. default Short parseShort(Node node)
  271. {
  272. return parseShort(node, null);
  273. }
  274. /**
  275. * Parses a short value.
  276. * @param attrs the attributes
  277. * @param name the name of the attribute to parse
  278. * @return if the node is not null, the value of the parsed node, otherwise null
  279. */
  280. default Short parseShort(NamedNodeMap attrs, String name)
  281. {
  282. return parseShort(attrs.getNamedItem(name));
  283. }
  284. /**
  285. * Parses a short value.
  286. * @param attrs the attributes
  287. * @param name the name of the attribute to parse
  288. * @param defaultValue the default value
  289. * @return if the node is not null, the value of the parsed node, otherwise the default value
  290. */
  291. default Short parseShort(NamedNodeMap attrs, String name, Short defaultValue)
  292. {
  293. return parseShort(attrs.getNamedItem(name), defaultValue);
  294. }
  295. /**
  296. * Parses an int value.
  297. * @param node the node to parse
  298. * @param defaultValue the default value
  299. * @return if the node is not null, the value of the parsed node, otherwise the default value
  300. */
  301. default int parseInt(Node node, Integer defaultValue)
  302. {
  303. return node != null ? Integer.parseInt(node.getNodeValue()) : defaultValue;
  304. }
  305. /**
  306. * Parses an int value.
  307. * @param node the node to parse
  308. * @return if the node is not null, the value of the parsed node, otherwise the default value
  309. */
  310. default int parseInt(Node node)
  311. {
  312. return parseInt(node, -1);
  313. }
  314. /**
  315. * Parses an integer value.
  316. * @param node the node to parse
  317. * @param defaultValue the default value
  318. * @return if the node is not null, the value of the parsed node, otherwise the default value
  319. */
  320. default Integer parseInteger(Node node, Integer defaultValue)
  321. {
  322. return node != null ? Integer.valueOf(node.getNodeValue()) : defaultValue;
  323. }
  324. /**
  325. * Parses an integer value.
  326. * @param node the node to parse
  327. * @return if the node is not null, the value of the parsed node, otherwise null
  328. */
  329. default Integer parseInteger(Node node)
  330. {
  331. return parseInteger(node, null);
  332. }
  333. /**
  334. * Parses an integer value.
  335. * @param attrs the attributes
  336. * @param name the name of the attribute to parse
  337. * @return if the node is not null, the value of the parsed node, otherwise null
  338. */
  339. default Integer parseInteger(NamedNodeMap attrs, String name)
  340. {
  341. return parseInteger(attrs.getNamedItem(name));
  342. }
  343. /**
  344. * Parses an integer value.
  345. * @param attrs the attributes
  346. * @param name the name of the attribute to parse
  347. * @param defaultValue the default value
  348. * @return if the node is not null, the value of the parsed node, otherwise the default value
  349. */
  350. default Integer parseInteger(NamedNodeMap attrs, String name, Integer defaultValue)
  351. {
  352. return parseInteger(attrs.getNamedItem(name), defaultValue);
  353. }
  354. /**
  355. * Parses a long value.
  356. * @param node the node to parse
  357. * @param defaultValue the default value
  358. * @return if the node is not null, the value of the parsed node, otherwise the default value
  359. */
  360. default Long parseLong(Node node, Long defaultValue)
  361. {
  362. return node != null ? Long.valueOf(node.getNodeValue()) : defaultValue;
  363. }
  364. /**
  365. * Parses a long value.
  366. * @param node the node to parse
  367. * @return if the node is not null, the value of the parsed node, otherwise null
  368. */
  369. default Long parseLong(Node node)
  370. {
  371. return parseLong(node, null);
  372. }
  373. /**
  374. * Parses a long value.
  375. * @param attrs the attributes
  376. * @param name the name of the attribute to parse
  377. * @return if the node is not null, the value of the parsed node, otherwise null
  378. */
  379. default Long parseLong(NamedNodeMap attrs, String name)
  380. {
  381. return parseLong(attrs.getNamedItem(name));
  382. }
  383. /**
  384. * Parses a long value.
  385. * @param attrs the attributes
  386. * @param name the name of the attribute to parse
  387. * @param defaultValue the default value
  388. * @return if the node is not null, the value of the parsed node, otherwise the default value
  389. */
  390. default Long parseLong(NamedNodeMap attrs, String name, Long defaultValue)
  391. {
  392. return parseLong(attrs.getNamedItem(name), defaultValue);
  393. }
  394. /**
  395. * Parses a float value.
  396. * @param node the node to parse
  397. * @param defaultValue the default value
  398. * @return if the node is not null, the value of the parsed node, otherwise the default value
  399. */
  400. default Float parseFloat(Node node, Float defaultValue)
  401. {
  402. return node != null ? Float.valueOf(node.getNodeValue()) : defaultValue;
  403. }
  404. /**
  405. * Parses a float value.
  406. * @param node the node to parse
  407. * @return if the node is not null, the value of the parsed node, otherwise null
  408. */
  409. default Float parseFloat(Node node)
  410. {
  411. return parseFloat(node, null);
  412. }
  413. /**
  414. * Parses a float value.
  415. * @param attrs the attributes
  416. * @param name the name of the attribute to parse
  417. * @return if the node is not null, the value of the parsed node, otherwise null
  418. */
  419. default Float parseFloat(NamedNodeMap attrs, String name)
  420. {
  421. return parseFloat(attrs.getNamedItem(name));
  422. }
  423. /**
  424. * Parses a float value.
  425. * @param attrs the attributes
  426. * @param name the name of the attribute to parse
  427. * @param defaultValue the default value
  428. * @return if the node is not null, the value of the parsed node, otherwise the default value
  429. */
  430. default Float parseFloat(NamedNodeMap attrs, String name, Float defaultValue)
  431. {
  432. return parseFloat(attrs.getNamedItem(name), defaultValue);
  433. }
  434. /**
  435. * Parses a double value.
  436. * @param node the node to parse
  437. * @param defaultValue the default value
  438. * @return if the node is not null, the value of the parsed node, otherwise the default value
  439. */
  440. default Double parseDouble(Node node, Double defaultValue)
  441. {
  442. return node != null ? Double.valueOf(node.getNodeValue()) : defaultValue;
  443. }
  444. /**
  445. * Parses a double value.
  446. * @param node the node to parse
  447. * @return if the node is not null, the value of the parsed node, otherwise null
  448. */
  449. default Double parseDouble(Node node)
  450. {
  451. return parseDouble(node, null);
  452. }
  453. /**
  454. * Parses a double value.
  455. * @param attrs the attributes
  456. * @param name the name of the attribute to parse
  457. * @return if the node is not null, the value of the parsed node, otherwise null
  458. */
  459. default Double parseDouble(NamedNodeMap attrs, String name)
  460. {
  461. return parseDouble(attrs.getNamedItem(name));
  462. }
  463. /**
  464. * Parses a double value.
  465. * @param attrs the attributes
  466. * @param name the name of the attribute to parse
  467. * @param defaultValue the default value
  468. * @return if the node is not null, the value of the parsed node, otherwise the default value
  469. */
  470. default Double parseDouble(NamedNodeMap attrs, String name, Double defaultValue)
  471. {
  472. return parseDouble(attrs.getNamedItem(name), defaultValue);
  473. }
  474. /**
  475. * Parses a string value.
  476. * @param node the node to parse
  477. * @param defaultValue the default value
  478. * @return if the node is not null, the value of the parsed node, otherwise the default value
  479. */
  480. default String parseString(Node node, String defaultValue)
  481. {
  482. return node != null ? node.getNodeValue() : defaultValue;
  483. }
  484. /**
  485. * Parses a string value.
  486. * @param node the node to parse
  487. * @return if the node is not null, the value of the parsed node, otherwise null
  488. */
  489. default String parseString(Node node)
  490. {
  491. return parseString(node, null);
  492. }
  493. /**
  494. * Parses a string value.
  495. * @param attrs the attributes
  496. * @param name the name of the attribute to parse
  497. * @return if the node is not null, the value of the parsed node, otherwise null
  498. */
  499. default String parseString(NamedNodeMap attrs, String name)
  500. {
  501. return parseString(attrs.getNamedItem(name));
  502. }
  503. /**
  504. * Parses a string value.
  505. * @param attrs the attributes
  506. * @param name the name of the attribute to parse
  507. * @param defaultValue the default value
  508. * @return if the node is not null, the value of the parsed node, otherwise the default value
  509. */
  510. default String parseString(NamedNodeMap attrs, String name, String defaultValue)
  511. {
  512. return parseString(attrs.getNamedItem(name), defaultValue);
  513. }
  514. /**
  515. * Parses an enumerated value.
  516. * @param <T> the enumerated type
  517. * @param node the node to parse
  518. * @param clazz the class of the enumerated
  519. * @param defaultValue the default value
  520. * @return if the node is not null and the node value is valid the parsed value, otherwise the default value
  521. */
  522. default <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz, T defaultValue)
  523. {
  524. if (node == null)
  525. {
  526. return defaultValue;
  527. }
  528. try
  529. {
  530. return Enum.valueOf(clazz, node.getNodeValue());
  531. }
  532. catch (IllegalArgumentException e)
  533. {
  534. LOGGER.warning("Invalid value specified for node: " + node.getNodeName() + " specified value: " + node.getNodeValue() + " should be enum value of \"" + clazz.getSimpleName() + "\" using default value: " + defaultValue);
  535. return defaultValue;
  536. }
  537. }
  538. /**
  539. * Parses an enumerated value.
  540. * @param <T> the enumerated type
  541. * @param node the node to parse
  542. * @param clazz the class of the enumerated
  543. * @return if the node is not null and the node value is valid the parsed value, otherwise null
  544. */
  545. default <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz)
  546. {
  547. return parseEnum(node, clazz, null);
  548. }
  549. /**
  550. * Parses an enumerated value.
  551. * @param <T> the enumerated type
  552. * @param attrs the attributes
  553. * @param clazz the class of the enumerated
  554. * @param name the name of the attribute to parse
  555. * @return if the node is not null and the node value is valid the parsed value, otherwise null
  556. */
  557. default <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name)
  558. {
  559. return parseEnum(attrs.getNamedItem(name), clazz);
  560. }
  561. /**
  562. * Parses an enumerated value.
  563. * @param <T> the enumerated type
  564. * @param attrs the attributes
  565. * @param clazz the class of the enumerated
  566. * @param name the name of the attribute to parse
  567. * @param defaultValue the default value
  568. * @return if the node is not null and the node value is valid the parsed value, otherwise the default value
  569. */
  570. default <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name, T defaultValue)
  571. {
  572. return parseEnum(attrs.getNamedItem(name), clazz, defaultValue);
  573. }
  574. /**
  575. * Gets the current file filter.
  576. * @return the current file filter
  577. */
  578. default FileFilter getCurrentFileFilter()
  579. {
  580. return XML_FILTER;
  581. }
  582. /**
  583. * Simple XML error handler.
  584. * @author Zoey76
  585. */
  586. static 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. }