1 24 package org.riotfamily.riot.editor.xml; 25 26 import java.util.Iterator ; 27 28 import org.riotfamily.common.xml.DocumentDigester; 29 import org.riotfamily.common.xml.XmlUtils; 30 import org.riotfamily.riot.editor.AbstractEditorDefinition; 31 import org.riotfamily.riot.editor.CustomEditorDefinition; 32 import org.riotfamily.riot.editor.EditorDefinition; 33 import org.riotfamily.riot.editor.FormChooserDefinition; 34 import org.riotfamily.riot.editor.FormDefinition; 35 import org.riotfamily.riot.editor.GroupDefinition; 36 import org.riotfamily.riot.editor.IntermediateDefinition; 37 import org.riotfamily.riot.editor.ListDefinition; 38 import org.riotfamily.riot.editor.ObjectEditorDefinition; 39 import org.riotfamily.riot.editor.TreeDefinition; 40 import org.riotfamily.riot.editor.ViewDefinition; 41 import org.springframework.beans.factory.BeanFactory; 42 import org.springframework.core.io.Resource; 43 import org.springframework.util.xml.DomUtils; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Element ; 46 47 50 public class XmlEditorRepositoryDigester implements DocumentDigester { 51 52 public static final String NAMESPACE = "http://www.riotfamily.org/schema/riot/editor-config"; 53 54 private static final String GROUP = "group"; 55 56 private static final String [] GROUP_ATTR = new String [] { 57 "name", "key", "icon", "hidden" 58 }; 59 60 private static final String LIST = "list"; 61 62 private static final String TREE = "tree"; 63 64 private static final String LIST_EDITOR = LIST + '|' + TREE; 65 66 private static final String [] LIST_ATTR = new String [] { 67 "name", "listId=list-ref", "icon", "hidden", "id" 68 }; 69 70 private static final String [] TREE_ATTR = new String [] { 71 "branch-class" 72 }; 73 74 private static final String FORM = "form"; 75 76 private static final String FORM_CHOOSER = "form-chooser"; 77 78 private static final String FORM_CHOOSER_COMMON = "common"; 79 80 private static final String VIEW = "view"; 81 82 private static final String [] VIEW_ATTR = new String [] { 83 "name", "view-name" 84 }; 85 86 private static final String CUSTOM = "custom"; 87 88 private static final String CUSTOM_REF = "ref"; 89 90 private static final String [] CUSTOM_ATTR = new String [] { 91 "name", "target", "url", "icon" 92 }; 93 94 private static final String OBJECT_EDITOR = FORM + '|' 95 + FORM_CHOOSER + '|' + VIEW + '|' + CUSTOM; 96 97 private static final String FORM_OPTION = "form-option"; 98 99 100 private static final String [] FORM_ATTR = new String [] { 101 "name", "formId=form-ref", "label-property", "hidden" 102 }; 103 104 private static final String ANYTHING = OBJECT_EDITOR + '|' + LIST_EDITOR; 105 106 107 private XmlEditorRepository editorRepository; 108 109 private BeanFactory beanFactory; 110 111 public XmlEditorRepositoryDigester(XmlEditorRepository editorRepository, 112 BeanFactory beanFactory) { 113 114 this.editorRepository = editorRepository; 115 this.beanFactory = beanFactory; 116 } 117 118 121 public void digest(Document doc, Resource resource) { 122 Element root = doc.getDocumentElement(); 123 GroupDefinition group = editorRepository.getRootGroupDefinition(); 124 if (group == null) { 125 group = new GroupDefinition(editorRepository); 126 group.setName("start"); editorRepository.addEditorDefinition(group); 128 editorRepository.setRootGroupDefinition(group); 129 } 130 digestGroupEntries(root, group); 131 } 132 133 protected void addEditorDefinition(AbstractEditorDefinition editor) { 134 if (editor.getId() == null) { 135 editor.setId(getUniqueId(editor.getName())); 136 } 137 editorRepository.addEditorDefinition(editor); 138 } 139 140 protected void digestGroupEntries(Element groupElement, GroupDefinition group) { 141 Iterator it = XmlUtils.getChildElements(groupElement).iterator(); 142 while (it.hasNext()) { 143 Element ele = (Element ) it.next(); 144 String namespace = ele.getNamespaceURI(); 145 if (namespace == null || namespace.equals(NAMESPACE)) { 146 EditorDefinition ed = digestEditorDefinition(ele); 147 group.addEditorDefinition(ed); 148 } 149 } 150 } 151 152 155 protected EditorDefinition digestEditorDefinition(Element ele) { 156 EditorDefinition ed = null; 157 if (isListElement(ele) || isTreeElement(ele)) { 158 ed = digestListOrTreeDefinition(ele); 159 } 160 else if (isGroupElement(ele)) { 161 ed = digestGroupDefinition(ele); 162 } 163 else if (isFormElement(ele)) { 164 ed = digestFormDefinition(ele, null); 165 } 166 else if (isCustomElement(ele)) { 167 ed = digestCustomDefinition(ele, null); 168 } 169 return ed; 170 } 171 172 177 protected GroupDefinition digestGroupDefinition(Element groupElement) { 178 GroupDefinition group = new GroupDefinition(editorRepository); 179 XmlUtils.populate(group, groupElement, GROUP_ATTR); 180 addEditorDefinition(group); 181 digestGroupEntries(groupElement, group); 182 return group; 183 } 184 185 protected ListDefinition digestListOrTreeDefinition(Element listElement) { 186 ListDefinition listDefinition; 187 if (isTreeElement(listElement)) { 188 listDefinition = new TreeDefinition(editorRepository); 189 XmlUtils.populate(listDefinition, listElement, TREE_ATTR); 190 } 191 else { 192 listDefinition = new ListDefinition(editorRepository); 193 } 194 initListDefinition(listDefinition, listElement); 195 return listDefinition; 196 } 197 198 201 protected void initListDefinition(ListDefinition listDefinition, 202 Element listElement) { 203 204 XmlUtils.populate(listDefinition, listElement, LIST_ATTR); 205 addEditorDefinition(listDefinition); 206 207 Element e = XmlUtils.getFirstChildByRegex(listElement, ANYTHING); 208 if (e != null) { 209 EditorDefinition def = null; 210 if (isListElement(e) || isTreeElement(e)) { 211 def = new IntermediateDefinition( 212 listDefinition, digestListOrTreeDefinition(e)); 213 } 214 else { 215 def = digestObjectEditorDefinition(e, listDefinition); 216 217 } 218 listDefinition.setDisplayDefinition(def); 219 } 220 } 221 222 223 protected EditorDefinition digestObjectEditorDefinition( 224 Element ele, EditorDefinition parentDef) { 225 226 if (isFormElement(ele)) { 227 return digestFormDefinition(ele, parentDef); 228 } 229 else if (isFormChooserElement(ele)) { 230 return digestFormChooserDefinition(ele, parentDef); 231 } 232 else if (isViewElement(ele)) { 233 return digestViewDefinition(ele, parentDef); 234 } 235 else if (isCustomElement(ele)) { 236 return digestCustomDefinition(ele, parentDef); 237 } 238 else { 239 throw new RuntimeException ("Expected " + OBJECT_EDITOR); 240 } 241 } 242 243 246 protected FormDefinition digestFormDefinition(Element formElement, 247 EditorDefinition parentDef) { 248 249 FormDefinition formDefinition = new FormDefinition(editorRepository); 250 XmlUtils.populate(formDefinition, formElement, FORM_ATTR, beanFactory); 251 formDefinition.setParentEditorDefinition(parentDef); 252 addEditorDefinition(formDefinition); 253 254 digestChildEditors(formElement, formDefinition); 255 256 return formDefinition; 257 } 258 259 protected void digestChildEditors(Element ele, ObjectEditorDefinition editorDef) { 260 Iterator it = XmlUtils.getChildElementsByRegex( 261 ele, ANYTHING).iterator(); 262 263 while (it.hasNext()) { 264 EditorDefinition childDef = digestChildEditorDefinition( 265 (Element ) it.next(), editorDef.getParentEditorDefinition()); 266 267 editorDef.addChildEditorDefinition(childDef); 268 } 269 } 270 271 protected EditorDefinition digestChildEditorDefinition(Element ele, 272 EditorDefinition parentDef) { 273 274 if (isListElement(ele) || isTreeElement(ele)) { 275 return digestListOrTreeDefinition(ele); 276 } 277 return digestObjectEditorDefinition(ele, parentDef); 278 } 279 280 protected ViewDefinition digestViewDefinition(Element formElement, 281 EditorDefinition parentDef) { 282 283 ViewDefinition viewDefinition = new ViewDefinition(editorRepository); 284 XmlUtils.populate(viewDefinition, formElement, VIEW_ATTR); 285 viewDefinition.setParentEditorDefinition(parentDef); 286 addEditorDefinition(viewDefinition); 287 return viewDefinition; 288 } 289 290 protected String getUniqueId(String name) { 291 String id = name != null ? name : "editor"; 292 String uniqueId = id; 293 EditorDefinition def = editorRepository.getEditorDefinition(uniqueId); 294 int i = 1; 295 while (def != null) { 296 uniqueId = id + i; 297 def = editorRepository.getEditorDefinition(uniqueId); 298 i++; 299 } 300 return uniqueId; 301 } 302 303 304 protected FormChooserDefinition digestFormChooserDefinition( 305 Element chooserElement, EditorDefinition parentDef) { 306 307 FormChooserDefinition formChooserDefinition = 308 new FormChooserDefinition(editorRepository); 309 310 XmlUtils.populate(formChooserDefinition, chooserElement, 311 FORM_ATTR, beanFactory); 312 313 formChooserDefinition.setParentEditorDefinition(parentDef); 314 addEditorDefinition(formChooserDefinition); 315 316 Iterator it = DomUtils.getChildElementsByTagName( 317 chooserElement, FORM_OPTION).iterator(); 318 319 while (it.hasNext()) { 320 FormDefinition formDefinition = 321 digestFormDefinition((Element ) it.next(), 322 formChooserDefinition); 323 324 formChooserDefinition.addFormDefinition(formDefinition); 325 } 326 327 Element commonElement = XmlUtils.getFirstChildByTagName( 328 chooserElement, FORM_CHOOSER_COMMON); 329 330 if (commonElement != null) { 331 digestChildEditors(commonElement, formChooserDefinition); 332 } 333 return formChooserDefinition; 334 } 335 336 protected EditorDefinition digestCustomDefinition(Element ele, 337 EditorDefinition parentDef) { 338 339 CustomEditorDefinition custom; 340 String beanName = XmlUtils.getAttribute(ele, CUSTOM_REF); 341 if (beanName != null) { 342 custom = (CustomEditorDefinition) beanFactory.getBean( 343 beanName, CustomEditorDefinition.class); 344 } 345 else { 346 custom = new CustomEditorDefinition(); 347 } 348 custom.setEditorRepository(editorRepository); 349 350 XmlUtils.populate(custom, ele, CUSTOM_ATTR, beanFactory); 351 custom.setParentEditorDefinition(parentDef); 352 addEditorDefinition(custom); 353 354 digestChildEditors(ele, custom); 355 356 return custom; 357 } 358 359 private static boolean isFormElement(Element ele) { 360 return ele != null && DomUtils.nodeNameEquals(ele, FORM); 361 } 362 363 private static boolean isFormChooserElement(Element ele) { 364 return ele != null && DomUtils.nodeNameEquals(ele, FORM_CHOOSER); 365 } 366 367 private static boolean isViewElement(Element ele) { 368 return ele != null && DomUtils.nodeNameEquals(ele, VIEW); 369 } 370 371 private static boolean isListElement(Element ele) { 372 return ele != null && DomUtils.nodeNameEquals(ele, LIST); 373 } 374 375 private static boolean isTreeElement(Element ele) { 376 return ele != null && DomUtils.nodeNameEquals(ele, TREE); 377 } 378 379 private static boolean isGroupElement(Element ele) { 380 return ele != null && DomUtils.nodeNameEquals(ele, GROUP); 381 } 382 383 private static boolean isCustomElement(Element ele) { 384 return ele != null && DomUtils.nodeNameEquals(ele, CUSTOM); 385 } 386 387 } 388 | Popular Tags |