1 10 package org.mmbase.util.xml; 11 12 import java.util.*; 13 import org.mmbase.module.core.MMObjectBuilder; 14 import org.mmbase.bridge.NodeManager; 15 import org.mmbase.core.CoreField; 16 import org.mmbase.core.util.Fields; 17 import org.mmbase.datatypes.DataType; 18 import org.mmbase.util.XMLEntityResolver; 19 import org.mmbase.util.DynamicDate; 20 import org.mmbase.util.logging.*; 21 22 import org.w3c.dom.*; 23 24 38 public class BuilderWriter extends DocumentWriter { 39 40 private static final Logger log = Logging.getLoggerInstance(BuilderWriter.class); 41 42 47 private boolean expandBuilder = false; 48 51 private MMObjectBuilder builder; 52 53 58 public BuilderWriter(MMObjectBuilder builder) throws DOMException { 59 super("builder", BuilderReader.PUBLIC_ID_BUILDER, 60 XMLEntityResolver.DOMAIN + XMLEntityResolver.DTD_SUBPATH + BuilderReader.DTD_BUILDER); 61 this.builder=builder; 62 getMessageRetriever("org.mmbase.util.xml.resources.builderwriter"); 63 } 64 65 69 protected void generate() throws DOMException { 70 Element root=document.getDocumentElement(); 71 addComment("builder.configuration",builder.getTableName(),root); 72 root.setAttribute("name",""+builder.getTableName()); 73 root.setAttribute("maintainer",builder.getMaintainer()); 74 root.setAttribute("version",""+builder.getVersion()); 75 MMObjectBuilder parent=null; 76 if (!expandBuilder) { 77 parent=builder.getParentBuilder(); 78 if (parent!=null) { 79 root.setAttribute("extends",parent.getTableName()); 80 } 81 } 82 addComment("builder.status",root); 84 addContentElement("status","active",root); 85 String data=builder.getClass().getName(); 87 if ((parent==null) || (!parent.getClass().getName().equals(data))) { 88 if (data.startsWith("org.mmbase.module.builders.")) { 90 data=data.substring(27); 91 } 92 addComment("builder.classfile",root); 93 addContentElement("classfile",data,root); 94 } 95 data=builder.getSearchAge(); 97 if ((parent==null) || (!parent.getSearchAge().equals(data))) { 98 addComment("builder.searchage",root); 99 addContentElement("searchage",data,root); 100 } 101 Element names=document.createElement("names"); 103 addComment("builder.names",root); 104 root.appendChild(names); 105 Map datamap=builder.getSingularNames(); 107 addComment("builder.singular",names); 108 for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) { 109 Map.Entry em= (Map.Entry)i.next(); 110 String language=(String )em.getKey(); 111 String name=(String )em.getValue(); 112 if ((parent==null) || !(name.equals(parent.getSingularName(language)))) { 113 Element elm=addContentElement("singular",name,names); 114 elm.setAttribute("xml:lang",language); 115 } 116 } 117 datamap=builder.getPluralNames(); 119 addComment("builder.plural",names); 120 for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) { 121 Map.Entry em= (Map.Entry)i.next(); 122 String language=(String )em.getKey(); 123 String name=(String )em.getValue(); 124 if ((parent==null) || !(name.equals(parent.getPluralName(language)))) { 125 Element elm=addContentElement("plural",name,names); 126 elm.setAttribute("xml:lang",language); 127 } 128 } 129 Element descriptions=document.createElement("descriptions"); 131 addComment("builder.descriptions",root); 132 root.appendChild(descriptions); 133 datamap=builder.getDescriptions(); 135 addComment("builder.description",root); 136 for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) { 137 Map.Entry em= (Map.Entry)i.next(); 138 String language=(String )em.getKey(); 139 String description=(String )em.getValue(); 140 if ((parent==null) || !(description.equals(parent.getDescription(language)))) { 141 Element elm=addContentElement("description",description,descriptions); 142 elm.setAttribute("xml:lang",language); 143 } 144 } 145 Element properties=document.createElement("properties"); 147 addComment("builder.properties",root); 148 root.appendChild(properties); 149 datamap=builder.getInitParameters(); 151 for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) { 152 Map.Entry em= (Map.Entry)i.next(); 153 String propname=(String )em.getKey(); 154 String propvalue=(String )em.getValue(); 155 if ((parent==null) || !(propvalue.equals(parent.getInitParameter(propname)))) { 156 Element elm=addContentElement("property",propvalue,properties); 157 elm.setAttribute("name",propname); 158 } 159 } 160 Element fieldlist=document.createElement("fieldlist"); 162 addComment("builder.fieldlist",root); 163 root.appendChild(fieldlist); 164 List fields=builder.getFields(NodeManager.ORDER_CREATE); 166 for (Iterator f=fields.iterator(); f.hasNext();) { 167 CoreField fielddef=(CoreField)f.next(); 168 String fieldname=fielddef.getName(); 170 if (fieldname.equals("otype")) continue; 171 CoreField parentField = null; 172 if (parent != null) { 173 parentField = parent.getField(fieldname); 174 } 175 Element descriptionsElm = null; 177 datamap = fielddef.getLocalizedDescription().asMap(); 178 for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) { 179 Map.Entry em= (Map.Entry)i.next(); 180 Locale locale = (Locale)em.getKey(); 181 String description=(String )em.getValue(); 182 if ((parentField==null) || !(description.equals(parentField.getDescription(locale)))) { 183 if (descriptionsElm == null) descriptionsElm = document.createElement("descriptions"); 184 Element elm=addContentElement("description", description, descriptionsElm); 185 elm.setAttribute("xml:lang", locale.toString()); 186 } 187 } 188 Element guiElm = null; 189 datamap = fielddef.getLocalizedGUIName().asMap(); 190 for (Iterator i=datamap.entrySet().iterator(); i.hasNext();) { 191 Map.Entry em= (Map.Entry)i.next(); 192 Locale locale = (Locale)em.getKey(); 193 String name=(String )em.getValue(); 194 if ((parentField==null) || !(name.equals(parentField.getGUIName(locale)))) { 195 if (guiElm == null) guiElm = document.createElement("gui"); 196 Element elm=addContentElement("guiname", name, guiElm); 197 elm.setAttribute("xml:lang", locale.toString()); 198 } 199 } 200 Element positionsElm = null; 201 202 int pos=fielddef.getEditPosition(); 205 if ((parentField==null) || (pos!=parentField.getEditPosition())) { 206 if (positionsElm == null) positionsElm = document.createElement("positions"); 207 addComment("builder.field.editor.positions.input", positionsElm); 208 addContentElement("input", "" + pos, positionsElm); 209 } 210 pos=fielddef.getListPosition(); 212 if ((parentField==null) || (pos!=parentField.getListPosition())) { 213 if (positionsElm == null) positionsElm = document.createElement("positions"); 214 addComment("builder.field.editor.positions.list", positionsElm); 215 addContentElement("list", "" + pos, positionsElm); 216 } 217 pos=fielddef.getSearchPosition(); 219 if ((parentField==null) || (pos!=parentField.getSearchPosition())) { 220 if (positionsElm == null) positionsElm = document.createElement("positions"); 221 addComment("builder.field.editor.positions.search", positionsElm); 222 addContentElement("search", "" + pos, positionsElm); 223 } 224 225 Element dataTypeElm = null; 226 DataType dataType = fielddef.getDataType(); 227 if ((parentField == null) || !dataType.equals(parentField.getDataType())) { 228 dataTypeElm = (Element) document.importNode(dataType.toXml(), true); 229 } 230 231 if ((parentField == null) || (dataTypeElm != null) || (descriptionsElm != null) || (guiElm != null) || (positionsElm != null)) { 232 addComment("builder.field", fieldname, ""+fielddef.getStoragePosition(), fieldlist); 233 Element field=document.createElement("field"); 234 fieldlist.appendChild(field); 235 if (descriptionsElm != null) { 236 addComment("builder.field.descriptions",field); 237 field.appendChild(descriptionsElm); 238 } 239 if (guiElm != null) { 240 addComment("builder.field.gui",field); 241 field.appendChild(guiElm); 242 } 243 if (positionsElm != null) { 244 addComment("builder.field.editor",field); 245 Element editor=document.createElement("editor"); 246 editor.appendChild(positionsElm); 247 field.appendChild(editor); 248 } 249 if (dataTypeElm != null) { 250 addComment("builder.field.datatype",field); 251 field.appendChild(dataTypeElm); 252 } 253 Element db = document.createElement("db"); 254 addComment("builder.field.db",field); 255 field.appendChild(db); 256 addComment("builder.field.db.name",db); 257 addContentElement("name",fielddef.getName(),db); 258 if (parentField==null) { 259 String sType = Fields.getTypeDescription(fielddef.getType()); 260 addComment("builder.field.db.type",db); 261 Element dbtype=addContentElement("type",sType,db); 262 String sState = Fields.getStateDescription(fielddef.getState()); 263 dbtype.setAttribute("state",sState); 264 int size = fielddef.getMaxLength(); 265 if (size>-1) { 266 dbtype.setAttribute("size",""+size); 267 } 268 dbtype.setAttribute("notnull",""+fielddef.isRequired()); 269 dbtype.setAttribute("key",""+fielddef.isUnique()); 270 } 271 } 272 } 273 } 274 275 281 public void setExpandBuilder(boolean value) { 282 expandBuilder=value; 283 } 284 285 291 public boolean getExpandBuilder() { 292 return expandBuilder; 293 } 294 } 295
| Popular Tags
|