IXmlReader.java 20 KB

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