I'm sorry, I had to give up as I was not able to load my resource while using it. I have no time for trying more.
Here is the version using the StringBuilder. import org.apache.velocity.runtime.resource.loader.FileResourceLoader; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; public class StructuredGlobbingResourceLoader extends FileResourceLoader { public static String FOLDER; public static class VTLIndentationGlobber extends FilterInputStream { protected StringBuffer buffer = new StringBuffer(AbstractTemplateEngine.BUFFER_SIZE); protected int bufpos = 0; protected enum State { defstate, hash, comment, directive, schmoo, eol, eof } protected State state = State.defstate; public VTLIndentationGlobber(InputStream is) { super(is); } private void cleanBuffer() { buffer.delete(0, buffer.length()); } // TODO - multiline comments #* ... *# not taken into account for now in all cases public int read() throws IOException { while (true) { switch (state) { case defstate: { int ch = in.read(); switch (ch) { case (int) '#': state = State.hash; cleanBuffer(); bufpos = 0; return ch; case (int) ' ': case (int) '\t': buffer.append((char) ch); break; case -1: state = State.eof; break; default: buffer.append((char) ch); state = State.schmoo; break; } break; } case eol: if (bufpos < buffer.length()) return (int) buffer.charAt(bufpos++); else { state = State.defstate; cleanBuffer(); bufpos = 0; return '\n'; } case eof: if (bufpos < buffer.length()) return (int) buffer.charAt(bufpos++); else return -1; case hash: { int ch = (int) in.read(); switch (ch) { case (int) '#': state = State.directive; return ch; case -1: state = State.eof; return -1; default: state = State.directive; cleanBuffer(); buffer.append("##"); return ch; } } case directive: { int ch = (int) in.read(); if (ch == (int) '\n') { state = State.eol; break; } else if (ch == -1) { state = State.eof; break; } else return ch; } case schmoo: { int ch = (int) in.read(); if (ch == (int) '\n') { state = State.eol; break; } else if (ch == -1) { state = State.eof; break; } else { buffer.append((char) ch); return (int) buffer.charAt(bufpos++); } } } } } public int read(byte[] b, int off, int len) throws IOException { int i; int ok = 0; while (len-- > 0) { i = read(); if (i == -1) return (ok == 0) ? -1 : ok; b[off++] = (byte) i; ok++; } return ok; } public int read(byte[] b) throws IOException { return read(b, 0, b.length); } public boolean markSupported() { return false; } } @Override public synchronized InputStream getResourceStream(String name) { final String ressource = name; return new VTLIndentationGlobber(super.getResourceStream(ressource)); } } On 23 mars 2011, at 14:04, Jean-Baptiste BRIAUD -- Novlog wrote: > OK, I'll try the FileResourceLoader as it failed at runtime with the > WebappResourceLoader. > I already change the code to use StringBuilder. > > I'll share it with pleasure ! > Let me just validate it work for me with the FileResourceLoader. > > On 23 mars 2011, at 14:01, Claude Brisson wrote: > >> As noted on the wiki page, this custom resource loader extends >> WebappResourceLoader but you can do exactly the same while extending >> FileResourceLoader. >> >> If you do so, be sure to share it! Also, the code could probably be >> optimized a bit (for instance by using a StringBuilder instead of a String >> for its inner buffer). >> >> >> Claude >> >> On 2011-03-23 10:47, Jean-Baptiste BRIAUD -- Novlog wrote: >>> It doesn't compile : >>> the WebappResourceLoader class is not found. >>> I guess it is in the velocity-tool.jar extra lib ... I'll try, I have >>> several emergency in parallel :-) >>> If my guess is correct, I'll just have to add a new dependency on my >>> project. >>> I'll let you know. >>> >>> On 22 mars 2011, at 17:20, Claude Brisson wrote: >>> >>>> It's a very straightforward input filter that uses the common resource >>>> loading API - it should work well with 1.7. >>>> >>>> It's the filter itself that should be considered beta. >>>> >>>> Claude >>>> >>>> On 2011-03-22 14:33, Jean-Baptiste BRIAUD -- Novlog wrote: >>>>> Does it work with current 1.7 (latest stable) version or should I migrate >>>>> to V2 beta ? >>>>> If yes, is that beta version stable enough ? >>>>> >>>>> On 22 mars 2011, at 11:41, Jean-Baptiste BRIAUD -- Novlog wrote: >>>>> >>>>>> Thanks for the pointer ! For years, I was thinking it was not possible. >>>>>> >>>>>> On 21 mars 2011, at 20:52, Claude Brisson wrote: >>>>>> >>>>>>> Yet, you can check a custom resource loader available on the wiki that >>>>>>> does precisely this: >>>>>>> http://wiki.apache.org/velocity/StructuredGlobbingResourceLoader >>>>>>> >>>>>>> >>>>>>> Claude >>>>>>> >>>>>>> On 2011-03-21 19:50, Sergiu Dumitriu wrote: >>>>>>>> On 03/21/2011 07:04 PM, Rich Wagner wrote: >>>>>>>>> Sorry in advance if this is a FAQ whose answer I haven't found... >>>>>>>>> >>>>>>>>> Instead of writing: >>>>>>>>> >>>>>>>>> >>>>>>>>> #foreach( $container in $Containers ) >>>>>>>>> #if( $container.prop("Generate") ) >>>>>>>>> ...stuff... >>>>>>>>> #end >>>>>>>>> #end >>>>>>>>> >>>>>>>>> >>>>>>>>> I'd like to indent the "#if" and its "#end", for the sake of better >>>>>>>>> readability: >>>>>>>>> >>>>>>>>> >>>>>>>>> #foreach( $container in $Containers ) >>>>>>>>> #if( $container.prop("Generate") ) >>>>>>>>> ...stuff... >>>>>>>>> #end >>>>>>>>> #end >>>>>>>>> >>>>>>>>> >>>>>>>>> But then I find the spaces before the "#if" and its matching "#end" >>>>>>>>> show up in the output, which I don't want to happen. >>>>>>>>> >>>>>>>>> To get around this, I've implemented a somewhat hack-ish Template >>>>>>>>> preprocesser: my resource loader wraps a template's stream inside my >>>>>>>>> own stream implementation which filters template lines. That is, if >>>>>>>>> a line starts with "<white-space>#blah", the initial spaces are >>>>>>>>> trimmed off. >>>>>>>>> >>>>>>>>> That works, and isn't all that intrusive. But if "off-the-shelf" >>>>>>>>> Velocity already provides an easier way to accomplish the same thing, >>>>>>>>> I'd prefer that... >>>>>>>> No, there's no similar feature directly in Velocity yet. >>>>>>>> >>>>>>> --------------------------------------------------------------------- >>>>>>> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org >>>>>>> For additional commands, e-mail: user-h...@velocity.apache.org >>>>>>> >>>>>> --------------------------------------------------------------------- >>>>>> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org >>>>>> For additional commands, e-mail: user-h...@velocity.apache.org >>>>>> >>>>> --------------------------------------------------------------------- >>>>> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org >>>>> For additional commands, e-mail: user-h...@velocity.apache.org >>>>> >>>>> >>>> >>>> --------------------------------------------------------------------- >>>> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org >>>> For additional commands, e-mail: user-h...@velocity.apache.org >>>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org >>> For additional commands, e-mail: user-h...@velocity.apache.org >>> >>> >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org >> For additional commands, e-mail: user-h...@velocity.apache.org >> > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org > For additional commands, e-mail: user-h...@velocity.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org For additional commands, e-mail: user-h...@velocity.apache.org