annotate src/org/tmatesoft/hg/internal/NewlineFilter.java @ 355:f2c11fe7f3e9

Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 06 Dec 2011 12:57:21 +0100
parents 0f3687e79f5a
children 91d75e1bac9f
rev   line source
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
130
7567f4a42fe5 Correct contact address
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
15 * contact TMate Software at support@hg4j.com
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
19 import static java.lang.Math.max;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
20 import static java.lang.Math.min;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import static org.tmatesoft.hg.internal.Filter.Direction.FromRepo;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import static org.tmatesoft.hg.internal.Filter.Direction.ToRepo;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import static org.tmatesoft.hg.internal.KeywordFilter.copySlice;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.io.File;
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
26 import java.io.IOException;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.nio.ByteBuffer;
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
28 import java.util.ArrayList;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
29 import java.util.Map;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
31 import org.tmatesoft.hg.core.HgBadStateException;
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
32 import org.tmatesoft.hg.repo.HgInternals;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 import org.tmatesoft.hg.repo.HgRepository;
133
4a948ec83980 core.Path to util.Path as it's not Hg repo dependant
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 130
diff changeset
34 import org.tmatesoft.hg.util.Path;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 /**
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 *
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 * @author Artem Tikhomirov
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 * @author TMate Software Ltd.
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 */
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
41 public class NewlineFilter implements Filter, Preview {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
43 // if processInconsistent is false, filter simply pass incorrect newline characters (single \r or \r\n on *nix and single \n on Windows) as is,
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
44 // i.e. doesn't try to convert them into appropriate newline characters.
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
45 // XXX revisit if Keyword extension behaves differently - WTF???
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
46 private final boolean processInconsistent;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 private final boolean winToNix;
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
48
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
49 // NOTE, if processInconsistent == true, foundCRLF and foundLoneLF are not initialized
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
50 private boolean foundLoneLF = false;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
51 private boolean foundCRLF = false;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
53 // next two factory methods for test purposes
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
54 public static NewlineFilter createWin2Nix(boolean processMixed) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
55 return new NewlineFilter(!processMixed, 0);
352
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
56 }
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
57
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
58 public static NewlineFilter createNix2Win(boolean processMixed) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
59 return new NewlineFilter(!processMixed, 1);
352
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
60 }
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
61
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
62 private NewlineFilter(boolean onlyConsistent, int transform) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 winToNix = transform == 0;
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
64 processInconsistent = !onlyConsistent;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 public ByteBuffer filter(ByteBuffer src) {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
68 if (!previewDone) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
69 throw new HgBadStateException("This filter requires preview operation prior to actual filtering");
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
70 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
71 if (!processInconsistent && foundLoneLF && foundCRLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
72 // do not process inconsistent newlines
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
73 return src;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
74 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 if (winToNix) {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
76 if (!processInconsistent && !foundCRLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
77 // no reason to process if no CRLF in the data stream
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
78 return src;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
79 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 return win2nix(src);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 } else {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
82 if (!processInconsistent && !foundLoneLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
83 return src;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
84 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 return nix2win(src);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
88
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
89 private boolean prevBufLastByteWasCR = false;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
90 private boolean previewDone = false;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
91
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
92 public void preview(ByteBuffer src) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
93 previewDone = true; // guard
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
94 if (processInconsistent) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
95 // gonna handle them anyway, no need to check. TODO Do not implement Preview directly, but rather
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
96 // conditionally through getAdapter when processInconsistent is false (sic!)
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
97 return;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
98 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
99 if (foundLoneLF && foundCRLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
100 // already know it's inconsistent
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
101 return;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
102 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
103 final byte CR = (byte) '\r';
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
104 final byte LF = (byte) '\n';
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
105 int x = src.position();
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
106 while (x < src.limit()) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
107 int in = indexOf(LF, src, x);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
108 if (in == -1) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
109 // no line feed, but what if it's CRLF broken in the middle?
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
110 prevBufLastByteWasCR = CR == src.get(src.limit() - 1);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
111 return;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
112 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
113 if (in == 0) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
114 if (prevBufLastByteWasCR) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
115 foundCRLF = true;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
116 } else {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
117 foundLoneLF = true;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
118 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
119 } else { // in > 0 && in >= x
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
120 if (src.get(in - 1) == CR) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
121 foundCRLF = true;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
122 } else {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
123 foundLoneLF = true;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
124 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
125 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
126 if (foundCRLF && foundLoneLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
127 return;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
128 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
129 x = in + 1;
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
130 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
131 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 private ByteBuffer win2nix(ByteBuffer src) {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
134 int lookupStart = src.position(); // source index
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 ByteBuffer dst = null;
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
136 final byte CR = (byte) '\r';
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
137 final byte LF = (byte) '\n';
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
138 while (lookupStart < src.limit()) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
139 // x, lookupStart, ir and in are absolute positions within src buffer, which is never read with modifying operations
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
140 int ir = indexOf(CR, src, lookupStart);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
141 int in = indexOf(LF, src, lookupStart);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
142 if (in != -1) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
143 if (ir == -1 || ir > in) {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
144 // lone LF. CR, if present, goes after LF, process up to that lone, closest LF; let next iteration decide what to do with CR@ir
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
145 if (!processInconsistent && foundCRLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
146 assert foundLoneLF == true : "preview() shall initialize this";
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
147 fail(src, in);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
148 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
149 dst = consume(src, lookupStart, in+1, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
150 lookupStart = in + 1;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 } else {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
152 // ir < in
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
153 if (onlyCRup2limit(src, ir, in)) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
154 // CR...CRLF;
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
155 if (!processInconsistent && foundLoneLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
156 assert foundCRLF == true : "preview() shall initialize this";
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
157 fail(src, ir);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
158 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
159 dst = consume(src, lookupStart, ir, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
160 dst.put(LF);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
161 lookupStart = in+1;
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
162 } else {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
163 // CR...CR...^CR....LF
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
164 dst = consume(src, lookupStart, ir+1, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
165 // although can search for ^CR, here I copy CR one by one as I don't expect huge sequences of CR to optimize for
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
166 lookupStart = ir+1;
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
167 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
169 } else {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
170 // no newlines
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
171 if (ir != -1 && onlyCRup2limit(src, ir, src.limit())) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
172 // \r as last character(s) is the only case we care about when there're no LF found
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
173 // cases like \r\r\r<EOB>\n shall be handled like \r\n, hence onlyCRup2limit
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
174 dst = consume(src, lookupStart, ir, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
175 lookupStart = src.limit() - 1; // leave only last CR for next buffer
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
176 } else {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
177 // consume all. don't create a copy of src if there's no dst yet
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
178 if (dst != null) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
179 copySlice(src, lookupStart, src.limit(), dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
180 lookupStart = src.limit();
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
181 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
182 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 break;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
184 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
185 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
186 src.position(lookupStart); // mark we've consumed up to x
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
187 return dst == null ? src : (ByteBuffer) dst.flip();
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
188 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
189
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
190 // true if [from..limit) are CR
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
191 private static boolean onlyCRup2limit(ByteBuffer src, int from, int limit) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
192 // extended version of (ir+1 == src.limit()): check all in [ir..src.limit) are CR
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
193 for (int i = from; i < limit; i++) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
194 if (src.get(i) != '\r') {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
195 return false;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
196 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
197 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
198 return true;
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
199 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
200 private static ByteBuffer consume(ByteBuffer src, int from, int to, ByteBuffer dst) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
201 if (dst == null) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
202 dst = ByteBuffer.allocate(src.remaining());
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
203 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
204 copySlice(src, from, to, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
205 return dst;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 private ByteBuffer nix2win(ByteBuffer src) {
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 int x = src.position();
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
210 ByteBuffer dst = null;
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
211 final byte CR = (byte) '\r';
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
212 final byte LF = (byte) '\n';
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
213 while (x < src.limit()) {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
214 int in = indexOf(LF, src, x);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
215 if (in != -1) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
216 if (in > x && src.get(in - 1) == CR) {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
217 // found CRLF
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
218 if (!processInconsistent && foundLoneLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
219 assert foundCRLF == true : "preview() shall initialize this";
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
220 fail(src, in-1);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
221 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
222 if (dst == null) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
223 dst = ByteBuffer.allocate(src.remaining() * 2);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
224 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
225 copySlice(src, x, in+1, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
226 x = in + 1;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
227 } else {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
228 // found stand-alone LF, need to output CRLF
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
229 if (!processInconsistent && foundCRLF) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
230 assert foundLoneLF == true : "preview() shall initialize this";
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
231 fail(src, in);
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
232 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
233 if (dst == null) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
234 dst = ByteBuffer.allocate(src.remaining() * 2);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
235 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
236 copySlice(src, x, in, dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
237 dst.put(CR);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
238 dst.put(LF);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
239 x = in + 1;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
240 }
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
241 } else {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
242 // no newlines (no LF), just copy what left
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
243 if (dst != null) {
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
244 copySlice(src, x, src.limit(), dst);
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
245 x = src.limit();
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
246 }
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
247 break;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
248 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
249 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
250 src.position(x);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
251 return dst == null ? src : (ByteBuffer) dst.flip();
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
252 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
253
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
254
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
255 // Test: nlFilter.fail(ByteBuffer.wrap(new "test string".getBytes()), 5);
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256 private void fail(ByteBuffer b, int pos) {
355
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
257 StringBuilder sb = new StringBuilder();
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
258 for (int i = max(pos-10, 0), x = min(pos + 10, b.limit()); i < x; i++) {
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
259 sb.append(String.format("%02x ", b.get(i)));
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
260 }
f2c11fe7f3e9 Newline filter shall respect whole stream when deciding whether to process line terminators, hence added stream preview functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 353
diff changeset
261 throw new HgBadStateException(String.format("Inconsistent newline characters in the stream %s (char 0x%x, local index:%d)", sb.toString(), b.get(pos), pos));
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
262 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
263
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
264 private static int indexOf(byte ch, ByteBuffer b, int from) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
265 return indexOf(ch, b, from, b.limit());
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
266 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
267
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
268 // looks up in buf[from..to)
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
269 private static int indexOf(byte ch, ByteBuffer b, int from, int to) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
270 for (int i = from; i < to; i++) {
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
271 byte c = b.get(i);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
272 if (ch == c) {
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
273 return i;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
274 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
275 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
276 return -1;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
277 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
278
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
279 public static class Factory implements Filter.Factory {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
280 private boolean processOnlyConsistent = true;
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
281 private Path.Matcher lfMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
282 private Path.Matcher crlfMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
283 private Path.Matcher binMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
284 private Path.Matcher nativeMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
285 private String nativeRepoFormat;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
286 private String nativeOSFormat;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
287
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
288 public void initialize(HgRepository hgRepo) {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
289 processOnlyConsistent = hgRepo.getConfiguration().getBooleanValue("eol", "only-consistent", true);
237
6e1373b54e9b Allow access to working copy content through HgDataFile. Give access to repository's working dir
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 133
diff changeset
290 File cfgFile = new File(hgRepo.getWorkingDir(), ".hgeol");
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
291 if (!cfgFile.canRead()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
292 return;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
293 }
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
294 // XXX if .hgeol is not checked out, we may get it from repository
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
295 // HgDataFile cfgFileNode = hgRepo.getFileNode(".hgeol");
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
296 // if (!cfgFileNode.exists()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
297 // return;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
298 // }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
299 // XXX perhaps, add HgDataFile.hasWorkingCopy and workingCopyContent()?
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
300 ConfigFile hgeol = new ConfigFile();
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
301 try {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
302 hgeol.addLocation(cfgFile);
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
303 } catch (IOException ex) {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
304 HgInternals.getContext(hgRepo).getLog().warn(getClass(), ex, null);
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
305 }
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
306 nativeRepoFormat = hgeol.getSection("repository").get("native");
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
307 if (nativeRepoFormat == null) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
308 nativeRepoFormat = "LF";
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
309 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
310 final String os = System.getProperty("os.name"); // XXX need centralized set of properties
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
311 nativeOSFormat = os.indexOf("Windows") != -1 ? "CRLF" : "LF";
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
312 // I assume pattern ordering in .hgeol is not important
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
313 ArrayList<String> lfPatterns = new ArrayList<String>();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
314 ArrayList<String> crlfPatterns = new ArrayList<String>();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
315 ArrayList<String> nativePatterns = new ArrayList<String>();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
316 ArrayList<String> binPatterns = new ArrayList<String>();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
317 for (Map.Entry<String,String> e : hgeol.getSection("patterns").entrySet()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
318 if ("CRLF".equals(e.getValue())) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
319 crlfPatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
320 } else if ("LF".equals(e.getValue())) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
321 lfPatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
322 } else if ("native".equals(e.getValue())) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
323 nativePatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
324 } else if ("BIN".equals(e.getValue())) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
325 binPatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
326 } else {
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
327 HgInternals.getContext(hgRepo).getLog().warn(getClass(), "Can't recognize .hgeol entry: %s for %s", e.getValue(), e.getKey());
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
328 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
329 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
330 if (!crlfPatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
331 crlfMatcher = new PathGlobMatcher(crlfPatterns.toArray(new String[crlfPatterns.size()]));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
332 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
333 if (!lfPatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
334 lfMatcher = new PathGlobMatcher(lfPatterns.toArray(new String[lfPatterns.size()]));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
335 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
336 if (!binPatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
337 binMatcher = new PathGlobMatcher(binPatterns.toArray(new String[binPatterns.size()]));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
338 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
339 if (!nativePatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
340 nativeMatcher = new PathGlobMatcher(nativePatterns.toArray(new String[nativePatterns.size()]));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
341 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
342 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
343
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
344 public Filter create(Path path, Options opts) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
345 if (binMatcher == null && crlfMatcher == null && lfMatcher == null && nativeMatcher == null) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
346 // not initialized - perhaps, no .hgeol found
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
347 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
348 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
349 if (binMatcher != null && binMatcher.accept(path)) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
350 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
351 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
352 if (crlfMatcher != null && crlfMatcher.accept(path)) {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
353 return new NewlineFilter(processOnlyConsistent, 1);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
354 } else if (lfMatcher != null && lfMatcher.accept(path)) {
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
355 return new NewlineFilter(processOnlyConsistent, 0);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
356 } else if (nativeMatcher != null && nativeMatcher.accept(path)) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
357 if (nativeOSFormat.equals(nativeRepoFormat)) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
358 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
359 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
360 if (opts.getDirection() == FromRepo) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
361 int transform = "CRLF".equals(nativeOSFormat) ? 1 : 0;
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
362 return new NewlineFilter(processOnlyConsistent, transform);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
363 } else if (opts.getDirection() == ToRepo) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
364 int transform = "CRLF".equals(nativeOSFormat) ? 0 : 1;
353
0f3687e79f5a Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 352
diff changeset
365 return new NewlineFilter(processOnlyConsistent, transform);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
366 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
367 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
368 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
369 return null;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
370 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
371 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
372 }