2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.openapi
.vcs
.changes
.patch
;
18 import com
.intellij
.openapi
.diff
.impl
.patch
.TextFilePatch
;
19 import com
.intellij
.openapi
.util
.SystemInfo
;
20 import com
.intellij
.openapi
.util
.text
.StringUtil
;
21 import com
.intellij
.openapi
.vfs
.VirtualFile
;
22 import com
.intellij
.util
.containers
.MultiMap
;
23 import org
.jetbrains
.annotations
.Nullable
;
27 abstract class AutoMatchStrategy
{
28 protected final VirtualFile myBaseDir
;
29 protected MultiMap
<String
,VirtualFile
> myFolderDecisions
;
30 protected final List
<FilePatchInProgress
> myResult
;
32 AutoMatchStrategy(final VirtualFile baseDir
) {
34 myResult
= new LinkedList
<FilePatchInProgress
>();
35 myFolderDecisions
= new MultiMap
<String
, VirtualFile
>() {
37 protected Collection
<VirtualFile
> createCollection() {
38 return new HashSet
<VirtualFile
>();
41 protected Collection
<VirtualFile
> createEmptyCollection() {
42 return Collections
.emptySet();
47 public abstract void acceptPatch(TextFilePatch patch
, final Collection
<VirtualFile
> foundByName
);
48 public abstract void processCreation(TextFilePatch creation
);
49 public abstract void beforeCreations();
50 public abstract boolean succeeded();
52 public List
<FilePatchInProgress
> getResult() {
56 protected void registerFolderDecision(final String patchPath
, final VirtualFile base
) {
57 final String path
= extractPathWithoutName(patchPath
);
59 myFolderDecisions
.putValue(path
, base
);
64 protected Collection
<VirtualFile
> suggestFolderForCreation(final TextFilePatch creation
) {
65 final String newFileParentPath
= extractPathWithoutName(creation
.getAfterName());
66 if (newFileParentPath
!= null) {
67 final Collection
<VirtualFile
> variants
= filterVariants(creation
, myFolderDecisions
.get(newFileParentPath
));
68 myResult
.add(new FilePatchInProgress(creation
, variants
, myBaseDir
));
73 protected void processCreationBasedOnFolderDecisions(final TextFilePatch creation
) {
74 final Collection
<VirtualFile
> variants
= suggestFolderForCreation(creation
);
75 if (variants
!= null) {
76 myResult
.add(new FilePatchInProgress(creation
, variants
, myBaseDir
));
78 myResult
.add(new FilePatchInProgress(creation
, null, myBaseDir
));
82 protected Collection
<VirtualFile
> filterVariants(final TextFilePatch patch
, final Collection
<VirtualFile
> in
) {
83 String path
= patch
.getBeforeName() == null ? patch
.getAfterName() : patch
.getBeforeName();
84 path
= path
.replace("\\", "/");
86 final boolean caseSensitive
= SystemInfo
.isFileSystemCaseSensitive
;
87 final Collection
<VirtualFile
> result
= new LinkedList
<VirtualFile
>();
88 for (VirtualFile vf
: in
) {
89 final String vfPath
= vf
.getPath();
90 if ((caseSensitive
&& vfPath
.endsWith(path
)) || ((! caseSensitive
) && StringUtil
.endsWithIgnoreCase(vfPath
, path
))) {
98 protected String
extractPathWithoutName(final String path
) {
99 final String replaced
= path
.replace("\\", "/");
100 final int idx
= replaced
.lastIndexOf('/');
101 if (idx
== -1) return null;
102 return replaced
.substring(0, idx
);
106 protected FilePatchInProgress
processMatch(final TextFilePatch patch
, final VirtualFile file
) {
107 final String beforeName
= patch
.getBeforeName();
108 if (beforeName
== null) return null;
109 final String
[] parts
= beforeName
.replace('\\', '/').split("/");
110 VirtualFile parent
= file
.getParent();
111 int idx
= parts
.length
- 2;
112 while ((parent
!= null) && (idx
>= 0)) {
113 if (! parent
.getName().equals(parts
[idx
])) {
116 parent
= parent
.getParent();
119 if (parent
!= null) {
120 final FilePatchInProgress result
= new FilePatchInProgress(patch
, null, myBaseDir
);
121 result
.setNewBase(parent
);
122 int numDown
= idx
+ 1;
123 for (int i
= 0; i
< numDown
; i
++) {