com.github.marceloverdijk.lesscss4j
Class LessCompiler

java.lang.Object
  extended by com.github.marceloverdijk.lesscss4j.LessCompiler

public class LessCompiler
extends Object

The LESS compiler to compile LESS sources to CSS stylesheets.

The compiler uses Rhino (JavaScript implementation written in Java), Envjs (simulated browser environment written in JavaScript), and the official LESS JavaScript compiler.
Note that lesscss4j is not a Java implementation of LESS itself, but rather integrates the LESS JavaScript compiler within a Java/JavaScript browser environment provided by Rhino and Envjs.

Lesscss4j comes bundled with the Envjs and LESS JavaScript, so there is no need to include them yourself. But if needed they can be overridden.

Basic code example:

 LessCompiler lessCompiler = new LessCompiler();
 String css = lessCompiler.compile("@color: #4D926F; #header { color: @color; }");
 

Author:
Marcel Overdijk
See Also:
LESS - The Dynamic Stylesheet language, Rhino - JavaScript for Java, Envjs - Bringing the Browser

Constructor Summary
LessCompiler()
          Constructs a new LessCompiler.
 
Method Summary
 String compile(File input)
          Compiles the LESS input File to CSS.
 void compile(File input, File output)
          Compiles the LESS input File to CSS and writes it to the specified output File.
 void compile(File input, File output, boolean force)
          Compiles the LESS input File to CSS and writes it to the specified output File.
 String compile(LessSource input)
          Compiles the input LessSource to CSS.
 void compile(LessSource input, File output)
          Compiles the input LessSource to CSS and writes it to the specified output File.
 void compile(LessSource input, File output, boolean force)
          Compiles the input LessSource to CSS and writes it to the specified output File.
 String compile(String input)
          Compiles the LESS input String to CSS.
 List<URL> getCustomJs()
          Returns the custom JavaScript files used by the compiler.
 String getEncoding()
          Returns the character encoding used by the compiler when writing the output File.
 URL getEnvJs()
          Returns the Envjs JavaScript file used by the compiler.
 URL getLessJs()
          Returns the LESS JavaScript file used by the compiler.
 void init()
          Initializes this LessCompiler.
 boolean isCompress()
          Returns whether the compiler will compress the CSS.
 void setCompress(boolean compress)
          Sets the compiler to compress the CSS.
 void setCustomJs(List<URL> customJs)
          Sets the custom JavaScript files used by the compiler.
 void setCustomJs(URL customJs)
          Sets a single custom JavaScript file used by the compiler.
 void setEncoding(String encoding)
          Sets the character encoding used by the compiler when writing the output File.
 void setEnvJs(URL envJs)
          Sets the Envjs JavaScript file used by the compiler.
 void setLessJs(URL lessJs)
          Sets the LESS JavaScript file used by the compiler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LessCompiler

public LessCompiler()
Constructs a new LessCompiler.

Method Detail

getEnvJs

public URL getEnvJs()
Returns the Envjs JavaScript file used by the compiler.

Returns:
The Envjs JavaScript file used by the compiler.

setEnvJs

public void setEnvJs(URL envJs)
Sets the Envjs JavaScript file used by the compiler. Must be set before init() is called.

Parameters:
envJs - The Envjs JavaScript file used by the compiler.

getLessJs

public URL getLessJs()
Returns the LESS JavaScript file used by the compiler.

Returns:
The LESS JavaScript file used by the compiler.

setLessJs

public void setLessJs(URL lessJs)
Sets the LESS JavaScript file used by the compiler. Must be set before init() is called.

Parameters:
The - LESS JavaScript file used by the compiler.

getCustomJs

public List<URL> getCustomJs()
Returns the custom JavaScript files used by the compiler.

Returns:
The custom JavaScript files used by the compiler.

setCustomJs

public void setCustomJs(URL customJs)
Sets a single custom JavaScript file used by the compiler. Must be set before init() is called.

Parameters:
customJs - A single custom JavaScript file used by the compiler.

setCustomJs

public void setCustomJs(List<URL> customJs)
Sets the custom JavaScript files used by the compiler. Must be set before init() is called.

Parameters:
customJs - The custom JavaScript files used by the compiler.

isCompress

public boolean isCompress()
Returns whether the compiler will compress the CSS.

Returns:
Whether the compiler will compress the CSS.

setCompress

public void setCompress(boolean compress)
Sets the compiler to compress the CSS.

Parameters:
compress - If true, sets the compiler to compress the CSS.

getEncoding

public String getEncoding()
Returns the character encoding used by the compiler when writing the output File.

Returns:
The character encoding used by the compiler when writing the output File.

setEncoding

public void setEncoding(String encoding)
Sets the character encoding used by the compiler when writing the output File. If not set the platform default will be used.

Parameters:
The - character encoding used by the compiler when writing the output File.

init

public void init()
Initializes this LessCompiler.

It is not needed to call this method manually, as it is called implicitly by the compile methods if needed.


compile

public String compile(String input)
               throws LessException
Compiles the LESS input String to CSS.

Parameters:
input - The LESS input String to compile.
Returns:
The CSS.
Throws:
LessException

compile

public String compile(File input)
               throws IOException,
                      LessException
Compiles the LESS input File to CSS.

Parameters:
input - The LESS input File to compile.
Returns:
The CSS.
Throws:
IOException - If the LESS file cannot be read.
LessException

compile

public void compile(File input,
                    File output)
             throws IOException,
                    LessException
Compiles the LESS input File to CSS and writes it to the specified output File.

Parameters:
input - The LESS input File to compile.
output - The output File to write the CSS to.
Throws:
IOException - If the LESS file cannot be read or the output file cannot be written.
LessException

compile

public void compile(File input,
                    File output,
                    boolean force)
             throws IOException,
                    LessException
Compiles the LESS input File to CSS and writes it to the specified output File.

Parameters:
input - The LESS input File to compile.
output - The output File to write the CSS to.
force - 'false' to only compile the LESS input file in case the LESS source has been modified (including imports) or the output file does not exists.
Throws:
IOException - If the LESS file cannot be read or the output file cannot be written.
LessException

compile

public String compile(LessSource input)
               throws LessException
Compiles the input LessSource to CSS.

Parameters:
input - The input LessSource to compile.
Returns:
The CSS.
Throws:
LessException

compile

public void compile(LessSource input,
                    File output)
             throws IOException,
                    LessException
Compiles the input LessSource to CSS and writes it to the specified output File.

Parameters:
input - The input LessSource to compile.
output - The output File to write the CSS to.
Throws:
IOException - If the LESS file cannot be read or the output file cannot be written.
LessException

compile

public void compile(LessSource input,
                    File output,
                    boolean force)
             throws IOException,
                    LessException
Compiles the input LessSource to CSS and writes it to the specified output File.

Parameters:
input - The input LessSource to compile.
output - The output File to write the CSS to.
force - 'false' to only compile the input LessSource in case the LESS source has been modified (including imports) or the output file does not exists.
Throws:
IOException - If the LESS file cannot be read or the output file cannot be written.
LessException


Copyright © 2012. All Rights Reserved.