1 18 19 package org.apache.struts.webapp.tiles.skin; 20 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.struts.tiles.ComponentDefinition; 31 import org.apache.struts.tiles.DefinitionsFactoryException; 32 import org.apache.struts.tiles.DefinitionsUtil; 33 import org.apache.struts.tiles.FactoryNotFoundException; 34 import org.apache.struts.tiles.NoSuchDefinitionException; 35 36 39 public class DefinitionCatalog 40 { 41 42 public static boolean debug = true; 43 44 public static final String LABEL_NAME_ATTRIBUTE = "skin.label"; 45 46 public static final String DEFINITION_LIST_ATTRIBUTE = "skin.list"; 47 48 51 private Map definitions = new HashMap (); 52 55 private ComponentDefinition defaultDefinition; 56 57 60 private List names = new ArrayList (); 61 62 65 private List keys = new ArrayList (); 66 67 74 public DefinitionCatalog( String catalogName, HttpServletRequest request, ServletContext context) 75 throws FactoryNotFoundException, DefinitionsFactoryException 76 { 77 ComponentDefinition catalogDef = DefinitionsUtil.getDefinition( catalogName, request, context); 79 if(debug) 80 System.out.println( "Got definition " + catalogDef ); 81 List list = (List )catalogDef.getAttribute( DEFINITION_LIST_ATTRIBUTE ); 83 Iterator i = list.iterator(); 84 while(i.hasNext() ) 85 { 86 String name = (String )i.next(); 87 System.out.println( "add " + name ); 88 ComponentDefinition def = DefinitionsUtil.getDefinition(name, request, context); 89 if(def==null) 90 throw new NoSuchDefinitionException("Can't find definition '" + name + "'" ); 91 add( name, def ); 92 } if(debug) 94 System.out.println( "Catalog initialized" ); 95 } 96 97 102 public ComponentDefinition get(Object key) 103 { 104 if(key==null) 105 return getDefault(); 106 return (ComponentDefinition)definitions.get(key); 107 } 108 109 114 public ComponentDefinition getDefault() 115 { 116 return defaultDefinition; 117 } 118 119 125 public List getNames() 126 { 127 return names; 128 } 129 130 135 public List getKeys() 136 { 137 return keys; 138 } 139 140 145 public String getKey( String key ) 146 { 147 if( definitions.get(key) != null) 148 return key; 149 150 return null; 151 } 152 153 157 public void add(String key, ComponentDefinition definition) 158 { 159 if( defaultDefinition == null ) 161 { 162 defaultDefinition = definition; 163 } 164 definitions.put( key , definition); 166 Object name = definition.getAttribute(LABEL_NAME_ATTRIBUTE); 167 if( name == null ) 168 name = key; 169 names.add( name ); 170 keys.add(key); 171 } 172 } 173 | Popular Tags |