All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclarion.clarion.setting.SettingFile Maven / Gradle / Ivy

There is a newer version: 1.86
Show newest version
/**
 * Copyright 2010, by Andrew Barnham
 *
 * The contents of this file are subject to
 * GNU Lesser General Public License (LGPL), v.3
 * http://www.gnu.org/licenses/lgpl.txt
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 */
package org.jclarion.clarion.setting;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.jclarion.clarion.ClarionObject;
import org.jclarion.clarion.ClarionString;
import org.jclarion.clarion.PropertyObject;

public class SettingFile 
{
    public static class Value
    {
        private String type;
        private String value;
        
        public Value(String type,String value)
        {
            this.type=type;
            this.value=value;
        }
        
        public Value(String value)
        {
            this.value=value;
        }
     
        public String getType()
        {
            return type;
        }
        
        public String getValue()
        {
            return value;
        }
        
        private ClarionObject result;

        public ClarionObject getClarionObject() 
        {
            if (result==null) result=new ClarionString(value);
            return result.genericLike();
        }
    }
    
    private Map> settings;
    
    public SettingFile()
    {
        //settings=new HashMap>();
    }
    
    private void init()
    {
        if (settings==null) settings=new HashMap>();
    }

    public void save(Writer w) throws IOException
    {
        if (settings==null) return;
        
        BufferedWriter bw;
        if (w instanceof BufferedWriter) {
            bw=(BufferedWriter)w;
        } else {
            bw=new BufferedWriter(w);
        }
        
        for (Map.Entry> es : settings.entrySet() ) {
            
            for (Map.Entry ses : es.getValue().entrySet() ) {
                bw.write(es.getKey());
                bw.write('-');
                bw.write(String.valueOf(ses.getKey()));
                if (ses.getValue().type!=null) {
                    bw.write('(');
                    bw.write(ses.getValue().type);
                    bw.write(')');
                }
                bw.write('=');
                bw.write(ses.getValue().value);
                bw.write('\n');
            }
            bw.flush();
        }
        
    }
    
    public void load(Reader r) throws IOException
    {
        BufferedReader br;
        if (r instanceof BufferedReader) {
            br=(BufferedReader)r;
        } else {
            br=new BufferedReader(r);
        }
        
        while ( true ) {
            String line = br.readLine();
            if (line==null) break;
            
            int keypos = line.indexOf('-');
            if (keypos<0) continue;

            int keyendpos = line.indexOf('(',keypos+1);
            
            int endpos = line.indexOf('=',keypos+1);
            if (endpos<0) continue;
            
            String type=null;
            
            if (keyendpos>0 && keyendpos entry = settings.get(obj.getId());
        if (entry==null) return;
        
        for (Map.Entry scan : entry.entrySet() )  {
            obj.setProperty(scan.getKey(),scan.getValue().getClarionObject());
        }
        
    }

    public Map getSettings(String key)
    {
        if (settings==null) return null;
        return settings.get(key);
    }

    public void set(String key, int indx, String type, String value) 
    {
        init();
        Map entry;
        entry = settings.get(key);
        if (entry==null) {
            entry=new HashMap();
            settings.put(key,entry);
        }
        
        Value val = entry.get(indx);
        if (val==null) {
            val=new Value(type,value);
            entry.put(indx,val);
        } else {
            val.value=value;
            if (type!=null) val.type=type;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy