My favorites | Sign in
Project Hosting will be READ-ONLY Thursday at 3:00pm UTC for up to 3 hours for network maintenance.
Project Home Downloads Wiki Issues Source
Search
for
ReusingProcessors  
How to use wro4j processors outside of wro4j context.
Featured
Updated Feb 7, 2012 by alex.obj...@gmail.com

Introduction

It is possible to reuse wro4j processors only, without needing to have a filter or a maven plugin - only java code. This might be useful for creating custom implementation which reuse processors like: CoffeeScript, LessCss or any other available processor.

Details

Most of the processors extend the same interface:

public interface ResourcePreProcessor {
  void process(final Resource resource, final Reader reader, final Writer writer) throws IOException;
}

The idea is simple, process a Resource whose content should be taken from the Reader and write the outcome to the Writer. You should not be interested about the underlying details (ex: if the processor is using third party libraries,like Rhino, or does other tricks). In most of the case, using a processor is as simple as:

Resource resource = Resource.valueOf("script.coffee", ResourceType.JS);
Reader reader = new FileReader("path/to/script.coffee");
Writer writer = new FileWriter("path/to/script.js");
new CoffeeScriptProcessor().process(resource, reader, writer);

The above code reads a file located at path/to/script.coffee containing a coffee script code and creates a javascript file located at path/to/script.js.

Since the location of the resource is abstracted to Reader/Writer you can easily read the content from a different location (not only files) and writing it to an arbitrary location (ex: servlet output stream).

Reusing context-aware Processors

Some of the processors require some extra details like: WroConfiguration or ResourceUriLocators or other wro4j related dependencies. For instance, the GoogleClosureCompressorProcessor relies on WroConfiguration object which holds the information about encoding. This kind of processors requires a little bit extra coding to help the processor become aware of this kind of configurations. As a result the code might look like:

//Create the configuration object and use it for current context
WroConfiguration config = new WroConfiguration();
Context.set(Context.standaloneContext(), config);

//Create injector which will inject all dependencies of the processor
Injector injector = new InjectorBuilder().build();

GoogleClosureCompressorProcessor processor = new GoogleClosureCompressorProcessor();

//this will inject all required fields, after this point it is safe to use processor outside of wro4j context. 
injector.inject(processor);

Resource resource = Resource.valueOf("script.js", ResourceType.JS);
Reader reader = new FileReader("path/to/script.js");
Writer writer = new FileWriter("path/to/script.min.js");

//Do the actual processing
processor.process(resource, reader, writer);
Comment by espina.e...@gmail.com, Jan 9, 2012

The last line should be:

processor.process("script.js", reader, writer);

Thanks:

Comment by project member alex.obj...@gmail.com, Jan 9, 2012

Actually it has to be a resource: Resource.create("script.js") or Resource.create("script.js", ResourceType?.JS). I've corrected it. Thanks.


Sign in to add a comment
Powered by Google Project Hosting