Debian Codegraph @

tools/buildoptions/OptionsBuilder.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2019.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package buildoptions;
14
15 import java.io.BufferedReader;
16 import java.io.BufferedWriter;
17 import java.io.File;
18 import java.io.FileWriter;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21
22 /**
23 * Convert options.txt to format needed in help file options.
24 * Saves some manual edits to keep both files in sync.
25 * Used with ant gen-options-file
26 * @author Mike Baggaley
27 *
28 */
29 public class OptionsBuilder {
30
31 public static void main(String[] args) {
32 final int indentSize = 4;
33 boolean hasNonAscii = false;
34 int lineNumber = 0;
35 File outputFile = new File(args[0]);
36 System.setProperty("line.separator", "\n");
37 try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
38 BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
39 String line;
40 String previousLine = "";
41 int indent = 0;
42 boolean preformatted = false;
43 while((line = br.readLine()) != null) {
44 ++lineNumber;
45 if (!line.matches("\\p{ASCII}*")) {
46 System.err.println("Line " + lineNumber + " contains one or more non-ASCII characters.\r\n" + line);
47 hasNonAscii = true;
48 }
49 if (preformatted) {
50 if (line.trim().compareToIgnoreCase("</pre>") == 0)
51 preformatted = false;
52 else {
53 bw.write(line);
54 bw.newLine();
55 }
56 } else {
57 line = line.replaceAll("\\s+", " ");
58 if (line.length() > 0) {
59 if (line.startsWith(";")) {
60 line = line.substring(1);
61 indent = 0;
62 if (!previousLine.isEmpty()) {
63 bw.write(previousLine);
64 bw.newLine();
65 previousLine = "";
66 }
67 }
68 else if (line.charAt(0) == ':') {
69 if (!previousLine.isEmpty()) {
70 bw.write(previousLine);
71 bw.newLine();
72 previousLine = "";
73 }
74 indent = 1;
75 line = line.substring(1);
76 while (line.charAt(0) == ':') {
77 indent++;
78 line = line.substring(1);
79 }
80 if (line.charAt(0) == ';')
81 line = line.substring(1);
82 }
83 else if (line.trim().compareToIgnoreCase("<p>") == 0) {
84 if (!previousLine.isEmpty()) {
85 bw.write(previousLine);
86 bw.newLine();
87 previousLine = "";
88 }
89 line = "";
90 bw.newLine();
91 }
92 else if (line.trim().compareToIgnoreCase("<pre>") == 0) {
93 if (!previousLine.isEmpty()) {
94 bw.write(previousLine);
95 bw.newLine();
96 previousLine = "";
97 }
98 line = "";
99 preformatted = true;
100 }
101 line = line.trim();
102 if (!previousLine.isEmpty()) {
103 if (!line.isEmpty()) {
104 previousLine += " " + line;
105 }
106 } else {
107 previousLine = line;
108 for (int i = 0; i < indent; i++) {
109 for (int j = 0; j < indentSize; j++)
110 previousLine = " " + previousLine;
111 }
112 }
113 while (previousLine.length() > 79) {
114 line = previousLine.substring(0, 80);
115 int lastSpaceIndex = line.lastIndexOf(' ');
116 int firstNonSpaceIndex = 0;
117 while (firstNonSpaceIndex < 79) {
118 if (line.charAt(firstNonSpaceIndex) != ' ')
119 break;
120 firstNonSpaceIndex++;
121 }
122 if (lastSpaceIndex > firstNonSpaceIndex) {
123 line = line.substring(0, lastSpaceIndex);
124 previousLine = previousLine.substring(lastSpaceIndex + 1);
125 for (int i = 0; i < indent; i++) {
126 for (int j = 0; j < indentSize; j++)
127 previousLine = " " + previousLine;
128 }
129 bw.write(line);
130 bw.newLine();
131 }
132 else {
133 bw.write(previousLine);
134 bw.newLine();
135 previousLine = "";
136 }
137 }
138 }
139 else {
140 indent = 0;
141 if (!previousLine.isEmpty()) {
142 bw.write(previousLine);
143 bw.newLine();
144 previousLine = "";
145 }
146 bw.newLine();
147 }
148 }
149 }
150 if (!previousLine.isEmpty()) {
151 bw.write(previousLine);
152 bw.newLine();
153 }
154 } catch (IOException e) {
155 e.printStackTrace();
156 System.exit(-1);
157 }
158 if (hasNonAscii) {
159 System.exit(-1);
160 }
161 }
162
163 }