Package jConfigLib.files

Source Code of jConfigLib.files.IniFile

/*
  jConfigLib - Library for simple File parsing.
  Copyright (C) 2010  Martin Milbret

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package jConfigLib.files;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
import java.util.Map.Entry;

import jFileLib.text.FileReader;
import jFileLib.text.FileWriter;

public class IniFile extends BaseFile
{
    public IniFile()
    {
      super();
    }
   
    public IniFile(String newFilePath)
    {
      super(newFilePath);
        readFile();
    }

    public IniFile(String newFilePath, String newSeparator)
    {
      super(newFilePath, newSeparator);
        readFile();
    }
   
    public IniFile(String newFilePath, String newSeparator, boolean toLowerCase)
    {
      super(newFilePath, newSeparator);
        readFile(toLowerCase);
    }
   
    public IniFile(String newFilePath, String newSeparator, HashMap<String, HashMap<String, String> > newSections)
    {
        super(newFilePath, newSeparator);
        setSections(newSections);
    }
   
    private HashMap<String, HashMap<String, String> > sections = new HashMap<String, HashMap<String, String> >();

    public HashMap<String, HashMap<String, String> > getSections()
    {
        return sections;
    }

    public void setSections(HashMap<String, HashMap<String, String> > newSections)
    {
        if(newSections != null)
        {
            sections = newSections;
        }
    }
   
    public boolean readFile(String newFilePath)
    {
      this.setFilePath(newFilePath);
      return this.readFile();
    }

    public boolean readFile()
    {
      return readFile(false);
    }
   
    public boolean readFile(boolean toLowerCase)
    {
      FileReader reader = new FileReader(getFilePath());
     
        HashMap<String, HashMap<String, String> > newSections = new HashMap<String, HashMap<String, String> >();
        List<String> lines = reader.getFileContent();
       
        int size = lines.size();
       
      if(size == 0)
            return false;

        for(int i = 0; i < size; i++)
        {
            String line = lines.get(i);
       
            line = removeAllComments(line);
           
            if(line.length() == 0)
                continue;
               
            int startPos = line.indexOf("[");
            int endPos = line.indexOf("]");
           
            if( startPos == -1 || endPos == -1 || startPos > endPos)
                continue;
               
            String sectionName = line.substring(startPos + 1, endPos);
            HashMap<String, String> keyValueMap = new HashMap<String, String>();
           
            for(++i;i< size; i++)
            {
                line = lines.get(i);
                line = this.removeAllComments(line);

                int sepPos = line.indexOf(this.getSeparator());
               
                startPos = line.indexOf("[");
                endPos = line.indexOf("]");
           
                if((startPos != -1 && endPos != -1) &&
                    (sepPos < startPos && sepPos > endPos || sepPos == -1))
                {
                    i--;
                    break;
                }
               
                if(line.length() == 0)
                    continue;
           
                if(sepPos != -1)
                {
                    String key = line.substring(0, sepPos).trim();
                    String value = line.substring(sepPos + 1, line.length()).trim();
                   
                    if(toLowerCase)
                    {
                      key = key.toLowerCase();
                      value = value.toLowerCase();
                    }
                   
                    keyValueMap.put(key, value);
                }
            }
           
            if(toLowerCase)
              sectionName = sectionName.toLowerCase();
           
            newSections.put(sectionName, keyValueMap);
        }

      setSections(newSections);
           
        return true;
    }
   
    public boolean writeFile(String newFilePath)
    {
        setFilePath(newFilePath);
        return writeFile();
    }

    public boolean writeFile()
    {
      if(getSections().entrySet().size() == 0)
        {
            return false;
        }
       
        ArrayList<String> lines = new ArrayList<String>();
       
        Set<Map.Entry<String, HashMap<String, String> > > sectionSet = getSections().entrySet();
        Iterator<Map.Entry<String, HashMap<String, String> > > sectionIterator = sectionSet.iterator();
       
        while(sectionIterator.hasNext())
        {
            Map.Entry<String, HashMap<String, String> > sectionEntry = (Map.Entry<String, HashMap<String, String> >)sectionIterator.next();
            String sectionName = (String)sectionEntry.getKey();
           
            HashMap<String, String> keyValueMap = (HashMap<String, String>)sectionEntry.getValue();
           
            String line = "[" + sectionName + "]";
            lines.add(line);
       
            Set<Entry< String, String> > keyValueSet = keyValueMap.entrySet();
            Iterator<Map.Entry<String, String>> keyValueIterator = keyValueSet.iterator();
           
            while(keyValueIterator.hasNext())
            {
              Map.Entry<String, String> keyValueEntry = (Map.Entry<String, String>)keyValueIterator.next();
                String key = (String)keyValueEntry.getKey();
                String value = (String)keyValueEntry.getValue();
               
                line = key + getSeparator() + value;
                lines.add(line);
            }
           
            lines.add("");
        }
       
      FileWriter writer = new FileWriter(getFilePath());
     
        return writer.writeLinesToFile(lines);
    }
   
    /*
    public String getValue(String section, String key)
    {
      HashMap<String, String> keyValueMap = getSection(section);

      if(keyValueMap == null)
        return null;
     
      return keyValueMap.get(key);
    }
   
    public HashMap<String, String> getSection(String sectionName)
    {  
      return sections.get(sectionName);
    }
    */
   
    public String getValue(String section, String key)
    {
      HashMap<String, String> keyValueMap = getSection(section);

      if(keyValueMap == null)
        return null;
     
      Set<Entry<String, String>> set = keyValueMap.entrySet();
      Iterator<Entry<String, String>> iterator = set.iterator();
     
      while(iterator.hasNext())
      {
        Entry<String, String> entry = iterator.next();
       
        if(entry.getKey().equalsIgnoreCase(key))
          return entry.getValue();
      }
     
      return null;
    }
   
    public HashMap<String, String> getSection(String sectionName)
    {  
      Set<Map.Entry<String, HashMap<String, String>>> entries = sections.entrySet();
     
      Iterator<Entry<String, HashMap<String, String>>> iterator = entries.iterator();
     
      while(iterator.hasNext())
      {
        Entry<String, HashMap<String, String>> entry = iterator.next();
       
        if(entry.getKey().equalsIgnoreCase(sectionName))
          return entry.getValue();
      }
     
      return null;
    }
   
    public boolean containsSection(String sectionName)
    {
      HashMap<String, String> section = getSection(sectionName);
     
      if(section == null)
        return false;
      else
        return true;
    }
   
    public boolean containsKey(String sectionName, String key)
    {
      HashMap<String, String> section = getSection(sectionName);
     
      if(section == null)
        return false;
     
      Set<Entry<String, String>> set = section.entrySet();
      Iterator<Entry<String, String>> iterator = set.iterator();
     
      while(iterator.hasNext())
      {
        Entry<String, String> entry = iterator.next();
       
        if(entry.getKey().equalsIgnoreCase(key))
          return true;
      }
     
      return false;
    }
}
TOP

Related Classes of jConfigLib.files.IniFile

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.