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