comparison src/org/tmatesoft/hg/internal/ConfigFileParser.java @ 498:0205a5c4566b

Issue 38: preserve user formatting and comments when updating configuration files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 26 Oct 2012 18:17:15 +0200
parents 02140be396d5
children
comparison
equal deleted inserted replaced
497:02140be396d5 498:0205a5c4566b
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream; 19 import java.io.ByteArrayOutputStream;
21 import java.io.IOException; 20 import java.io.IOException;
22 import java.io.InputStream; 21 import java.io.InputStream;
23 import java.io.OutputStream; 22 import java.io.OutputStream;
24 import java.util.ArrayList; 23 import java.util.ArrayList;
207 } 206 }
208 } 207 }
209 // if section comes more than once, update only first one. 208 // if section comes more than once, update only first one.
210 processedSections.add(section.name); 209 processedSections.add(section.name);
211 } 210 }
211 // push rest of the contents
212 out.write(contents, contentsOffset, contents.length - contentsOffset); 212 out.write(contents, contentsOffset, contents.length - contentsOffset);
213 //
214 // add entries in new sections
215 LinkedHashSet<String> newSections = new LinkedHashSet<String>();
216 for (Iterator<String> it = additions.iterator(); it.hasNext();) {
217 String s = it.next(); it.next(); it.next();
218 if (!processedSections.contains(s)) {
219 newSections.add(s);
220 }
221 }
222 for (String newSectionName : newSections) {
223 out.write(String.format("\n[%s]", newSectionName).getBytes());
224 for (Iterator<String> it = additions.iterator(); it.hasNext();) {
225 String s = it.next(), k = it.next(), v = it.next();
226 if (newSectionName.equals(s)) {
227 out.write(String.format("\n%s = %s", k, v).getBytes());
228 }
229 }
230 out.write("\n".getBytes());
231 }
213 } 232 }
214 233
215 private void processLine(int lineNumber, int offset, byte[] line) throws IOException { 234 private void processLine(int lineNumber, int offset, byte[] line) throws IOException {
216 int localOffset = 0, i = 0; 235 int localOffset = 0, i = 0;
217 while (i < line.length && Character.isWhitespace(line[i])) { 236 while (i < line.length && Character.isWhitespace(line[i])) {
357 end = endOffset; 376 end = endOffset;
358 entries = new Entry[e.size()]; 377 entries = new Entry[e.size()];
359 e.toArray(entries); 378 e.toArray(entries);
360 } 379 }
361 } 380 }
362
363 public static void main(String[] args) throws Exception {
364 ConfigFileParser p = new ConfigFileParser();
365 p.parse(new ByteArrayInputStream(xx.getBytes()));
366 System.out.println(">>>");
367 System.out.println(xx);
368 System.out.println("===");
369 p.add("sect1", "key5", "x");
370 ByteArrayOutputStream out = new ByteArrayOutputStream(xx.length());
371 p.update(out);
372 System.out.println(new String(out.toByteArray()));
373 /*
374 for (Section s : p.sections) {
375 System.out.printf("[%s@%d]\n", s.name, s.start);
376 for (Entry e : s.entries) {
377 System.out.printf("%s@%d = %d..%d\n", e.name, e.start, e.valueStart, e.valueEnd);
378 }
379 }
380 */
381 }
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";
383 } 381 }