Search in sources :

Example 1 with EdgeProperties

use of org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties in project gephi by gephi.

the class ImporterGraphML method readAttribute.

private void readAttribute(XMLStreamReader reader) throws Exception {
    String id = "";
    String type = "";
    String title = "";
    String defaultStr = "";
    String forStr = "all";
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attName = reader.getAttributeName(i).getLocalPart();
        if (ATTRIBUTE_ID.equalsIgnoreCase(attName)) {
            id = reader.getAttributeValue(i).trim();
        } else if (ATTRIBUTE_TYPE.equalsIgnoreCase(attName)) {
            type = reader.getAttributeValue(i).trim();
        } else if (ATTRIBUTE_TITLE.equalsIgnoreCase(attName)) {
            title = reader.getAttributeValue(i).trim();
        } else if (ATTRIBUTE_FOR.equalsIgnoreCase(attName)) {
            forStr = reader.getAttributeValue(i).trim();
        }
    }
    if (title.isEmpty()) {
        title = id;
    }
    boolean property = false;
    if (!id.isEmpty()) {
        // Properties
        if (forStr.equalsIgnoreCase("node")) {
            NodeProperties prop = properties.getNodeProperty(id) == null ? properties.getNodeProperty(title) : properties.getNodeProperty(id);
            if (prop != null) {
                nodePropertiesAttributes.put(id, prop);
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_nodeproperty", title));
                property = true;
            }
        } else if (forStr.equalsIgnoreCase("edge")) {
            EdgeProperties prop = properties.getEdgeProperty(id) == null ? properties.getEdgeProperty(title) : properties.getEdgeProperty(id);
            if (prop != null) {
                edgePropertiesAttributes.put(id, prop);
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_edgeproperty", title));
                property = true;
            }
        }
        if (property) {
            return;
        }
    } else {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributeempty", title), Issue.Level.SEVERE));
        return;
    }
    if (!property && type.isEmpty()) {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributetype1", title), Issue.Level.SEVERE));
        type = "string";
    }
    if (!property) {
        // Class type
        if (forStr.isEmpty() || !(forStr.equalsIgnoreCase("node") || forStr.equalsIgnoreCase("edge") || forStr.equalsIgnoreCase("all"))) {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributeclass", title), Issue.Level.SEVERE));
            return;
        }
        // Default?
        boolean end = false;
        boolean defaultFlag = false;
        while (reader.hasNext() && !end) {
            int xmltype = reader.next();
            switch(xmltype) {
                case XMLStreamReader.START_ELEMENT:
                    if (ATTRIBUTE_DEFAULT.equalsIgnoreCase(xmlReader.getLocalName())) {
                        defaultFlag = true;
                    }
                    break;
                case XMLStreamReader.CHARACTERS:
                    if (defaultFlag && !xmlReader.isWhiteSpace()) {
                        defaultStr = xmlReader.getText();
                    }
                    break;
                case XMLStreamReader.END_ELEMENT:
                    if (ATTRIBUTE.equalsIgnoreCase(xmlReader.getLocalName())) {
                        end = true;
                    }
                    break;
            }
        }
        // Type
        Class attributeType = String.class;
        if (type.equalsIgnoreCase("boolean") || type.equalsIgnoreCase("bool")) {
            attributeType = boolean.class;
        } else if (type.equalsIgnoreCase("integer") || type.equalsIgnoreCase("int")) {
            attributeType = int.class;
        } else if (type.equalsIgnoreCase("long")) {
            attributeType = Long.class;
        } else if (type.equalsIgnoreCase("float")) {
            attributeType = Float.class;
        } else if (type.equalsIgnoreCase("double")) {
            attributeType = Double.class;
        } else if (type.equalsIgnoreCase("string")) {
            attributeType = String.class;
        } else if (type.equalsIgnoreCase("bigdecimal")) {
            attributeType = BigDecimal.class;
        } else if (type.equalsIgnoreCase("biginteger")) {
            attributeType = BigInteger.class;
        } else if (type.equalsIgnoreCase("byte")) {
            attributeType = Byte.class;
        } else if (type.equalsIgnoreCase("char")) {
            attributeType = Character.class;
        } else if (type.equalsIgnoreCase("short")) {
            attributeType = Short.class;
        } else if (type.equalsIgnoreCase("listboolean")) {
            attributeType = boolean[].class;
        } else if (type.equalsIgnoreCase("listint")) {
            attributeType = int[].class;
        } else if (type.equalsIgnoreCase("listlong")) {
            attributeType = long[].class;
        } else if (type.equalsIgnoreCase("listfloat")) {
            attributeType = float[].class;
        } else if (type.equalsIgnoreCase("listdouble")) {
            attributeType = double[].class;
        } else if (type.equalsIgnoreCase("liststring")) {
            attributeType = String[].class;
        } else if (type.equalsIgnoreCase("listbigdecimal")) {
            attributeType = BigDecimal[].class;
        } else if (type.equalsIgnoreCase("listbiginteger")) {
            attributeType = BigInteger[].class;
        } else if (type.equalsIgnoreCase("listbyte")) {
            attributeType = byte[].class;
        } else if (type.equalsIgnoreCase("listchar")) {
            attributeType = char[].class;
        } else if (type.equalsIgnoreCase("listshort")) {
            attributeType = short[].class;
        } else {
            report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributetype2", type), Issue.Level.SEVERE));
            return;
        }
        // Add to model
        ColumnDraft column = null;
        if ("node".equalsIgnoreCase(forStr) || "all".equalsIgnoreCase(forStr)) {
            if (container.getNodeColumn(id) != null) {
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributecolumn_exist", id));
                return;
            }
            column = container.addNodeColumn(id, attributeType);
            column.setTitle(title);
            report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_nodeattribute", title, attributeType.getCanonicalName()));
        }
        if ("edge".equalsIgnoreCase(forStr) || "all".equalsIgnoreCase(forStr)) {
            if (container.getEdgeColumn(id) != null) {
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributecolumn_exist", id));
                return;
            }
            column = container.addEdgeColumn(id, attributeType);
            column.setTitle(title);
            report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_edgeattribute", title, attributeType.getCanonicalName()));
        }
        if (column != null && !defaultStr.isEmpty()) {
            try {
                column.setDefaultValueString(defaultStr);
                report.log(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_log_default", defaultStr, title));
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributedefault", title, attributeType.getCanonicalName()), Issue.Level.SEVERE));
            }
        }
    } else {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_attributeempty", title), Issue.Level.SEVERE));
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) XMLStreamException(javax.xml.stream.XMLStreamException) NodeProperties(org.gephi.io.importer.api.PropertiesAssociations.NodeProperties) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties) BigInteger(java.math.BigInteger)

Example 2 with EdgeProperties

use of org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties in project gephi by gephi.

the class ImporterGraphML method readEdgeAttValue.

private void readEdgeAttValue(XMLStreamReader reader, EdgeDraft edge) throws Exception {
    String fore = "";
    String value = "";
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String attName = reader.getAttributeName(i).getLocalPart();
        if (ATTVALUE_FOR.equalsIgnoreCase(attName)) {
            fore = reader.getAttributeValue(i).trim();
        }
    }
    if (fore.isEmpty()) {
        report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datakey", edge), Issue.Level.SEVERE));
        return;
    }
    boolean end = false;
    while (reader.hasNext() && !end) {
        int xmltype = reader.next();
        switch(xmltype) {
            case XMLStreamReader.CHARACTERS:
                if (!xmlReader.isWhiteSpace()) {
                    value += xmlReader.getText();
                }
                break;
            case XMLStreamReader.END_ELEMENT:
                if (ATTVALUE.equalsIgnoreCase(xmlReader.getLocalName())) {
                    end = true;
                }
                break;
        }
    }
    if (!value.isEmpty()) {
        EdgeProperties prop = edgePropertiesAttributes.get(fore);
        if (prop != null) {
            try {
                switch(prop) {
                    case WEIGHT:
                        edge.setWeight(parseFloat(value));
                        break;
                    case LABEL:
                        edge.setLabel(value);
                        break;
                    case COLOR:
                        edge.setColor(value);
                        break;
                    case R:
                        if (edge.getColor() == null) {
                            edge.setColor(Integer.parseInt(value), 0, 0);
                        } else {
                            edge.setColor(Integer.parseInt(value), edge.getColor().getGreen(), edge.getColor().getBlue());
                        }
                        break;
                    case G:
                        if (edge.getColor() == null) {
                            edge.setColor(0, Integer.parseInt(value), 0);
                        } else {
                            edge.setColor(edge.getColor().getRed(), Integer.parseInt(value), edge.getColor().getBlue());
                        }
                        break;
                    case B:
                        if (edge.getColor() == null) {
                            edge.setColor(0, 0, Integer.parseInt(value));
                        } else {
                            edge.setColor(edge.getColor().getRed(), edge.getColor().getGreen(), Integer.parseInt(value));
                        }
                        break;
                }
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, edge, prop.toString()), Issue.Level.SEVERE));
            }
        }
        // Data attribute value
        ColumnDraft column = container.getEdgeColumn(fore);
        if (column != null) {
            try {
                edge.parseAndSetValue(column.getId(), value);
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, edge, column.getTitle()), Issue.Level.SEVERE));
            }
        }
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 3 with EdgeProperties

use of org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties in project gephi by gephi.

the class ImporterEdgeList method findEdgeAttributesColumns.

private void findEdgeAttributesColumns(ResultSet rs) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    int columnsCount = metaData.getColumnCount();
    for (int i = 0; i < columnsCount; i++) {
        String columnName = metaData.getColumnLabel(i + 1);
        EdgeProperties p = database.getPropertiesAssociations().getEdgeProperty(columnName);
        if (p == null) {
            // No property associated to this column is found, so we append it as an attribute
            Class typeClass = findTypeClass(metaData, i);
            container.addEdgeColumn(columnName, typeClass);
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties)

Example 4 with EdgeProperties

use of org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties in project gephi by gephi.

the class ImporterEdgeList method getEdges.

private void getEdges(Connection connection) throws SQLException {
    // Factory
    ElementDraft.Factory factory = container.factory();
    // Properties
    PropertiesAssociations properties = database.getPropertiesAssociations();
    Statement s = connection.createStatement();
    ResultSet rs = null;
    try {
        rs = s.executeQuery(database.getEdgeQuery());
    } catch (SQLException ex) {
        report.logIssue(new Issue("Failed to execute Edge query", Issue.Level.SEVERE, ex));
        return;
    }
    findEdgeAttributesColumns(rs);
    ResultSetMetaData metaData = rs.getMetaData();
    int columnsCount = metaData.getColumnCount();
    int idColumn = edgeColumns.findIdIndex(metaData, properties);
    while (rs.next()) {
        EdgeDraft edge = edgeColumns.getEdgeDraft(factory, rs, idColumn);
        for (int i = 0; i < columnsCount; i++) {
            String columnName = metaData.getColumnLabel(i + 1);
            EdgeProperties p = properties.getEdgeProperty(columnName);
            if (p != null) {
                injectEdgeProperty(p, rs, i + 1, edge);
            } else {
                // Inject edge attributes
                ColumnDraft col = container.getEdgeColumn(columnName);
                injectElementAttribute(rs, i + 1, col, edge);
            }
        }
        injectTimeIntervalProperty(edge);
        container.addEdge(edge);
    }
    rs.close();
    s.close();
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) SQLException(java.sql.SQLException) ElementDraft(org.gephi.io.importer.api.ElementDraft) PropertiesAssociations(org.gephi.io.importer.api.PropertiesAssociations) Statement(java.sql.Statement) ResultSetMetaData(java.sql.ResultSetMetaData) EdgeDraft(org.gephi.io.importer.api.EdgeDraft) ResultSet(java.sql.ResultSet) EdgeProperties(org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties)

Aggregations

EdgeProperties (org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties)4 ColumnDraft (org.gephi.io.importer.api.ColumnDraft)3 Issue (org.gephi.io.importer.api.Issue)3 ResultSetMetaData (java.sql.ResultSetMetaData)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 BigInteger (java.math.BigInteger)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 EdgeDraft (org.gephi.io.importer.api.EdgeDraft)1 ElementDraft (org.gephi.io.importer.api.ElementDraft)1 PropertiesAssociations (org.gephi.io.importer.api.PropertiesAssociations)1 NodeProperties (org.gephi.io.importer.api.PropertiesAssociations.NodeProperties)1