Mercurial > jhg
annotate src/org/tmatesoft/hg/internal/ConfigFileParser.java @ 497:02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 25 Oct 2012 19:59:08 +0200 |
parents | |
children | 0205a5c4566b |
rev | line source |
---|---|
497
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012 TMate Software Ltd |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.internal; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import java.io.ByteArrayInputStream; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import java.io.ByteArrayOutputStream; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
22 import java.io.InputStream; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
23 import java.io.OutputStream; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
24 import java.util.ArrayList; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import java.util.Collections; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import java.util.HashSet; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
27 import java.util.Iterator; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 import java.util.LinkedHashMap; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 import java.util.LinkedHashSet; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 import java.util.List; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
32 /** |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
33 * Simplistic parser to allow altering configuration files without touching user modifications/formatting/comments |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 * |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35 * @author Artem Tikhomirov |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 * @author TMate Software Ltd. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 */ |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
38 public class ConfigFileParser { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 private enum ParseState {Initial, Section, Entry}; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 private ParseState state = ParseState.Initial; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 private int lastNonEmptyLineEndOffset = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 private String sectionName; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 private int sectionStart = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 private String entryKey; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 private int entryStart = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
46 private int valueStart = -1, valueEnd = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
47 private ArrayList<Entry> entries; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
48 private ArrayList<Section> sections = new ArrayList<Section>(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 private byte[] contents; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 private List<String> deletions = new ArrayList<String>(5); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 private List<String> additions = new ArrayList<String>(5), changes = new ArrayList<String>(5); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 public boolean exists(String section, String key) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 assert contents != null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 for (Section s : sections) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 if (s.name.equals(section)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 for (Entry e : s.entries) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 if (e.name.equals(key)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 return true; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 return false; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 return false; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 public void add(String section, String key, String newValue) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
71 additions.add(section); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 additions.add(key); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 additions.add(newValue); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 public void change(String section, String key, String newValue) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 changes.add(section); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 changes.add(key); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 changes.add(newValue); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 public void delete(String section, String key) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 deletions.add(section); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 deletions.add(key); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 public void parse(InputStream is) throws IOException { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 state = ParseState.Initial; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 sections.clear(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 contents = null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 ByteArrayOutputStream line = new ByteArrayOutputStream(80); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 int offset = 0; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 int lineOffset = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 int lineNumber = 1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 boolean crDetected = false; // true when previous char was \r |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 int b; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 while ( (b = is.read()) != -1) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 bos.write(b); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 if (b == '\n' || b == '\r') { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
101 if (line.size() > 0) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 processLine(lineNumber, lineOffset, line.toByteArray()); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 line.reset(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 lineOffset = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 lastNonEmptyLineEndOffset = bos.size() - 1; // offset points to EOL char |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
107 // else: XXX does empty line closes entry??? |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
108 // when \n follows \r, increment line count only once |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 if (!(b == '\n' && crDetected)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 lineNumber++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
111 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 crDetected = b == '\r'; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 } else { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 crDetected = false; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 if (line.size() == 0) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 lineOffset = offset; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
117 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
118 line.write(b); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
120 offset++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
121 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
122 // handle last line in case it's not EOL-terminated |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
123 if (line.size() > 0) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
124 processLine(lineNumber, lineOffset, line.toByteArray()); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
125 // might need it for #closeSection() below |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
126 lastNonEmptyLineEndOffset = bos.size(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
127 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
128 if (state == ParseState.Entry) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
129 closeEntry(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
130 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
131 if (state == ParseState.Section) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
132 closeSection(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
133 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
134 contents = bos.toByteArray(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
135 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
136 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
137 public void update(OutputStream out) throws IOException { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
138 if (contents == null) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
139 throw new IOException("Shall parse first"); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
140 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
141 HashSet<String> processedSections = new HashSet<String>(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
142 int contentsOffset = 0; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
143 for (Section section : sections) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
144 LinkedHashMap<String,String> additionsInSection = new LinkedHashMap<String,String>(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 LinkedHashMap<String,String> changesInSection = new LinkedHashMap<String,String>(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
146 LinkedHashSet<String> deletionsInSection = new LinkedHashSet<String>(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
147 if (!processedSections.contains(section.name)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
148 for (Iterator<String> it = additions.iterator(); it.hasNext();) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
149 String s = it.next(), k = it.next(), v = it.next(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
150 if (section.name.equals(s)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
151 additionsInSection.put(k, v); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
153 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
154 for (Iterator<String> it = changes.iterator(); it.hasNext();) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
155 String s = it.next(), k = it.next(), v = it.next(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
156 if (section.name.equals(s)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
157 changesInSection.put(k, v); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
158 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
159 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
160 for (Iterator<String> it = deletions.iterator(); it.hasNext();) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
161 String s = it.next(), k = it.next(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
162 if (section.name.equals(s)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
163 deletionsInSection.add(k); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
164 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
165 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
166 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
167 for (Entry e : section.entries) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
168 if (deletionsInSection.contains(e.name)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
169 // write up to key start only |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
170 out.write(contents, contentsOffset, e.start - contentsOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
171 contentsOffset = e.valueEnd + 1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
172 } else if (changesInSection.containsKey(e.name)) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
173 if (e.valueStart == -1) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
174 // e.valueEnd determines insertion point |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
175 out.write(contents, contentsOffset, e.valueEnd + 1 - contentsOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
176 } else { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
177 // e.valueEnd points to last character of the value |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
178 out.write(contents, contentsOffset, e.valueStart - contentsOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
179 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
180 String value = changesInSection.get(e.name); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
181 out.write(value == null ? new byte[0] : value.getBytes()); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
182 contentsOffset = e.valueEnd + 1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
183 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
184 // else: keep contentsOffset to point to first uncopied character |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
185 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
186 if (section.entries.length == 0) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
187 // no entries, empty or only comments, perhaps. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
188 // use end of last meaningful line (whether [section] or comment string), |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
189 // which points to newline character |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
190 out.write(contents, contentsOffset, section.end - contentsOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
191 contentsOffset = section.end; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
192 // since it's tricky to track \n or \r\n with lastNonEmptyLineEndOffset, |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
193 // we copy up to the line delimiter and insert new lines, if any, with \n prepended, |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
194 // so that original EOL will be moved to the very end of the section. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
195 // Indeed, would be better to insert *after* lastNonEmptyLineEndOffset, |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
196 // but I don't want to complicate #parse (if line.size() > 0 part) method. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
197 // Hope, this won't make too much trouble (if any, at all - |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
198 // if String.format translates \n to system EOL, then nobody would notice) |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
199 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
200 if (!additionsInSection.isEmpty()) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
201 // make sure additions are written once everything else is there |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
202 out.write(contents, contentsOffset, section.end - contentsOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
203 contentsOffset = section.end; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
204 for (String k : additionsInSection.keySet()) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
205 String v = additionsInSection.get(k); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
206 out.write(String.format("\n%s = %s", k, v == null ? "" : v).getBytes()); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
207 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
208 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
209 // if section comes more than once, update only first one. |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
210 processedSections.add(section.name); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
211 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
212 out.write(contents, contentsOffset, contents.length - contentsOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
213 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
214 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
215 private void processLine(int lineNumber, int offset, byte[] line) throws IOException { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
216 int localOffset = 0, i = 0; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
217 while (i < line.length && Character.isWhitespace(line[i])) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
218 i++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
219 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
220 if (i == line.length) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
221 return; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
222 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
223 localOffset = i; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
224 if (line[i] == '[') { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
225 if (state == ParseState.Entry) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
226 closeEntry(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
227 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
228 if (state == ParseState.Section) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
229 closeSection(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
230 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
231 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
232 while (i < line.length && line[i] != ']') { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
233 i++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
234 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
235 if (i == line.length) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
236 throw new IOException(String.format("Can't find closing ']' for section name in line %d", lineNumber)); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
237 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
238 sectionName = new String(line, localOffset+1, i-localOffset-1); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
239 sectionStart = offset + localOffset; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
240 state = ParseState.Section; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
241 } else if (line[i] == '#' || line[i] == ';') { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
242 // comment line, nothing to process |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
243 return; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
244 } else { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
245 // entry |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
246 if (state == ParseState.Initial) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
247 throw new IOException(String.format("Line %d doesn't belong to any section", lineNumber)); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
248 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
249 if (localOffset > 0) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
250 if (state == ParseState.Section) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
251 throw new IOException(String.format("Non-indented key is expected in line %d", lineNumber)); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
252 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
253 assert state == ParseState.Entry; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
254 // whitespace-indented continuation of the previous entry |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
255 if (valueStart == -1) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
256 // value didn't start at the same line the key was found at |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
257 valueStart = offset + localOffset; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
258 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
259 // value ends with eol (assumption is trailing comments are not allowed) |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
260 valueEnd = offset + line.length - 1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
261 } else { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
262 if (state == ParseState.Entry) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
263 closeEntry(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
264 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
265 assert state == ParseState.Section; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
266 // it's a new entry |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
267 state = ParseState.Entry; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
268 // get name of the entry |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
269 while (i < line.length && !Character.isWhitespace(line[i]) && line[i] != '=') { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
270 i++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
271 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
272 if (i == line.length) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
273 throw new IOException(String.format("Can't process entry in line %d", lineNumber)); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
274 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
275 entryKey = new String(line, localOffset, i - localOffset); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
276 entryStart = offset + localOffset; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
277 // look for '=' after key name |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
278 while (i < line.length && line[i] != '=') { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
279 i++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
280 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
281 if (i == line.length) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
282 throw new IOException(String.format("Can't find '=' after key %s in line %d", entryKey, lineNumber)); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
283 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
284 // skip whitespaces after '=' |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
285 i++; // line[i] == '=' |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
286 while (i < line.length && Character.isWhitespace(line[i])) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
287 i++; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
288 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
289 // valueStart might be -1 in case no value is specified in the same line as key |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
290 // but valueEnd is always initialized just in case there's no next, value continuation line |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
291 if (i == line.length) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
292 valueStart = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
293 } else { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
294 valueStart = offset + i; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
295 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
296 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
297 // if trailing comments are allowed, shall |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
298 // look up comment char and set valueEnd to its position-1 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
299 valueEnd = offset + line.length - 1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
300 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
301 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
302 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
303 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
304 private void closeSection() { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
305 assert state == ParseState.Section; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
306 assert sectionName != null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
307 assert lastNonEmptyLineEndOffset != -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
308 Section s = new Section(sectionName, sectionStart, lastNonEmptyLineEndOffset, entries == null ? Collections.<Entry>emptyList() : entries); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
309 sections.add(s); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
310 sectionName = null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
311 sectionStart = -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
312 state = ParseState.Initial; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
313 entries = null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
314 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
315 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
316 private void closeEntry() { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
317 assert state == ParseState.Entry; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
318 assert entryKey != null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
319 state = ParseState.Section; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
320 Entry e = new Entry(entryKey, entryStart, valueStart, valueEnd); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
321 if (entries == null) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
322 entries = new ArrayList<Entry>(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
323 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
324 entries.add(e); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
325 entryKey = null; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
326 entryStart = valueStart = valueEnd -1; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
327 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
328 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
329 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
330 private static class Block { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
331 public final int start; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
332 Block(int s) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
333 start = s; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
334 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
335 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
336 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
337 private static class Entry extends Block { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
338 public final int valueStart, valueEnd; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
339 public final String name; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
340 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
341 Entry(String n, int s, int vs, int ve) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
342 super(s); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
343 name = n; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
344 valueStart = vs; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
345 valueEnd = ve; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
346 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
347 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
348 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
349 private static class Section extends Block { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
350 public final String name; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
351 public final Entry[] entries; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
352 public final int end; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
353 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
354 Section(String n, int s, int endOffset, List<Entry> e) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
355 super(s); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
356 name = n; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
357 end = endOffset; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
358 entries = new Entry[e.size()]; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
359 e.toArray(entries); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
360 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
361 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
362 |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
363 public static void main(String[] args) throws Exception { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
364 ConfigFileParser p = new ConfigFileParser(); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
365 p.parse(new ByteArrayInputStream(xx.getBytes())); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
366 System.out.println(">>>"); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
367 System.out.println(xx); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
368 System.out.println("==="); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
369 p.add("sect1", "key5", "x"); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
370 ByteArrayOutputStream out = new ByteArrayOutputStream(xx.length()); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
371 p.update(out); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
372 System.out.println(new String(out.toByteArray())); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
373 /* |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
374 for (Section s : p.sections) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
375 System.out.printf("[%s@%d]\n", s.name, s.start); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
376 for (Entry e : s.entries) { |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
377 System.out.printf("%s@%d = %d..%d\n", e.name, e.start, e.valueStart, e.valueEnd); |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
378 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
379 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
380 */ |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
381 } |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
382 private static final String xx = "#comment1\n [sect1]\nkey = value #not a comment2\n#comment3\nkey2= \nkey3 = \n value1, #cc\n value2\nkey4 = v1,\n v2 \n ,v3\n\n\n[sect2]\nx = a"; |
02140be396d5
Issue 38. Towards gentle handling of config files - parse them and keep every possible user change
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
383 } |