1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* Copyright 2000-2013 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.sass.internal.resolver;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.w3c.css.sac.InputSource;
import com.vaadin.sass.internal.ScssStylesheet;
/**
* Base class for resolvers. Implements functionality for locating paths which
* an import can be relative to and helpers for extracting path information from
* the identifier.
*
* @since 7.2
* @author Vaadin Ltd
*/
public abstract class AbstractResolver implements ScssStylesheetResolver,
Serializable {
/*
* (non-Javadoc)
*
* @see
* com.vaadin.sass.internal.resolver.ScssStylesheetResolver#resolve(java
* .lang.String)
*/
@Override
public InputSource resolve(ScssStylesheet parentStylesheet,
String identifier) {
// Remove a possible ".scss" suffix
identifier = identifier.replaceFirst(".scss$", "");
List<String> potentialParentPaths = getPotentialParentPaths(
parentStylesheet, identifier);
// remove path from identifier as it has already been added to the
// parent path
if (identifier.contains("/")) {
identifier = identifier.substring(identifier.lastIndexOf("/") + 1);
}
for (String path : potentialParentPaths) {
InputSource source = normalizeAndResolve(path + "/" + identifier);
if (source != null) {
return source;
}
// Try to find partial import (_identifier.scss)
source = normalizeAndResolve(path + "/_" + identifier);
if (source != null) {
return source;
}
}
return normalizeAndResolve(identifier);
}
/**
* Retrieves the parent paths which should be used while resolving relative
* identifiers. By default uses the parent stylesheet location and a
* possible absolute path in the identifier.
*
* @param parentStylesheet
* The parent stylesheet or null if there is no parent
* @param identifier
* The identifier to be resolved
* @return a list of paths in which to look for the relative import
*/
protected List<String> getPotentialParentPaths(
ScssStylesheet parentStylesheet, String identifier) {
List<String> potentialParents = new ArrayList<String>();
if (parentStylesheet != null) {
potentialParents.add(extractFullPath(
parentStylesheet.getDirectory(), identifier));
}
// Identifier can be a full path so extract the path part also as a
// potential parent
if (identifier.contains("/")) {
potentialParents.add(extractFullPath("", identifier));
}
return potentialParents;
}
/**
* Extracts the full path from the path combined with the identifier
*
* @param path
* The base path
* @param identifier
* The identifier which may contain a path part, separated by "/"
* from the real identifier
* @return a normalized version of the path where identifier does not
* contain any directory information
*/
protected String extractFullPath(String path, String identifier) {
int lastSlashPosition = identifier.lastIndexOf("/");
if (lastSlashPosition == -1) {
return path;
}
String identifierPath = identifier.substring(0, lastSlashPosition);
if ("".equals(path)) {
return identifierPath;
} else {
return path + "/" + identifierPath;
}
}
/**
* Resolves the normalized version of the given identifier
*
* @param identifier
* The identifier to resolve
* @return An input source if the resolver found one or null otherwise
*/
protected InputSource normalizeAndResolve(String identifier) {
String normalized = normalize(identifier);
return resolveNormalized(normalized);
}
/**
* Resolves the identifier after it has been normalized using
* {@link #normalize(String)}.
*
* @param identifier
* The normalized identifier
* @return an InputSource if the resolver found a source or null otherwise
*/
protected abstract InputSource resolveNormalized(String identifier);
/**
* Normalizes "." and ".." from the path string where parent path segments
* can be removed. Preserve leading "..". Also ensure / is used instead of \
* in all places.
*
* @param path
* A relative or absolute file path
* @return The normalized path
*/
protected String normalize(String path) {
// Ensure only "/" is used, also in Windows
path = path.replace(File.separatorChar, '/');
// Split into segments
String[] segments = path.split("/");
Stack<String> result = new Stack<String>();
// Replace '.' and '..' segments
for (int i = 0; i < segments.length; i++) {
if (segments[i].equals(".")) {
// Segments marked '.' are ignored
} else if (segments[i].equals("..") && !result.isEmpty()
&& !result.lastElement().equals("..")) {
// If segment is ".." then remove the previous iff the previous
// element is not a ".." and the result stack is not empty
result.pop();
} else {
// Other segments are just added to the stack
result.push(segments[i]);
}
}
// Reconstruct path
StringBuilder pathBuilder = new StringBuilder();
for (int i = 0; i < result.size(); i++) {
if (i > 0) {
pathBuilder.append("/");
}
pathBuilder.append(result.get(i));
}
return pathBuilder.toString();
}
}
|