Advertisement
Sarada-L2

XMLDocument

May 4th, 2021
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. +/*
  2. + * Copyright 2010 InC-Gaming, Patrick Biesenbach aka. Forsaiken. All rights reserved.
  3. + */
  4. +package Dev.SpecialMods;
  5. +
  6. +import java.io.File;
  7. +
  8. +import javax.xml.parsers.DocumentBuilder;
  9. +import javax.xml.parsers.DocumentBuilderFactory;
  10. +
  11. +import org.w3c.dom.Document;
  12. +
  13. +public final class XMLDocumentFactory
  14. +{
  15. +    public static final XMLDocumentFactory getInstance()
  16. +    {
  17. +        return SingletonHolder._instance;
  18. +    }
  19. +    
  20. +    private final DocumentBuilder _builder;
  21. +    
  22. +    protected XMLDocumentFactory() throws Exception
  23. +    {
  24. +        try
  25. +        {
  26. +            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  27. +            factory.setValidating(false);
  28. +            factory.setIgnoringComments(true);
  29. +            
  30. +            _builder = factory.newDocumentBuilder();
  31. +        }
  32. +        catch (Exception e)
  33. +        {
  34. +            throw new Exception("Failed initializing", e);
  35. +        }
  36. +    }
  37. +    
  38. +    public final Document loadDocument(final String filePath) throws Exception
  39. +    {
  40. +        return loadDocument(new File(filePath));
  41. +    }
  42. +    
  43. +    public final Document loadDocument(final File file) throws Exception
  44. +    {
  45. +        if (!file.exists() || !file.isFile())
  46. +            throw new Exception("File: " + file.getAbsolutePath() + " doesn't exist and/or is not a file.");
  47. +        
  48. +        return _builder.parse(file);
  49. +    }
  50. +    
  51. +    public final Document newDocument()
  52. +    {
  53. +        return _builder.newDocument();
  54. +    }
  55. +    
  56. +    private static class SingletonHolder
  57. +    {
  58. +        protected static final XMLDocumentFactory _instance;
  59. +        
  60. +        static
  61. +        {
  62. +            try
  63. +            {
  64. +                _instance = new XMLDocumentFactory();
  65. +            }
  66. +            catch (Exception e)
  67. +            {
  68. +                throw new ExceptionInInitializerError(e);
  69. +            }
  70. +        }
  71. +    }
  72. +}
  73.  
  74. Gameserver.java
  75.  
  76. +import Dev.SpecialMods.XMLDocumentFactory;
  77.  
  78.  
  79. +XMLDocumentFactory.getInstance();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement