Search in sources :

Example 1 with NodeProperties

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

the class ImporterGraphML method readNodeAttValue.

private void readNodeAttValue(XMLStreamReader reader, NodeDraft node) 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", node), 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()) {
        // Property
        NodeProperties prop = nodePropertiesAttributes.get(fore);
        if (prop != null) {
            try {
                switch(prop) {
                    case X:
                        node.setX(parseFloat(value));
                        break;
                    case Y:
                        node.setY(parseFloat(value));
                        break;
                    case Z:
                        node.setZ(parseFloat(value));
                        break;
                    case SIZE:
                        node.setSize(parseFloat(value));
                        break;
                    case LABEL:
                        node.setLabel(value);
                        break;
                    case COLOR:
                        node.setColor(value);
                        break;
                    case R:
                        if (node.getColor() == null) {
                            node.setColor(Integer.parseInt(value), 0, 0);
                        } else {
                            node.setColor(Integer.parseInt(value), node.getColor().getGreen(), node.getColor().getBlue());
                        }
                        break;
                    case G:
                        if (node.getColor() == null) {
                            node.setColor(0, Integer.parseInt(value), 0);
                        } else {
                            node.setColor(node.getColor().getRed(), Integer.parseInt(value), node.getColor().getBlue());
                        }
                        break;
                    case B:
                        if (node.getColor() == null) {
                            node.setColor(0, 0, Integer.parseInt(value));
                        } else {
                            node.setColor(node.getColor().getRed(), node.getColor().getGreen(), Integer.parseInt(value));
                        }
                        break;
                }
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, node, prop.toString()), Issue.Level.SEVERE));
            }
        }
        // Data attribute value
        ColumnDraft column = container.getNodeColumn(fore);
        if (column != null) {
            try {
                node.parseAndSetValue(column.getId(), value);
            } catch (Exception e) {
                report.logIssue(new Issue(NbBundle.getMessage(ImporterGraphML.class, "importerGraphML_error_datavalue", fore, node, column.getTitle()), Issue.Level.SEVERE));
            }
        }
    }
}
Also used : ColumnDraft(org.gephi.io.importer.api.ColumnDraft) Issue(org.gephi.io.importer.api.Issue) NodeProperties(org.gephi.io.importer.api.PropertiesAssociations.NodeProperties) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with NodeProperties

use of org.gephi.io.importer.api.PropertiesAssociations.NodeProperties 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 3 with NodeProperties

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

the class ImporterEdgeList method findNodeAttributesColumns.

private void findNodeAttributesColumns(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);
        NodeProperties p = database.getPropertiesAssociations().getNodeProperty(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.addNodeColumn(columnName, typeClass);
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) NodeProperties(org.gephi.io.importer.api.PropertiesAssociations.NodeProperties)

Example 4 with NodeProperties

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

the class ImporterEdgeList method getNodes.

private void getNodes(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.getNodeQuery());
    } catch (SQLException ex) {
        report.logIssue(new Issue("Failed to execute Node query", Issue.Level.SEVERE, ex));
        return;
    }
    findNodeAttributesColumns(rs);
    ResultSetMetaData metaData = rs.getMetaData();
    int columnsCount = metaData.getColumnCount();
    int idColumn = nodeColumns.findIdIndex(metaData, properties);
    while (rs.next()) {
        final NodeDraft node = nodeColumns.getNodeDraft(factory, rs, idColumn);
        for (int i = 0; i < columnsCount; i++) {
            String columnName = metaData.getColumnLabel(i + 1);
            NodeProperties p = properties.getNodeProperty(columnName);
            if (p != null) {
                injectNodeProperty(p, rs, i + 1, node);
            } else {
                // Inject node attributes
                ColumnDraft col = container.getNodeColumn(columnName);
                injectElementAttribute(rs, i + 1, col, node);
            }
        }
        injectTimeIntervalProperty(node);
        container.addNode(node);
    }
    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) NodeDraft(org.gephi.io.importer.api.NodeDraft) ResultSetMetaData(java.sql.ResultSetMetaData) NodeProperties(org.gephi.io.importer.api.PropertiesAssociations.NodeProperties) ResultSet(java.sql.ResultSet)

Aggregations

NodeProperties (org.gephi.io.importer.api.PropertiesAssociations.NodeProperties)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 ElementDraft (org.gephi.io.importer.api.ElementDraft)1 NodeDraft (org.gephi.io.importer.api.NodeDraft)1 PropertiesAssociations (org.gephi.io.importer.api.PropertiesAssociations)1 EdgeProperties (org.gephi.io.importer.api.PropertiesAssociations.EdgeProperties)1