annotate src/org/tmatesoft/hg/internal/NewlineFilter.java @ 490:b3c16d1aede0

Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 Aug 2012 17:08:34 +0200
parents e31e85cf4d4c
children 6526d8adbc0f
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
483
e31e85cf4d4c Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 456
diff changeset
31 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
32 import org.tmatesoft.hg.repo.HgInvalidStateException;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 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
34 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
35 import org.tmatesoft.hg.util.Path;
113
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 *
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 * @author Artem Tikhomirov
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 * @author TMate Software Ltd.
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 */
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
42 public class NewlineFilter implements Filter, Preview, Adaptable {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43
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
44 // 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
45 // 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
46 // 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
47 private final boolean processInconsistent;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 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
49
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 // 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
51 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
52 private boolean foundCRLF = false;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53
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
54 // 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
55 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
56 return new NewlineFilter(!processMixed, 0);
352
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
57 }
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
58
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
59 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
60 return new NewlineFilter(!processMixed, 1);
352
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
61 }
7b34d24b8f4d Tests for newline filter (eol extension) functionality
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
62
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
63 private NewlineFilter(boolean onlyConsistent, int transform) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 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
65 processInconsistent = !onlyConsistent;
113
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
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 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
69 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
70 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
71 }
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 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
73 // 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
74 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
75 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 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
77 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
78 // 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
79 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
80 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 return win2nix(src);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 } 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
83 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
84 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
85 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 return nix2win(src);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 }
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
89
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
90 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
91 // 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
92 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
93 // 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
94 // 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
95 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
96 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
97 }
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 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
100 }
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
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
102 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
103 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
104
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 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
106 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
107 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
108 // 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
109 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
110 }
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 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
112 // 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
113 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
114 }
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 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
116 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
117 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
118 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
119 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
120 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
121 // 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
122 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
123 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
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 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
126 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
127 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
128 } 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
129 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
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 } 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
132 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
133 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
134 } 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
135 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
136 }
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 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
139 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
140 }
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 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
142 }
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 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 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
146 int lookupStart = src.position(); // source index
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 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
148 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
149 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
150 while (lookupStart < src.limit()) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 // 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
152 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
153 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
154 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
155 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
156 // 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
157 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
158 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
159 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
160 }
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
161 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
162 lookupStart = in + 1;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 } 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
164 // 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
165 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
166 // 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
167 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
168 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
169 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
170 }
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
171 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
172 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
173 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
174 } 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
175 // 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
176 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
177 // 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
178 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
179 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
180 }
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
181 } 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
182 // 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
183 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
184 // \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
185 // 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
186 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
187 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
188 } 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
189 // 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
190 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
191 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
192 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
193 }
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 }
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195 break;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
196 }
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
197 }
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 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
199 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
200 }
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 // 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
203 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
204 // 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
205 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
206 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
207 return false;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 }
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
210 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
211 }
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 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
213 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
214 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
215 }
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 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
217 return dst;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
218 }
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 private ByteBuffer nix2win(ByteBuffer src) {
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
221 int x = src.position();
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
222 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
223 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
224 final byte LF = (byte) '\n';
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
225 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
226 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
227 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
228 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
229 // 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
230 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
231 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
232 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
233 }
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
234 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
235 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
236 }
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 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
238 x = in + 1;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
239 } 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
240 // 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
241 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
242 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
243 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
244 }
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
245 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
246 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
247 }
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 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
249 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
250 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
251 x = in + 1;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
252 }
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
253 } 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
254 // 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
255 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
256 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
257 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
258 }
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 break;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
260 }
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 src.position(x);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
263 return dst == null ? src : (ByteBuffer) dst.flip();
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
264 }
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
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
267 // 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
268 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
269 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
270 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
271 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
272 }
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
273 // 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
274 // 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
275 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
276 }
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
277
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
278 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
279 return indexOf(ch, b, from, b.limit());
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
280 }
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 // 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
283 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
284 for (int i = from; i < to; i++) {
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
285 byte c = b.get(i);
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
286 if (ch == c) {
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
287 return i;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
288 }
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 return -1;
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
291 }
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 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
294 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
295 private Path.Matcher lfMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
296 private Path.Matcher crlfMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
297 private Path.Matcher binMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
298 private Path.Matcher nativeMatcher;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
299 private String nativeRepoFormat;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
300 private String nativeOSFormat;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
301
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 295
diff changeset
302 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
303 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
304 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
305 if (!cfgFile.canRead()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
306 return;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
307 }
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
308 // 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
309 // 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
310 // if (!cfgFileNode.exists()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
311 // return;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
312 // }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
313 // XXX perhaps, add HgDataFile.hasWorkingCopy and workingCopyContent()?
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 483
diff changeset
314 ConfigFile hgeol = new ConfigFile(hgRepo.getSessionContext());
295
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
315 try {
981f9f50bb6c Issue 11: Error log facility. SessionContext to share common facilities
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 237
diff changeset
316 hgeol.addLocation(cfgFile);
483
e31e85cf4d4c Handle include and unset directives in config files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 456
diff changeset
317 } catch (HgInvalidFileException ex) {
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 483
diff changeset
318 hgRepo.getSessionContext().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
319 }
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
320 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
321 if (nativeRepoFormat == null) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
322 nativeRepoFormat = "LF";
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
323 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
324 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
325 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
326 // 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
327 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
328 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
329 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
330 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
331 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
332 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
333 crlfPatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
334 } 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
335 lfPatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
336 } 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
337 nativePatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
338 } 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
339 binPatterns.add(e.getKey());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
340 } else {
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 483
diff changeset
341 hgRepo.getSessionContext().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
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 if (!crlfPatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
345 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
346 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
347 if (!lfPatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
348 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
349 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
350 if (!binPatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
351 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
352 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
353 if (!nativePatterns.isEmpty()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
354 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
355 }
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 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
359 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
360 // 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
361 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
362 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
363 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
364 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
365 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
366 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
367 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
368 } 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
369 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
370 } 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
371 if (nativeOSFormat.equals(nativeRepoFormat)) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
372 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
373 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
374 if (opts.getDirection() == FromRepo) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
375 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
376 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
377 } 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
378 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
379 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
380 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
381 return null;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
382 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
383 return null;
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
384 }
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 }