001: /*---------------------------------------------------------------------------*\
002: $Id: IndentTextMethod.java 7041 2007-09-09 01:04:47Z bmc $
003: ---------------------------------------------------------------------------
004: This software is released under a BSD-style license:
005:
006: Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions are
010: met:
011:
012: 1. Redistributions of source code must retain the above copyright notice,
013: this list of conditions and the following disclaimer.
014:
015: 2. The end-user documentation included with the redistribution, if any,
016: must include the following acknowlegement:
017:
018: "This product includes software developed by Brian M. Clapper
019: (bmc@clapper.org, http://www.clapper.org/bmc/). That software is
020: copyright (c) 2004-2007 Brian M. Clapper."
021:
022: Alternately, this acknowlegement may appear in the software itself,
023: if wherever such third-party acknowlegements normally appear.
024:
025: 3. Neither the names "clapper.org", "curn", nor any of the names of the
026: project contributors may be used to endorse or promote products
027: derived from this software without prior written permission. For
028: written permission, please contact bmc@clapper.org.
029:
030: 4. Products derived from this software may not be called "curn", nor may
031: "clapper.org" appear in their names without prior written permission
032: of Brian M. Clapper.
033:
034: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
036: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
037: NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
038: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
039: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
040: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
041: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
042: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
043: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
044: \*---------------------------------------------------------------------------*/
045:
046: package org.clapper.curn.output.freemarker;
047:
048: import java.util.List;
049:
050: import freemarker.template.SimpleScalar;
051: import freemarker.template.TemplateModel;
052: import freemarker.template.TemplateMethodModel;
053: import freemarker.template.TemplateModelException;
054:
055: /**
056: * FreeMarker method (put in the FreeMarker data model) that permits a
057: * template to indent plain text, without wrapping it.
058: *
059: * @see WrapTextMethod
060: *
061: * @version <tt>$Revision: 7041 $</tt>
062: */
063: class IndentTextMethod implements TemplateMethodModel {
064: /*----------------------------------------------------------------------*\
065: Private Data Items
066: \*----------------------------------------------------------------------*/
067:
068: /*----------------------------------------------------------------------*\
069: Constructor
070: \*----------------------------------------------------------------------*/
071:
072: /**
073: * Construct a new <tt>IndentTextMethod</tt> object.
074: */
075: public IndentTextMethod() {
076: // Nothing to do
077: }
078:
079: /*----------------------------------------------------------------------*\
080: Public Methods
081: \*----------------------------------------------------------------------*/
082:
083: /**
084: * Execute the method.
085: *
086: * @param args the arguments:
087: * <ul>
088: * <li> text to be wrapped (required)
089: * <li> indentation (required)
090: * </ul>
091: */
092: public TemplateModel exec(List args) throws TemplateModelException {
093: if (args.size() != 2)
094: throw new TemplateModelException(
095: "Wrong number of arguments");
096:
097: StringBuilder buf = new StringBuilder();
098:
099: String sIndent = (String) args.get(1);
100: int indentation = 0;
101: try {
102: indentation = Integer.parseInt(sIndent);
103: }
104:
105: catch (NumberFormatException ex) {
106: throw new TemplateModelException("Bad indentation value \""
107: + sIndent + "\"", ex);
108: }
109:
110: while (indentation-- > 0)
111: buf.append(' ');
112:
113: buf.append(args.get(0));
114:
115: return new SimpleScalar(buf.toString());
116: }
117: }
|