Skip to content
This repository was archived by the owner on Mar 15, 2021. It is now read-only.

Commit 15d5715

Browse files
committed
#3 cleanup parsing configuration mechanism
1 parent af59db7 commit 15d5715

File tree

8 files changed

+270
-144
lines changed

8 files changed

+270
-144
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* LessCSS Compiler
3+
* http://lesscss-compiler.projects.gabrys.biz/
4+
*
5+
* Copyright (c) 2015 Adam Gabryś
6+
*
7+
* This file is licensed under the BSD 3-Clause (the "License").
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain:
10+
* - a copy of the License at project page
11+
* - a template of the License at https://opensource.org/licenses/BSD-3-Clause
12+
*/
13+
package biz.gabrys.lesscss.compiler2;
14+
15+
/**
16+
* Thrown to indicate that compiler has been configured incorrectly (e.g. missing required options).
17+
* @since 2.0.0
18+
*/
19+
public class ConfigurationException extends CompilerException {
20+
21+
private static final long serialVersionUID = -2386288381210804468L;
22+
23+
/**
24+
* Constructs a new instance with the specified detail message.
25+
* @param message the detail message.
26+
* @since 2.0.0
27+
*/
28+
public ConfigurationException(final String message) {
29+
super(message);
30+
}
31+
32+
/**
33+
* Constructs a new instance with the specified detail message and cause.
34+
* @param message the detail message.
35+
* @param cause the cause.
36+
* @since 2.0.0
37+
*/
38+
public ConfigurationException(final String message, final Throwable cause) {
39+
super(message, cause);
40+
}
41+
42+
/**
43+
* Constructs a new instance with the specified cause.
44+
* @param cause the cause.
45+
* @since 2.0.0
46+
*/
47+
public ConfigurationException(final Throwable cause) {
48+
super(cause);
49+
}
50+
}

src/main/java/biz/gabrys/lesscss/compiler2/LessCompiler.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,69 +73,106 @@ protected NativeLessCompilerOptionsBuilder createOptionsBuilder() {
7373

7474
/**
7575
* Compiles a Less source file.
76-
* @param source the source file.
76+
* @param source the source file (cannot be {@code null} and must exist).
7777
* @return CSS code.
7878
* @throws InitializationException if an error occurred during compiler initialization.
79+
* @throws ConfigurationException if the compiler is configured incorrectly.
80+
* @throws ReadFileException if the compiler cannot read file content.
7981
* @throws ImportNotFoundException if any @import operation point to non-existent file.
80-
* @throws SourceFileNotFoundException if a source (input) file does not exist.
8182
* @throws SyntaxException if a syntax error occurred during source file compilation.
8283
* @throws CompilerException if an other error occurred during execution.
8384
* @since 2.0.0
8485
*/
8586
public String compile(final File source) {
87+
validateSourceFile(source);
8688
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
8789
builder.withInputFile(source);
8890
return compiler.execute(builder.create());
8991
}
9092

9193
/**
9294
* Compiles and compress a Less source file.
93-
* @param source the source file.
95+
* @param source the source file (cannot be {@code null} and must exist).
9496
* @return compressed CSS code.
9597
* @throws InitializationException if an error occurred during compiler initialization.
98+
* @throws ConfigurationException if the compiler is configured incorrectly.
99+
* @throws ReadFileException if the compiler cannot read file content.
96100
* @throws ImportNotFoundException if any @import operation point to non-existent file.
97-
* @throws SourceFileNotFoundException if a source (input) file does not exist.
98101
* @throws SyntaxException if a syntax error occurred during source file compilation.
99102
* @throws CompilerException if an other error occurred during execution.
100103
* @since 2.0.0
101104
*/
102105
public String compileAndCompress(final File source) {
106+
validateSourceFile(source);
103107
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
104108
builder.withInputFile(source).withCompress(true);
105109
return compiler.execute(builder.create());
106110
}
107111

108112
/**
109113
* Compiles a Less source file and saves result in an output file.
110-
* @param source the source file.
114+
* @param source the source file (cannot be {@code null} and must exist).
111115
* @param output the output file.
112116
* @throws InitializationException if an error occurred during compiler initialization.
117+
* @throws ConfigurationException if the compiler is configured incorrectly.
118+
* @throws ReadFileException if the compiler cannot read file content.
113119
* @throws ImportNotFoundException if any @import operation point to non-existent file.
114-
* @throws SourceFileNotFoundException if a source (input) file does not exist.
115120
* @throws SyntaxException if a syntax error occurred during source file compilation.
116121
* @throws CompilerException if an other error occurred during execution.
117122
* @since 2.0.0
118123
*/
119124
public void compile(final File source, final File output) {
125+
validateSourceFile(source);
126+
validateOutputFile(output);
120127
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
121128
builder.withInputFile(source).withOutputFile(output);
122129
compiler.execute(builder.create());
123130
}
124131

125132
/**
126133
* Compiles and compress a Less source file and saves result in an output file.
127-
* @param source the source file.
134+
* @param source the source file (cannot be {@code null} and must exist).
128135
* @param output the output file.
129136
* @throws InitializationException if an error occurred during compiler initialization.
137+
* @throws ConfigurationException if the compiler is configured incorrectly.
138+
* @throws ReadFileException if the compiler cannot read file content.
130139
* @throws ImportNotFoundException if any @import operation point to non-existent file.
131-
* @throws SourceFileNotFoundException if a source (input) file does not exist.
132140
* @throws SyntaxException if a syntax error occurred during source file compilation.
133141
* @throws CompilerException if an other error occurred during execution.
134142
* @since 2.0.0
135143
*/
136144
public void compileAndCompress(final File source, final File output) {
145+
validateSourceFile(source);
146+
validateOutputFile(output);
137147
final NativeLessCompilerOptionsBuilder builder = createOptionsBuilder();
138148
builder.withInputFile(source).withOutputFile(output).withCompress(true);
139149
compiler.execute(builder.create());
140150
}
151+
152+
/**
153+
* Validates source file.
154+
* @param source the source file.
155+
* @throws ConfigurationException if source file is {@code null} or does not exist.
156+
* @since 2.0.0
157+
*/
158+
protected void validateSourceFile(final File source) {
159+
if (source == null) {
160+
throw new ConfigurationException("Source file cannot be null");
161+
}
162+
if (!source.exists()) {
163+
throw new ConfigurationException("Source file does not exist: " + source.getAbsolutePath());
164+
}
165+
}
166+
167+
/**
168+
* Validates output file.
169+
* @param output the output file.
170+
* @throws ConfigurationException if output file is {@code null}.
171+
* @since 2.0.0
172+
*/
173+
protected void validateOutputFile(final File output) {
174+
if (output == null) {
175+
throw new ConfigurationException("Output file cannot be null");
176+
}
177+
}
141178
}

src/main/java/biz/gabrys/lesscss/compiler2/NativeLessCompiler.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@
6767
*/
6868
public class NativeLessCompiler {
6969

70-
private static final Pattern IMPORT_DOES_NOT_EXIST_PATTERN = Pattern.compile("^FileError:\\s+'(.+)'\\s+wasn't\\s+found\\s+(?s).*");
71-
private static final int IMPORT_ERROR_FILE_NAME_GROUP_INDEX = 1;
72-
private static final Pattern SOURCE_DOES_NOT_EXIST_PATTERN = Pattern.compile("Couldn't open source file: (.*)");
73-
private static final int SOURCE_ERROR_FILE_PATH_GROUP_INDEX = 1;
70+
private static final Pattern CONFIGURATION_EXCEPTION_PATTERN = Pattern.compile("^Configuration problem: (.*)");
71+
private static final int CONFIGURATION_EXCEPTION_MESSAGE_GROUP_INDEX = 1;
72+
private static final Pattern IMPORT_EXCEPTION_PATTERN = Pattern.compile("^FileError:\\s+'(.+)'\\s+wasn't\\s+found\\s+(?s).*");
73+
private static final int IMPORT_EXCEPTION_FILE_NAME_GROUP_INDEX = 1;
74+
private static final Pattern READ_FILE_EXCEPTION_PATTERN = Pattern.compile("^Couldn't open source file: (.*)");
75+
private static final int READ_FILE_EXCEPTION_FILE_PATH_GROUP_INDEX = 1;
7476

7577
private static final String CHARSET = "UTF-8";
7678

@@ -93,8 +95,9 @@ public NativeLessCompiler() {
9395
* @param options the compiler options.
9496
* @return the compiler output (depends on options it can be e.g. CSS code, Source Map etc).
9597
* @throws InitializationException if an error occurred during compiler initialization.
98+
* @throws ConfigurationException if the compiler is configured incorrectly.
99+
* @throws ReadFileException if the compiler cannot read file content.
96100
* @throws ImportNotFoundException if any @import operation point to non-existent file.
97-
* @throws SourceFileNotFoundException if a source (input) file does not exist.
98101
* @throws SyntaxException if a syntax error occurred during source file compilation.
99102
* @throws CompilerException if an other error occurred during execution.
100103
* @since 2.0.0
@@ -168,13 +171,17 @@ private static CompilerException parseException(final JavaScriptException except
168171
final Scriptable value = (Scriptable) exception.getValue();
169172
if (value != null && ScriptableObject.hasProperty(value, "message")) {
170173
final String message = ScriptableObject.getProperty(value, "message").toString();
171-
Matcher matcher = IMPORT_DOES_NOT_EXIST_PATTERN.matcher(message);
174+
Matcher matcher = IMPORT_EXCEPTION_PATTERN.matcher(message);
172175
if (matcher.find()) {
173-
return new ImportNotFoundException(message, exception, matcher.group(IMPORT_ERROR_FILE_NAME_GROUP_INDEX));
176+
return new ImportNotFoundException(message, exception, matcher.group(IMPORT_EXCEPTION_FILE_NAME_GROUP_INDEX));
174177
}
175-
matcher = SOURCE_DOES_NOT_EXIST_PATTERN.matcher(message);
178+
matcher = CONFIGURATION_EXCEPTION_PATTERN.matcher(message);
176179
if (matcher.find()) {
177-
return new SourceFileNotFoundException(message, matcher.group(SOURCE_ERROR_FILE_PATH_GROUP_INDEX));
180+
return new ConfigurationException(matcher.group(CONFIGURATION_EXCEPTION_MESSAGE_GROUP_INDEX));
181+
}
182+
matcher = READ_FILE_EXCEPTION_PATTERN.matcher(message);
183+
if (matcher.find()) {
184+
return new ReadFileException(message, matcher.group(READ_FILE_EXCEPTION_FILE_PATH_GROUP_INDEX));
178185
}
179186
return new SyntaxException(message, exception);
180187
}

src/main/java/biz/gabrys/lesscss/compiler2/SourceFileNotFoundException.java renamed to src/main/java/biz/gabrys/lesscss/compiler2/ReadFileException.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,52 @@
1313
package biz.gabrys.lesscss.compiler2;
1414

1515
/**
16-
* Thrown to indicate that a source file does not exist.
16+
* Thrown to indicate that a compiler cannot read file content.
1717
* @since 2.0.0
1818
*/
19-
public class SourceFileNotFoundException extends CompilerException {
19+
public class ReadFileException extends CompilerException {
2020

2121
private static final long serialVersionUID = 3711806897577477826L;
2222

2323
private final String filePath;
2424

2525
/**
26-
* Constructs a new instance with the specified detail message and source file path.
26+
* Constructs a new instance with the specified detail message and file path.
2727
* @param message the detail message.
28-
* @param filePath the source file path.
28+
* @param filePath the file path.
2929
* @since 2.0.0
3030
*/
31-
public SourceFileNotFoundException(final String message, final String filePath) {
31+
public ReadFileException(final String message, final String filePath) {
3232
super(message);
3333
this.filePath = filePath;
3434
}
3535

3636
/**
37-
* Constructs a new instance with the specified detail message, cause and source file path.
37+
* Constructs a new instance with the specified detail message, cause and file path.
3838
* @param message the detail message.
3939
* @param cause the cause.
40-
* @param filePath the source file path.
40+
* @param filePath the file path.
4141
* @since 2.0.0
4242
*/
43-
public SourceFileNotFoundException(final String message, final Throwable cause, final String filePath) {
43+
public ReadFileException(final String message, final Throwable cause, final String filePath) {
4444
super(message, cause);
4545
this.filePath = filePath;
4646
}
4747

4848
/**
49-
* Constructs a new instance with the cause and source file path.
49+
* Constructs a new instance with the cause and file path.
5050
* @param cause the cause.
51-
* @param filePath the source file path.
51+
* @param filePath the file path.
5252
* @since 2.0.0
5353
*/
54-
public SourceFileNotFoundException(final Throwable cause, final String filePath) {
54+
public ReadFileException(final Throwable cause, final String filePath) {
5555
super(cause);
5656
this.filePath = filePath;
5757
}
5858

5959
/**
60-
* Returns a source file path.
61-
* @return the source file path.
60+
* Returns a file path.
61+
* @return the file path.
6262
* @since 2.0.0
6363
*/
6464
public String getFilePath() {

src/main/javascript/biz/gabrys/lesscss/compiler2/less-rhino-1.7.5.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -252,34 +252,30 @@ less.Parser = function Parser(env) {
252252
callback(e, root, importedPreviously, fullPath);
253253
};
254254

255-
if (less.Parser.importer) {
256-
less.Parser.importer(path, currentFileInfo, fileParsedFunc, env);
257-
} else {
258-
less.Parser.fileLoader(path, currentFileInfo, function(e, contents, fullPath, newFileInfo) {
259-
if (e) {
260-
fileParsedFunc(e);
261-
return;
262-
}
255+
less.Parser.fileLoader(path, currentFileInfo, function(e, contents, fullPath, newFileInfo) {
256+
if (e) {
257+
fileParsedFunc(e);
258+
return;
259+
}
263260

264-
var newEnv = new tree.parseEnv(env);
261+
var newEnv = new tree.parseEnv(env);
265262

266-
newEnv.currentFileInfo = newFileInfo;
267-
newEnv.processImports = false;
268-
newEnv.contents[fullPath] = contents;
263+
newEnv.currentFileInfo = newFileInfo;
264+
newEnv.processImports = false;
265+
newEnv.contents[fullPath] = contents;
269266

270-
if (currentFileInfo.reference || importOptions.reference) {
271-
newFileInfo.reference = true;
272-
}
267+
if (currentFileInfo.reference || importOptions.reference) {
268+
newFileInfo.reference = true;
269+
}
273270

274-
if (importOptions.inline) {
275-
fileParsedFunc(null, contents, fullPath);
276-
} else {
277-
new(less.Parser)(newEnv).parse(contents, function(e, root) {
278-
fileParsedFunc(e, root, fullPath);
279-
});
280-
}
281-
}, env);
282-
}
271+
if (importOptions.inline) {
272+
fileParsedFunc(null, contents, fullPath);
273+
} else {
274+
new(less.Parser)(newEnv).parse(contents, function(e, root) {
275+
fileParsedFunc(e, root, fullPath);
276+
});
277+
}
278+
}, env);
283279
}
284280
};
285281

0 commit comments

Comments
 (0)