1 24 package org.riotfamily.revolt.config; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.riotfamily.revolt.ChangeSet; 31 import org.riotfamily.revolt.EvolutionHistory; 32 import org.riotfamily.revolt.Refactoring; 33 import org.riotfamily.revolt.definition.Column; 34 import org.riotfamily.revolt.definition.ForeignKey; 35 import org.riotfamily.revolt.definition.Index; 36 import org.riotfamily.revolt.definition.RecordEntry; 37 import org.riotfamily.revolt.definition.Reference; 38 import org.riotfamily.revolt.definition.UniqueConstraint; 39 import org.riotfamily.revolt.definition.UpdateStatement; 40 import org.riotfamily.revolt.refactor.AddColumn; 41 import org.riotfamily.revolt.refactor.AddForeignKey; 42 import org.riotfamily.revolt.refactor.AddUniqueConstraint; 43 import org.riotfamily.revolt.refactor.CreateAutoIncrementSequence; 44 import org.riotfamily.revolt.refactor.CreateIndex; 45 import org.riotfamily.revolt.refactor.CreateTable; 46 import org.riotfamily.revolt.refactor.DropColumn; 47 import org.riotfamily.revolt.refactor.DropForeignKey; 48 import org.riotfamily.revolt.refactor.DropIndex; 49 import org.riotfamily.revolt.refactor.DropTable; 50 import org.riotfamily.revolt.refactor.DropUniqueConstraint; 51 import org.riotfamily.revolt.refactor.InsertData; 52 import org.riotfamily.revolt.refactor.ModifyColumn; 53 import org.riotfamily.revolt.refactor.RenameColumn; 54 import org.riotfamily.revolt.refactor.RenameTable; 55 import org.riotfamily.revolt.refactor.UpdateData; 56 import org.springframework.beans.MutablePropertyValues; 57 import org.springframework.beans.factory.config.BeanDefinition; 58 import org.springframework.beans.factory.config.BeanDefinitionHolder; 59 import org.springframework.beans.factory.config.ConstructorArgumentValues; 60 import org.springframework.beans.factory.config.RuntimeBeanReference; 61 import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; 62 import org.springframework.beans.factory.support.RootBeanDefinition; 63 import org.springframework.beans.factory.xml.NamespaceHandler; 64 import org.springframework.beans.factory.xml.ParserContext; 65 import org.springframework.util.StringUtils; 66 import org.springframework.util.xml.DomUtils; 67 import org.w3c.dom.Element ; 68 import org.w3c.dom.Node ; 69 import org.w3c.dom.NodeList ; 70 71 76 public class RevoltNamespaceHandler implements NamespaceHandler { 77 78 public void init() { 79 } 80 81 public BeanDefinition parse(Element element, ParserContext parserContext) { 82 if (DomUtils.nodeNameEquals(element, "history")) { 83 RootBeanDefinition definition = new RootBeanDefinition(); 84 definition.setBeanClass(EvolutionHistory.class); 85 86 ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues(); 87 ctorArgs.addGenericArgumentValue(new RuntimeBeanReference( 88 element.getAttribute("data-source"))); 89 90 definition.setConstructorArgumentValues(ctorArgs); 91 MutablePropertyValues pv = new MutablePropertyValues(); 92 pv.addPropertyValue("changeSets", parseChangeSets(element)); 93 definition.setPropertyValues(pv); 94 BeanDefinitionHolder holder = new BeanDefinitionHolder(definition, element.getAttribute("module")); 95 BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry()); 96 return definition; 97 } 98 throw new IllegalArgumentException ( 99 "Expected <history> but found: " + element.getNodeName()); 100 } 101 102 private List parseChangeSets(Element element) { 103 ArrayList changeSets = new ArrayList (); 104 Iterator it = DomUtils.getChildElementsByTagName(element, "change-set").iterator(); 105 while (it.hasNext()) { 106 Element ele = (Element ) it.next(); 107 changeSets.add(new ChangeSet(ele.getAttribute("id"), parseRefactorings(ele))); 108 } 109 return changeSets; 110 } 111 112 private List parseRefactorings(Element element) { 113 ArrayList refactorings = new ArrayList (); 114 NodeList childNodes = element.getChildNodes(); 115 116 for (int i = 0; i < childNodes.getLength(); i++) { 117 Node child = childNodes.item(i); 118 if (child instanceof Element ) { 119 Refactoring refactoring = parseRefactoring((Element ) child); 120 if (refactoring != null) { 121 refactorings.add(refactoring); 122 } 123 } 124 } 125 return refactorings; 126 } 127 128 private Refactoring parseRefactoring(Element ele) { 129 Refactoring refactoring = null; 130 if (DomUtils.nodeNameEquals(ele, "add-column")) { 131 refactoring = new AddColumn(ele.getAttribute("table"), 132 parseColumn(ele)); 133 } 134 if (DomUtils.nodeNameEquals(ele, "add-foreign-key")) { 135 refactoring = new AddForeignKey(ele.getAttribute("table"), 136 new ForeignKey(ele.getAttribute("name"), 137 ele.getAttribute("references"), 138 parseReferences(ele))); 139 } 140 if (DomUtils.nodeNameEquals(ele, "add-unique-constraint")) { 141 refactoring = new AddUniqueConstraint(ele.getAttribute("table"), 142 new UniqueConstraint(ele.getAttribute("name"), 143 StringUtils.tokenizeToStringArray(ele.getAttribute("on"), ","))); 144 } 145 if (DomUtils.nodeNameEquals(ele, "create-index")) { 146 refactoring = new CreateIndex(ele.getAttribute("table"), 147 new Index(ele.getAttribute("name"), 148 StringUtils.tokenizeToStringArray(ele.getAttribute("on"), ","), 149 Boolean.valueOf(ele.getAttribute("unique")).booleanValue())); 150 } 151 if (DomUtils.nodeNameEquals(ele, "create-table")) { 152 refactoring = new CreateTable(ele.getAttribute("name"), 153 parseColumns(ele)); 154 } 155 if (DomUtils.nodeNameEquals(ele, "drop-column")) { 156 refactoring = new DropColumn(ele.getAttribute("table"), 157 ele.getAttribute("column")); 158 } 159 if (DomUtils.nodeNameEquals(ele, "drop-foreign-key")) { 160 refactoring = new DropForeignKey(ele.getAttribute("table"), 161 ele.getAttribute("foreign-key")); 162 } 163 if (DomUtils.nodeNameEquals(ele, "drop-index")) { 164 refactoring = new DropIndex(ele.getAttribute("table"), 165 ele.getAttribute("index")); 166 } 167 if (DomUtils.nodeNameEquals(ele, "drop-table")) { 168 refactoring = new DropTable(ele.getAttribute("table")); 169 } 170 if (DomUtils.nodeNameEquals(ele, "drop-unique-constraint")) { 171 refactoring = new DropUniqueConstraint(ele.getAttribute("table"), 172 ele.getAttribute("constraint")); 173 } 174 if (DomUtils.nodeNameEquals(ele, "modify-column")) { 175 refactoring = new ModifyColumn(ele.getAttribute("table"), 176 parseColumn(ele)); 177 } 178 if (DomUtils.nodeNameEquals(ele, "rename-column")) { 179 refactoring = new RenameColumn(ele.getAttribute("table"), 180 ele.getAttribute("column"), ele.getAttribute("rename-to")); 181 } 182 if (DomUtils.nodeNameEquals(ele, "rename-table")) { 183 refactoring = new RenameTable(ele.getAttribute("table"), 184 ele.getAttribute("rename-to")); 185 } 186 if (DomUtils.nodeNameEquals(ele, "insert-data")) { 187 refactoring = new InsertData(ele.getAttribute("table"), 188 parseEntries(ele)); 189 } 190 if (DomUtils.nodeNameEquals(ele, "update-data")) { 191 refactoring = new UpdateData(parseUpdateStatements(ele)); 192 } 193 if (DomUtils.nodeNameEquals(ele, "create-auto-increment-seq")) { 194 refactoring = new CreateAutoIncrementSequence( 195 ele.getAttribute("name")); 196 } 197 return refactoring; 198 } 199 200 private List parseColumns(Element ele) { 201 ArrayList columns = new ArrayList (); 202 Iterator it = DomUtils.getChildElementsByTagName(ele, "column").iterator(); 203 while (it.hasNext()) { 204 columns.add(parseColumn((Element ) it.next())); 205 } 206 return columns; 207 } 208 209 private Column parseColumn(Element ele) { 210 Column column = new Column(); 211 column.setName(ele.getAttribute("name")); 212 if (StringUtils.hasLength(ele.getAttribute("type"))) { 213 column.setType(ele.getAttribute("type")); 214 } 215 if (StringUtils.hasLength(ele.getAttribute("length"))) { 216 column.setLength(Integer.valueOf( 217 ele.getAttribute("length")).intValue()); 218 } 219 if (StringUtils.hasLength(ele.getAttribute("primary-key"))) { 220 column.setPrimaryKey(Boolean.valueOf( 221 ele.getAttribute("primary-key")).booleanValue()); 222 } 223 if (StringUtils.hasLength(ele.getAttribute("not-null"))) { 224 column.setNotNull(Boolean.valueOf( 225 ele.getAttribute("not-null")).booleanValue()); 226 } 227 if (StringUtils.hasLength(ele.getAttribute("auto-increment"))) { 228 column.setAutoIncrement(Boolean.valueOf( 229 ele.getAttribute("auto-increment")).booleanValue()); 230 } 231 if (StringUtils.hasLength(ele.getAttribute("default-value"))) { 232 column.setDefaultValue(ele.getAttribute("default-value")); 233 } 234 return column; 235 } 236 237 private List parseReferences(Element ele) { 238 ArrayList references = new ArrayList (); 239 Iterator it = DomUtils.getChildElementsByTagName(ele, "ref").iterator(); 240 while (it.hasNext()) { 241 Element e = (Element ) it.next(); 242 references.add(new Reference( 243 e.getAttribute("local"), 244 e.getAttribute("foreign"))); 245 } 246 return references; 247 } 248 249 private List parseEntries(Element ele) { 250 ArrayList entries = new ArrayList (); 251 Iterator it = DomUtils.getChildElementsByTagName(ele, "entry").iterator(); 252 while (it.hasNext()) { 253 Element e = (Element ) it.next(); 254 entries.add(new RecordEntry( 255 e.getAttribute("column"), 256 e.getAttribute("value"))); 257 } 258 return entries; 259 } 260 261 private List parseUpdateStatements(Element ele) { 262 ArrayList statements = new ArrayList (); 263 Iterator it = DomUtils.getChildElementsByTagName(ele, "statement").iterator(); 264 while (it.hasNext()) { 265 Element e = (Element ) it.next(); 266 statements.add(new UpdateStatement( 267 e.getAttribute("dialects"), 268 e.getAttribute("sql"))); 269 } 270 return statements; 271 } 272 273 public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder 274 holder, ParserContext parserContext) { 275 276 throw new UnsupportedOperationException ( 277 "Bean decoration is not supported."); 278 } 279 280 } 281 | Popular Tags |