Skip to content

Commit 873712d

Browse files
committed
add line ending support
1 parent 87ae39a commit 873712d

File tree

7 files changed

+171
-52
lines changed

7 files changed

+171
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.xipki.apppackage;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.io.FileWriter;
7+
import java.io.Writer;
8+
import java.nio.file.Path;
9+
10+
public class LineEncodingConverter
11+
{
12+
13+
private final PackageConf conf;
14+
15+
public LineEncodingConverter(File confFile) {
16+
try {
17+
conf = new PackageConf(confFile);
18+
} catch (Exception e) {
19+
throw new RuntimeException(e);
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
try {
25+
if (args == null || args.length != 2) {
26+
System.out.println("Usage: java " + LineEncodingConverter.class.getName() +
27+
" <conf-file> <dir>");
28+
System.exit(1);
29+
}
30+
31+
File confFile = new File(args[0]);
32+
File dir = new File(args[1]);
33+
new LineEncodingConverter(confFile).convertLineEncodingInDir(dir, dir.toPath());
34+
} catch (Exception e) {
35+
//e.printStackTrace();
36+
}
37+
}
38+
39+
private void convertLineEncodingInDir(File dir, Path baseDir) throws Exception
40+
{
41+
if (!(dir.exists() && dir.isDirectory())) {
42+
throw new IllegalArgumentException("dir does not exist");
43+
}
44+
45+
File[] subDirsOrFiles = dir.listFiles();
46+
for (File subDirOrFile : subDirsOrFiles) {
47+
if (subDirOrFile.isDirectory()) {
48+
convertLineEncodingInDir(subDirOrFile, baseDir);
49+
} else if (subDirOrFile.isFile()) {
50+
boolean isDosLineEncoding = conf.isDosLineEncoding(baseDir, subDirOrFile.toPath());
51+
boolean isUnixLineEncoding = conf.isUnixLineEncoding(baseDir, subDirOrFile.toPath());
52+
if (isDosLineEncoding || isUnixLineEncoding) {
53+
String lineEncoding = isDosLineEncoding ? "\r\n" : "\n";
54+
StringBuilder sb = new StringBuilder();
55+
try (BufferedReader r = new BufferedReader(new FileReader(subDirOrFile))) {
56+
String line;
57+
while ((line = r.readLine()) != null) {
58+
sb.append(line);
59+
sb.append(lineEncoding);
60+
}
61+
}
62+
63+
try (Writer w = new FileWriter(subDirOrFile)) {
64+
w.write(sb.toString());
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
}

app-package/src/main/java/org/xipki/apppackage/MyUtil.java

-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ public static byte[] readByteString(CborDecoder decoder) throws IOException {
141141
}
142142
}
143143

144-
145144
public static List<String> readTextList(CborDecoder decoder) throws IOException {
146145
if (MyUtil.isNull(decoder)) {
147146
decoder.readNull();

app-package/src/main/java/org/xipki/apppackage/PackageConf.java

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ enum SuffixMode {
2121

2222
private Map<String, Integer> posixPermissions;
2323

24+
private final Set<String> dosLineEncodings;
25+
26+
private final Set<String> unixLineEncodings;
27+
2428
public PackageConf(File confFile) throws IOException {
2529
Properties props = new Properties();
2630
try (Reader reader = new FileReader(confFile)) {
@@ -54,6 +58,24 @@ public PackageConf(File confFile) throws IOException {
5458
this.posixPermissions.put(MyUtil.toUnixPath(tokens[0]), Integer.parseInt(tokens[1]));
5559
}
5660
}
61+
62+
value = props.getProperty("lineEnding.dos");
63+
this.dosLineEncodings = new HashSet<>();
64+
if (value != null) {
65+
Set<String> _files = splitStr(value);
66+
for (String m : _files) {
67+
this.dosLineEncodings.add(m.toLowerCase());
68+
}
69+
}
70+
71+
value = props.getProperty("lineEnding.unix");
72+
this.unixLineEncodings = new HashSet<>();
73+
if (value != null) {
74+
Set<String> _files = splitStr(value);
75+
for (String m : _files) {
76+
this.unixLineEncodings.add(m.toLowerCase());
77+
}
78+
}
5779
}
5880

5981
public String getSuffix(Path path) {
@@ -82,6 +104,20 @@ public Integer posixPermission(Path baseDir, Path filePath) {
82104
return str == null ? null : posixPermissions.get(str);
83105
}
84106

107+
public boolean isDosLineEncoding(Path baseDir, Path filePath)
108+
{
109+
String extension = getExtension(filePath.getFileName().toString());
110+
String path = getMatchElement(baseDir, filePath, extension, dosLineEncodings);
111+
return path != null;
112+
}
113+
114+
public boolean isUnixLineEncoding(Path baseDir, Path filePath)
115+
{
116+
String extension = getExtension(filePath.getFileName().toString());
117+
String path = getMatchElement(baseDir, filePath, extension, unixLineEncodings);
118+
return path != null;
119+
}
120+
85121
private static String getMatchElement(Path baseDir, Path filePath, String extension, Collection<String> coll) {
86122
String canonicalPath = MyUtil.toUnixPath(baseDir, filePath);
87123
// full pah mach

app-package/src/main/java/org/xipki/apppackage/cbor/CborConstants.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JACOB - CBOR implementation in Java.
3-
*
3+
*
44
* (C) Copyright - 2013 - J.W. Janssen <[email protected]>
55
*
66
* Licensed under Apache License v2.0.
@@ -27,7 +27,7 @@ public interface CborConstants {
2727
int TYPE_TAG = 0x06;
2828
/** Major type 7: floating point, simple data types. */
2929
int TYPE_FLOAT_SIMPLE = 0x07;
30-
30+
3131
/** Denotes a one-byte value (uint8). */
3232
int ONE_BYTE = 0x18;
3333
/** Denotes a two-byte value (uint16). */

app-package/src/main/java/org/xipki/apppackage/cbor/CborDecoder.java

+31-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JACOB - CBOR implementation in Java.
3-
*
3+
*
44
* (C) Copyright - 2013 - J.W. Janssen <[email protected]>
55
*/
66
package org.xipki.apppackage.cbor;
@@ -18,7 +18,7 @@ public class CborDecoder {
1818

1919
/**
2020
* Creates a new {@link CborDecoder} instance.
21-
*
21+
*
2222
* @param is the actual input stream to read the CBOR-encoded data from, cannot be <code>null</code>.
2323
*/
2424
public CborDecoder(InputStream is) {
@@ -34,7 +34,7 @@ private static void fail(String msg, Object... args) throws IOException {
3434

3535
/**
3636
* Peeks in the input stream for the upcoming type.
37-
*
37+
*
3838
* @return the upcoming type in the stream, or <code>null</code> in case of an end-of-stream.
3939
* @throws IOException in case of I/O problems reading the CBOR-type from the underlying input stream.
4040
*/
@@ -50,7 +50,7 @@ public CborType peekType() throws IOException {
5050

5151
/**
5252
* Prolog to reading an array value in CBOR format.
53-
*
53+
*
5454
* @return the number of elements in the array to read, or -1 in case of infinite-length arrays.
5555
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
5656
*/
@@ -60,7 +60,7 @@ public long readArrayLength() throws IOException {
6060

6161
/**
6262
* Reads a boolean value in CBOR format.
63-
*
63+
*
6464
* @return the read boolean.
6565
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
6666
*/
@@ -75,7 +75,8 @@ public boolean readBoolean() throws IOException {
7575
/**
7676
* Reads a byte string value in CBOR format.
7777
*
78-
* @return the read byte string, never <code>null</code>. In case the encoded string has a length of 0, an empty string is returned.
78+
* @return the read byte string, never <code>null</code>. In case the encoded string has a length of 0, an
79+
* empty string is returned.
7980
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
8081
*/
8182
public byte[] readByteString() throws IOException {
@@ -91,7 +92,7 @@ public byte[] readByteString() throws IOException {
9192

9293
/**
9394
* Reads a signed or unsigned integer value in CBOR format.
94-
*
95+
*
9596
* @return the read integer value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
9697
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
9798
*/
@@ -106,7 +107,7 @@ public long readInt() throws IOException {
106107

107108
/**
108109
* Reads a <code>null</code>-value in CBOR format.
109-
*
110+
*
110111
* @return always <code>null</code>.
111112
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
112113
*/
@@ -117,8 +118,9 @@ public Object readNull() throws IOException {
117118

118119
/**
119120
* Reads an UTF-8 encoded string value in CBOR format.
120-
*
121-
* @return the read UTF-8 encoded string, never <code>null</code>. In case the encoded string has a length of 0, an empty string is returned.
121+
*
122+
* @return the read UTF-8 encoded string, never <code>null</code>. In case the encoded string has a length of
123+
* 0, an empty string is returned.
122124
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
123125
*/
124126
public String readTextString() throws IOException {
@@ -133,24 +135,27 @@ public String readTextString() throws IOException {
133135
}
134136

135137
/**
136-
* Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
137-
*
138+
* Reads the next major type from the underlying input stream, and verifies whether it matches the given
139+
* expectation.
140+
*
138141
* @param ib the expected major type, cannot be <code>null</code> (unchecked).
139142
* @return either -1 if the major type was an signed integer, or 0 otherwise.
140143
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
141144
*/
142145
protected long expectIntegerType(int ib) throws IOException {
143146
int majorType = ((ib & 0xFF) >>> 5);
144147
if ((majorType != CborConstants.TYPE_UNSIGNED_INTEGER) && (majorType != CborConstants.TYPE_NEGATIVE_INTEGER)) {
145-
fail("Unexpected type: %s, expected type %s or %s!", CborType.getName(majorType), CborType.getName(CborConstants.TYPE_UNSIGNED_INTEGER),
148+
fail("Unexpected type: %s, expected type %s or %s!", CborType.getName(majorType),
149+
CborType.getName(CborConstants.TYPE_UNSIGNED_INTEGER),
146150
CborType.getName(CborConstants.TYPE_NEGATIVE_INTEGER));
147151
}
148152
return -majorType;
149153
}
150154

151155
/**
152-
* Reads the next major type from the underlying input stream, and verifies whether it matches the given expectation.
153-
*
156+
* Reads the next major type from the underlying input stream, and verifies whether it matches the given
157+
* expectation.
158+
*
154159
* @param majorType the expected major type, cannot be <code>null</code> (unchecked).
155160
* @return the read subtype, or payload, of the read major type.
156161
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
@@ -164,8 +169,9 @@ protected int readMajorType(int majorType) throws IOException {
164169
}
165170

166171
/**
167-
* Reads the next major type from the underlying input stream, and verifies whether it matches the given expectations.
168-
*
172+
* Reads the next major type from the underlying input stream, and verifies whether it matches the given
173+
* expectations.
174+
*
169175
* @param majorType the expected major type, cannot be <code>null</code> (unchecked);
170176
* @param subtype the expected subtype.
171177
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
@@ -178,8 +184,9 @@ protected void readMajorTypeExact(int majorType, int subtype) throws IOException
178184
}
179185

180186
/**
181-
* Reads the next major type from the underlying input stream, verifies whether it matches the given expectation, and decodes the payload into a size.
182-
*
187+
* Reads the next major type from the underlying input stream, verifies whether it matches the given
188+
* expectation, and decodes the payload into a size.
189+
*
183190
* @param majorType the expected major type, cannot be <code>null</code> (unchecked).
184191
* @return the number of succeeding bytes, &gt;= 0, or -1 if an infinite-length type is read.
185192
* @throws IOException in case of I/O problems reading the CBOR-encoded value from the underlying input stream.
@@ -190,7 +197,7 @@ protected long readMajorTypeWithSize(int majorType) throws IOException {
190197

191198
/**
192199
* Reads an unsigned integer with a given length-indicator.
193-
*
200+
*
194201
* @param length the length indicator to use;
195202
* @param breakAllowed whether break is allowed.
196203
* @return the read unsigned integer, as long value.
@@ -219,7 +226,7 @@ protected long readUInt(int length, boolean breakAllowed) throws IOException {
219226

220227
/**
221228
* Reads an unsigned 16-bit integer value
222-
*
229+
*
223230
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
224231
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
225232
*/
@@ -230,7 +237,7 @@ protected int readUInt16() throws IOException {
230237

231238
/**
232239
* Reads an unsigned 32-bit integer value
233-
*
240+
*
234241
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
235242
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
236243
*/
@@ -241,7 +248,7 @@ protected long readUInt32() throws IOException {
241248

242249
/**
243250
* Reads an unsigned 64-bit integer value
244-
*
251+
*
245252
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
246253
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
247254
*/
@@ -253,7 +260,7 @@ protected long readUInt64() throws IOException {
253260

254261
/**
255262
* Reads an unsigned 8-bit integer value
256-
*
263+
*
257264
* @return value the read value, values from {@link Long#MIN_VALUE} to {@link Long#MAX_VALUE} are supported.
258265
* @throws IOException in case of I/O problems writing the CBOR-encoded value to the underlying output stream.
259266
*/

0 commit comments

Comments
 (0)