This commit is contained in:
1326
trunk/java/com/l2jserver/gameserver/engines/DocumentBase.java
Normal file
1326
trunk/java/com/l2jserver/gameserver/engines/DocumentBase.java
Normal file
File diff suppressed because it is too large
Load Diff
131
trunk/java/com/l2jserver/gameserver/engines/DocumentEngine.java
Normal file
131
trunk/java/com/l2jserver/gameserver/engines/DocumentEngine.java
Normal file
@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.engines;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javolution.util.FastList;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.gameserver.datatables.SkillData;
|
||||
import com.l2jserver.gameserver.engines.items.DocumentItem;
|
||||
import com.l2jserver.gameserver.engines.skills.DocumentSkill;
|
||||
import com.l2jserver.gameserver.model.items.L2Item;
|
||||
import com.l2jserver.gameserver.model.skills.Skill;
|
||||
import com.l2jserver.util.file.filter.XMLFilter;
|
||||
|
||||
/**
|
||||
* @author mkizub
|
||||
*/
|
||||
public class DocumentEngine
|
||||
{
|
||||
private static final Logger _log = Logger.getLogger(DocumentEngine.class.getName());
|
||||
|
||||
private final List<File> _itemFiles = new FastList<>();
|
||||
private final List<File> _skillFiles = new FastList<>();
|
||||
|
||||
public static DocumentEngine getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
protected DocumentEngine()
|
||||
{
|
||||
hashFiles("data/stats/items", _itemFiles);
|
||||
if (Config.CUSTOM_ITEMS_LOAD)
|
||||
{
|
||||
hashFiles("data/stats/items/custom", _itemFiles);
|
||||
}
|
||||
hashFiles("data/stats/skills", _skillFiles);
|
||||
if (Config.CUSTOM_SKILLS_LOAD)
|
||||
{
|
||||
hashFiles("data/stats/skills/custom", _skillFiles);
|
||||
}
|
||||
}
|
||||
|
||||
private void hashFiles(String dirname, List<File> hash)
|
||||
{
|
||||
File dir = new File(Config.DATAPACK_ROOT, dirname);
|
||||
if (!dir.exists())
|
||||
{
|
||||
_log.warning("Dir " + dir.getAbsolutePath() + " not exists");
|
||||
return;
|
||||
}
|
||||
File[] files = dir.listFiles(new XMLFilter());
|
||||
for (File f : files)
|
||||
{
|
||||
hash.add(f);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Skill> loadSkills(File file)
|
||||
{
|
||||
if (file == null)
|
||||
{
|
||||
_log.warning("Skill file not found.");
|
||||
return null;
|
||||
}
|
||||
DocumentSkill doc = new DocumentSkill(file);
|
||||
doc.parse();
|
||||
return doc.getSkills();
|
||||
}
|
||||
|
||||
public void loadAllSkills(final Map<Integer, Skill> allSkills)
|
||||
{
|
||||
int count = 0;
|
||||
for (File file : _skillFiles)
|
||||
{
|
||||
List<Skill> s = loadSkills(file);
|
||||
if (s == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
for (Skill skill : s)
|
||||
{
|
||||
allSkills.put(SkillData.getSkillHashCode(skill), skill);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
_log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return created items
|
||||
* @return List of {@link L2Item}
|
||||
*/
|
||||
public List<L2Item> loadItems()
|
||||
{
|
||||
List<L2Item> list = new FastList<>();
|
||||
for (File f : _itemFiles)
|
||||
{
|
||||
DocumentItem document = new DocumentItem(f);
|
||||
document.parse();
|
||||
list.addAll(document.getItemList());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final DocumentEngine _instance = new DocumentEngine();
|
||||
}
|
||||
}
|
649
trunk/java/com/l2jserver/gameserver/engines/DocumentParser.java
Normal file
649
trunk/java/com/l2jserver/gameserver/engines/DocumentParser.java
Normal file
@ -0,0 +1,649 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.engines;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.SAXParseException;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.util.file.filter.XMLFilter;
|
||||
|
||||
/**
|
||||
* Abstract class for XML parsers.
|
||||
* @author Zoey76
|
||||
*/
|
||||
public interface DocumentParser
|
||||
{
|
||||
static final Logger LOGGER = Logger.getLogger(DocumentParser.class.getName());
|
||||
|
||||
static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
|
||||
static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
|
||||
/** The default file filter, ".xml" files only. */
|
||||
static final XMLFilter XML_FILTER = new XMLFilter();
|
||||
|
||||
/**
|
||||
* This method can be used to load/reload the data.<br>
|
||||
* It's highly recommended to clear the data storage, either the list or map.
|
||||
*/
|
||||
public void load();
|
||||
|
||||
/**
|
||||
* Wrapper for {@link #parseFile(File)} method.
|
||||
* @param path the relative path to the datapack root of the XML file to parse.
|
||||
*/
|
||||
default void parseDatapackFile(String path)
|
||||
{
|
||||
parseFile(new File(Config.DATAPACK_ROOT, path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a single XML file.<br>
|
||||
* If the file was successfully parsed, call {@link #parseDocument(Document, File)} for the parsed document.<br>
|
||||
* <b>Validation is enforced.</b>
|
||||
* @param f the XML file to parse.
|
||||
*/
|
||||
default void parseFile(File f)
|
||||
{
|
||||
if (!getCurrentFileFilter().accept(f))
|
||||
{
|
||||
LOGGER.warning(getClass().getSimpleName() + ": Could not parse " + f.getName() + " is not a file or it doesn't exist!");
|
||||
return;
|
||||
}
|
||||
|
||||
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setNamespaceAware(true);
|
||||
dbf.setValidating(true);
|
||||
dbf.setIgnoringComments(true);
|
||||
try
|
||||
{
|
||||
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
|
||||
final DocumentBuilder db = dbf.newDocumentBuilder();
|
||||
db.setErrorHandler(new XMLErrorHandler());
|
||||
parseDocument(db.parse(f), f);
|
||||
}
|
||||
catch (SAXParseException e)
|
||||
{
|
||||
LOGGER.warning(getClass().getSimpleName() + ": Could not parse file " + f.getName() + " at line " + e.getLineNumber() + ", column " + e.getColumnNumber() + ": " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LOGGER.warning(getClass().getSimpleName() + ": Could not parse file " + f.getName() + ": " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for {@link #parseDirectory(File, boolean)}.
|
||||
* @param file the path to the directory where the XML files are.
|
||||
* @return {@code false} if it fails to find the directory, {@code true} otherwise.
|
||||
*/
|
||||
default boolean parseDirectory(File file)
|
||||
{
|
||||
return parseDirectory(file, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all XML files from {@code path} and calls {@link #parseFile(File)} for each one of them.
|
||||
* @param dir the directory object to scan.
|
||||
* @param recursive parses all sub folders if there is.
|
||||
* @return {@code false} if it fails to find the directory, {@code true} otherwise.
|
||||
*/
|
||||
default boolean parseDirectory(File dir, boolean recursive)
|
||||
{
|
||||
if (!dir.exists())
|
||||
{
|
||||
LOGGER.warning(getClass().getSimpleName() + ": Folder " + dir.getAbsolutePath() + " doesn't exist!");
|
||||
return false;
|
||||
}
|
||||
|
||||
final File[] listOfFiles = dir.listFiles();
|
||||
for (File f : listOfFiles)
|
||||
{
|
||||
if (recursive && f.isDirectory())
|
||||
{
|
||||
parseDirectory(f, recursive);
|
||||
}
|
||||
else if (getCurrentFileFilter().accept(f))
|
||||
{
|
||||
parseFile(f);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper for {@link #parseDirectory(File, boolean)}.
|
||||
* @param path the path to the directory where the XML files are
|
||||
* @param recursive parses all sub folders if there is
|
||||
* @return {@code false} if it fails to find the directory, {@code true} otherwise
|
||||
*/
|
||||
default boolean parseDatapackDirectory(String path, boolean recursive)
|
||||
{
|
||||
File dir = new File(path);
|
||||
if (!dir.exists())
|
||||
{
|
||||
dir = new File(Config.DATAPACK_ROOT, path);
|
||||
}
|
||||
return parseDirectory(dir, recursive);
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method that when implemented will parse the current document.<br>
|
||||
* Is expected to be call from {@link #parseFile(File)}.
|
||||
* @param doc the current document to parse
|
||||
* @param f the current file
|
||||
*/
|
||||
default void parseDocument(Document doc, File f)
|
||||
{
|
||||
parseDocument(doc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract method that when implemented will parse the current document.<br>
|
||||
* Is expected to be call from {@link #parseFile(File)}.
|
||||
* @param doc the current document to parse
|
||||
*/
|
||||
default void parseDocument(Document doc)
|
||||
{
|
||||
LOGGER.severe("Parser not implemented!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a boolean value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Boolean parseBoolean(Node node, Boolean defaultValue)
|
||||
{
|
||||
return node != null ? Boolean.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a boolean value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Boolean parseBoolean(Node node)
|
||||
{
|
||||
return parseBoolean(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a boolean value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Boolean parseBoolean(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseBoolean(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a boolean value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Boolean parseBoolean(NamedNodeMap attrs, String name, Boolean defaultValue)
|
||||
{
|
||||
return parseBoolean(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a byte value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Byte parseByte(Node node, Byte defaultValue)
|
||||
{
|
||||
return node != null ? Byte.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a byte value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Byte parseByte(Node node)
|
||||
{
|
||||
return parseByte(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a byte value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Byte parseByte(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseByte(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a byte value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Byte parseByte(NamedNodeMap attrs, String name, Byte defaultValue)
|
||||
{
|
||||
return parseByte(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a short value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Short parseShort(Node node, Short defaultValue)
|
||||
{
|
||||
return node != null ? Short.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a short value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Short parseShort(Node node)
|
||||
{
|
||||
return parseShort(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a short value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Short parseShort(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseShort(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a short value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Short parseShort(NamedNodeMap attrs, String name, Short defaultValue)
|
||||
{
|
||||
return parseShort(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an int value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default int parseInt(Node node, Integer defaultValue)
|
||||
{
|
||||
return node != null ? Integer.parseInt(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an int value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default int parseInt(Node node)
|
||||
{
|
||||
return parseInt(node, -1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an integer value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Integer parseInteger(Node node, Integer defaultValue)
|
||||
{
|
||||
return node != null ? Integer.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an integer value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Integer parseInteger(Node node)
|
||||
{
|
||||
return parseInteger(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an integer value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Integer parseInteger(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseInteger(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an integer value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Integer parseInteger(NamedNodeMap attrs, String name, Integer defaultValue)
|
||||
{
|
||||
return parseInteger(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a long value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Long parseLong(Node node, Long defaultValue)
|
||||
{
|
||||
return node != null ? Long.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a long value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Long parseLong(Node node)
|
||||
{
|
||||
return parseLong(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a long value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Long parseLong(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseLong(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a long value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Long parseLong(NamedNodeMap attrs, String name, Long defaultValue)
|
||||
{
|
||||
return parseLong(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a float value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Float parseFloat(Node node, Float defaultValue)
|
||||
{
|
||||
return node != null ? Float.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a float value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Float parseFloat(Node node)
|
||||
{
|
||||
return parseFloat(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a float value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Float parseFloat(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseFloat(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a float value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Float parseFloat(NamedNodeMap attrs, String name, Float defaultValue)
|
||||
{
|
||||
return parseFloat(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a double value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Double parseDouble(Node node, Double defaultValue)
|
||||
{
|
||||
return node != null ? Double.valueOf(node.getNodeValue()) : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a double value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Double parseDouble(Node node)
|
||||
{
|
||||
return parseDouble(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a double value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default Double parseDouble(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseDouble(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a double value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default Double parseDouble(NamedNodeMap attrs, String name, Double defaultValue)
|
||||
{
|
||||
return parseDouble(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string value.
|
||||
* @param node the node to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default String parseString(Node node, String defaultValue)
|
||||
{
|
||||
return node != null ? node.getNodeValue() : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string value.
|
||||
* @param node the node to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default String parseString(Node node)
|
||||
{
|
||||
return parseString(node, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null, the value of the parsed node, otherwise null
|
||||
*/
|
||||
default String parseString(NamedNodeMap attrs, String name)
|
||||
{
|
||||
return parseString(attrs.getNamedItem(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string value.
|
||||
* @param attrs the attributes
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null, the value of the parsed node, otherwise the default value
|
||||
*/
|
||||
default String parseString(NamedNodeMap attrs, String name, String defaultValue)
|
||||
{
|
||||
return parseString(attrs.getNamedItem(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an enumerated value.
|
||||
* @param <T> the enumerated type
|
||||
* @param node the node to parse
|
||||
* @param clazz the class of the enumerated
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null and the node value is valid the parsed value, otherwise the default value
|
||||
*/
|
||||
default <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz, T defaultValue)
|
||||
{
|
||||
if (node == null)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return Enum.valueOf(clazz, node.getNodeValue());
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
{
|
||||
LOGGER.warning("Invalid value specified for node: " + node.getNodeName() + " specified value: " + node.getNodeValue() + " should be enum value of \"" + clazz.getSimpleName() + "\" using default value: " + defaultValue);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an enumerated value.
|
||||
* @param <T> the enumerated type
|
||||
* @param node the node to parse
|
||||
* @param clazz the class of the enumerated
|
||||
* @return if the node is not null and the node value is valid the parsed value, otherwise null
|
||||
*/
|
||||
default <T extends Enum<T>> T parseEnum(Node node, Class<T> clazz)
|
||||
{
|
||||
return parseEnum(node, clazz, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an enumerated value.
|
||||
* @param <T> the enumerated type
|
||||
* @param attrs the attributes
|
||||
* @param clazz the class of the enumerated
|
||||
* @param name the name of the attribute to parse
|
||||
* @return if the node is not null and the node value is valid the parsed value, otherwise null
|
||||
*/
|
||||
default <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name)
|
||||
{
|
||||
return parseEnum(attrs.getNamedItem(name), clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an enumerated value.
|
||||
* @param <T> the enumerated type
|
||||
* @param attrs the attributes
|
||||
* @param clazz the class of the enumerated
|
||||
* @param name the name of the attribute to parse
|
||||
* @param defaultValue the default value
|
||||
* @return if the node is not null and the node value is valid the parsed value, otherwise the default value
|
||||
*/
|
||||
default <T extends Enum<T>> T parseEnum(NamedNodeMap attrs, Class<T> clazz, String name, T defaultValue)
|
||||
{
|
||||
return parseEnum(attrs.getNamedItem(name), clazz, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current file filter.
|
||||
* @return the current file filter
|
||||
*/
|
||||
default FileFilter getCurrentFileFilter()
|
||||
{
|
||||
return XML_FILTER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple XML error handler.
|
||||
* @author Zoey76
|
||||
*/
|
||||
class XMLErrorHandler implements ErrorHandler
|
||||
{
|
||||
@Override
|
||||
public void warning(SAXParseException e) throws SAXParseException
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(SAXParseException e) throws SAXParseException
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatalError(SAXParseException e) throws SAXParseException
|
||||
{
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.engines.items;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import javolution.util.FastList;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.l2jserver.gameserver.engines.DocumentBase;
|
||||
import com.l2jserver.gameserver.model.StatsSet;
|
||||
import com.l2jserver.gameserver.model.conditions.Condition;
|
||||
import com.l2jserver.gameserver.model.items.L2Item;
|
||||
|
||||
/**
|
||||
* @author mkizub, JIV
|
||||
*/
|
||||
public final class DocumentItem extends DocumentBase
|
||||
{
|
||||
private Item _currentItem = null;
|
||||
private final List<L2Item> _itemsInFile = new FastList<>();
|
||||
|
||||
/**
|
||||
* @param file
|
||||
*/
|
||||
public DocumentItem(File file)
|
||||
{
|
||||
super(file);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StatsSet getStatsSet()
|
||||
{
|
||||
return _currentItem.set;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTableValue(String name)
|
||||
{
|
||||
return _tables.get(name)[_currentItem.currentLevel];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTableValue(String name, int idx)
|
||||
{
|
||||
return _tables.get(name)[idx - 1];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void parseDocument(Document doc)
|
||||
{
|
||||
for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
|
||||
{
|
||||
if ("list".equalsIgnoreCase(n.getNodeName()))
|
||||
{
|
||||
|
||||
for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
|
||||
{
|
||||
if ("item".equalsIgnoreCase(d.getNodeName()))
|
||||
{
|
||||
try
|
||||
{
|
||||
_currentItem = new Item();
|
||||
parseItem(d);
|
||||
_itemsInFile.add(_currentItem.item);
|
||||
resetTable();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Cannot create item " + _currentItem.id, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void parseItem(Node n) throws InvocationTargetException
|
||||
{
|
||||
int itemId = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
|
||||
String className = n.getAttributes().getNamedItem("type").getNodeValue();
|
||||
String itemName = n.getAttributes().getNamedItem("name").getNodeValue();
|
||||
|
||||
_currentItem.id = itemId;
|
||||
_currentItem.name = itemName;
|
||||
_currentItem.type = className;
|
||||
_currentItem.set = new StatsSet();
|
||||
_currentItem.set.set("item_id", itemId);
|
||||
_currentItem.set.set("name", itemName);
|
||||
|
||||
Node first = n.getFirstChild();
|
||||
for (n = first; n != null; n = n.getNextSibling())
|
||||
{
|
||||
if ("table".equalsIgnoreCase(n.getNodeName()))
|
||||
{
|
||||
if (_currentItem.item != null)
|
||||
{
|
||||
throw new IllegalStateException("Item created but table node found! Item " + itemId);
|
||||
}
|
||||
parseTable(n);
|
||||
}
|
||||
else if ("set".equalsIgnoreCase(n.getNodeName()))
|
||||
{
|
||||
if (_currentItem.item != null)
|
||||
{
|
||||
throw new IllegalStateException("Item created but set node found! Item " + itemId);
|
||||
}
|
||||
parseBeanSet(n, _currentItem.set, 1);
|
||||
}
|
||||
else if ("for".equalsIgnoreCase(n.getNodeName()))
|
||||
{
|
||||
makeItem();
|
||||
parseTemplate(n, _currentItem.item);
|
||||
}
|
||||
else if ("cond".equalsIgnoreCase(n.getNodeName()))
|
||||
{
|
||||
makeItem();
|
||||
Condition condition = parseCondition(n.getFirstChild(), _currentItem.item);
|
||||
Node msg = n.getAttributes().getNamedItem("msg");
|
||||
Node msgId = n.getAttributes().getNamedItem("msgId");
|
||||
if ((condition != null) && (msg != null))
|
||||
{
|
||||
condition.setMessage(msg.getNodeValue());
|
||||
}
|
||||
else if ((condition != null) && (msgId != null))
|
||||
{
|
||||
condition.setMessageId(Integer.decode(getValue(msgId.getNodeValue(), null)));
|
||||
Node addName = n.getAttributes().getNamedItem("addName");
|
||||
if ((addName != null) && (Integer.decode(getValue(msgId.getNodeValue(), null)) > 0))
|
||||
{
|
||||
condition.addName();
|
||||
}
|
||||
}
|
||||
_currentItem.item.attach(condition);
|
||||
}
|
||||
}
|
||||
// bah! in this point item doesn't have to be still created
|
||||
makeItem();
|
||||
}
|
||||
|
||||
private void makeItem() throws InvocationTargetException
|
||||
{
|
||||
if (_currentItem.item != null)
|
||||
{
|
||||
return; // item is already created
|
||||
}
|
||||
try
|
||||
{
|
||||
Constructor<?> c = Class.forName("com.l2jserver.gameserver.model.items.L2" + _currentItem.type).getConstructor(StatsSet.class);
|
||||
_currentItem.item = (L2Item) c.newInstance(_currentItem.set);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new InvocationTargetException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
public List<L2Item> getItemList()
|
||||
{
|
||||
return _itemsInFile;
|
||||
}
|
||||
}
|
32
trunk/java/com/l2jserver/gameserver/engines/items/Item.java
Normal file
32
trunk/java/com/l2jserver/gameserver/engines/items/Item.java
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2004-2014 L2J Server
|
||||
*
|
||||
* This file is part of L2J Server.
|
||||
*
|
||||
* L2J Server is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* L2J Server is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.l2jserver.gameserver.engines.items;
|
||||
|
||||
import com.l2jserver.gameserver.model.StatsSet;
|
||||
import com.l2jserver.gameserver.model.items.L2Item;
|
||||
|
||||
public class Item
|
||||
{
|
||||
public int id;
|
||||
public String type;
|
||||
public String name;
|
||||
public StatsSet set;
|
||||
public int currentLevel;
|
||||
public L2Item item;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user