View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j.spi;
17  
18  import org.ini4j.Config;
19  import org.ini4j.Options;
20  
21  public class OptionsBuilder implements OptionsHandler
22  {
23      private boolean _header;
24      private String _lastComment;
25      private Options _options;
26  
27      public static OptionsBuilder newInstance(Options opts)
28      {
29          OptionsBuilder instance = newInstance();
30  
31          instance.setOptions(opts);
32  
33          return instance;
34      }
35  
36      public void setOptions(Options value)
37      {
38          _options = value;
39      }
40  
41      @Override public void endOptions()
42      {
43  
44          // comment only .opt file ...
45          if ((_lastComment != null) && _header)
46          {
47              setHeaderComment();
48          }
49      }
50  
51      @Override public void handleComment(String comment)
52      {
53          if ((_lastComment != null) && _header)
54          {
55              setHeaderComment();
56              _header = false;
57          }
58  
59          _lastComment = comment;
60      }
61  
62      @Override public void handleOption(String name, String value)
63      {
64          if (getConfig().isMultiOption())
65          {
66              _options.add(name, value);
67          }
68          else
69          {
70              _options.put(name, value);
71          }
72  
73          if (_lastComment != null)
74          {
75              if (_header)
76              {
77                  setHeaderComment();
78              }
79              else
80              {
81                  putComment(name);
82              }
83  
84              _lastComment = null;
85          }
86  
87          _header = false;
88      }
89  
90      @Override public void startOptions()
91      {
92          if (getConfig().isHeaderComment())
93          {
94              _header = true;
95          }
96      }
97  
98      protected static OptionsBuilder newInstance()
99      {
100         return ServiceFinder.findService(OptionsBuilder.class);
101     }
102 
103     private Config getConfig()
104     {
105         return _options.getConfig();
106     }
107 
108     private void setHeaderComment()
109     {
110         if (getConfig().isComment())
111         {
112             _options.setComment(_lastComment);
113         }
114     }
115 
116     private void putComment(String key)
117     {
118         if (getConfig().isComment())
119         {
120             _options.putComment(key, _lastComment);
121         }
122     }
123 }