001    /*
002     * Copyright 2005,2009 Ivan SZKIBA
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.ini4j.demo;
017    
018    import bsh.ConsoleInterface;
019    import bsh.EvalError;
020    import bsh.Interpreter;
021    import bsh.NameSpace;
022    
023    import org.ini4j.Config;
024    import org.ini4j.Ini;
025    import org.ini4j.Options;
026    import org.ini4j.Persistable;
027    import org.ini4j.Reg;
028    
029    import java.io.IOException;
030    import java.io.InputStream;
031    import java.io.InputStreamReader;
032    import java.io.Reader;
033    import java.io.StringReader;
034    
035    public class DemoModel implements Runnable
036    {
037        public static enum Mode
038        {
039            INI,
040            REG,
041            OPTIONS;
042        }
043    
044        private Persistable _data;
045        private Interpreter _interpreter;
046        private Mode _mode = Mode.INI;
047    
048        public DemoModel(ConsoleInterface console)
049        {
050            _interpreter = new Interpreter(console);
051            NameSpace namespace = _interpreter.getNameSpace();
052    
053            namespace.importPackage("org.ini4j.spi");
054            namespace.importPackage("org.ini4j");
055            namespace.importPackage("org.ini4j.sample");
056        }
057    
058        public Object getData()
059        {
060            return _data;
061        }
062    
063        public Mode getMode()
064        {
065            return _mode;
066        }
067    
068        public void setMode(Mode mode)
069        {
070            _mode = mode;
071        }
072    
073        public void clear() throws EvalError
074        {
075            _interpreter.unset("data");
076        }
077    
078        public String help() throws IOException
079        {
080            return readResource("help.txt");
081        }
082    
083        public String load() throws IOException
084        {
085            return readResource(_mode.name().toLowerCase() + "-data.txt");
086        }
087    
088        public void parse(String text) throws IOException, EvalError
089        {
090            Persistable data = newData();
091    
092            data.load(new StringReader(text));
093            _interpreter.set("data", data);
094            _data = data;
095        }
096    
097        @Override public void run()
098        {
099            _interpreter.setExitOnEOF(false);
100            _interpreter.run();
101        }
102    
103        public String tip() throws IOException
104        {
105            return readResource(_mode.name().toLowerCase() + "-tip.txt");
106        }
107    
108        private Persistable newData()
109        {
110            Persistable ret = null;
111    
112            switch (_mode)
113            {
114    
115                case INI:
116                    ret = new Ini();
117                    break;
118    
119                case REG:
120                    ret = new Reg();
121                    break;
122    
123                case OPTIONS:
124                    ret = new Options();
125                    break;
126            }
127    
128            return ret;
129        }
130    
131        private String readResource(String path) throws IOException
132        {
133            InputStream in = getClass().getResourceAsStream(path);
134            Reader reader = new InputStreamReader(in, Config.DEFAULT_FILE_ENCODING);
135            StringBuilder str = new StringBuilder();
136            char[] buff = new char[8192];
137            int n;
138    
139            while ((n = reader.read(buff)) >= 0)
140            {
141                str.append(buff, 0, n);
142            }
143    
144            return str.toString();
145        }
146    }