1 24 package org.riotfamily.riot.list.xml; 25 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.riotfamily.common.beans.PropertyUtils; 31 import org.riotfamily.common.xml.XmlUtils; 32 import org.riotfamily.common.xml.DocumentDigester; 33 import org.riotfamily.riot.dao.RiotDao; 34 import org.riotfamily.riot.list.ColumnConfig; 35 import org.riotfamily.riot.list.ListConfig; 36 import org.riotfamily.riot.list.ListRepository; 37 import org.springframework.beans.factory.BeanFactory; 38 import org.springframework.core.io.Resource; 39 import org.springframework.util.xml.DomUtils; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.Element ; 42 43 46 public class XmlListRepositoryDigester implements DocumentDigester { 47 48 public static final String NAMESPACE = "http://www.riotfamily.org/schema/riot/list-config"; 49 50 private static final String LIST = "list"; 51 52 private static final String ID = "id"; 53 54 private static final String [] LIST_ATTRS = new String [] { 55 "id", "id-property", "filterFormId=filter-form", 56 "defaultCommandId=default-command", "order-by", 57 "row-style-property", "search" 58 }; 59 60 private static final String DAO = "dao"; 61 62 private static final String DAO_REF = "ref"; 63 64 private static final String DAO_CLASS = "class"; 65 66 private static final String PROPERTY = "property"; 67 68 private static final String COLUMNS = "columns"; 69 70 private static final String COLUMN = "column"; 71 72 private static final String [] COLUMN_ATTRS = new String [] { 73 "sortable", "lookup-level", "@renderer", "property", "case-sensitive" 74 }; 75 76 private static final String COMMAND = "command"; 77 78 private ListRepository listRepository; 79 80 private BeanFactory beanFactory; 81 82 public XmlListRepositoryDigester(ListRepository listRepository, 83 BeanFactory beanFactory) { 84 85 this.listRepository = listRepository; 86 this.beanFactory = beanFactory; 87 } 88 89 public void digest(Document doc, Resource resource) { 90 Element root = doc.getDocumentElement(); 91 List nodes = DomUtils.getChildElementsByTagName(root, LIST); 92 Iterator it = nodes.iterator(); 93 while (it.hasNext()) { 94 Element ele = (Element ) it.next(); 95 String namespace = ele.getNamespaceURI(); 96 if (namespace == null || namespace.equals(NAMESPACE)) { 97 listRepository.addListConfig(digestListConfig(ele)); 98 } 99 } 100 } 101 102 105 protected ListConfig digestListConfig(Element listElement) { 106 ListConfig listConfig = new ListConfig(); 107 108 XmlUtils.populate(listConfig, listElement, LIST_ATTRS); 109 110 digestDao(listConfig, listElement); 111 digestColumns(listConfig, listElement); 112 digestCommands(listConfig, listElement); 113 114 return listConfig; 115 } 116 117 120 protected void digestDao(ListConfig listConfig, 121 Element listElement) { 122 123 Element ele = DomUtils.getChildElementByTagName(listElement, DAO); 124 125 RiotDao dao = null; 126 boolean singleton = false; 127 128 String ref = XmlUtils.getAttribute(ele, DAO_REF); 129 if (ref != null) { 130 dao = (RiotDao) beanFactory.getBean(ref, RiotDao.class); 131 singleton = beanFactory.isSingleton(ref); 132 } 133 else { 134 String className = XmlUtils.getAttribute(ele, DAO_CLASS); 135 if (className != null) { 136 dao = instanciateDao(className); 137 } 138 } 139 List nodes = DomUtils.getChildElementsByTagName(ele, PROPERTY); 140 if (singleton && !nodes.isEmpty()) { 141 throw new RuntimeException (PROPERTY 142 + " must not be applied to singleton beans."); 143 } 144 XmlUtils.populate(dao, nodes, beanFactory); 145 146 listConfig.setDao(dao); 147 } 148 149 152 protected RiotDao instanciateDao(String className) { 153 return (RiotDao) PropertyUtils.newInstance(className); 154 } 155 156 160 protected void digestColumns(ListConfig listConfig, 161 Element listElement) { 162 163 Element columns = DomUtils.getChildElementByTagName(listElement, COLUMNS); 164 165 List nodes = DomUtils.getChildElementsByTagName(columns, COLUMN); 166 Iterator it = nodes.iterator(); 167 while (it.hasNext()) { 168 listConfig.addColumnConfig(digestColumn((Element ) it.next())); 169 } 170 171 nodes = DomUtils.getChildElementsByTagName(columns, COMMAND); 172 it = nodes.iterator(); 173 while (it.hasNext()) { 174 Element e = (Element ) it.next(); 175 String commandId = XmlUtils.getAttribute(e, ID); 176 listConfig.addColumnCommand(listRepository.getCommand(commandId)); 177 } 178 } 179 180 183 protected ColumnConfig digestColumn(Element ele) { 184 ColumnConfig columnConfig = new ColumnConfig(); 185 XmlUtils.populate(columnConfig, ele, COLUMN_ATTRS, beanFactory); 186 if (columnConfig.getRenderer() == null) { 187 columnConfig.setRenderer(listRepository.getDefaultCellRenderer()); 188 } 189 return columnConfig; 190 } 191 192 protected void digestCommands(ListConfig listConfig, Element listElement) { 193 List nodes = DomUtils.getChildElementsByTagName(listElement, COMMAND); 194 Iterator it = nodes.iterator(); 195 while (it.hasNext()) { 196 Element ele = (Element ) it.next(); 197 String commandId = XmlUtils.getAttribute(ele, ID); 198 listConfig.addCommand(listRepository.getCommand(commandId)); 199 } 200 } 201 202 } 203 | Popular Tags |