warc222

XMLs Document l2jmobius

Dec 12th, 2023
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. diff --git java/Base/XML/XMLDocument.java java/Base/XML/XMLDocument.java
  2. new file mode 100644
  3. index 0000000..756a5ee
  4. --- /dev/null
  5. +++ java/Base/XML/XMLDocument.java
  6. @@ -0,0 +1,119 @@
  7. +package Base.XML;
  8. +
  9. +import java.io.File;
  10. +import java.util.logging.Level;
  11. +import java.util.logging.Logger;
  12. +
  13. +import javax.xml.parsers.DocumentBuilderFactory;
  14. +import javax.xml.transform.OutputKeys;
  15. +import javax.xml.transform.Transformer;
  16. +import javax.xml.transform.TransformerException;
  17. +import javax.xml.transform.TransformerFactory;
  18. +import javax.xml.transform.dom.DOMSource;
  19. +import javax.xml.transform.stream.StreamResult;
  20. +
  21. +import org.w3c.dom.Document;
  22. +import org.w3c.dom.NamedNodeMap;
  23. +import org.w3c.dom.Node;
  24. +
  25. +import org.l2jmobius.gameserver.model.StatSet;
  26. +
  27. +/**
  28. + * An XML document, relying on a static and single DocumentBuilderFactory.
  29. + */
  30. +public abstract class XMLDocument
  31. +{
  32. +   protected static final Logger LOG = Logger.getLogger(XMLDocument.class.getName());
  33. +  
  34. +   protected Document document;
  35. +  
  36. +   private static final DocumentBuilderFactory BUILDER;
  37. +   static
  38. +   {
  39. +       BUILDER = DocumentBuilderFactory.newInstance();
  40. +       BUILDER.setValidating(false);
  41. +       BUILDER.setIgnoringComments(true);
  42. +   }
  43. +  
  44. +   abstract protected void load();
  45. +  
  46. +   abstract protected void parseDocument(Document doc, File f);
  47. +  
  48. +   public void loadDocument(String filePath)
  49. +   {
  50. +       loadDocument(new File(filePath));
  51. +   }
  52. +  
  53. +   public void writeDocument(Document doc, String fileName)
  54. +   {
  55. +       try
  56. +       {
  57. +           TransformerFactory transformerFactory = TransformerFactory.newInstance();
  58. +           Transformer transformer = transformerFactory.newTransformer();
  59. +           transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  60. +           transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  61. +           transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  62. +          
  63. +           DOMSource source = new DOMSource(doc);
  64. +           StreamResult result = new StreamResult(new File(fileName));
  65. +          
  66. +           transformer.transform(source, result);
  67. +           LOG.info("XML file saved to " + fileName);
  68. +       }
  69. +       catch (TransformerException e)
  70. +       {
  71. +           LOG.warning("Error saving XML file: " + e.getMessage());
  72. +       }
  73. +   }
  74. +  
  75. +   /**
  76. +    * Parse an entire directory or file if found.
  77. +    * @param file
  78. +    */
  79. +   public void loadDocument(File file)
  80. +   {
  81. +       if (!file.exists())
  82. +       {
  83. +           LOG.severe("The following file or directory doesn't exist: " + file.getName());
  84. +           return;
  85. +       }
  86. +      
  87. +       if (file.isDirectory())
  88. +       {
  89. +           for (File f : file.listFiles())
  90. +           {
  91. +               loadDocument(f);
  92. +           }
  93. +       }
  94. +       else if (file.isFile())
  95. +       {
  96. +           try
  97. +           {
  98. +               parseDocument(BUILDER.newDocumentBuilder().parse(file), file);
  99. +           }
  100. +           catch (Exception e)
  101. +           {
  102. +               LOG.log(Level.SEVERE, "Error loading XML file " + file.getName(), e);
  103. +           }
  104. +       }
  105. +   }
  106. +  
  107. +   public Document getDocument()
  108. +   {
  109. +       return document;
  110. +   }
  111. +  
  112. +   /**
  113. +    * This method parses the content of a NamedNodeMap and feed the given StatsSet.
  114. +    * @param attrs : The NamedNodeMap to parse.
  115. +    * @param set : The StatsSet to feed.
  116. +    */
  117. +   public static void parseAndFeed(NamedNodeMap attrs, StatSet set)
  118. +   {
  119. +       for (int i = 0; i < attrs.getLength(); i++)
  120. +       {
  121. +           final Node attr = attrs.item(i);
  122. +           set.set(attr.getNodeName(), attr.getNodeValue());
  123. +       }
  124. +   }
  125. +}
  126. diff --git java/Base/XML/XMLDocumentFactory.java java/Base/XML/XMLDocumentFactory.java
  127. new file mode 100644
  128. index 0000000..9594e19
  129. --- /dev/null
  130. +++ java/Base/XML/XMLDocumentFactory.java
  131. @@ -0,0 +1,74 @@
  132. +package Base.XML;
  133. +
  134. +import java.io.File;
  135. +
  136. +import javax.xml.parsers.DocumentBuilder;
  137. +import javax.xml.parsers.DocumentBuilderFactory;
  138. +
  139. +import org.w3c.dom.Document;
  140. +
  141. +/**
  142. + */
  143. +public final class XMLDocumentFactory
  144. +{
  145. +   public static final XMLDocumentFactory getInstance()
  146. +   {
  147. +       return SingletonHolder._instance;
  148. +   }
  149. +  
  150. +   private final DocumentBuilder _builder;
  151. +  
  152. +   protected XMLDocumentFactory() throws Exception
  153. +   {
  154. +       try
  155. +       {
  156. +           final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  157. +           factory.setValidating(false);
  158. +           factory.setIgnoringComments(true);
  159. +          
  160. +           _builder = factory.newDocumentBuilder();
  161. +       }
  162. +       catch (Exception e)
  163. +       {
  164. +           throw new Exception("Failed initializing", e);
  165. +       }
  166. +   }
  167. +  
  168. +   public final Document loadDocument(final String filePath) throws Exception
  169. +   {
  170. +       return loadDocument(new File(filePath));
  171. +   }
  172. +  
  173. +   public final Document loadDocument(final File file) throws Exception
  174. +   {
  175. +       if (!file.exists() || !file.isFile())
  176. +       {
  177. +           throw new Exception("File: " + file.getAbsolutePath() + " doesn't exist and/or is not a file.");
  178. +       }
  179. +      
  180. +       return _builder.parse(file);
  181. +   }
  182. +  
  183. +   public final Document newDocument()
  184. +   {
  185. +       return _builder.newDocument();
  186. +   }
  187. +  
  188. +   private static class SingletonHolder
  189. +   {
  190. +       protected static final XMLDocumentFactory _instance;
  191. +      
  192. +       static
  193. +       {
  194. +           try
  195. +           {
  196. +               _instance = new XMLDocumentFactory();
  197. +           }
  198. +           catch (Exception e)
  199. +           {
  200. +               throw new ExceptionInInitializerError(e);
  201. +           }
  202. +       }
  203. +   }
  204. +}
Add Comment
Please, Sign In to add comment