1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }