Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/NewlineFilter.java @ 483:e31e85cf4d4c
Handle include and unset directives in config files
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 07 Aug 2012 19:14:53 +0200 |
parents | 909306e412e2 |
children | b3c16d1aede0 |
rev | line source |
---|---|
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
456
909306e412e2
Refactor LogFacility and SessionContext, better API for both
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
2 * Copyright (c) 2011-2012 TMate Software Ltd |
113
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; |
456
909306e412e2
Refactor LogFacility and SessionContext, better API for both
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
24 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import java.io.File; |
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 |
295
981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
31 import org.tmatesoft.hg.repo.HgInternals; |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
456
diff
changeset
|
32 import org.tmatesoft.hg.repo.HgInvalidFileException; |
423
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
33 import org.tmatesoft.hg.repo.HgInvalidStateException; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 import org.tmatesoft.hg.repo.HgRepository; |
356
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
35 import org.tmatesoft.hg.util.Adaptable; |
133
4a948ec83980
core.Path to util.Path as it's not Hg repo dependant
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
130
diff
changeset
|
36 import org.tmatesoft.hg.util.Path; |
113
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 /** |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
39 * |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 * @author Artem Tikhomirov |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 * @author TMate Software Ltd. |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 */ |
356
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
43 public class NewlineFilter implements Filter, Preview, Adaptable { |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 |
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
|
45 // 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
|
46 // 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
|
47 // 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
|
48 private final boolean processInconsistent; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
49 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
|
50 |
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 // 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
|
52 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
|
53 private boolean foundCRLF = false; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 |
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
|
55 // 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
|
56 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
|
57 return new NewlineFilter(!processMixed, 0); |
352
7b34d24b8f4d
Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
58 } |
7b34d24b8f4d
Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
59 |
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
|
60 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
|
61 return new NewlineFilter(!processMixed, 1); |
352
7b34d24b8f4d
Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
62 } |
7b34d24b8f4d
Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
331
diff
changeset
|
63 |
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 private NewlineFilter(boolean onlyConsistent, int transform) { |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 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
|
66 processInconsistent = !onlyConsistent; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 public ByteBuffer filter(ByteBuffer src) { |
356
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
70 if (!processInconsistent && !previewDone) { |
423
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
71 throw new HgInvalidStateException("This filter requires preview operation prior to actual filtering when eol.only-consistent is true"); |
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
|
72 } |
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 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
|
74 // 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
|
75 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
|
76 } |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 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
|
78 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
|
79 // 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
|
80 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
|
81 } |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 return win2nix(src); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 } 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
|
84 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
|
85 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
|
86 } |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
87 return nix2win(src); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
88 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 } |
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
|
90 |
356
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
91 public <T> T getAdapter(Class<T> adapterClass) { |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
92 // conditionally through getAdapter |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
93 if (Preview.class == adapterClass) { |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
94 // when processInconsistent is false, we need to preview data stream to ensure line terminators are consistent. |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
95 // otherwise, no need to look into the stream |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
96 if (!processInconsistent) { |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
97 return adapterClass.cast(this); |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
98 } |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
99 } |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
100 return null; |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
101 } |
91d75e1bac9f
Consistent approach to deal with adaptable objects. Give adaptable precedence over instanceof to allow conditional response when classes do implement desired interface
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
355
diff
changeset
|
102 |
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
|
103 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
|
104 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
|
105 |
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 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
|
107 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
|
108 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
|
109 // 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
|
110 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
|
111 } |
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 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
|
113 // 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
|
114 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
|
115 } |
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 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 // 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
|
123 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
|
124 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
|
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 (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
|
127 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
|
128 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
|
129 } 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
|
130 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
|
131 } |
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
|
132 } 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
|
133 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
|
134 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
|
135 } 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
|
136 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
|
137 } |
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
|
138 } |
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
|
139 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
|
140 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
|
141 } |
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
|
142 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
|
143 } |
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 } |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
145 |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
146 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
|
147 int lookupStart = src.position(); // source index |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
148 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
|
149 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
|
150 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
|
151 while (lookupStart < src.limit()) { |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
152 // 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
|
153 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
|
154 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
|
155 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
|
156 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
|
157 // 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
|
158 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
|
159 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
|
160 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
|
161 } |
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
|
162 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
|
163 lookupStart = in + 1; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
164 } 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
|
165 // 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
|
166 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
|
167 // 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
|
168 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
|
169 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
|
170 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
|
171 } |
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
|
172 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
|
173 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
|
174 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
|
175 } 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
|
176 // 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
|
177 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
|
178 // 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
|
179 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
|
180 } |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
181 } |
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
|
182 } 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
|
183 // 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
|
184 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
|
185 // \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
|
186 // 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
|
187 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
|
188 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
|
189 } 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
|
190 // 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
|
191 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
|
192 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
|
193 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
|
194 } |
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 } |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
196 break; |
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 } |
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 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
|
200 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
|
201 } |
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 |
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 // 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
|
204 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
|
205 // 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
|
206 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
|
207 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
|
208 return false; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
209 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
210 } |
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 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
|
212 } |
0f3687e79f5a
Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
352
diff
changeset
|
213 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
|
214 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
|
215 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
|
216 } |
0f3687e79f5a
Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
352
diff
changeset
|
217 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
|
218 return dst; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
219 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
220 |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
221 private ByteBuffer nix2win(ByteBuffer src) { |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
222 int x = src.position(); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
223 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
|
224 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
|
225 final byte LF = (byte) '\n'; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 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
|
230 // 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
|
231 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
|
232 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
|
233 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
|
234 } |
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
|
235 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
|
236 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
|
237 } |
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 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
|
239 x = in + 1; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
240 } 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
|
241 // 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
|
242 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
|
243 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
|
244 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
|
245 } |
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
|
246 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
|
247 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
|
248 } |
0f3687e79f5a
Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
352
diff
changeset
|
249 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
|
250 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
|
251 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
|
252 x = in + 1; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
253 } |
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
|
254 } 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
|
255 // 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
|
256 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
|
257 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
|
258 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
|
259 } |
0f3687e79f5a
Treat content with target line endings as correct regardless eol.only-consistent setting
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
352
diff
changeset
|
260 break; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
261 } |
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 src.position(x); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
264 return dst == null ? src : (ByteBuffer) dst.flip(); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
265 } |
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 |
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
|
268 // 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
|
269 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
|
270 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
|
271 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
|
272 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
|
273 } |
423
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
274 // TODO post-1.0 need HgBadDataException (not InvalidState but smth closer to data stream error) |
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
275 // but don't want to add class for the single use now |
9c9c442b5f2e
Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
356
diff
changeset
|
276 throw new HgInvalidStateException(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
|
277 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
278 |
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
|
279 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
|
280 return indexOf(ch, b, from, b.limit()); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
281 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
282 |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
283 // 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
|
284 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
|
285 for (int i = from; i < to; i++) { |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
286 byte c = b.get(i); |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
287 if (ch == c) { |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
288 return i; |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
289 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
290 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
291 return -1; |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
292 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
293 |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
294 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
|
295 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
|
296 private Path.Matcher lfMatcher; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
297 private Path.Matcher crlfMatcher; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
298 private Path.Matcher binMatcher; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
299 private Path.Matcher nativeMatcher; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
300 private String nativeRepoFormat; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
301 private String nativeOSFormat; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
302 |
331
a37ce7145c3f
Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
295
diff
changeset
|
303 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
|
304 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
|
305 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
|
306 if (!cfgFile.canRead()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
307 return; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
308 } |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
309 // 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
|
310 // 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
|
311 // if (!cfgFileNode.exists()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
312 // return; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
313 // } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
314 // XXX perhaps, add HgDataFile.hasWorkingCopy and workingCopyContent()? |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
456
diff
changeset
|
315 ConfigFile hgeol = new ConfigFile(HgInternals.getContext(hgRepo)); |
295
981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
316 try { |
981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
317 hgeol.addLocation(cfgFile); |
483
e31e85cf4d4c
Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
456
diff
changeset
|
318 } catch (HgInvalidFileException ex) { |
456
909306e412e2
Refactor LogFacility and SessionContext, better API for both
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
319 HgInternals.getContext(hgRepo).getLog().dump(getClass(), Warn, ex, null); |
295
981f9f50bb6c
Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
237
diff
changeset
|
320 } |
114
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
321 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
|
322 if (nativeRepoFormat == null) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
323 nativeRepoFormat = "LF"; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
324 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
325 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
|
326 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
|
327 // 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 crlfPatterns.add(e.getKey()); |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
335 } 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
|
336 lfPatterns.add(e.getKey()); |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
337 } 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
|
338 nativePatterns.add(e.getKey()); |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
339 } 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
|
340 binPatterns.add(e.getKey()); |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
341 } else { |
456
909306e412e2
Refactor LogFacility and SessionContext, better API for both
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
423
diff
changeset
|
342 HgInternals.getContext(hgRepo).getLog().dump(getClass(), Warn, "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
|
343 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
344 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
345 if (!crlfPatterns.isEmpty()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
346 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
|
347 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
348 if (!lfPatterns.isEmpty()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
349 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
|
350 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
351 if (!binPatterns.isEmpty()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
352 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
|
353 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
354 if (!nativePatterns.isEmpty()) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
355 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
|
356 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
357 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
358 |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
359 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
|
360 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
|
361 // 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
|
362 return null; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
363 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
364 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
|
365 return null; |
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 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
|
368 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
|
369 } 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
|
370 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
|
371 } 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
|
372 if (nativeOSFormat.equals(nativeRepoFormat)) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
373 return null; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
374 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
375 if (opts.getDirection() == FromRepo) { |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
376 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
|
377 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
|
378 } 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
|
379 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
|
380 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
|
381 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
382 return null; |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
383 } |
46291ec605a0
Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
113
diff
changeset
|
384 return null; |
113
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
385 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
386 } |
67ae317408c9
Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
387 } |