1 3 package com.teamkonzept.lib.templates; 4 5 import java.io.*; 6 7 import com.oroinc.text.regex.*; 8 9 import com.teamkonzept.lib.*; 10 11 12 17 public class TKTrimTag extends TKLabelTag { 18 public TKTemplateSyntax trimText; 19 static final int TAG_TYPE = TKNoExpandTag.TAG_TYPE+1; 20 21 30 public TKTrimTag( TKTemplateSyntax parent, String def, PatternMatcherInput matcherInput, boolean hasSubTags ) throws TKTemplateSyntaxException 31 { 32 super( parent, def, hasSubTags ); 33 try { 34 trimText = parent.newChild( matcherInput, parent.getSource() ); 35 } 36 catch (ArrayIndexOutOfBoundsException e) { throw new TKTemplateSyntaxException(parent.getSource(), "NOEND","TRIM", def); 38 } 39 checkError(trimText,"TRIM", def); 40 } 41 42 50 public String apply( TKTemplateData td ) throws TKTemplateSyntaxException 51 { 52 String label = getLabel(td); 53 54 int keep = 0; 55 if (label != null && label.length() != 0) 56 keep = Integer.parseInt (label); 57 58 char[] untrimmed = trimText.apply(td).toCharArray(); 59 char[] trimmed = new char[untrimmed.length]; 60 int trimmedLength = 0; 61 62 int toKeep = keep; 63 int length = untrimmed.length; 64 for (int i = 0; i < length; i++) { 65 char current = untrimmed[i]; 66 if ( Character.isWhitespace(current) ){ 68 if ( toKeep > 0 ){ 69 trimmed[trimmedLength++] = current; 70 toKeep--; 72 } 73 } 74 else{ 76 trimmed[trimmedLength++] = current; 78 toKeep = keep; 79 } 80 } 81 return new String (trimmed, 0, trimmedLength); 82 } 83 84 91 public void apply(TKTemplateData td, Writer writer) 92 throws TKTemplateSyntaxException, IOException{ 93 writer.write(apply(td)); 94 } 95 } 96 | Popular Tags |