DocumentItem.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.engines.items;
  16. import java.io.File;
  17. import java.lang.reflect.Constructor;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.util.List;
  20. import java.util.logging.Level;
  21. import javolution.util.FastList;
  22. import org.w3c.dom.Document;
  23. import org.w3c.dom.Node;
  24. import com.l2jserver.gameserver.engines.DocumentBase;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.conditions.Condition;
  27. import com.l2jserver.gameserver.model.items.L2Item;
  28. /**
  29. * @author mkizub, JIV
  30. */
  31. public final class DocumentItem extends DocumentBase
  32. {
  33. private Item _currentItem = null;
  34. private final List<L2Item> _itemsInFile = new FastList<>();
  35. /**
  36. * @param file
  37. */
  38. public DocumentItem(File file)
  39. {
  40. super(file);
  41. }
  42. @Override
  43. protected StatsSet getStatsSet()
  44. {
  45. return _currentItem.set;
  46. }
  47. @Override
  48. protected String getTableValue(String name)
  49. {
  50. return _tables.get(name)[_currentItem.currentLevel];
  51. }
  52. @Override
  53. protected String getTableValue(String name, int idx)
  54. {
  55. return _tables.get(name)[idx - 1];
  56. }
  57. @Override
  58. protected void parseDocument(Document doc)
  59. {
  60. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  61. {
  62. if ("list".equalsIgnoreCase(n.getNodeName()))
  63. {
  64. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  65. {
  66. if ("item".equalsIgnoreCase(d.getNodeName()))
  67. {
  68. try
  69. {
  70. _currentItem = new Item();
  71. parseItem(d);
  72. _itemsInFile.add(_currentItem.item);
  73. resetTable();
  74. }
  75. catch (Exception e)
  76. {
  77. _log.log(Level.WARNING, "Cannot create item " + _currentItem.id, e);
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }
  84. protected void parseItem(Node n) throws InvocationTargetException
  85. {
  86. int itemId = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
  87. String className = n.getAttributes().getNamedItem("type").getNodeValue();
  88. String itemName = n.getAttributes().getNamedItem("name").getNodeValue();
  89. _currentItem.id = itemId;
  90. _currentItem.name = itemName;
  91. _currentItem.type = className;
  92. _currentItem.set = new StatsSet();
  93. _currentItem.set.set("item_id", itemId);
  94. _currentItem.set.set("name", itemName);
  95. Node first = n.getFirstChild();
  96. for (n = first; n != null; n = n.getNextSibling())
  97. {
  98. if ("table".equalsIgnoreCase(n.getNodeName()))
  99. {
  100. if (_currentItem.item != null)
  101. {
  102. throw new IllegalStateException("Item created but table node found! Item " + itemId);
  103. }
  104. parseTable(n);
  105. }
  106. else if ("set".equalsIgnoreCase(n.getNodeName()))
  107. {
  108. if (_currentItem.item != null)
  109. {
  110. throw new IllegalStateException("Item created but set node found! Item " + itemId);
  111. }
  112. parseBeanSet(n, _currentItem.set, 1);
  113. }
  114. else if ("for".equalsIgnoreCase(n.getNodeName()))
  115. {
  116. makeItem();
  117. parseTemplate(n, _currentItem.item);
  118. }
  119. else if ("cond".equalsIgnoreCase(n.getNodeName()))
  120. {
  121. makeItem();
  122. Condition condition = parseCondition(n.getFirstChild(), _currentItem.item);
  123. Node msg = n.getAttributes().getNamedItem("msg");
  124. Node msgId = n.getAttributes().getNamedItem("msgId");
  125. if ((condition != null) && (msg != null))
  126. {
  127. condition.setMessage(msg.getNodeValue());
  128. }
  129. else if ((condition != null) && (msgId != null))
  130. {
  131. condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
  132. Node addName = n.getAttributes().getNamedItem("addName");
  133. if ((addName != null) && (Integer.decode(getValue(msgId.getNodeValue(), null)) > 0))
  134. {
  135. condition.addName();
  136. }
  137. }
  138. _currentItem.item.attach(condition);
  139. }
  140. }
  141. // bah! in this point item doesn't have to be still created
  142. makeItem();
  143. }
  144. private void makeItem() throws InvocationTargetException
  145. {
  146. if (_currentItem.item != null)
  147. {
  148. return; // item is already created
  149. }
  150. try
  151. {
  152. Constructor<?> c = Class.forName("com.l2jserver.gameserver.model.items.L2" + _currentItem.type).getConstructor(StatsSet.class);
  153. _currentItem.item = (L2Item) c.newInstance(_currentItem.set);
  154. }
  155. catch (Exception e)
  156. {
  157. throw new InvocationTargetException(e);
  158. }
  159. }
  160. /**
  161. * @return
  162. */
  163. public List<L2Item> getItemList()
  164. {
  165. return _itemsInFile;
  166. }
  167. }