Skip to content

Commit a8bbebd

Browse files
committed
Use the ReaderFactory also for loading of Image URLs. fix #55
1 parent be4681e commit a8bbebd

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/com/inet/lib/less/CssFormatter.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ private static class SharedState {
116116

117117
private LessExtendMap lessExtends = state.lessExtends;
118118

119+
private ReaderFactory readerFactory;
120+
119121
private CssOutput currentOutput;
120122

121123
private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
@@ -170,12 +172,18 @@ private CssFormatter copy( @Nullable StringBuilder output ) {
170172
/**
171173
* Format the a parsed less file.
172174
*
173-
* @param parser the parser result
174-
* @param baseURL the URL of the less file
175-
* @param target the output of the resulting string
176-
*/
177-
void format( LessParser parser, URL baseURL, StringBuilder target ) {
175+
* @param parser
176+
* the parser result
177+
* @param baseURL
178+
* the URL of the less file
179+
* @param readerFactory
180+
* A factory for the readers for imports.
181+
* @param target
182+
* the output of the resulting string
183+
*/
184+
void format( LessParser parser, URL baseURL, ReaderFactory readerFactory, StringBuilder target ) {
178185
state.baseURL = baseURL;
186+
this.readerFactory = readerFactory;
179187
addVariables( parser.getVariables() );
180188
state.isReference = false;
181189

@@ -304,6 +312,14 @@ URL getBaseURL() {
304312
return state.baseURL;
305313
}
306314

315+
/**
316+
* Get the reader factory.
317+
* @return the factory
318+
*/
319+
ReaderFactory getReaderFactory() {
320+
return readerFactory;
321+
}
322+
307323
/**
308324
* Get a variable expression from the current stack
309325
*

src/com/inet/lib/less/Less.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public static String compile( URL baseURL, String lessData, boolean compress, Re
7878
StringBuilder builder = new StringBuilder();
7979
CssFormatter formatter = compress ? new CompressCssFormatter() : new CssFormatter();
8080
parser.parseLazy( formatter );
81-
formatter.format( parser, baseURL, builder );
81+
formatter.format( parser, baseURL, readerFactory, builder );
8282
return builder.toString();
8383
} catch( LessException ex ) {
8484
throw ex;

src/com/inet/lib/less/ReaderFactory.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,31 @@
2727
package com.inet.lib.less;
2828

2929
import java.io.IOException;
30+
import java.io.InputStream;
3031
import java.io.InputStreamReader;
3132
import java.io.Reader;
3233
import java.net.URL;
3334
import java.nio.charset.StandardCharsets;
3435

3536
/**
36-
* A factory to create a reader for parsing. You can override it to resolve the URL, implement a cache or use another
37+
* A factory to create a reader and streams for parsing. You can override it to resolve the URL, implement a cache or use another
3738
* encoding as UFT-8.
3839
*/
3940
public class ReaderFactory {
4041

42+
/**
43+
* Open an InputStream for the given URL.
44+
*
45+
* @param url
46+
* the url, not null
47+
* @return the stream, never null
48+
* @throws IOException
49+
* If any I/O error occur on reading the URL.
50+
*/
51+
public InputStream openStream( URL url ) throws IOException {
52+
return url.openStream();
53+
}
54+
4155
/**
4256
* Create a Reader for the given URL.
4357
*
@@ -48,6 +62,6 @@ public class ReaderFactory {
4862
* If any I/O error occur on reading the URL.
4963
*/
5064
public Reader create( URL url ) throws IOException {
51-
return new InputStreamReader( url.openStream(), StandardCharsets.UTF_8 );
65+
return new InputStreamReader( openStream( url ), StandardCharsets.UTF_8 );
5266
}
5367
}

src/com/inet/lib/less/UrlUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static void dataUri( CssFormatter formatter, String relativeURL, final String ur
174174
url = new URL( url, urlStr );
175175
InputStream input;
176176
try {
177-
input = url.openStream();
177+
input = formatter.getReaderFactory().openStream( url );
178178
} catch( Exception e ) {
179179
formatter.append( "url(" ).append( urlString ).append( ')' );
180180
return;

0 commit comments

Comments
 (0)