DocumentItem.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.items;
  20. import java.io.File;
  21. import java.lang.reflect.Constructor;
  22. import java.lang.reflect.InvocationTargetException;
  23. import java.util.List;
  24. import java.util.logging.Level;
  25. import javolution.util.FastList;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.Node;
  28. import com.l2jserver.gameserver.engines.DocumentBase;
  29. import com.l2jserver.gameserver.model.StatsSet;
  30. import com.l2jserver.gameserver.model.conditions.Condition;
  31. import com.l2jserver.gameserver.model.items.L2Item;
  32. /**
  33. * @author mkizub, JIV
  34. */
  35. public final class DocumentItem extends DocumentBase
  36. {
  37. private Item _currentItem = null;
  38. private final List<L2Item> _itemsInFile = new FastList<>();
  39. /**
  40. * @param file
  41. */
  42. public DocumentItem(File file)
  43. {
  44. super(file);
  45. }
  46. @Override
  47. protected StatsSet getStatsSet()
  48. {
  49. return _currentItem.set;
  50. }
  51. @Override
  52. protected String getTableValue(String name)
  53. {
  54. return _tables.get(name)[_currentItem.currentLevel];
  55. }
  56. @Override
  57. protected String getTableValue(String name, int idx)
  58. {
  59. return _tables.get(name)[idx - 1];
  60. }
  61. @Override
  62. protected void parseDocument(Document doc)
  63. {
  64. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  65. {
  66. if ("list".equalsIgnoreCase(n.getNodeName()))
  67. {
  68. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  69. {
  70. if ("item".equalsIgnoreCase(d.getNodeName()))
  71. {
  72. try
  73. {
  74. _currentItem = new Item();
  75. parseItem(d);
  76. _itemsInFile.add(_currentItem.item);
  77. resetTable();
  78. }
  79. catch (Exception e)
  80. {
  81. _log.log(Level.WARNING, "Cannot create item " + _currentItem.id, e);
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. protected void parseItem(Node n) throws InvocationTargetException
  89. {
  90. int itemId = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
  91. String className = n.getAttributes().getNamedItem("type").getNodeValue();
  92. String itemName = n.getAttributes().getNamedItem("name").getNodeValue();
  93. _currentItem.id = itemId;
  94. _currentItem.name = itemName;
  95. _currentItem.type = className;
  96. _currentItem.set = new StatsSet();
  97. _currentItem.set.set("item_id", itemId);
  98. _currentItem.set.set("name", itemName);
  99. Node first = n.getFirstChild();
  100. for (n = first; n != null; n = n.getNextSibling())
  101. {
  102. if ("table".equalsIgnoreCase(n.getNodeName()))
  103. {
  104. if (_currentItem.item != null)
  105. {
  106. throw new IllegalStateException("Item created but table node found! Item " + itemId);
  107. }
  108. parseTable(n);
  109. }
  110. else if ("set".equalsIgnoreCase(n.getNodeName()))
  111. {
  112. if (_currentItem.item != null)
  113. {
  114. throw new IllegalStateException("Item created but set node found! Item " + itemId);
  115. }
  116. parseBeanSet(n, _currentItem.set, 1);
  117. }
  118. else if ("for".equalsIgnoreCase(n.getNodeName()))
  119. {
  120. makeItem();
  121. parseTemplate(n, _currentItem.item);
  122. }
  123. else if ("cond".equalsIgnoreCase(n.getNodeName()))
  124. {
  125. makeItem();
  126. Condition condition = parseCondition(n.getFirstChild(), _currentItem.item);
  127. Node msg = n.getAttributes().getNamedItem("msg");
  128. Node msgId = n.getAttributes().getNamedItem("msgId");
  129. if ((condition != null) && (msg != null))
  130. {
  131. condition.setMessage(msg.getNodeValue());
  132. }
  133. else if ((condition != null) && (msgId != null))
  134. {
  135. condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
  136. Node addName = n.getAttributes().getNamedItem("addName");
  137. if ((addName != null) && (Integer.decode(getValue(msgId.getNodeValue(), null)) > 0))
  138. {
  139. condition.addName();
  140. }
  141. }
  142. _currentItem.item.attach(condition);
  143. }
  144. }
  145. // bah! in this point item doesn't have to be still created
  146. makeItem();
  147. }
  148. private void makeItem() throws InvocationTargetException
  149. {
  150. if (_currentItem.item != null)
  151. {
  152. return; // item is already created
  153. }
  154. try
  155. {
  156. Constructor<?> c = Class.forName("com.l2jserver.gameserver.model.items.L2" + _currentItem.type).getConstructor(StatsSet.class);
  157. _currentItem.item = (L2Item) c.newInstance(_currentItem.set);
  158. }
  159. catch (Exception e)
  160. {
  161. throw new InvocationTargetException(e);
  162. }
  163. }
  164. /**
  165. * @return
  166. */
  167. public List<L2Item> getItemList()
  168. {
  169. return _itemsInFile;
  170. }
  171. }