1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jgit.lib;
20
21 import static java.nio.charset.StandardCharsets.UTF_8;
22
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Set;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.atomic.AtomicReference;
31
32 import org.eclipse.jgit.errors.ConfigInvalidException;
33 import org.eclipse.jgit.events.ConfigChangedEvent;
34 import org.eclipse.jgit.events.ConfigChangedListener;
35 import org.eclipse.jgit.events.ListenerHandle;
36 import org.eclipse.jgit.events.ListenerList;
37 import org.eclipse.jgit.internal.JGitText;
38 import org.eclipse.jgit.transport.RefSpec;
39 import org.eclipse.jgit.util.RawParseUtils;
40
41
42
43
44 public class Config {
45
46 private static final String[] EMPTY_STRING_ARRAY = {};
47
48 static final long KiB = 1024;
49 static final long MiB = 1024 * KiB;
50 static final long GiB = 1024 * MiB;
51 private static final int MAX_DEPTH = 10;
52
53 private static final TypedConfigGetter DEFAULT_GETTER = new DefaultTypedConfigGetter();
54
55 private static TypedConfigGetter typedGetter = DEFAULT_GETTER;
56
57
58 private final ListenerListhtml#ListenerList">ListenerList listeners = new ListenerList();
59
60
61
62
63
64
65
66 private final AtomicReference<ConfigSnapshot> state;
67
68 private final Config baseConfig;
69
70
71
72
73
74
75
76
77 private static final String MISSING_ENTRY = new String();
78
79
80
81
82 public Config() {
83 this(null);
84 }
85
86
87
88
89
90
91
92
93 public Configig" href="../../../../org/eclipse/jgit/lib/Config.html#Config">Config(Config defaultConfig) {
94 baseConfig = defaultConfig;
95 state = new AtomicReference<>(newState());
96 }
97
98
99
100
101
102
103
104
105 public Config getBaseConfig() {
106 return baseConfig;
107 }
108
109
110
111
112
113
114
115
116
117 @SuppressWarnings({ "ReferenceEquality", "StringEquality" })
118 public static boolean isMissing(String value) {
119 return value == MISSING_ENTRY;
120 }
121
122
123
124
125
126
127
128
129
130 public static void setTypedConfigGetter(TypedConfigGetter getter) {
131 typedGetter = getter == null ? DEFAULT_GETTER : getter;
132 }
133
134
135
136
137
138
139
140
141 static String escapeValue(String x) {
142 if (x.isEmpty()) {
143 return "";
144 }
145
146 boolean needQuote = x.charAt(0) == ' ' || x.charAt(x.length() - 1) == ' ';
147 StringBuilder r = new StringBuilder(x.length());
148 for (int k = 0; k < x.length(); k++) {
149 char c = x.charAt(k);
150
151
152
153
154
155
156
157
158
159
160
161
162 switch (c) {
163 case '\0':
164
165
166
167 throw new IllegalArgumentException(
168 JGitText.get().configValueContainsNullByte);
169
170 case '\n':
171 r.append('\\').append('n');
172 break;
173
174 case '\t':
175 r.append('\\').append('t');
176 break;
177
178 case '\b':
179
180
181
182 r.append('\\').append('b');
183 break;
184
185 case '\\':
186 r.append('\\').append('\\');
187 break;
188
189 case '"':
190 r.append('\\').append('"');
191 break;
192
193 case '#':
194 case ';':
195 needQuote = true;
196 r.append(c);
197 break;
198
199 default:
200 r.append(c);
201 break;
202 }
203 }
204
205 return needQuote ? '"' + r.toString() + '"' : r.toString();
206 }
207
208 static String escapeSubsection(String x) {
209 if (x.isEmpty()) {
210 return "\"\"";
211 }
212
213 StringBuilder r = new StringBuilder(x.length() + 2).append('"');
214 for (int k = 0; k < x.length(); k++) {
215 char c = x.charAt(k);
216
217
218
219 switch (c) {
220 case '\0':
221 throw new IllegalArgumentException(
222 JGitText.get().configSubsectionContainsNullByte);
223
224 case '\n':
225 throw new IllegalArgumentException(
226 JGitText.get().configSubsectionContainsNewline);
227
228 case '\\':
229 case '"':
230 r.append('\\').append(c);
231 break;
232
233 default:
234 r.append(c);
235 break;
236 }
237 }
238
239 return r.append('"').toString();
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253 public int getInt(final String section, final String name,
254 final int defaultValue) {
255 return typedGetter.getInt(this, section, null, name, defaultValue);
256 }
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 public int getInt(final String section, String subsection,
272 final String name, final int defaultValue) {
273 return typedGetter.getInt(this, section, subsection, name,
274 defaultValue);
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288 public long getLong(String section, String name, long defaultValue) {
289 return typedGetter.getLong(this, section, null, name, defaultValue);
290 }
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305 public long getLong(final String section, String subsection,
306 final String name, final long defaultValue) {
307 return typedGetter.getLong(this, section, subsection, name,
308 defaultValue);
309 }
310
311
312
313
314
315
316
317
318
319
320
321
322
323 public boolean getBoolean(final String section, final String name,
324 final boolean defaultValue) {
325 return typedGetter.getBoolean(this, section, null, name, defaultValue);
326 }
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342 public boolean getBoolean(final String section, String subsection,
343 final String name, final boolean defaultValue) {
344 return typedGetter.getBoolean(this, section, subsection, name,
345 defaultValue);
346 }
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 public <T extends Enum<?>> T getEnum(final String section,
362 final String subsection, final String name, final T defaultValue) {
363 final T[] all = allValuesOf(defaultValue);
364 return typedGetter.getEnum(this, all, section, subsection, name,
365 defaultValue);
366 }
367
368 @SuppressWarnings("unchecked")
369 private static <T> T[] allValuesOf(T value) {
370 try {
371 return (T[]) value.getClass().getMethod("values").invoke(null);
372 } catch (Exception err) {
373 String typeName = value.getClass().getName();
374 String msg = MessageFormat.format(
375 JGitText.get().enumValuesNotAvailable, typeName);
376 throw new IllegalArgumentException(msg, err);
377 }
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396 public <T extends Enum<?>> T getEnum(final T[] all, final String section,
397 final String subsection, final String name, final T defaultValue) {
398 return typedGetter.getEnum(this, all, section, subsection, name,
399 defaultValue);
400 }
401
402
403
404
405
406
407
408
409
410
411
412
413 public String getString(final String section, String subsection,
414 final String name) {
415 return getRawString(section, subsection, name);
416 }
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432 public String[] getStringList(final String section, String subsection,
433 final String name) {
434 String[] base;
435 if (baseConfig != null)
436 base = baseConfig.getStringList(section, subsection, name);
437 else
438 base = EMPTY_STRING_ARRAY;
439
440 String[] self = getRawStringList(section, subsection, name);
441 if (self == null)
442 return base;
443 if (base.length == 0)
444 return self;
445 String[] res = new String[base.length + self.length];
446 int n = base.length;
447 System.arraycopy(base, 0, res, 0, n);
448 System.arraycopy(self, 0, res, n, self.length);
449 return res;
450 }
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471 public long getTimeUnit(String section, String subsection, String name,
472 long defaultValue, TimeUnit wantUnit) {
473 return typedGetter.getTimeUnit(this, section, subsection, name,
474 defaultValue, wantUnit);
475 }
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491 public List<RefSpec> getRefSpecs(String section, String subsection,
492 String name) {
493 return typedGetter.getRefSpecs(this, section, subsection, name);
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507
508 public Set<String> getSubsections(String section) {
509 return getState().getSubsections(section);
510 }
511
512
513
514
515
516
517
518
519
520 public Set<String> getSections() {
521 return getState().getSections();
522 }
523
524
525
526
527
528
529
530
531 public Set<String> getNames(String section) {
532 return getNames(section, null);
533 }
534
535
536
537
538
539
540
541
542
543
544 public Set<String> getNames(String section, String subsection) {
545 return getState().getNames(section, subsection);
546 }
547
548
549
550
551
552
553
554
555
556
557
558
559 public Set<String> getNames(String section, boolean recursive) {
560 return getState().getNames(section, null, recursive);
561 }
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576 public Set<String> getNames(String section, String subsection,
577 boolean recursive) {
578 return getState().getNames(section, subsection, recursive);
579 }
580
581
582
583
584
585
586
587
588
589
590
591
592
593 @SuppressWarnings("unchecked")
594 public <T> T get(SectionParser<T> parser) {
595 final ConfigSnapshot myState = getState();
596 T obj = (T) myState.cache.get(parser);
597 if (obj == null) {
598 obj = parser.parse(this);
599 myState.cache.put(parser, obj);
600 }
601 return obj;
602 }
603
604
605
606
607
608
609
610
611
612
613
614 public void uncache(SectionParser<?> parser) {
615 state.get().cache.remove(parser);
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629 public ListenerHandle addChangeListener(ConfigChangedListener listener) {
630 return listeners.addConfigChangedListener(listener);
631 }
632
633
634
635
636
637
638
639
640
641
642
643
644
645 protected boolean notifyUponTransientChanges() {
646 return true;
647 }
648
649
650
651
652 protected void fireConfigChangedEvent() {
653 listeners.dispatch(new ConfigChangedEvent());
654 }
655
656 String getRawString(final String section, final String subsection,
657 final String name) {
658 String[] lst = getRawStringList(section, subsection, name);
659 if (lst != null) {
660 return lst[lst.length - 1];
661 } else if (baseConfig != null) {
662 return baseConfig.getRawString(section, subsection, name);
663 } else {
664 return null;
665 }
666 }
667
668 private String[] getRawStringList(String section, String subsection,
669 String name) {
670 return state.get().get(section, subsection, name);
671 }
672
673 private ConfigSnapshot getState() {
674 ConfigSnapshot cur, upd;
675 do {
676 cur = state.get();
677 final ConfigSnapshot base = getBaseState();
678 if (cur.baseState == base)
679 return cur;
680 upd = new ConfigSnapshot(cur.entryList, base);
681 } while (!state.compareAndSet(cur, upd));
682 return upd;
683 }
684
685 private ConfigSnapshot getBaseState() {
686 return baseConfig != null ? baseConfig.getState() : null;
687 }
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707 public void setInt(final String section, final String subsection,
708 final String name, final int value) {
709 setLong(section, subsection, name, value);
710 }
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730 public void setLong(final String section, final String subsection,
731 final String name, final long value) {
732 final String s;
733
734 if (value >= GiB && (value % GiB) == 0)
735 s = String.valueOf(value / GiB) + "g";
736 else if (value >= MiB && (value % MiB) == 0)
737 s = String.valueOf(value / MiB) + "m";
738 else if (value >= KiB && (value % KiB) == 0)
739 s = String.valueOf(value / KiB) + "k";
740 else
741 s = String.valueOf(value);
742
743 setString(section, subsection, name, s);
744 }
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764 public void setBoolean(final String section, final String subsection,
765 final String name, final boolean value) {
766 setString(section, subsection, name, value ? "true" : "false");
767 }
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787 public <T extends Enum<?>> void setEnum(final String section,
788 final String subsection, final String name, final T value) {
789 String n;
790 if (value instanceof ConfigEnum)
791 n = ((ConfigEnum) value).toConfigValue();
792 else
793 n = value.name().toLowerCase(Locale.ROOT).replace('_', ' ');
794 setString(section, subsection, name, n);
795 }
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815 public void setString(final String section, final String subsection,
816 final String name, final String value) {
817 setStringList(section, subsection, name, Collections
818 .singletonList(value));
819 }
820
821
822
823
824
825
826
827
828
829
830
831 public void unset(final String section, final String subsection,
832 final String name) {
833 setStringList(section, subsection, name, Collections
834 .<String> emptyList());
835 }
836
837
838
839
840
841
842
843
844
845 public void unsetSection(String section, String subsection) {
846 ConfigSnapshot src, res;
847 do {
848 src = state.get();
849 res = unsetSection(src, section, subsection);
850 } while (!state.compareAndSet(src, res));
851 }
852
853 private ConfigSnapshotonfigSnapshot">ConfigSnapshot unsetSection(final ConfigSnapshot srcState,
854 final String section,
855 final String subsection) {
856 final int max = srcState.entryList.size();
857 final ArrayList<ConfigLine> r = new ArrayList<>(max);
858
859 boolean lastWasMatch = false;
860 for (ConfigLine e : srcState.entryList) {
861 if (e.includedFrom == null && e.match(section, subsection)) {
862
863 lastWasMatch = true;
864 continue;
865 }
866
867 if (lastWasMatch && e.section == null && e.subsection == null)
868 continue;
869 r.add(e);
870 }
871
872 return newState(r);
873 }
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893 public void setStringList(final String section, final String subsection,
894 final String name, final List<String> values) {
895 ConfigSnapshot src, res;
896 do {
897 src = state.get();
898 res = replaceStringList(src, section, subsection, name, values);
899 } while (!state.compareAndSet(src, res));
900 if (notifyUponTransientChanges())
901 fireConfigChangedEvent();
902 }
903
904 private ConfigSnapshotSnapshot">ConfigSnapshot replaceStringList(final ConfigSnapshot srcState,
905 final String section, final String subsection, final String name,
906 final List<String> values) {
907 final List<ConfigLine> entries = copy(srcState, values);
908 int entryIndex = 0;
909 int valueIndex = 0;
910 int insertPosition = -1;
911
912
913
914 while (entryIndex < entries.size() && valueIndex < values.size()) {
915 final ConfigLine e = entries.get(entryIndex);
916 if (e.includedFrom == null && e.match(section, subsection, name)) {
917 entries.set(entryIndex, e.forValue(values.get(valueIndex++)));
918 insertPosition = entryIndex + 1;
919 }
920 entryIndex++;
921 }
922
923
924
925 if (valueIndex == values.size() && entryIndex < entries.size()) {
926 while (entryIndex < entries.size()) {
927 final ConfigLine e = entries.get(entryIndex++);
928 if (e.includedFrom == null
929 && e.match(section, subsection, name))
930 entries.remove(--entryIndex);
931 }
932 }
933
934
935
936 if (valueIndex < values.size() && entryIndex == entries.size()) {
937 if (insertPosition < 0) {
938
939
940
941
942 insertPosition = findSectionEnd(entries, section, subsection,
943 true);
944 }
945 if (insertPosition < 0) {
946
947
948
949 final ConfigLineLine.html#ConfigLine">ConfigLine e = new ConfigLine();
950 e.section = section;
951 e.subsection = subsection;
952 entries.add(e);
953 insertPosition = entries.size();
954 }
955 while (valueIndex < values.size()) {
956 final ConfigLineLine.html#ConfigLine">ConfigLine e = new ConfigLine();
957 e.section = section;
958 e.subsection = subsection;
959 e.name = name;
960 e.value = values.get(valueIndex++);
961 entries.add(insertPosition++, e);
962 }
963 }
964
965 return newState(entries);
966 }
967
968 private static List<ConfigLine> copy(final ConfigSnapshot src,
969 final List<String> values) {
970
971
972
973 final int max = src.entryList.size() + values.size() + 1;
974 final ArrayList<ConfigLine> r = new ArrayList<>(max);
975 r.addAll(src.entryList);
976 return r;
977 }
978
979 private static int findSectionEnd(final List<ConfigLine> entries,
980 final String section, final String subsection,
981 boolean skipIncludedLines) {
982 for (int i = 0; i < entries.size(); i++) {
983 ConfigLine e = entries.get(i);
984 if (e.includedFrom != null && skipIncludedLines) {
985 continue;
986 }
987
988 if (e.match(section, subsection, null)) {
989 i++;
990 while (i < entries.size()) {
991 e = entries.get(i);
992 if (e.match(section, subsection, e.name))
993 i++;
994 else
995 break;
996 }
997 return i;
998 }
999 }
1000 return -1;
1001 }
1002
1003
1004
1005
1006
1007
1008 public String toText() {
1009 final StringBuilder out = new StringBuilder();
1010 for (ConfigLine e : state.get().entryList) {
1011 if (e.includedFrom != null)
1012 continue;
1013 if (e.prefix != null)
1014 out.append(e.prefix);
1015 if (e.section != null && e.name == null) {
1016 out.append('[');
1017 out.append(e.section);
1018 if (e.subsection != null) {
1019 out.append(' ');
1020 String escaped = escapeValue(e.subsection);
1021
1022 boolean quoted = escaped.startsWith("\"")
1023 && escaped.endsWith("\"");
1024 if (!quoted)
1025 out.append('"');
1026 out.append(escaped);
1027 if (!quoted)
1028 out.append('"');
1029 }
1030 out.append(']');
1031 } else if (e.section != null && e.name != null) {
1032 if (e.prefix == null || "".equals(e.prefix))
1033 out.append('\t');
1034 out.append(e.name);
1035 if (!isMissing(e.value)) {
1036 out.append(" =");
1037 if (e.value != null) {
1038 out.append(' ');
1039 out.append(escapeValue(e.value));
1040 }
1041 }
1042 if (e.suffix != null)
1043 out.append(' ');
1044 }
1045 if (e.suffix != null)
1046 out.append(e.suffix);
1047 out.append('\n');
1048 }
1049 return out.toString();
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061 public void fromText(String text) throws ConfigInvalidException {
1062 state.set(newState(fromTextRecurse(text, 1, null)));
1063 }
1064
1065 private List<ConfigLine> fromTextRecurse(String text, int depth,
1066 String includedFrom) throws ConfigInvalidException {
1067 if (depth > MAX_DEPTH) {
1068 throw new ConfigInvalidException(
1069 JGitText.get().tooManyIncludeRecursions);
1070 }
1071 final List<ConfigLine> newEntries = new ArrayList<>();
1072 final StringReader in = new StringReader(text);
1073 ConfigLine last = null;
1074 ConfigLine e = new ConfigLine();
1075 e.includedFrom = includedFrom;
1076 for (;;) {
1077 int input = in.read();
1078 if (-1 == input) {
1079 if (e.section != null)
1080 newEntries.add(e);
1081 break;
1082 }
1083
1084 final char c = (char) input;
1085 if ('\n' == c) {
1086
1087 newEntries.add(e);
1088 if (e.section != null)
1089 last = e;
1090 e = new ConfigLine();
1091 e.includedFrom = includedFrom;
1092 } else if (e.suffix != null) {
1093
1094 e.suffix += c;
1095
1096 } else if (';' == c || '#' == c) {
1097
1098 e.suffix = String.valueOf(c);
1099
1100 } else if (e.section == null && Character.isWhitespace(c)) {
1101
1102 if (e.prefix == null)
1103 e.prefix = "";
1104 e.prefix += c;
1105
1106 } else if ('[' == c) {
1107
1108 e.section = readSectionName(in);
1109 input = in.read();
1110 if ('"' == input) {
1111 e.subsection = readSubsectionName(in);
1112 input = in.read();
1113 }
1114 if (']' != input)
1115 throw new ConfigInvalidException(JGitText.get().badGroupHeader);
1116 e.suffix = "";
1117
1118 } else if (last != null) {
1119
1120 e.section = last.section;
1121 e.subsection = last.subsection;
1122 in.reset();
1123 e.name = readKeyName(in);
1124 if (e.name.endsWith("\n")) {
1125 e.name = e.name.substring(0, e.name.length() - 1);
1126 e.value = MISSING_ENTRY;
1127 } else
1128 e.value = readValue(in);
1129
1130 if (e.section.equalsIgnoreCase("include")) {
1131 addIncludedConfig(newEntries, e, depth);
1132 }
1133 } else
1134 throw new ConfigInvalidException(JGitText.get().invalidLineInConfigFile);
1135 }
1136
1137 return newEntries;
1138 }
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151 protected byte[] readIncludedConfig(String relPath)
1152 throws ConfigInvalidException {
1153 return null;
1154 }
1155
1156 private void addIncludedConfig(final List<ConfigLine> newEntries,
1157 ConfigLine line, int depth) throws ConfigInvalidException {
1158 if (!line.name.equalsIgnoreCase("path") ||
1159 line.value == null || line.value.equals(MISSING_ENTRY)) {
1160 throw new ConfigInvalidException(MessageFormat.format(
1161 JGitText.get().invalidLineInConfigFileWithParam, line));
1162 }
1163 byte[] bytes = readIncludedConfig(line.value);
1164 if (bytes == null) {
1165 return;
1166 }
1167
1168 String decoded;
1169 if (isUtf8(bytes)) {
1170 decoded = RawParseUtils.decode(UTF_8, bytes, 3, bytes.length);
1171 } else {
1172 decoded = RawParseUtils.decode(bytes);
1173 }
1174 try {
1175 newEntries.addAll(fromTextRecurse(decoded, depth + 1, line.value));
1176 } catch (ConfigInvalidException e) {
1177 throw new ConfigInvalidException(MessageFormat
1178 .format(JGitText.get().cannotReadFile, line.value), e);
1179 }
1180 }
1181
1182 private ConfigSnapshot newState() {
1183 return new ConfigSnapshot(Collections.<ConfigLine> emptyList(),
1184 getBaseState());
1185 }
1186
1187 private ConfigSnapshot newState(List<ConfigLine> entries) {
1188 return new ConfigSnapshot(Collections.unmodifiableList(entries),
1189 getBaseState());
1190 }
1191
1192
1193
1194
1195 protected void clear() {
1196 state.set(newState());
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207 protected boolean isUtf8(final byte[] bytes) {
1208 return bytes.length >= 3 && bytes[0] == (byte) 0xEF
1209 && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF;
1210 }
1211
1212 private static String readSectionName(StringReader in)
1213 throws ConfigInvalidException {
1214 final StringBuilder name = new StringBuilder();
1215 for (;;) {
1216 int c = in.read();
1217 if (c < 0)
1218 throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);
1219
1220 if (']' == c) {
1221 in.reset();
1222 break;
1223 }
1224
1225 if (' ' == c || '\t' == c) {
1226 for (;;) {
1227 c = in.read();
1228 if (c < 0)
1229 throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);
1230
1231 if ('"' == c) {
1232 in.reset();
1233 break;
1234 }
1235
1236 if (' ' == c || '\t' == c)
1237 continue;
1238 throw new ConfigInvalidException(MessageFormat.format(JGitText.get().badSectionEntry, name));
1239 }
1240 break;
1241 }
1242
1243 if (Character.isLetterOrDigit((char) c) || '.' == c || '-' == c)
1244 name.append((char) c);
1245 else
1246 throw new ConfigInvalidException(MessageFormat.format(JGitText.get().badSectionEntry, name));
1247 }
1248 return name.toString();
1249 }
1250
1251 private static String readKeyName(StringReader in)
1252 throws ConfigInvalidException {
1253 final StringBuilder name = new StringBuilder();
1254 for (;;) {
1255 int c = in.read();
1256 if (c < 0)
1257 throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);
1258
1259 if ('=' == c)
1260 break;
1261
1262 if (' ' == c || '\t' == c) {
1263 for (;;) {
1264 c = in.read();
1265 if (c < 0)
1266 throw new ConfigInvalidException(JGitText.get().unexpectedEndOfConfigFile);
1267
1268 if ('=' == c)
1269 break;
1270
1271 if (';' == c || '#' == c || '\n' == c) {
1272 in.reset();
1273 break;
1274 }
1275
1276 if (' ' == c || '\t' == c)
1277 continue;
1278 throw new ConfigInvalidException(JGitText.get().badEntryDelimiter);
1279 }
1280 break;
1281 }
1282
1283 if (Character.isLetterOrDigit((char) c) || c == '-') {
1284
1285
1286
1287 name.append((char) c);
1288 } else if ('\n' == c) {
1289 in.reset();
1290 name.append((char) c);
1291 break;
1292 } else
1293 throw new ConfigInvalidException(MessageFormat.format(JGitText.get().badEntryName, name));
1294 }
1295 return name.toString();
1296 }
1297
1298 private static String readSubsectionName(StringReader in)
1299 throws ConfigInvalidException {
1300 StringBuilder r = new StringBuilder();
1301 for (;;) {
1302 int c = in.read();
1303 if (c < 0) {
1304 break;
1305 }
1306
1307 if ('\n' == c) {
1308 throw new ConfigInvalidException(
1309 JGitText.get().newlineInQuotesNotAllowed);
1310 }
1311 if ('\\' == c) {
1312 c = in.read();
1313 switch (c) {
1314 case -1:
1315 throw new ConfigInvalidException(JGitText.get().endOfFileInEscape);
1316
1317 case '\\':
1318 case '"':
1319 r.append((char) c);
1320 continue;
1321
1322 default:
1323
1324
1325 r.append((char) c);
1326 continue;
1327 }
1328 }
1329 if ('"' == c) {
1330 break;
1331 }
1332
1333 r.append((char) c);
1334 }
1335 return r.toString();
1336 }
1337
1338 private static String readValue(StringReader in)
1339 throws ConfigInvalidException {
1340 StringBuilder value = new StringBuilder();
1341 StringBuilder trailingSpaces = null;
1342 boolean quote = false;
1343 boolean inLeadingSpace = true;
1344
1345 for (;;) {
1346 int c = in.read();
1347 if (c < 0) {
1348 break;
1349 }
1350 if ('\n' == c) {
1351 if (quote) {
1352 throw new ConfigInvalidException(
1353 JGitText.get().newlineInQuotesNotAllowed);
1354 }
1355 in.reset();
1356 break;
1357 }
1358
1359 if (!quote && (';' == c || '#' == c)) {
1360 if (trailingSpaces != null) {
1361 trailingSpaces.setLength(0);
1362 }
1363 in.reset();
1364 break;
1365 }
1366
1367 char cc = (char) c;
1368 if (Character.isWhitespace(cc)) {
1369 if (inLeadingSpace) {
1370 continue;
1371 }
1372 if (trailingSpaces == null) {
1373 trailingSpaces = new StringBuilder();
1374 }
1375 trailingSpaces.append(cc);
1376 continue;
1377 }
1378 inLeadingSpace = false;
1379 if (trailingSpaces != null) {
1380 value.append(trailingSpaces);
1381 trailingSpaces.setLength(0);
1382 }
1383
1384 if ('\\' == c) {
1385 c = in.read();
1386 switch (c) {
1387 case -1:
1388 throw new ConfigInvalidException(JGitText.get().endOfFileInEscape);
1389 case '\n':
1390 continue;
1391 case 't':
1392 value.append('\t');
1393 continue;
1394 case 'b':
1395 value.append('\b');
1396 continue;
1397 case 'n':
1398 value.append('\n');
1399 continue;
1400 case '\\':
1401 value.append('\\');
1402 continue;
1403 case '"':
1404 value.append('"');
1405 continue;
1406 case '\r': {
1407 int next = in.read();
1408 if (next == '\n') {
1409 continue;
1410 } else if (next >= 0) {
1411 in.reset();
1412 }
1413 break;
1414 }
1415 default:
1416 break;
1417 }
1418 throw new ConfigInvalidException(
1419 MessageFormat.format(JGitText.get().badEscape,
1420 Character.isAlphabetic(c)
1421 ? Character.valueOf(((char) c))
1422 : toUnicodeLiteral(c)));
1423 }
1424
1425 if ('"' == c) {
1426 quote = !quote;
1427 continue;
1428 }
1429
1430 value.append(cc);
1431 }
1432 return value.length() > 0 ? value.toString() : null;
1433 }
1434
1435 private static String toUnicodeLiteral(int c) {
1436 return String.format("\\u%04x",
1437 Integer.valueOf(c));
1438 }
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454 public static interface SectionParser<T> {
1455
1456
1457
1458
1459
1460
1461
1462 T parse(Config cfg);
1463 }
1464
1465 private static class StringReader {
1466 private final char[] buf;
1467
1468 private int pos;
1469
1470 StringReader(String in) {
1471 buf = in.toCharArray();
1472 }
1473
1474 int read() {
1475 if (pos >= buf.length) {
1476 return -1;
1477 }
1478 return buf[pos++];
1479 }
1480
1481 void reset() {
1482 pos--;
1483 }
1484 }
1485
1486
1487
1488
1489
1490
1491 public static interface ConfigEnum {
1492
1493
1494
1495
1496
1497 String toConfigValue();
1498
1499
1500
1501
1502
1503
1504
1505
1506 boolean matchConfigValue(String in);
1507 }
1508 }