Skip to content

Commit 395554f

Browse files
author
szkiba
committed
comment support
git-svn-id: svn://svn.code.sf.net/p/ini4j/code/trunk@110 7549a03c-2a44-0410-8989-d71c416b450a
1 parent 5fe9b20 commit 395554f

24 files changed

+556
-56
lines changed

NOTICE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Unless required by applicable law or agreed to in writing, software
1010
distributed under the License is distributed on an "AS IS" BASIS,
1111
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
See the License for the specific language governing permissions and
13-
limitations under the License.
13+
limitations under the License.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
<phase>verify</phase>
138138
<goals>
139139
<goal>check</goal>
140-
<goal>cpd-check</goal>
140+
<!-- <goal>cpd-check</goal> -->
141141
</goals>
142142
</execution>
143143
</executions>

src/changes/changes.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
</properties>
2323
<body>
2424
<release version="0.4.2" description="JNDI support">
25+
<action type="add" dev="szkiba">
26+
Comment support for Ini and Options. There is a header comment, section
27+
comment and option comment. Comments at beginig of file are header
28+
comments. Continous comment block before section is a section comment,
29+
continous comment bleck before option is an option comment.
30+
</action>
2531
<action type="update" dev="szkiba">
2632
Refactor Ini class, extract Profile interface and ProfileImpl superclass.
2733
</action>

src/main/java/org/ini4j/AbstractParser.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,28 @@ protected String unescape(String line)
110110
return getConfig().isEscape() ? EscapeTool.getInstance().unescape(line) : line;
111111
}
112112

113-
IniSource newIniSource(InputStream input)
113+
IniSource newIniSource(InputStream input, OptionHandler handler)
114114
{
115-
return new IniSource(input, getConfig().isInclude(), _comments);
115+
return addCommentHandler(new IniSource(input, getConfig().isInclude(), _comments), handler);
116116
}
117117

118-
IniSource newIniSource(Reader input)
118+
IniSource newIniSource(Reader input, OptionHandler handler)
119119
{
120-
return new IniSource(input, getConfig().isInclude(), _comments);
120+
return addCommentHandler(new IniSource(input, getConfig().isInclude(), _comments), handler);
121121
}
122122

123-
IniSource newIniSource(URL input) throws IOException
123+
IniSource newIniSource(URL input, OptionHandler handler) throws IOException
124124
{
125-
return new IniSource(input, getConfig().isInclude(), _comments);
125+
return addCommentHandler(new IniSource(input, getConfig().isInclude(), _comments), handler);
126+
}
127+
128+
private IniSource addCommentHandler(IniSource source, OptionHandler handler)
129+
{
130+
if (handler instanceof CommentHandler)
131+
{
132+
source.setCommentHandler((CommentHandler) handler);
133+
}
134+
135+
return source;
126136
}
127137
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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;
17+
18+
public interface CommentHandler
19+
{
20+
void handleComment(String comment);
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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;
17+
18+
public interface CommentMultiMap<K, V> extends MultiMap<K, V>
19+
{
20+
String getComment(Object key);
21+
22+
String putComment(K key, String comment);
23+
24+
String removeComment(Object key);
25+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
public class CommentMultiMapImpl<K, V> extends MultiMapImpl<K, V> implements CommentMultiMap<K, V>
22+
{
23+
private Map<K, String> _comments;
24+
25+
@Override public String getComment(Object key)
26+
{
27+
return (_comments == null) ? null : _comments.get(key);
28+
}
29+
30+
@Override public void clear()
31+
{
32+
super.clear();
33+
if (_comments != null)
34+
{
35+
_comments.clear();
36+
}
37+
}
38+
39+
@SuppressWarnings("unchecked")
40+
@Override public void putAll(Map<? extends K, ? extends V> map)
41+
{
42+
super.putAll(map);
43+
if (map instanceof CommentMultiMapImpl)
44+
{
45+
Map<K, String> cms = ((CommentMultiMapImpl) map)._comments;
46+
47+
if (cms != null)
48+
{
49+
comments().putAll(cms);
50+
}
51+
}
52+
}
53+
54+
@Override public String putComment(K key, String comment)
55+
{
56+
return comments().put(key, comment);
57+
}
58+
59+
@Override public V remove(Object key)
60+
{
61+
V ret = super.remove(key);
62+
63+
if (_comments != null)
64+
{
65+
_comments.remove(key);
66+
}
67+
68+
return ret;
69+
}
70+
71+
@Override public V remove(Object key, int index)
72+
{
73+
V ret = super.remove(key, index);
74+
75+
if ((length(key) == 0) && (_comments != null))
76+
{
77+
_comments.remove(key);
78+
}
79+
80+
return ret;
81+
}
82+
83+
@Override public String removeComment(Object key)
84+
{
85+
return (_comments == null) ? null : _comments.remove(key);
86+
}
87+
88+
private Map<K, String> comments()
89+
{
90+
if (_comments == null)
91+
{
92+
_comments = new HashMap<K, String>();
93+
}
94+
95+
return _comments;
96+
}
97+
}

src/main/java/org/ini4j/Ini.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
public class Ini extends ProfileImpl implements Persistable
3535
{
36+
private String _comment;
3637
private Config _config = Config.getGlobal();
3738
private File _file;
3839

@@ -66,6 +67,16 @@ public Ini(File input) throws IOException, InvalidIniFormatException
6667
load();
6768
}
6869

70+
public String getComment()
71+
{
72+
return _comment;
73+
}
74+
75+
public void setComment(String value)
76+
{
77+
_comment = value;
78+
}
79+
6980
public void setConfig(Config value)
7081
{
7182
_config = value;
@@ -198,22 +209,41 @@ protected void store(IniHandler formatter) throws IOException
198209
formatter.endIni();
199210
}
200211

201-
private class Builder implements IniHandler
212+
private class Builder implements IniHandler, CommentHandler
202213
{
203214
private Section _currentSection;
215+
private boolean _header;
216+
private String _lastComment;
204217

205218
@Override public void endIni()
206219
{
207-
assert true;
220+
221+
// comment only .ini files....
222+
if ((_lastComment != null) && _header)
223+
{
224+
setComment(_lastComment);
225+
}
208226
}
209227

210228
@Override public void endSection()
211229
{
212230
_currentSection = null;
213231
}
214232

233+
@Override public void handleComment(String comment)
234+
{
235+
if ((_lastComment != null) && _header)
236+
{
237+
setComment(_lastComment);
238+
_header = false;
239+
}
240+
241+
_lastComment = comment;
242+
}
243+
215244
@Override public void handleOption(String name, String value)
216245
{
246+
_header = false;
217247
if (getConfig().isMultiOption())
218248
{
219249
_currentSection.add(name, value);
@@ -222,11 +252,17 @@ private class Builder implements IniHandler
222252
{
223253
_currentSection.put(name, value);
224254
}
255+
256+
if (_lastComment != null)
257+
{
258+
_currentSection.putComment(name, _lastComment);
259+
_lastComment = null;
260+
}
225261
}
226262

227263
@Override public void startIni()
228264
{
229-
assert true;
265+
_header = true;
230266
}
231267

232268
@Override public void startSection(String sectionName)
@@ -241,6 +277,22 @@ private class Builder implements IniHandler
241277

242278
_currentSection = (s == null) ? add(sectionName) : s;
243279
}
280+
281+
if (_lastComment != null)
282+
{
283+
if (_header)
284+
{
285+
setComment(_lastComment);
286+
}
287+
else
288+
{
289+
putComment(sectionName, _lastComment);
290+
}
291+
292+
_lastComment = null;
293+
}
294+
295+
_header = false;
244296
}
245297
}
246298
}

src/main/java/org/ini4j/IniParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ public static IniParser newInstance(Config config)
6262

6363
public void parse(InputStream input, IniHandler handler) throws IOException, InvalidIniFormatException
6464
{
65-
parse(newIniSource(input), handler);
65+
parse(newIniSource(input, handler), handler);
6666
}
6767

6868
public void parse(Reader input, IniHandler handler) throws IOException, InvalidIniFormatException
6969
{
70-
parse(newIniSource(input), handler);
70+
parse(newIniSource(input, handler), handler);
7171
}
7272

7373
public void parse(URL input, IniHandler handler) throws IOException, InvalidIniFormatException
7474
{
75-
parse(newIniSource(input), handler);
75+
parse(newIniSource(input, handler), handler);
7676
}
7777

7878
public void parseXML(InputStream input, IniHandler handler) throws IOException, InvalidIniFormatException

0 commit comments

Comments
 (0)