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  
20  import java.io.PrintWriter;
21  
22  abstract class AbstractFormatter implements HandlerBase
23  {
24      private static final char OPERATOR = '=';
25      private static final char COMMENT = '#';
26      private static final char SPACE = ' ';
27      private Config _config = Config.getGlobal();
28      private boolean _header = true;
29      private PrintWriter _output;
30  
31      @Override public void handleComment(String comment)
32      {
33          if (getConfig().isComment() && (!_header || getConfig().isHeaderComment()) && (comment != null) && (comment.length() != 0))
34          {
35              for (String line : comment.split(getConfig().getLineSeparator()))
36              {
37                  getOutput().print(COMMENT);
38                  getOutput().print(line);
39                  getOutput().print(getConfig().getLineSeparator());
40              }
41  
42              if (_header)
43              {
44                  getOutput().print(getConfig().getLineSeparator());
45              }
46          }
47  
48          _header = false;
49      }
50  
51      @Override public void handleOption(String optionName, String optionValue)
52      {
53          if (getConfig().isStrictOperator())
54          {
55              if (getConfig().isEmptyOption() || (optionValue != null))
56              {
57                  getOutput().print(escapeFilter(optionName));
58                  getOutput().print(OPERATOR);
59              }
60  
61              if (optionValue != null)
62              {
63                  getOutput().print(escapeFilter(optionValue));
64              }
65  
66              if (getConfig().isEmptyOption() || (optionValue != null))
67              {
68                  getOutput().print(getConfig().getLineSeparator());
69              }
70          }
71          else
72          {
73              String value = ((optionValue == null) && getConfig().isEmptyOption()) ? "" : optionValue;
74  
75              if (value != null)
76              {
77                  getOutput().print(escapeFilter(optionName));
78                  getOutput().print(SPACE);
79                  getOutput().print(OPERATOR);
80                  getOutput().print(SPACE);
81                  getOutput().print(escapeFilter(value));
82                  getOutput().print(getConfig().getLineSeparator());
83              }
84          }
85  
86          setHeader(false);
87      }
88  
89      protected Config getConfig()
90      {
91          return _config;
92      }
93  
94      protected void setConfig(Config value)
95      {
96          _config = value;
97      }
98  
99      protected PrintWriter getOutput()
100     {
101         return _output;
102     }
103 
104     protected void setOutput(PrintWriter value)
105     {
106         _output = value;
107     }
108 
109     void setHeader(boolean value)
110     {
111         _header = value;
112     }
113 
114     String escapeFilter(String input)
115     {
116         return getConfig().isEscape() ? EscapeTool.getInstance().escape(input) : input;
117     }
118 }