gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r18031 - gnunet-java/src/org/gnunet/util/config


From: gnunet
Subject: [GNUnet-SVN] r18031 - gnunet-java/src/org/gnunet/util/config
Date: Mon, 7 Nov 2011 00:20:07 +0100

Author: dold
Date: 2011-11-07 00:20:06 +0100 (Mon, 07 Nov 2011)
New Revision: 18031

Modified:
   gnunet-java/src/org/gnunet/util/config/Configuration.java
Log:
configuration parsing

Modified: gnunet-java/src/org/gnunet/util/config/Configuration.java
===================================================================
--- gnunet-java/src/org/gnunet/util/config/Configuration.java   2011-11-06 
21:46:08 UTC (rev 18030)
+++ gnunet-java/src/org/gnunet/util/config/Configuration.java   2011-11-06 
23:20:06 UTC (rev 18031)
@@ -1,155 +1,214 @@
 package org.gnunet.util.config;
 
-import java.io.*;
-import java.util.*;
+import java.io.BufferedReader;
+import java.io.DataInputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.gnunet.util.time.RelativeTime;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-
 /**
  * Configuration management
- *
+ * 
+ * @author Florian Dold
  */
 public class Configuration {
-       
-       private LinkedList<Map<String,String>> sections;
-       
-       /**
-        * Start with an empty configuration.
-        */
-       public Configuration() {
-               sections = new LinkedList<Map<String,String>>();
-       }
-       
-       /**
-        * Parse a configuration file, add all of the options in the file to the
-        * configuration environment.
-        * 
-        * @param filename
-        *            name of the configuration file
-        */
-       public void parse(String filename) {
-               FileInputStream fstream;
-               try {
-                       fstream = new FileInputStream(filename);
-                       DataInputStream in = new DataInputStream(fstream);
-                       BufferedReader br = new BufferedReader(new 
InputStreamReader(in));
-               } catch (FileNotFoundException e) {
-                       e.printStackTrace();
-               }
-               
-               throw new UnsupportedOperationException();
-       }
-       
-       /**
-        * Start with defaults, the parse configuration file.
-        */
-       public boolean load(String filename) {
-               throw new UnsupportedOperationException();
-       }
+    private static final Logger logger = LoggerFactory
+            .getLogger(Configuration.class);
+    
+    
+    private static Pattern section = Pattern.compile("\\[(.*?)\\]");
+    private static Pattern tag = Pattern.compile("(.*?) =( .*?)");
 
-       /**
-        * Test if there are configuration options that were changed since the 
last
-        * save.
-        */
-       public boolean isDirty() {
-               throw new UnsupportedOperationException();
-       }
+    private Map<String,Map<String, String>> sections;
 
-       /**
-        * Write configuration file.
-        * 
-        * @param filename
-        *            where to write the configuration
-        */
-       public void write(String filename) {
-               throw new UnsupportedOperationException();
-       }
+    /**
+     * Start with an empty configuration.
+     */
+    public Configuration() {
+        sections = new HashMap<String, Map<String,String>>();
+    }
 
-       /**
-        * Remove the given section and all options in it.
-        */
-       public void removeSection(String section) {
-               throw new UnsupportedOperationException();
-       }
-       
-       
-       @Override
-       public Configuration clone() {
-               throw new UnsupportedOperationException();
-       }
-       
-       
-        /* Write only configuration entries that have been changed to 
configuration file
-        * @param cfgNew new configuration
-        * @param filename where to write the configuration diff between 
default and new
-        */
+    /**
+     * Parse a configuration file, add all of the options in the file to the
+     * configuration environment.
+     * 
+     * @param filename
+     *            name of the configuration file
+     * @throws FileNotFoundException 
+     */
+    public void parse(String filename) throws FileNotFoundException {
+        DataInputStream in = new DataInputStream(new 
FileInputStream(filename));
+        BufferedReader br = new BufferedReader(new InputStreamReader(in));
+        
+        String current_section = "";
+        
+        while (true) {
+            String line;
+            try {
+                line = br.readLine();
+            } catch (IOException e) {
+                logger.warn("error parsing file", e);
+                break;
+            }
+            line = line.trim();
+            if (line.charAt(0) == '#' || line.charAt(0) == '%') {
+                continue;
+            }
+            
+            Matcher m;
+            
+            m = section.matcher(line);
+            if (m.matches()) {
+                current_section = m.group(1);
+                continue;
+            }
+            
+            m = tag.matcher(line);
+            if (m.matches()) {
+                String option = m.group(1).trim();
+                String value = m.group(2).trim();
+                if (value.charAt(0) == '"') {
+                    int last = value.lastIndexOf('"', 1);
+                    if (last == -1) {
+                        logger.warn("error parsing file: incorrect quoting");
+                        continue;
+                    }
+                    value = value.substring(1, last).trim();
+                }
+                setValueString(current_section, option, value);
+            }
+            
+        }
+    }
 
-       public void writeDiffs(Configuration cfgNew, String filename) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public void setValueString(String section, String option, String value) 
{
-               throw new UnsupportedOperationException();
-       }
-       
-       public void setValueNumber(String section, String option, long value) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public long getValueNumer(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public String getValueString(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       
-       public RelativeTime getValueTime(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public String getValueChoice(String section, String option, 
List<String> choices) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public boolean haveValue(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       
-       /**
-        * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
-        * where either in the "PATHS" section or the environtment
-        * "FOO" is set to "DIRECTORY".
-        *
-        * @param orig string to $-expand
-        * @return $-expanded string
-        */
+    /**
+     * Start with defaults, the parse configuration file.
+     */
+    public boolean load(String filename) {
+        throw new UnsupportedOperationException();
+    }
 
-       public String expandDollar(String orig) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public String getValueFileName(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public boolean getValueYesNo(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public List<String> getValueFilenames(String section, String option) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public boolean appendValueFilename(String section, String option, 
String value) {
-               throw new UnsupportedOperationException();
-       }
-       
-       public boolean removeValueFilename(String section, String option, 
String value) {
-               throw new UnsupportedOperationException();
-       }
+    /**
+     * Test if there are configuration options that were changed since the last
+     * save.
+     */
+    public boolean isDirty() {
+        throw new UnsupportedOperationException();
+    }
 
+    /**
+     * Write configuration file.
+     * 
+     * @param filename
+     *            where to write the configuration
+     */
+    public void write(String filename) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Remove the given section and all options in it.
+     */
+    public void removeSection(String section) {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public Configuration clone() {
+        throw new UnsupportedOperationException();
+    }
+
+    /*
+     * Write only configuration entries that have been changed to configuration
+     * file
+     * 
+     * @param cfgNew new configuration
+     * 
+     * @param filename where to write the configuration diff between default 
and
+     * new
+     */
+
+    public void writeDiffs(Configuration cfgNew, String filename) {
+        throw new UnsupportedOperationException();
+    }
+
+    public void setValueString(String section, String option, String value) {
+        Map<String,String> table = sections.get(section);
+        if (table == null) {
+            table = new HashMap<String, String>();
+            sections.put(section, table);
+        }
+        table.put(option, value);
+    }
+
+    public void setValueNumber(String section, String option, long value) {
+        throw new UnsupportedOperationException();
+    }
+
+    public long getValueNumer(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    public String getValueString(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    public RelativeTime getValueTime(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    public String getValueChoice(String section, String option,
+            List<String> choices) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean haveValue(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    /**
+     * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where
+     * either in the "PATHS" section or the environtment "FOO" is set to
+     * "DIRECTORY".
+     * 
+     * @param orig
+     *            string to $-expand
+     * @return $-expanded string
+     */
+    public String expandDollar(String orig) {
+        throw new UnsupportedOperationException();
+    }
+
+    public String getValueFileName(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean getValueYesNo(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    public List<String> getValueFilenames(String section, String option) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean appendValueFilename(String section, String option,
+            String value) {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean removeValueFilename(String section, String option,
+            String value) {
+        throw new UnsupportedOperationException();
+    }
+
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]