001    /**
002     * Implements the CFML Function cjustify
003     */
004    package railo.runtime.functions.string;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.ext.function.Function;
009    
010    public final class CJustify implements Function {
011    
012            public static String call(PageContext pc , String string, double length) throws ExpressionException {
013                    int len=(int)length;
014                    if(len<1) throw new ExpressionException("Parameter 2 of function cJustify which is now ["+len+"] must be a non-negative integer");
015                    else if((len-=string.length())<=0) return string;
016                    else {
017                            
018                            char[] chrs=new char[string.length()+len];
019                            int part=len/2;
020                            
021                            for(int i=0;i<part;i++)chrs[i]=' '; 
022                            for(int i=string.length()-1;i>=0;i--)chrs[part+i]=string.charAt(i); 
023                            for(int i=part+string.length();i<chrs.length;i++)chrs[i]=' '; 
024                            
025                            return new String(chrs);
026                    }
027            }
028    }
029    
030    
031    
032    
033    
034    
035    
036    
037    
038    
039