Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: s IT Solutions AT Spardat GmbH - initial API and implementation /
 
 
 package at.spardat.xma.boot;
 
 import java.util.Map;

Helper class to read a ini style file. Sections and keys are treated case sensitive. Lines beginning with ; or # are treated as comment. Used by Launcher to read the XmaLauncherServlet response file.

Author(s):
s3460
Since:
version_number
 
 class IniFileReader{
 
     private Map sections = new LinkedHashMap();
     String path;

    
constructor reads already complete file

Parameters:
filePath
 
     public IniFileReader(String filePath) {
         BufferedReader reader = null;
          = filePath;
 
         try {
             reader = new BufferedReader(new FileReader(filePath));
             initialize(reader);
         } catch (IOException e) {
             throw new RuntimeException(e);
         } finally{
             if(reader != null){
                 try {
                     reader.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }

    

Parameters:
section - section without []
key
Returns:
Author(s):
s3460
Since:
version_number
 
     public String getProperty(String sectionString key) {
         Map map = getSection(section);
         if (map != null) {
             String value = (Stringmap.get(key);
             return value;
         } else{
             return null;
         }
     }

    

Parameters:
section - section without []
key
Returns:
Author(s):
s3460
Since:
version_number
 
    public String getProperty(String sectionString keyString defaultValue) {
        String value = getProperty(sectionkey);
        if(value!=null){
            return value;
        }else{
            return defaultValue;
        }
    }

    

Parameters:
section - section without []
Returns:
Author(s):
s3460
Since:
version_number
    public Map getSection(String section) {
        return  (Map.get(section);
    }

    

Returns:
all available sections
Author(s):
s3460
Since:
version_number
    public String[] getSectionNames(){
        String[] names = new String[.size()];
        int i = 0;
        for (Iterator iter = .keySet().iterator(); iter.hasNext(); i++) {
            String element = (Stringiter.next();
            names[i] = element;
        }
        return names;
    }

    
reads already complete file and stores values in map

Parameters:
r
Throws:
java.io.IOException
Author(s):
s3460
Since:
version_number
    private void initialize(BufferedReader rthrows IOException {
        String section = nullline;
        while ((line = r.readLine()) != null) {
            line = line.trim();
            if (line.equals("") || line.startsWith(";") || line.startsWith("#")) {
                continue;
            }
            if (line.startsWith("[") && line.endsWith("]")) {
                section = line.substring(1, line.length() - 1);
            } else if (section == null) {
                throw new RuntimeException("Error reading " +  + " [section] header expected");
            } else {
                int index = line.indexOf('=');
                if (index < 0) {
                    throw new RuntimeException("Error reading " +  + " key/value pair without = ; see line: " + line);
                }
                String key = line.substring(0, index).trim();
                String value = line.substring(index + 1).trim();
                Map map = (Map.get(section);
                if (map == null) {
                    .put(section, (map = new LinkedHashMap()));
                }
                map.put(keyvalue);
            }
        }
    }
New to GrepCode? Check out our FAQ X