Search in sources :

Example 26 with XMLBuilder

use of org.jkiss.utils.xml.XMLBuilder in project dbeaver by dbeaver.

the class ERDExportGraphML method exportDiagram.

@Override
public void exportDiagram(EntityDiagram diagram, IFigure figure, DiagramPart diagramPart, File targetFile) throws DBException {
    try {
        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
            XMLBuilder xml = new XMLBuilder(fos, GeneralUtils.UTF8_ENCODING);
            xml.setButify(true);
            xml.startElement("graphml");
            xml.addAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
            xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xml.addAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
            xml.addAttribute("xmlns:y", "http://www.yworks.com/xml/graphml");
            xml.startElement("key");
            xml.addAttribute("for", "node");
            xml.addAttribute("id", "nodegraph");
            xml.addAttribute("yfiles.type", "nodegraphics");
            xml.endElement();
            xml.startElement("key");
            xml.addAttribute("for", "edge");
            xml.addAttribute("id", "edgegraph");
            xml.addAttribute("yfiles.type", "edgegraphics");
            xml.endElement();
            xml.startElement("graph");
            xml.addAttribute("edgedefault", "directed");
            xml.addAttribute("id", "G");
            Map<ERDEntity, String> entityMap = new HashMap<>();
            int nodeNum = 0;
            for (ERDEntity entity : diagram.getEntities()) {
                nodeNum++;
                String nodeId = "n" + nodeNum;
                entityMap.put(entity, nodeId);
                // node
                xml.startElement("node");
                xml.addAttribute("id", nodeId);
                {
                    // Graph data
                    xml.startElement("data");
                    xml.addAttribute("key", "nodegraph");
                    {
                        // Generic node
                        EntityPart entityPart = diagramPart.getEntityPart(entity);
                        EntityFigure entityFigure = (EntityFigure) entityPart.getFigure();
                        Rectangle partBounds = entityPart.getBounds();
                        xml.startElement("y:GenericNode");
                        xml.addAttribute("configuration", "com.yworks.entityRelationship.big_entity");
                        // Geometry
                        xml.startElement("y:Geometry");
                        xml.addAttribute("height", partBounds.height());
                        xml.addAttribute("width", partBounds.width);
                        xml.addAttribute("x", partBounds.x());
                        xml.addAttribute("y", partBounds.y());
                        xml.endElement();
                        // Fill
                        xml.startElement("y:Fill");
                        xml.addAttribute("color", getHtmlColor(entityFigure.getBackgroundColor()));
                        // xml.addAttribute("color2", partBounds.width);
                        xml.addAttribute("transparent", "false");
                        xml.endElement();
                        // Border
                        xml.startElement("y:BorderStyle");
                        xml.addAttribute("color", getHtmlColor(entityFigure.getForegroundColor()));
                        xml.addAttribute("type", "line");
                        xml.addAttribute("width", "1.0");
                        xml.endElement();
                        {
                            // Entity Name
                            Rectangle nameBounds = entityFigure.getNameLabel().getBounds();
                            xml.startElement("y:NodeLabel");
                            xml.addAttribute("alignment", "center");
                            xml.addAttribute("autoSizePolicy", "content");
                            xml.addAttribute("configuration", "com.yworks.entityRelationship.label.name");
                            xml.addAttribute("fontFamily", "Courier");
                            xml.addAttribute("fontSize", "12");
                            xml.addAttribute("fontStyle", "plain");
                            xml.addAttribute("hasLineColor", "false");
                            xml.addAttribute("modelName", "internal");
                            xml.addAttribute("modelPosition", "t");
                            xml.addAttribute("backgroundColor", getHtmlColor(entityFigure.getNameLabel().getBackgroundColor()));
                            xml.addAttribute("textColor", getHtmlColor(entityFigure.getNameLabel().getForegroundColor()));
                            xml.addAttribute("visible", "true");
                            xml.addAttribute("height", nameBounds.height());
                            xml.addAttribute("width", nameBounds.width);
                            xml.addAttribute("x", nameBounds.x());
                            xml.addAttribute("y", nameBounds.y());
                            xml.addText(entity.getName());
                            xml.endElement();
                        }
                        {
                            // Attributes
                            AttributeListFigure columnsFigure = entityFigure.getColumnsFigure();
                            Rectangle attrsBounds = columnsFigure.getBounds();
                            xml.startElement("y:NodeLabel");
                            xml.addAttribute("alignment", "left");
                            xml.addAttribute("autoSizePolicy", "content");
                            xml.addAttribute("configuration", "com.yworks.entityRelationship.label.attributes");
                            xml.addAttribute("fontFamily", "Courier");
                            xml.addAttribute("fontSize", "12");
                            xml.addAttribute("fontStyle", "plain");
                            xml.addAttribute("hasLineColor", "false");
                            xml.addAttribute("modelName", "custom");
                            xml.addAttribute("modelPosition", "t");
                            xml.addAttribute("backgroundColor", getHtmlColor(columnsFigure.getBackgroundColor()));
                            xml.addAttribute("textColor", getHtmlColor(columnsFigure.getForegroundColor()));
                            xml.addAttribute("visible", "true");
                            xml.addAttribute("height", attrsBounds.height());
                            xml.addAttribute("width", attrsBounds.width);
                            xml.addAttribute("x", attrsBounds.x());
                            xml.addAttribute("y", attrsBounds.y());
                            StringBuilder attrsString = new StringBuilder();
                            for (ERDEntityAttribute attr : entity.getColumns()) {
                                if (attrsString.length() > 0) {
                                    attrsString.append("\n");
                                }
                                attrsString.append(attr.getName());
                            }
                            xml.addText(attrsString.toString());
                            xml.startElement("y:LabelModel");
                            xml.startElement("y:ErdAttributesNodeLabelModel");
                            xml.endElement();
                            xml.endElement();
                            xml.startElement("y:ModelParameter");
                            xml.startElement("y:ErdAttributesNodeLabelModelParameter");
                            xml.endElement();
                            xml.endElement();
                            xml.endElement();
                        }
                        xml.endElement();
                    }
                    xml.endElement();
                }
                xml.endElement();
            }
            int edgeNum = 0;
            for (ERDEntity entity : diagram.getEntities()) {
                EntityPart entityPart = diagramPart.getEntityPart(entity);
                for (ERDAssociation association : entity.getForeignKeyRelationships()) {
                    AssociationPart associationPart = entityPart.getConnectionPart(association, true);
                    if (associationPart == null) {
                        log.debug("Association part not found");
                        continue;
                    }
                    edgeNum++;
                    String edgeId = "e" + edgeNum;
                    xml.startElement("edge");
                    xml.addAttribute("id", edgeId);
                    xml.addAttribute("source", entityMap.get(entity));
                    xml.addAttribute("target", entityMap.get(association.getPrimaryKeyEntity()));
                    xml.startElement("data");
                    xml.addAttribute("key", "edgegraph");
                    xml.startElement("y:PolyLineEdge");
                    // sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
                    xml.startElement("y:Path");
                    for (Bendpoint bp : associationPart.getBendpoints()) {
                        xml.startElement("y:Point");
                        xml.addAttribute("x", bp.getLocation().x);
                        xml.addAttribute("y", bp.getLocation().x);
                        xml.endElement();
                    }
                    xml.endElement();
                    xml.startElement("y:LineStyle");
                    xml.addAttribute("color", "#000000");
                    xml.addAttribute("type", !association.isIdentifying() || association.isLogical() ? "dashed" : "line");
                    xml.addAttribute("width", "1.0");
                    xml.endElement();
                    xml.startElement("y:Arrows");
                    String sourceStyle = !association.isIdentifying() ? "white_diamond" : "none";
                    xml.addAttribute("source", sourceStyle);
                    xml.addAttribute("target", "circle");
                    xml.endElement();
                    xml.startElement("y:BendStyle");
                    xml.addAttribute("smoothed", "false");
                    xml.endElement();
                    xml.endElement();
                    xml.endElement();
                    xml.endElement();
                }
            }
            xml.endElement();
            xml.endElement();
            xml.flush();
            fos.flush();
        }
        UIUtils.launchProgram(targetFile.getAbsolutePath());
    } catch (Exception e) {
        DBUserInterface.getInstance().showError("Save ERD as GraphML", null, e);
    }
}
Also used : EntityFigure(org.jkiss.dbeaver.ext.erd.figures.EntityFigure) HashMap(java.util.HashMap) ERDEntity(org.jkiss.dbeaver.ext.erd.model.ERDEntity) Rectangle(org.eclipse.draw2d.geometry.Rectangle) ERDAssociation(org.jkiss.dbeaver.ext.erd.model.ERDAssociation) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) Bendpoint(org.eclipse.draw2d.Bendpoint) AttributeListFigure(org.jkiss.dbeaver.ext.erd.figures.AttributeListFigure) DBException(org.jkiss.dbeaver.DBException) ERDEntityAttribute(org.jkiss.dbeaver.ext.erd.model.ERDEntityAttribute) FileOutputStream(java.io.FileOutputStream) AssociationPart(org.jkiss.dbeaver.ext.erd.part.AssociationPart) EntityPart(org.jkiss.dbeaver.ext.erd.part.EntityPart) Bendpoint(org.eclipse.draw2d.Bendpoint)

Example 27 with XMLBuilder

use of org.jkiss.utils.xml.XMLBuilder in project dbeaver by dbeaver.

the class SQLQueryParameterRegistry method save.

public void save() {
    File storeFile = DBeaverActivator.getConfigurationFile(CONFIG_FILE_NAME);
    try (OutputStream os = new FileOutputStream(storeFile)) {
        XMLBuilder xml = new XMLBuilder(os, GeneralUtils.UTF8_ENCODING);
        xml.setButify(true);
        xml.startElement("bindings");
        for (ParameterInfo param : parameterMap.values()) {
            xml.startElement(TAG_PARAMETER);
            xml.addAttribute(RegistryConstants.ATTR_NAME, param.name);
            xml.addAttribute(RegistryConstants.ATTR_VALUE, param.value);
            xml.endElement();
        }
        xml.endElement();
        xml.flush();
    } catch (IOException ex) {
        log.warn("IO error", ex);
    }
}
Also used : XMLBuilder(org.jkiss.utils.xml.XMLBuilder)

Example 28 with XMLBuilder

use of org.jkiss.utils.xml.XMLBuilder in project dbeaver by dbeaver.

the class DBVModel method serializeEntity.

private static void serializeEntity(XMLBuilder xml, DBVEntity entity) throws IOException {
    xml.startElement(TAG_ENTITY);
    xml.addAttribute(ATTR_NAME, entity.getName());
    if (!CommonUtils.isEmpty(entity.getDescriptionColumnNames())) {
        xml.addAttribute(ATTR_DESCRIPTION, entity.getDescriptionColumnNames());
    }
    if (!CommonUtils.isEmpty(entity.properties)) {
        for (Map.Entry<String, String> prop : entity.properties.entrySet()) {
            xml.startElement(TAG_PROPERTY);
            xml.addAttribute(ATTR_NAME, prop.getKey());
            xml.addAttribute(ATTR_VALUE, prop.getValue());
            xml.endElement();
        }
    }
    // Attributes
    for (DBVEntityAttribute attr : CommonUtils.safeCollection(entity.entityAttributes)) {
        try (final XMLBuilder.Element e3 = xml.startElement(TAG_ATTRIBUTE)) {
            xml.addAttribute(ATTR_NAME, attr.getName());
            final DBVTransformSettings transformSettings = attr.getTransformSettings();
            if (transformSettings != null && transformSettings.hasValuableData()) {
                try (final XMLBuilder.Element e4 = xml.startElement(TAG_TRANSFORM)) {
                    if (!CommonUtils.isEmpty(transformSettings.getCustomTransformer())) {
                        xml.addAttribute(ATTR_CUSTOM, transformSettings.getCustomTransformer());
                    }
                    for (String id : CommonUtils.safeCollection(transformSettings.getIncludedTransformers())) {
                        try (final XMLBuilder.Element e5 = xml.startElement(TAG_INCLUDE)) {
                            xml.addAttribute(ATTR_ID, id);
                        }
                    }
                    for (String id : CommonUtils.safeCollection(transformSettings.getExcludedTransformers())) {
                        try (final XMLBuilder.Element e5 = xml.startElement(TAG_EXCLUDE)) {
                            xml.addAttribute(ATTR_ID, id);
                        }
                    }
                    final Map<String, String> transformOptions = transformSettings.getTransformOptions();
                    if (transformOptions != null) {
                        for (Map.Entry<String, String> prop : transformOptions.entrySet()) {
                            try (final XMLBuilder.Element e5 = xml.startElement(TAG_PROPERTY)) {
                                if (prop.getValue() != null) {
                                    xml.addAttribute(ATTR_NAME, prop.getKey());
                                    xml.addAttribute(ATTR_VALUE, prop.getValue());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // Constraints
    for (DBVEntityConstraint c : CommonUtils.safeCollection(entity.entityConstraints)) {
        if (c.hasAttributes()) {
            xml.startElement(TAG_CONSTRAINT);
            xml.addAttribute(ATTR_NAME, c.getName());
            xml.addAttribute(ATTR_TYPE, c.getConstraintType().getName());
            for (DBVEntityConstraintColumn cc : CommonUtils.safeCollection(c.getAttributeReferences(null))) {
                xml.startElement(TAG_ATTRIBUTE);
                xml.addAttribute(ATTR_NAME, cc.getAttributeName());
                xml.endElement();
            }
            xml.endElement();
        }
    }
    // Colors
    if (!CommonUtils.isEmpty(entity.colorOverrides)) {
        xml.startElement(TAG_COLORS);
        for (DBVColorOverride color : entity.colorOverrides) {
            xml.startElement(TAG_COLOR);
            xml.addAttribute(ATTR_NAME, color.getAttributeName());
            xml.addAttribute(ATTR_OPERATOR, color.getOperator().name());
            if (color.getColorForeground() != null) {
                xml.addAttribute(ATTR_FOREGROUND, color.getColorForeground());
            }
            if (color.getColorBackground() != null) {
                xml.addAttribute(ATTR_BACKGROUND, color.getColorBackground());
            }
            if (!ArrayUtils.isEmpty(color.getAttributeValues())) {
                for (Object value : color.getAttributeValues()) {
                    if (value == null) {
                        continue;
                    }
                    xml.startElement(TAG_VALUE);
                    xml.addText(GeneralUtils.serializeObject(value));
                    xml.endElement();
                }
            }
            xml.endElement();
        }
        xml.endElement();
    }
    xml.endElement();
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) Map(java.util.Map) XMLBuilder(org.jkiss.utils.xml.XMLBuilder)

Example 29 with XMLBuilder

use of org.jkiss.utils.xml.XMLBuilder in project dbeaver by dbeaver.

the class ProjectExportWizard method exportProjects.

private void exportProjects(DBRProgressMonitor monitor, final ProjectExportData exportData) throws IOException, CoreException, InterruptedException {
    if (!exportData.getOutputFolder().exists()) {
        if (!exportData.getOutputFolder().mkdirs()) {
            // $NON-NLS-2$
            throw new IOException("Cannot create directory '" + exportData.getOutputFolder().getAbsolutePath() + "'");
        }
    }
    String archiveName = exportData.getArchiveFileName() + ExportConstants.ARCHIVE_FILE_EXT;
    File archiveFile = new File(exportData.getOutputFolder(), archiveName);
    FileOutputStream exportStream = new FileOutputStream(archiveFile);
    try {
        ByteArrayOutputStream metaBuffer = new ByteArrayOutputStream(10000);
        ZipOutputStream archiveStream = new ZipOutputStream(exportStream);
        // Start meta
        XMLBuilder meta = new XMLBuilder(metaBuffer, GeneralUtils.UTF8_ENCODING);
        meta.startElement(ExportConstants.TAG_ARCHIVE);
        meta.addAttribute(ExportConstants.ATTR_VERSION, ExportConstants.ARCHIVE_VERSION_CURRENT);
        exportData.initExport(DBWorkbench.getPlatform().getWorkspace(), meta, archiveStream);
        {
            // Export source info
            meta.startElement(ExportConstants.TAG_SOURCE);
            meta.addAttribute(ExportConstants.ATTR_TIME, System.currentTimeMillis());
            meta.addAttribute(ExportConstants.ATTR_ADDRESS, InetAddress.getLocalHost().getHostAddress());
            meta.addAttribute(ExportConstants.ATTR_HOST, InetAddress.getLocalHost().getHostName());
            meta.endElement();
        }
        Map<DBPProject, Integer> resCountMap = new HashMap<>();
        monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_collect_info, exportData.getProjectsToExport().size());
        for (DBPProject project : exportData.getProjectsToExport()) {
            // Add used drivers to export data
            final DBPDataSourceRegistry dataSourceRegistry = project.getDataSourceRegistry();
            if (dataSourceRegistry != null) {
                for (DBPDataSourceContainer dataSourceDescriptor : dataSourceRegistry.getDataSources()) {
                    exportData.usedDrivers.add(dataSourceDescriptor.getDriver());
                }
            }
            resCountMap.put(project, getChildCount(exportData, project.getEclipseProject()));
            monitor.worked(1);
        }
        monitor.done();
        {
            // Export projects
            exportData.meta.startElement(ExportConstants.TAG_PROJECTS);
            for (DBPProject project : exportData.getProjectsToExport()) {
                monitor.beginTask(NLS.bind(CoreMessages.dialog_project_export_wizard_monitor_export_project, project.getName()), resCountMap.get(project));
                try {
                    exportProject(monitor, exportData, project.getEclipseProject());
                } finally {
                    monitor.done();
                }
            }
            exportData.meta.endElement();
        }
        if (exportData.isExportDrivers()) {
            // Export driver libraries
            Set<File> libFiles = new HashSet<>();
            Map<String, File> libPathMap = new HashMap<>();
            for (DBPDriver driver : exportData.usedDrivers) {
                for (DBPDriverLibrary fileDescriptor : driver.getDriverLibraries()) {
                    final File libraryFile = fileDescriptor.getLocalFile();
                    if (libraryFile != null && !fileDescriptor.isDisabled() && libraryFile.exists()) {
                        libFiles.add(libraryFile);
                        libPathMap.put(fileDescriptor.getPath(), libraryFile);
                    }
                }
            }
            if (!libFiles.isEmpty()) {
                monitor.beginTask(CoreMessages.dialog_project_export_wizard_monitor_export_libraries, libFiles.size());
                // $NON-NLS-1$
                final ZipEntry driversFolder = new ZipEntry(ExportConstants.DIR_DRIVERS + "/");
                // $NON-NLS-1$
                driversFolder.setComment("Database driver libraries");
                exportData.archiveStream.putNextEntry(driversFolder);
                exportData.archiveStream.closeEntry();
                exportData.meta.startElement(ExportConstants.TAG_LIBRARIES);
                Set<String> libFileNames = new HashSet<>();
                for (String libPath : libPathMap.keySet()) {
                    final File libFile = libPathMap.get(libPath);
                    // Check for file name duplications
                    final String libFileName = libFile.getName();
                    if (libFileNames.contains(libFileName)) {
                        // $NON-NLS-1$
                        log.warn("Duplicate driver library file name: " + libFileName);
                        continue;
                    }
                    libFileNames.add(libFileName);
                    monitor.subTask(libFileName);
                    exportData.meta.startElement(RegistryConstants.TAG_FILE);
                    exportData.meta.addAttribute(ExportConstants.ATTR_PATH, libPath);
                    // $NON-NLS-1$
                    exportData.meta.addAttribute(ExportConstants.ATTR_FILE, "drivers/" + libFileName);
                    exportData.meta.endElement();
                    // $NON-NLS-1$
                    final ZipEntry driverFile = new ZipEntry(ExportConstants.DIR_DRIVERS + "/" + libFileName);
                    // $NON-NLS-1$
                    driverFile.setComment("Driver library");
                    exportData.archiveStream.putNextEntry(driverFile);
                    try (InputStream is = new FileInputStream(libFile)) {
                        IOUtils.copyStream(is, exportData.archiveStream, COPY_BUFFER_SIZE);
                    }
                    exportData.archiveStream.closeEntry();
                    monitor.worked(1);
                }
                exportData.meta.endElement();
                monitor.done();
            }
        }
        // Add meta to archive
        {
            exportData.meta.endElement();
            exportData.meta.flush();
            archiveStream.putNextEntry(new ZipEntry(ExportConstants.META_FILENAME));
            archiveStream.write(metaBuffer.toByteArray());
            archiveStream.closeEntry();
        }
        // Finish archive creation
        archiveStream.finish();
    } finally {
        ContentUtils.close(exportStream);
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) DBPProject(org.jkiss.dbeaver.model.app.DBPProject) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) DBPDriver(org.jkiss.dbeaver.model.connection.DBPDriver) ZipOutputStream(java.util.zip.ZipOutputStream) DBPDriverLibrary(org.jkiss.dbeaver.model.connection.DBPDriverLibrary) DBPDataSourceRegistry(org.jkiss.dbeaver.model.app.DBPDataSourceRegistry) IFile(org.eclipse.core.resources.IFile) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) HashSet(java.util.HashSet)

Example 30 with XMLBuilder

use of org.jkiss.utils.xml.XMLBuilder in project dbeaver by serge-rider.

the class ERDExportGraphML method exportDiagram.

@Override
public void exportDiagram(EntityDiagram diagram, IFigure figure, DiagramPart diagramPart, File targetFile) throws DBException {
    try {
        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
            XMLBuilder xml = new XMLBuilder(fos, GeneralUtils.UTF8_ENCODING);
            xml.setButify(true);
            xml.startElement("graphml");
            xml.addAttribute("xmlns", "http://graphml.graphdrawing.org/xmlns");
            xml.addAttribute("xmlns:java", "http://www.yworks.com/xml/yfiles-common/1.0/java");
            xml.addAttribute("xmlns:sys", "http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0");
            xml.addAttribute("xmlns:x", "http://www.yworks.com/xml/yfiles-common/markup/2.0");
            xml.addAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xml.addAttribute("xmlns:y", "http://www.yworks.com/xml/graphml");
            xml.addAttribute("xmlns:yed", "http://www.yworks.com/xml/yed/3");
            xml.addAttribute("xsi:schemaLocation", "http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd");
            xml.startElement("key");
            xml.addAttribute("for", "node");
            xml.addAttribute("id", "nodegraph");
            xml.addAttribute("yfiles.type", "nodegraphics");
            xml.endElement();
            xml.startElement("key");
            xml.addAttribute("for", "edge");
            xml.addAttribute("id", "edgegraph");
            xml.addAttribute("yfiles.type", "edgegraphics");
            xml.endElement();
            xml.startElement("graph");
            xml.addAttribute("edgedefault", "directed");
            xml.addAttribute("id", "G");
            Map<ERDEntity, String> entityMap = new HashMap<>();
            int nodeNum = 0;
            for (ERDEntity entity : diagram.getEntities()) {
                nodeNum++;
                String nodeId = "n" + nodeNum;
                entityMap.put(entity, nodeId);
                // node
                xml.startElement("node");
                xml.addAttribute("id", nodeId);
                {
                    // Graph data
                    xml.startElement("data");
                    xml.addAttribute("key", "nodegraph");
                    {
                        // Generic node
                        EntityPart entityPart = diagramPart.getEntityPart(entity);
                        EntityFigure entityFigure = entityPart.getFigure();
                        Rectangle partBounds = entityPart.getBounds();
                        xml.startElement("y:GenericNode");
                        xml.addAttribute("configuration", "com.yworks.entityRelationship.big_entity");
                        // Geometry
                        xml.startElement("y:Geometry");
                        xml.addAttribute("height", partBounds.height);
                        xml.addAttribute("width", partBounds.width + getExtraTableLength(diagram, entity));
                        xml.addAttribute("x", partBounds.x());
                        xml.addAttribute("y", partBounds.y());
                        xml.endElement();
                        // Fill
                        xml.startElement("y:Fill");
                        xml.addAttribute("color", getHtmlColor(entityFigure.getBackgroundColor()));
                        // xml.addAttribute("color2", partBounds.width);
                        xml.addAttribute("transparent", "false");
                        xml.endElement();
                        // Border
                        xml.startElement("y:BorderStyle");
                        xml.addAttribute("color", getHtmlColor(entityFigure.getForegroundColor()));
                        xml.addAttribute("type", "line");
                        xml.addAttribute("width", "1.0");
                        xml.endElement();
                        {
                            // Entity Name
                            Rectangle nameBounds = entityFigure.getNameLabel().getBounds();
                            xml.startElement("y:NodeLabel");
                            xml.addAttribute("alignment", "center");
                            xml.addAttribute("autoSizePolicy", "content");
                            xml.addAttribute("configuration", "com.yworks.entityRelationship.label.name");
                            xml.addAttribute("fontFamily", "Courier");
                            xml.addAttribute("fontSize", fontSize);
                            xml.addAttribute("fontStyle", "plain");
                            xml.addAttribute("hasLineColor", "false");
                            xml.addAttribute("modelName", "internal");
                            xml.addAttribute("modelPosition", "t");
                            xml.addAttribute("backgroundColor", getHtmlColor(entityFigure.getNameLabel().getBackgroundColor()));
                            xml.addAttribute("textColor", "#FFFFFF");
                            xml.addAttribute("visible", "true");
                            xml.addAttribute("horizontalTextPosition", "center");
                            xml.addAttribute("iconTextGap", "4");
                            xml.addAttribute("height", nameBounds.height);
                            xml.addAttribute("width", nameBounds.width);
                            xml.addAttribute("x", 0);
                            xml.addAttribute("y", 4);
                            xml.addText(entity.getName());
                            xml.endElement();
                        }
                        {
                            // Attributes
                            AttributeListFigure columnsFigure = entityFigure.getColumnsFigure();
                            Rectangle attrsBounds = columnsFigure.getBounds();
                            xml.startElement("y:NodeLabel");
                            xml.addAttribute("alignment", "left");
                            xml.addAttribute("autoSizePolicy", "content");
                            xml.addAttribute("configuration", "com.yworks.entityRelationship.label.attributes");
                            xml.addAttribute("fontFamily", "Courier");
                            xml.addAttribute("fontSize", fontSize);
                            xml.addAttribute("fontStyle", "plain");
                            xml.addAttribute("hasLineColor", "false");
                            xml.addAttribute("modelName", "custom");
                            xml.addAttribute("modelPosition", "t");
                            xml.addAttribute("backgroundColor", getHtmlColor(columnsFigure.getBackgroundColor()));
                            xml.addAttribute("textColor", getHtmlColor(columnsFigure.getForegroundColor()));
                            xml.addAttribute("visible", "true");
                            xml.addAttribute("horizontalTextPosition", "center");
                            xml.addAttribute("iconTextGap", "4");
                            xml.addAttribute("height", attrsBounds.height);
                            xml.addAttribute("width", attrsBounds.width);
                            // numbers from yEd Graph Editor
                            xml.addAttribute("x", 2);
                            xml.addAttribute("y", 31.66796875);
                            StringBuilder attrsString = new StringBuilder();
                            for (ERDEntityAttribute attr : entity.getAttributes()) {
                                if (attrsString.length() > 0) {
                                    attrsString.append("\n");
                                }
                                attrsString.append(ERDUIUtils.getFullAttributeLabel(diagram, attr, true));
                            }
                            xml.addText(attrsString.toString());
                            xml.startElement("y:LabelModel");
                            xml.startElement("y:ErdAttributesNodeLabelModel");
                            xml.endElement();
                            xml.endElement();
                            xml.startElement("y:ModelParameter");
                            xml.startElement("y:ErdAttributesNodeLabelModelParameter");
                            xml.endElement();
                            xml.endElement();
                            xml.endElement();
                        }
                        xml.endElement();
                    }
                    xml.endElement();
                }
                xml.endElement();
            }
            int edgeNum = 0;
            for (ERDEntity entity : diagram.getEntities()) {
                EntityPart entityPart = diagramPart.getEntityPart(entity);
                for (ERDAssociation association : entity.getAssociations()) {
                    AssociationPart associationPart = entityPart.getConnectionPart(association, true);
                    if (associationPart == null) {
                        log.debug("Association part not found");
                        continue;
                    }
                    edgeNum++;
                    String edgeId = "e" + edgeNum;
                    xml.startElement("edge");
                    xml.addAttribute("id", edgeId);
                    xml.addAttribute("source", entityMap.get(entity));
                    xml.addAttribute("target", entityMap.get(association.getTargetEntity()));
                    xml.startElement("data");
                    xml.addAttribute("key", "edgegraph");
                    xml.startElement("y:PolyLineEdge");
                    // sx="0.0" sy="0.0" tx="0.0" ty="0.0"/>
                    xml.startElement("y:Path");
                    xml.addAttribute("sx", 0.0);
                    xml.addAttribute("sy", 0.0);
                    xml.addAttribute("tx", 0.0);
                    xml.addAttribute("ty", 0.0);
                    for (Bendpoint bp : associationPart.getBendpoints()) {
                        xml.startElement("y:Point");
                        xml.addAttribute("x", bp.getLocation().x);
                        xml.addAttribute("y", bp.getLocation().y);
                        xml.endElement();
                    }
                    xml.endElement();
                    boolean identifying = ERDUtils.isIdentifyingAssociation(association);
                    xml.startElement("y:LineStyle");
                    xml.addAttribute("color", "#000000");
                    xml.addAttribute("type", !identifying || association.isLogical() ? "dashed" : "line");
                    xml.addAttribute("width", "1.0");
                    xml.endElement();
                    xml.startElement("y:Arrows");
                    String sourceStyle = !identifying ? "white_diamond" : "none";
                    xml.addAttribute("source", sourceStyle);
                    xml.addAttribute("target", "circle");
                    xml.endElement();
                    xml.startElement("y:BendStyle");
                    xml.addAttribute("smoothed", "false");
                    xml.endElement();
                    xml.endElement();
                    xml.endElement();
                    xml.endElement();
                }
            }
            xml.endElement();
            xml.endElement();
            xml.flush();
            fos.flush();
        }
        UIUtils.launchProgram(targetFile.getAbsolutePath());
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Save ERD as GraphML", null, e);
    }
}
Also used : EntityFigure(org.jkiss.dbeaver.erd.ui.figures.EntityFigure) HashMap(java.util.HashMap) ERDEntity(org.jkiss.dbeaver.erd.model.ERDEntity) Rectangle(org.eclipse.draw2d.geometry.Rectangle) ERDAssociation(org.jkiss.dbeaver.erd.model.ERDAssociation) XMLBuilder(org.jkiss.utils.xml.XMLBuilder) Bendpoint(org.eclipse.draw2d.Bendpoint) AttributeListFigure(org.jkiss.dbeaver.erd.ui.figures.AttributeListFigure) DBException(org.jkiss.dbeaver.DBException) ERDEntityAttribute(org.jkiss.dbeaver.erd.model.ERDEntityAttribute) FileOutputStream(java.io.FileOutputStream) AssociationPart(org.jkiss.dbeaver.erd.ui.part.AssociationPart) EntityPart(org.jkiss.dbeaver.erd.ui.part.EntityPart) Bendpoint(org.eclipse.draw2d.Bendpoint)

Aggregations

XMLBuilder (org.jkiss.utils.xml.XMLBuilder)40 FileOutputStream (java.io.FileOutputStream)10 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Bendpoint (org.eclipse.draw2d.Bendpoint)8 Rectangle (org.eclipse.draw2d.geometry.Rectangle)8 DBException (org.jkiss.dbeaver.DBException)7 OutputStream (java.io.OutputStream)6 XMLException (org.jkiss.utils.xml.XMLException)6 AbsoluteBendpoint (org.eclipse.draw2d.AbsoluteBendpoint)4 RelativeBendpoint (org.eclipse.draw2d.RelativeBendpoint)4 DBPDriverLibrary (org.jkiss.dbeaver.model.connection.DBPDriverLibrary)4 AssociationPart (org.jkiss.dbeaver.ext.erd.part.AssociationPart)3 EntityPart (org.jkiss.dbeaver.ext.erd.part.EntityPart)3 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2