DocumentItem.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 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. throw new IllegalStateException("Item created but table node found! Item "+itemId);
  102. parseTable(n);
  103. }
  104. else if ("set".equalsIgnoreCase(n.getNodeName()))
  105. {
  106. if (_currentItem.item != null)
  107. throw new IllegalStateException("Item created but set node found! Item "+itemId);
  108. parseBeanSet(n, _currentItem.set, 1);
  109. }
  110. else if ("for".equalsIgnoreCase(n.getNodeName()))
  111. {
  112. makeItem();
  113. parseTemplate(n, _currentItem.item);
  114. }
  115. else if ("cond".equalsIgnoreCase(n.getNodeName()))
  116. {
  117. makeItem();
  118. Condition condition = parseCondition(n.getFirstChild(), _currentItem.item );
  119. Node msg = n.getAttributes().getNamedItem("msg");
  120. Node msgId = n.getAttributes().getNamedItem("msgId");
  121. if (condition != null && msg != null)
  122. condition.setMessage(msg.getNodeValue());
  123. else if (condition != null && msgId != null)
  124. {
  125. condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
  126. Node addName = n.getAttributes().getNamedItem("addName");
  127. if (addName != null && Integer.decode(getValue(msgId.getNodeValue(), null)) > 0)
  128. condition.addName();
  129. }
  130. _currentItem.item.attach(condition);
  131. }
  132. }
  133. //bah! in this point item doesn't have to be still created
  134. makeItem();
  135. }
  136. private void makeItem() throws InvocationTargetException
  137. {
  138. if (_currentItem.item != null)
  139. return; // item is already created
  140. try
  141. {
  142. Constructor<?> c = Class.forName("com.l2jserver.gameserver.model.items.L2"+_currentItem.type).getConstructor(StatsSet.class);
  143. _currentItem.item = (L2Item) c.newInstance(_currentItem.set);
  144. }
  145. catch (Exception e)
  146. {
  147. throw new InvocationTargetException(e);
  148. }
  149. }
  150. /**
  151. * @return
  152. */
  153. public List<L2Item> getItemList()
  154. {
  155. return _itemsInFile;
  156. }
  157. }