summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/StoredUserConfig.java
blob: c8f93b20a5f437f01a0997070551a6e9bc5dbbcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
generated by cgit v1.2.3 (git 2.39.1) at 2025-08-13 22:29:40 +0000
 


/span>, String name, boolean value) {
		setString(section, subsection, name, String.valueOf(value));
	}

	public void setStringList(String section, String subsection, String name, List<String> list) {
		for (String value : list) {
			setString(section, subsection, name, value);
		}
	}

	public void save() throws IOException {
		try (FileWriter fileWriter = new FileWriter(realmFileCopy);
				PrintWriter printWriter = new PrintWriter(fileWriter);) {
			for (Map.Entry<String,Section> entry : sections.entrySet()) {
				writeSection(printWriter, entry.getKey(), entry.getValue());
			}
		}
	}

	private static void writeSection(PrintWriter printWriter, String key, Section section) {
		if (section.getSubSection() == null) {
			printWriter.printf("[%s]\n", section.getName());
		}
		else {
			printWriter.printf("[%s \"%s\"]\n", section.getName(), section.getSubSection());
		}
		for (Entry entry : section.getEntries().values()) {
			writeEntry(printWriter, entry.getKey(), entry.getValue());
		}
	}

	private static void writeEntry(PrintWriter printWriter, String key, String value) {
		printWriter.printf("\t%s = %s\n", key, escape(value));
	}

	private static String escape(String value) {
		boolean quoteIt = false;
		StringBuilder fixedValue = new StringBuilder(value.length() + 20);

		for (char c : value.toCharArray()) {
			switch (c) {
				case '\n':
					fixedValue.append("\\n");
					break;

				case '\t':
					fixedValue.append("\\t");
					break;

				case '\b':
					fixedValue.append("\\b");
					break;

				case '\\':
					fixedValue.append("\\\\");
					break;

				case '"':
					fixedValue.append("\\\"");
					break;

				case ';':
				case '#':
					quoteIt = true;
					fixedValue.append(c);
					break;

				default:
					fixedValue.append(c);
					break;
			}
		}

		if (quoteIt) {
			fixedValue.insert(0,"\"");
			fixedValue.append("\"");
		}
		return fixedValue.toString();
	}

	private static String generateKey(String key, String subKey) {
		return "k:" + key + "s:" + (subKey == null ? "" : subKey);
	}

	private static class Section {
		private final String name;
		private final String subSection;
		private final SortedMap<String, Entry> entries = new TreeMap<>();

		public Section(String name, String subSection) {
			this.name = name;
			this.subSection = subSection;
		}

		public void addEntry(final String key, final String value) {
			entries.put(generateKey(key, value), new Entry(key, value));
		}

		public String getName() {
			return name;
		}

		public String getSubSection() {
			return subSection;
		}

		public SortedMap<String, Entry> getEntries() {
			return entries;
		}

		@Override
		public int hashCode() {
			return Objects.hash(name, subSection);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Section other = (Section) obj;
			return Objects.equals(name, other.name) && Objects.equals(subSection, other.subSection);
		}

		@Override
		public String toString() {
			return String.format("Section [name=%s, subSection=%s]", name, subSection);
		}

	}

	private static class Entry {
		private final String key;
		private final String value;

		public Entry(String key, String value) {
			this.key = key;
			this.value = value;
		}

		public String getKey() {
			return key;
		}

		public String getValue() {
			return value;
		}

		@Override
		public int hashCode() {
			return Objects.hash(key, value);
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Entry other = (Entry) obj;
			return Objects.equals(key, other.key) && Objects.equals(value, other.value);
		}

		@Override
		public String toString() {
			return String.format("Entry [key=%s, value=%s]", key, value);
		}

	}

}