1 30 package com.genimen.djeneric.tools.modeler.io; 31 32 import java.io.ByteArrayOutputStream ; 33 import java.io.UnsupportedEncodingException ; 34 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.transform.OutputKeys ; 38 import javax.xml.transform.Transformer ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 43 import org.w3c.dom.CDATASection ; 44 import org.w3c.dom.DOMException ; 45 import org.w3c.dom.Document ; 46 import org.w3c.dom.Element ; 47 48 import com.genimen.djeneric.structure.EditorDefinition; 49 import com.genimen.djeneric.structure.ExtentUsage; 50 import com.genimen.djeneric.structure.PropertyUsage; 51 import com.genimen.djeneric.structure.ResourceDefinition; 52 import com.genimen.djeneric.structure.ScriptDefinition; 53 import com.genimen.djeneric.tools.io.DjenericDocumentHandler; 54 import com.genimen.djeneric.tools.modeler.ModelEditor; 55 import com.genimen.djeneric.tools.modeler.diagrammer.ExtentViewer; 56 import com.genimen.djeneric.tools.modeler.userperspective.ExtentNode; 57 import com.genimen.djeneric.tools.modeler.userperspective.ViewEditor; 58 59 public class ModelDocumentHandler extends DjenericDocumentHandler 60 { 61 public ModelDocumentHandler(String xmlString) throws Exception 62 { 63 super(xmlString); 64 } 65 66 public ModelDocumentHandler() throws Exception 67 { 68 } 69 70 protected Element addExtentUsageToDoc(ExtentNode theNode, Document doc, Element parent) 71 { 72 Element extentElement = doc.createElement(NAVIGATOR_EXTENTNODE_ELEMENT); 73 74 parent.appendChild(extentElement); 75 76 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_NAME, "" + theNode.getExtent().getName()); 77 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_ALIAS, "" + theNode.getExtent().getInternalCode()); 78 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_TYPE, "" + theNode.getExtent().getObjectType()); 79 80 if (theNode.getTitle() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_TITLE, theNode.getTitle()); 81 if (theNode.getOqlFilter() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_QBE_FILTER, theNode 82 .getOqlFilter()); 83 if (theNode.getCustomNodeClass() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_CUSTOM_NODE_CLASS, 84 theNode.getCustomNodeClass()); 85 if (theNode.getId() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_ID, theNode.getId()); 86 if (theNode.getDescriptorExpression() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DESCRIPTOR, theNode 87 .getDescriptorExpression()); 88 89 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_EDIT, "" + theNode.isEditAllowed()); 90 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_INSERT, "" + theNode.isInsertAllowed()); 91 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DELETE, "" + theNode.isDeleteAllowed()); 92 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_RECURSIVE, "" + theNode.isRecursive()); 93 94 if (theNode.getVia() != null) 95 { 96 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_VIA, theNode.getVia().getRelationName()); 97 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DETAIL_COLUMN, theNode.getVia().getDetailPropertyName()); 98 } 99 100 if (!theNode.isRecursive()) 102 { 103 EditorDefinition editor = theNode.getEditor(); 104 if (editor != null && !editor.isMarkedForDelete()) 105 { 106 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_EDITOR, editor.getName()); 107 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_EDITORTARGET, theNode.getEditorTarget()); 108 } 109 110 ResourceDefinition img = theNode.getImageIconResource(); 111 if (img != null && !img.isMarkedForDelete()) 112 { 113 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_IMAGEICON, img.getAbsolutePath()); 114 } 115 116 ExtentNode[] children = theNode.getChildren(); 117 for (int i = 0; i < children.length; i++) 118 { 119 addExtentUsageToDoc(children[i], doc, extentElement); 120 } 121 } 122 return extentElement; 123 } 124 125 protected Element addEditorUsagesDoc(ExtentUsage theNode, Document doc, Element parent) 126 { 127 Element extentElement = doc.createElement(EDITOR_EXTENT_USAGE_ELEMENT); 128 129 parent.appendChild(extentElement); 130 131 extentElement.setAttribute(EDITOR_NAME, "" + theNode.getExtent().getName()); 132 extentElement.setAttribute(EDITOR_EXTENTNODE_ALIAS, "" + theNode.getExtent().getInternalCode()); 133 extentElement.setAttribute(EDITOR_EXTENTNODE_TYPE, "" + theNode.getExtent().getObjectType()); 134 135 if (theNode.getTitle() != null) extentElement.setAttribute(EDITOR_TITLE, theNode.getTitle()); 136 if (theNode.getOqlExpression() != null) extentElement.setAttribute(EDITOR_QBE_FILTER, theNode.getOqlExpression()); 137 138 if (theNode.getId() != null) extentElement.setAttribute(EDITOR_EXTENTNODE_ID, theNode.getId()); 139 if (theNode.getDescriptorExpression() != null) extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DESCRIPTOR, theNode 140 .getDescriptorExpression()); 141 142 extentElement.setAttribute(EDITOR_EDIT, "" + theNode.isEditAllowed()); 143 extentElement.setAttribute(EDITOR_INSERT, "" + theNode.isInsertAllowed()); 144 extentElement.setAttribute(EDITOR_DELETE, "" + theNode.isDeleteAllowed()); 145 146 extentElement.setAttribute(EDITOR_MULTIROW, "" + theNode.isMultiRow()); 147 extentElement.setAttribute(EDITOR_ROWSDISPLAYED, "" + theNode.getRowsDisplayed()); 148 addPropertyUsagesToDoc(theNode, doc, extentElement); 149 150 if (theNode.getVia() != null) 151 { 152 extentElement.setAttribute(EDITOR_EXTENTNODE_VIA, theNode.getVia().getRelationName()); 153 extentElement.setAttribute(NAVIGATOR_EXTENTNODE_DETAIL_COLUMN, theNode.getVia().getDetailPropertyName()); 154 } 155 156 ExtentUsage[] children = theNode.getChildren(); 157 for (int i = 0; i < children.length; i++) 158 { 159 addEditorUsagesDoc(children[i], doc, extentElement); 160 } 161 return extentElement; 162 } 163 164 private String nvl(String value) 165 { 166 if (value == null) return ""; 167 return value; 168 } 169 170 public void addPropertyUsagesToDoc(ExtentUsage theNode, Document doc, Element parent) 171 { 172 theNode.sortPropertyUsages(); 173 for (int i = 0; i < theNode.getPropertyUsageCount(); i++) 174 { 175 Element columnElement = doc.createElement(EDITOR_PROPERTY_USAGE_ELEMENT); 176 parent.appendChild(columnElement); 177 PropertyUsage cu = theNode.getPropertyUsage(i); 178 179 if (cu.getBaseProperty() == null) continue; 180 181 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_SEQ, "" + cu.getSeq()); 182 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_COLUMN, cu.getBaseProperty().getName()); 183 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_PROMPT, cu.getPrompt()); 184 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_DESCRIPTION, cu.getDescription()); 185 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_COMPONENT, cu.getComponentTypeName()); 186 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_REQUIRED, "" + cu.isRequired()); 187 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_UPDATEABLE, "" + cu.isUpdateable()); 188 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_WIDTH, "" + cu.getDisplayWidth()); 189 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_HEIGHT, "" + cu.getDisplayHeight()); 190 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_SORT_ORDER, "" + cu.getSortOrder()); 191 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_DEFAULT_VALUE, nvl(cu.getDefaultValue())); 192 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_CUSTOM_EDITOR, nvl(cu.getCustomEditorClass())); 193 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_RELATIVE_PATH, nvl(cu.getRelativePath())); 194 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_QUERYABLE, "" + cu.isQueryable()); 195 columnElement.setAttribute(EDITOR_PROPERTY_USAGE_DISPLAYED, "" + cu.isDisplayed()); 196 } 197 } 198 199 protected void addEditorToDoc(EditorDefinition definition, Document doc, Element editors) throws DOMException , 200 UnsupportedEncodingException 201 { 202 Element editor = doc.createElement(EDITOR_ELEMENT); 203 editors.appendChild(editor); 204 editor.setAttribute(EDITOR_NAME, definition.getName()); 205 if (definition.getCustomEditorClass() != null) editor.setAttribute(EDITOR_CUSTOMCLASS, definition 206 .getCustomEditorClass()); 207 if (definition.getUsages() != null) addEditorUsagesDoc(definition.getUsages(), doc, editor); 208 209 Element scriptElem = doc.createElement(EDITOR_EMBEDDED_SCRIPT); 210 211 CDATASection descr = doc.createCDATASection(definition.getScriptDefinition().getBase64()); 212 scriptElem.appendChild(descr); 213 editor.appendChild(scriptElem); 214 } 215 216 public void addResourceToDoc(ResourceDefinition def, Document doc, Element parent) throws DOMException , 217 UnsupportedEncodingException 218 { 219 220 Element descrElem = doc.createElement(RESOURCE_ELEMENT); 221 descrElem.setAttribute(RESOURCE_ELEMENT_ABSOLUTE_PATH, def.getAbsolutePath()); 222 descrElem.setAttribute(RESOURCE_ELEMENT_TIMESTAMP, def.getTimeStamp()); 223 224 CDATASection descr = doc.createCDATASection(def.getBase64()); 225 descrElem.appendChild(descr); 226 parent.appendChild(descrElem); 227 } 228 229 public void addScriptToDoc(ScriptDefinition script, Document doc, Element parent) throws DOMException , 230 UnsupportedEncodingException 231 { 232 Element scriptElem = doc.createElement(SCRIPT_ELEMENT); 233 scriptElem.setAttribute(SCRIPT_ELEMENT_TITLE, script.getTitle()); 234 235 CDATASection descr = doc.createCDATASection(script.getBase64()); 236 scriptElem.appendChild(descr); 237 parent.appendChild(scriptElem); 238 } 239 240 public String createXmlString(ModelEditor editor) throws Exception 241 { 242 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 243 DocumentBuilder builder = factory.newDocumentBuilder(); 244 Document doc = builder.newDocument(); 245 246 Element root = doc.createElement(VIEW); 247 root.setAttribute(VIEW_STRUCTURE_VERSION, String.valueOf(VIEW_STRUCTURE_VERSION_NUMBER)); 248 Element diagram = doc.createElement(DIAGRAM_ELEMENT); 249 250 doc.appendChild(root); 251 root.appendChild(diagram); 252 253 for (int i = 0; i < editor.getExtentViewerCount(); i++) 254 { 255 ExtentViewer tv = editor.getExtentViewer(i); 256 257 Element extentViewer = doc.createElement(EXTENTVIEW_ELEMENT); 258 extentViewer.setAttribute(EXTENTVIEW_TABLE, "" + tv.getExtent().getName()); 259 extentViewer.setAttribute(EXTENTVIEW_ALIAS, "" + tv.getExtent().getInternalCode()); 260 extentViewer.setAttribute(EXTENTVIEW_TYPE, "" + tv.getExtent().getObjectType()); 261 extentViewer.setAttribute(EXTENTVIEW_X, "" + tv.getX()); 262 extentViewer.setAttribute(EXTENTVIEW_Y, "" + tv.getY()); 263 extentViewer.setAttribute(EXTENTVIEW_WIDTH, "" + tv.getWidth()); 264 extentViewer.setAttribute(EXTENTVIEW_HEIGHT, "" + tv.getHeight()); 265 extentViewer.setAttribute(EXTENTVIEW_TCOLOR, encodeColor(tv.getExtentColor())); 266 extentViewer.setAttribute(EXTENTVIEW_NCOLOR, encodeColor(tv.getNameColor())); 267 extentViewer.setAttribute(EXTENTVIEW_CCOLOR, encodeColor(tv.getPropertyColor())); 268 269 diagram.appendChild(extentViewer); 270 } 271 272 Element navigator = doc.createElement(NAVIGATOR_ELEMENT); 273 root.appendChild(navigator); 274 ViewEditor viewEditor = editor.getViewEditor(); 275 276 ExtentNode[] extentNodes = viewEditor.getRootExtents(); 277 for (int i = 0; i < extentNodes.length; i++) 278 { 279 addExtentUsageToDoc(extentNodes[i], doc, navigator); 280 } 281 282 Element editors = doc.createElement(EDITORS_ELEMENT); 283 root.appendChild(editors); 284 285 EditorDefinition[] editorDefs = viewEditor.getEditors(); 286 for (int i = 0; i < editorDefs.length; i++) 287 { 288 addEditorToDoc(editorDefs[i], doc, editors); 289 } 290 291 ResourceDefinition[] resources = viewEditor.getResources(); 292 for (int i = 0; i < resources.length; i++) 293 { 294 if (!resources[i].isMarkedForDelete()) addResourceToDoc(resources[i], doc, root); 295 } 296 297 ScriptDefinition[] scripts = viewEditor.getScripts(); 298 for (int i = 0; i < scripts.length; i++) 299 { 300 if (!scripts[i].isMarkedForDelete()) addScriptToDoc(scripts[i], doc, root); 301 } 302 303 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 304 305 Transformer serializer = TransformerFactory.newInstance().newTransformer(); 306 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 307 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 308 serializer.transform(new DOMSource (doc), new StreamResult (baos)); 309 310 return baos.toString(); 311 } 312 313 } | Popular Tags |