problem description:

current version of DOM4JConfiguration can read xml config only from File

decision:

New version of DOM4JConfiguration allow to read xml config from any resource that ClassLoader support. But if use resource type other than file it is not possible to save config :(

Mikhail
--- old.java    Thu Sep 11 07:29:34 2003
+++ DOM4JConfiguration.java     Thu Sep 11 20:05:58 2003
@@ -59,6 +59,7 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.Iterator;
 import java.util.List;
@@ -89,6 +90,7 @@
  */
 public class DOM4JConfiguration extends XMLConfiguration
 {
+       private final static String FILE_PROTOCOL = "file";
     // For conformance with xpath
     private static final char ATTRIB_MARKER = '@';
     private static final String ATTRIB_START_MARKER = "[" + ATTRIB_MARKER;
@@ -106,6 +108,16 @@
      * A handle to our data source.
      */
     private String fileName;
+       
+       /**
+        * The name of the resource
+        */
+       private String resourceName;
+       
+       /**
+        *
+        */
+       private boolean isFile;
 
     /**
      * The XML document from our data source.
@@ -137,7 +149,15 @@
      */
     public DOM4JConfiguration(String resource) throws Exception
     {
-        setFile(resourceURLToFile(resource));
+               URL url = resourceToURL(resource);
+               if (url.getProtocol().equals(FILE_PROTOCOL)){
+                       setFile(new File(url.getFile()));
+                       isFile = true;
+               }else{
+                       resourceName = resource;
+                       isFile = false;
+               }
+                       
         load();
     }
 
@@ -156,20 +176,28 @@
     public void load() throws Exception
     {
 
-        document = new SAXReader().read(getFile());
+               if (isFile)
+                       document = new SAXReader().read(getFile());
+               else {                  
+                       InputStream is = 
+                               
DOM4JConfiguration.class.getClassLoader().getResourceAsStream(resourceName);
+                       if (is == null)
+                               is = ClassLoader.getSystemResourceAsStream(fileName);
+                       document = new SAXReader().read(is);
+               }
         initProperties(document.getRootElement(), new StringBuffer());
 
     }
-
-    private static File resourceURLToFile(String resource)
-    {
+       
+       private static URL resourceToURL(String resource)
+       {
         URL confURL = DOM4JConfiguration.class.getClassLoader().getResource(resource);
         if (confURL == null)
         {
             confURL = ClassLoader.getSystemResource(resource);
         }
-        return new File(confURL.getFile());
-    }
+               return confURL;
+       }
 
     /**
      * Loads and initializes from the XML file.
@@ -347,27 +375,31 @@
 
     public synchronized void save() throws IOException
     {
-        XMLWriter writer = null;
-        OutputStream out = null;
-        try
-        {
-            OutputFormat outputter = OutputFormat.createPrettyPrint();
-            out = new BufferedOutputStream(new FileOutputStream(getFile()));
-            writer = new XMLWriter(out, outputter);
-            writer.write(document);
-        }
-        finally
-        {
-            if (out != null)
-            {
-                out.close();
-            }
-
-            if (writer != null)
-            {
-                writer.close();
-            }
-        }
+               if (!isFile){
+                       throw new IOException("Can not save configuration to resource 
("+resourceName+")");
+               }else{
+                       XMLWriter writer = null;
+                       OutputStream out = null;
+                       try
+                       {
+                               OutputFormat outputter = 
OutputFormat.createPrettyPrint();
+                               out = new BufferedOutputStream(new 
FileOutputStream(getFile()));
+                               writer = new XMLWriter(out, outputter);
+                               writer.write(document);
+                       }
+                       finally
+                       {
+                               if (out != null)
+                               {
+                                       out.close();
+                               }
+       
+                               if (writer != null)
+                               {
+                                       writer.close();
+                               }
+                       }
+               }
     }
     /**
      * Returns the file.
@@ -376,7 +408,6 @@
     public File getFile()
     {
         File file = null;
-
         if (StringUtils.isEmpty(getBasePath()))
         {
             // Good luck... This will fail 99 out of 100 times.
@@ -419,14 +450,14 @@
      */
     public void setFile(File file)
     {
+               isFile = true;
         this.fileName = file.getAbsolutePath();
     }
 
     public void setFileName(String fileName)
     {
-
-        this.fileName = fileName;
-        
+               isFile = true;
+        this.fileName = fileName;        
     }
 
     /**

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to