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.CommentedMap;
19 import org.ini4j.Config;
20 import org.ini4j.Ini;
21 import org.ini4j.Profile;
22
23 abstract class AbstractProfileBuilder implements IniHandler
24 {
25 private Profile.Section _currentSection;
26 private boolean _header;
27 private String _lastComment;
28
29 @Override public void endIni()
30 {
31
32
33 if ((_lastComment != null) && _header)
34 {
35 setHeaderComment();
36 }
37 }
38
39 @Override public void endSection()
40 {
41 _currentSection = null;
42 }
43
44 @Override public void handleComment(String comment)
45 {
46 if ((_lastComment != null) && _header)
47 {
48 _header = false;
49 setHeaderComment();
50 }
51
52 _lastComment = comment;
53 }
54
55 @Override public void handleOption(String name, String value)
56 {
57 _header = false;
58 if (getConfig().isMultiOption())
59 {
60 _currentSection.add(name, value);
61 }
62 else
63 {
64 _currentSection.put(name, value);
65 }
66
67 if (_lastComment != null)
68 {
69 putComment(_currentSection, name);
70 _lastComment = null;
71 }
72 }
73
74 @Override public void startIni()
75 {
76 if (getConfig().isHeaderComment())
77 {
78 _header = true;
79 }
80 }
81
82 @Override public void startSection(String sectionName)
83 {
84 if (getConfig().isMultiSection())
85 {
86 _currentSection = getProfile().add(sectionName);
87 }
88 else
89 {
90 Ini.Section s = getProfile().get(sectionName);
91
92 _currentSection = (s == null) ? getProfile().add(sectionName) : s;
93 }
94
95 if (_lastComment != null)
96 {
97 if (_header)
98 {
99 setHeaderComment();
100 }
101 else
102 {
103 putComment(getProfile(), sectionName);
104 }
105
106 _lastComment = null;
107 }
108
109 _header = false;
110 }
111
112 abstract Config getConfig();
113
114 abstract Profile getProfile();
115
116 Profile.Section getCurrentSection()
117 {
118 return _currentSection;
119 }
120
121 private void setHeaderComment()
122 {
123 if (getConfig().isComment())
124 {
125 getProfile().setComment(_lastComment);
126 }
127 }
128
129 private void putComment(CommentedMap<String, ?> map, String key)
130 {
131 if (getConfig().isComment())
132 {
133 map.putComment(key, _lastComment);
134 }
135 }
136 }