1 package org.apache.slide.projector.processor.tree; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.StringTokenizer ; 8 9 import org.apache.slide.projector.ConfigurationException; 10 import org.apache.slide.projector.Context; 11 import org.apache.slide.projector.Result; 12 import org.apache.slide.projector.URI; 13 import org.apache.slide.projector.descriptor.MapValueDescriptor; 14 import org.apache.slide.projector.descriptor.ParameterDescriptor; 15 import org.apache.slide.projector.descriptor.StringValueDescriptor; 16 import org.apache.slide.projector.descriptor.TreeValueDescriptor; 17 import org.apache.slide.projector.i18n.ParameterMessage; 18 import org.apache.slide.projector.processor.SimpleProcessor; 19 import org.apache.slide.projector.processor.TemplateRenderer; 20 import org.apache.slide.projector.value.ArrayValue; 21 import org.apache.slide.projector.value.MapValue; 22 import org.apache.slide.projector.value.StreamableValue; 23 import org.apache.slide.projector.value.StringValue; 24 import org.apache.slide.projector.value.Value; 25 26 public class TreeRenderer extends TemplateRenderer { 27 final private static String HEADER_FRAGMENT = "header "; 28 final private static String FOOTER_FRAGMENT = "footer "; 29 final private static String FIRST_FRAGMENT = "first "; 30 final private static String LAST_FRAGMENT = "last "; 31 final private static String EVEN_FRAGMENT = "even "; 32 final private static String DEFAULT_FRAGMENT = "default "; 33 final private static String EMPTY_FRAGMENT = "empty "; 34 final private static String OPEN_FOLDER_FRAGMENT = "open "; 35 final private static String CLOSED_FOLDER_FRAGMENT = "closed "; 36 final private static String ACTIVE_FRAGMENT = "active "; 37 38 final public static String ID = "id"; 39 final public static String ACTIVE_ID = "activeId"; 40 41 private ParameterDescriptor []parameterDescriptors; 42 43 public TreeRenderer() { 44 setRequiredFragments(new String [] { DEFAULT_FRAGMENT }); 45 setOptionalFragments(new String [] { HEADER_FRAGMENT, FOOTER_FRAGMENT, FIRST_FRAGMENT, LAST_FRAGMENT, 46 EVEN_FRAGMENT, EMPTY_FRAGMENT, OPEN_FOLDER_FRAGMENT, CLOSED_FOLDER_FRAGMENT, ACTIVE_FRAGMENT }); 47 } 48 49 public void configure(StreamableValue config) throws ConfigurationException { 50 super.configure(config); 51 List validMapEntries = new ArrayList (); 52 for ( Iterator i = parameterDescriptions.iterator(); i.hasNext(); ) { 53 ParameterDescriptor parentParameter = (ParameterDescriptor)i.next(); 54 if ( !parentParameter.getName().equals(FRAGMENT) ) { 55 validMapEntries.add(parentParameter); 56 } 57 } 58 parameterDescriptors = new ParameterDescriptor[] { 59 new ParameterDescriptor(ID, new ParameterMessage("treeRenderer/id"), new TreeValueDescriptor(new StringValueDescriptor())), 60 new ParameterDescriptor(SimpleProcessor.INPUT, new ParameterMessage("treeRenderer/input"), new TreeValueDescriptor(new MapValueDescriptor(validMapEntries))) 61 }; 62 } 63 64 public Result process(Map parameter, Context context) throws Exception { 65 Value[] idArray = ((ArrayValue)parameter.get(ID)).getArray(); 66 Value[] inputArray = ((ArrayValue)parameter.get(SimpleProcessor.INPUT)).getArray(); 67 URI activeIdResource = (URI)context.getBookmark(); 68 String activeId = null; 69 int[] active = new int[0]; 70 if ( activeIdResource != null ) { 71 activeId = activeIdResource.toString(); 72 String path = buildPathToActiveNode(null, activeId, idArray); 73 if ( path != null ) { 74 StringTokenizer tokenizer = new StringTokenizer (path, ","); 75 active = new int[tokenizer.countTokens()]; 76 int counter = 0; 77 while ( tokenizer.hasMoreTokens() ) { 78 active[counter] = Integer.parseInt(tokenizer.nextToken()); 79 counter++; 80 } 81 } 82 } 83 return new Result(OK, OUTPUT, new StringValue(process(parameter, idArray, inputArray, active, 0, context), "text/html", false )); 84 } 85 86 private String buildPathToActiveNode(String path, String activeId, Value[] idArray) { 87 for ( int i = 0; i < idArray.length; i++ ) { 88 if ( idArray[i] instanceof StringValue && ((StringValue)idArray[i]).toString().equals(activeId) ) { 89 if ( path != null) { 90 return path+","+i; 91 } else { 92 return String.valueOf(i); 93 } 94 } else if ( idArray[i] instanceof ArrayValue ) { 95 String newPath; 96 if ( path != null) { 97 newPath = path+","+i; 98 } else { 99 newPath = String.valueOf(i); 100 } 101 String nestedPath = buildPathToActiveNode(newPath, activeId, ((ArrayValue)idArray[i]).getArray()); 102 if ( nestedPath != null ) { 103 return nestedPath; 104 } 105 } 106 } 107 return null; 108 } 109 110 public String process(Map parameter, Value[] idArray, Value[] inputArray, int[] active, int level, Context context) throws Exception { 111 StringBuffer buffer = new StringBuffer (1024); 112 Template defaultTemplate = getRequiredFragment(DEFAULT_FRAGMENT+level); 113 Template headerTemplate = getOptionalFragment(HEADER_FRAGMENT+level); 114 Template footerTemplate = getOptionalFragment(FOOTER_FRAGMENT+level); 115 Template firstTemplate = getOptionalFragment(FIRST_FRAGMENT+level); 116 Template lastTemplate = getOptionalFragment(LAST_FRAGMENT+level); 117 Template evenTemplate = getOptionalFragment(EVEN_FRAGMENT+level); 118 Template activeTemplate = getOptionalFragment(ACTIVE_FRAGMENT+level); 119 Template emptyTemplate = getOptionalFragment(EMPTY_FRAGMENT+level); 120 Template openTemplate = getOptionalFragment(OPEN_FOLDER_FRAGMENT+level); 121 Template closedTemplate = getOptionalFragment(CLOSED_FOLDER_FRAGMENT+level); 122 if ( headerTemplate != null ) headerTemplate.evaluate(buffer, parameter); 123 for ( int i = 0; i < idArray.length; i++ ) { 124 if ( inputArray[i] instanceof MapValue ) { 125 parameter.putAll(((MapValue)inputArray[i]).getMap()); 126 } 127 if ( inputArray[i] instanceof ArrayValue ) { 128 if ( ( active.length <= level || active[level] == i)) { 130 buffer.append(process(parameter, ((ArrayValue)idArray[i]).getArray(), ((ArrayValue)inputArray[i]).getArray(), active, level+1, context)); 131 } 132 } else if ( i < idArray.length-1 && idArray[i+1] instanceof ArrayValue && openTemplate != null && (active.length <= level || active[level] == i)) { 133 openTemplate.evaluate(buffer, parameter); 134 } else if ( i < idArray.length-1 && idArray[i+1] instanceof ArrayValue && closedTemplate != null ) { 135 closedTemplate.evaluate(buffer, parameter); 136 } else if ( level == active.length-1 && i == active[level] && activeTemplate != null ) { 137 activeTemplate.evaluate(buffer, parameter); 138 } else if ( i == 0 && firstTemplate != null ) { 139 firstTemplate.evaluate(buffer, parameter); 140 } else if ( i == idArray.length-1 && lastTemplate != null ) { 141 lastTemplate.evaluate(buffer, parameter); 142 } else if ( evenTemplate != null && i%2 == 0 ) { 143 evenTemplate.evaluate(buffer, parameter); 144 } else if ( defaultTemplate != null ) { 145 defaultTemplate.evaluate(buffer, parameter); 146 } 147 } 148 if ( footerTemplate != null ) footerTemplate.evaluate(buffer, parameter); 149 return buffer.toString(); 150 } 151 152 public ParameterDescriptor[] getParameterDescriptors() { 153 return parameterDescriptors; 154 } 155 } | Popular Tags |