Search in sources :

Example 1 with ParseState

use of org.cytoscape.io.internal.read.xgmml.ParseState in project cytoscape-impl by cytoscape.

the class AttributeValueUtil method handleAttribute.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected ParseState handleAttribute(Attributes atts) throws SAXParseException {
    ParseState parseState = ParseState.NONE;
    final String name = atts.getValue("name");
    String type = atts.getValue("type");
    String cyType = atts.getValue("cy:type");
    if ("has_nested_network".equals(name))
        type = ObjectType.BOOLEAN.getXgmmlValue();
    final boolean isEquation = ObjectTypeMap.fromXGMMLBoolean(atts.getValue("cy:equation"));
    final boolean isHidden = ObjectTypeMap.fromXGMMLBoolean(atts.getValue("cy:hidden"));
    final CyIdentifiable curElement = manager.getCurrentElement();
    CyNetwork curNet = manager.getCurrentNetwork();
    // under the group subgraph, but the edge will be created on the root-network only,
    if (curElement instanceof CyNode || curElement instanceof CyEdge) {
        boolean containsElement = (curElement instanceof CyNode && curNet.containsNode((CyNode) curElement));
        containsElement |= (curElement instanceof CyEdge && curNet.containsEdge((CyEdge) curElement));
        // So if the current network does not contain this element, the CyRootNetwork should contain it
        if (!containsElement)
            curNet = manager.getRootNetwork();
    }
    CyRow row = null;
    if (isHidden) {
        row = curNet.getRow(curElement, CyNetwork.HIDDEN_ATTRS);
    } else {
        // Network name must be local, right? What about other network attributes?
        if (CyNetwork.SELECTED.equals(name) || (curElement instanceof CyNetwork))
            row = curNet.getRow(curElement, CyNetwork.LOCAL_ATTRS);
        else
            // Will be created in the shared table
            row = curNet.getRow(curElement, CyNetwork.DEFAULT_ATTRS);
    }
    CyTable table = row.getTable();
    CyColumn column = table.getColumn(name);
    if (column != null) {
        // Check if it's a virtual column
        // It's necessary because the source row may not exist yet, which would throw an exception
        // when the value is set. Doing this forces the creation of the source row.
        final VirtualColumnInfo info = column.getVirtualColumnInfo();
        if (info.isVirtual()) {
            final CyTable srcTable = info.getSourceTable();
            final CyColumn srcColumn = srcTable.getColumn(info.getSourceColumn());
            final Class<?> jkColType = table.getColumn(info.getTargetJoinKey()).getType();
            final Object jkValue = row.get(info.getTargetJoinKey(), jkColType);
            final Collection<CyRow> srcRowList = srcTable.getMatchingRows(info.getSourceJoinKey(), jkValue);
            final CyRow srcRow;
            if (srcRowList == null || srcRowList.isEmpty()) {
                if (info.getTargetJoinKey().equals(CyIdentifiable.SUID)) {
                    // Try to create the row
                    srcRow = srcTable.getRow(jkValue);
                } else {
                    logger.error("Unable to import virtual column \"" + name + "\": The source table \"" + srcTable.getTitle() + "\" does not have any matching rows for join key \"" + info.getSourceJoinKey() + "=" + jkValue + "\".");
                    return parseState;
                }
            } else {
                srcRow = srcRowList.iterator().next();
            }
            // Use the source table instead
            table = srcTable;
            column = srcColumn;
            row = srcRow;
        }
    } else {
        // Also check if the column exists in a network's local table
        final Class<? extends CyIdentifiable> tableType;
        if (curElement instanceof CyNode)
            tableType = CyNode.class;
        else if (curElement instanceof CyEdge)
            tableType = CyEdge.class;
        else
            tableType = CyNetwork.class;
        if (existsInLocalTable(name, tableType, manager.getRootNetwork()))
            row = curNet.getRow(curElement, CyNetwork.LOCAL_ATTRS);
    }
    Object value = null;
    final ObjectType objType = typeMap.fromXgmml(cyType, type);
    if (isEquation) {
        // It is an equation...
        String formula = atts.getValue("value");
        if (name != null && formula != null) {
            manager.addEquationString(row, name, formula);
        }
    } else {
        // Regular attribute value...
        value = getTypedAttributeValue(objType, atts, name);
    }
    switch(objType) {
        case BOOLEAN:
            if (name != null)
                setAttribute(row, name, Boolean.class, (Boolean) value);
            break;
        case REAL:
            if (name != null) {
                if (SUIDUpdater.isUpdatable(name))
                    setAttribute(row, name, Long.class, (Long) value);
                else
                    setAttribute(row, name, Double.class, (Double) value);
            }
            break;
        case INTEGER:
            if (name != null) {
                if (SUIDUpdater.isUpdatable(name))
                    setAttribute(row, name, Long.class, (Long) value);
                else
                    setAttribute(row, name, Integer.class, (Integer) value);
            }
            break;
        case LONG:
            if (name != null)
                setAttribute(row, name, Long.class, (Long) value);
            break;
        case STRING:
            if (name != null)
                setAttribute(row, name, String.class, (String) value);
            break;
        // must make sure to clear out any existing values before we parse.
        case LIST:
            manager.currentAttributeID = name;
            manager.setCurrentRow(row);
            if (column != null && List.class.isAssignableFrom(column.getType()))
                row.set(name, null);
            if (column == null) {
                // Since Cytoscape v3.3
                final String elementType = atts.getValue("cy:elementType");
                if (elementType != null) {
                    final ObjectType elementObjType = typeMap.fromXgmml(elementType, null);
                    final Class<?> clazz = typeMap.getClass(elementObjType, name);
                    table.createListColumn(name, clazz, false, new ArrayList());
                    column = row.getTable().getColumn(name);
                    manager.listAttrHolder = new ArrayList<Object>();
                }
            }
            return ParseState.LIST_ATT;
    }
    return parseState;
}
Also used : CyColumn(org.cytoscape.model.CyColumn) ArrayList(java.util.ArrayList) CyNetwork(org.cytoscape.model.CyNetwork) ParseState(org.cytoscape.io.internal.read.xgmml.ParseState) CyRow(org.cytoscape.model.CyRow) CyEdge(org.cytoscape.model.CyEdge) CyTable(org.cytoscape.model.CyTable) ObjectType(org.cytoscape.io.internal.util.xgmml.ObjectType) CyNode(org.cytoscape.model.CyNode) ArrayList(java.util.ArrayList) List(java.util.List) VirtualColumnInfo(org.cytoscape.model.VirtualColumnInfo) CyIdentifiable(org.cytoscape.model.CyIdentifiable)

Example 2 with ParseState

use of org.cytoscape.io.internal.read.xgmml.ParseState in project cytoscape-impl by cytoscape.

the class HandleEdgeAttribute method handle.

@Override
public ParseState handle(String tag, Attributes atts, ParseState current) throws SAXException {
    if (atts == null)
        return current;
    manager.attState = current;
    // application generates the CYS file with redundant <att> tags for 3.0+.
    if (!manager.isSessionFormat() || manager.getDocumentVersion() < 3.0) {
        // Is this a graphics override?
        String name = atts.getValue(NAME);
        // Check for blank attribute
        final String value = atts.getValue(VALUE);
        if (name == null && value == null)
            return current;
        if (manager.getDocumentVersion() < 3.0) {
            // Writing locked visual properties as regular <att> tags is deprecated!
            if (name.startsWith("edge.")) {
                // It is a bypass attribute...
                name = name.replace(".", "").toLowerCase();
                manager.addGraphicsAttribute(manager.getCurrentEdge(), name, value);
            }
        }
        ParseState nextState = attributeValueUtil.handleAttribute(atts);
        if (nextState != ParseState.NONE)
            return nextState;
    }
    return current;
}
Also used : ParseState(org.cytoscape.io.internal.read.xgmml.ParseState)

Example 3 with ParseState

use of org.cytoscape.io.internal.read.xgmml.ParseState in project cytoscape-impl by cytoscape.

the class HandleGraphAttribute method handle.

@Override
public ParseState handle(String tag, Attributes atts, ParseState current) throws SAXException {
    if (atts == null)
        return current;
    manager.attState = current;
    ParseState nextState = current;
    String attName = atts.getValue(AttributeValueUtil.ATTR_NAME);
    if (attName == null && atts.getValue("value") == null)
        return current;
    // Look for "special" network attributes
    if (attName.equals("documentVersion")) {
        // Old format only!
        manager.setDocumentVersion(attributeValueUtil.getAttributeValue(atts, attName));
    } else if (attName.matches("backgroundColor|GRAPH_VIEW_ZOOM|GRAPH_VIEW_CENTER_[XY]|NODE_SIZE_LOCKED")) {
        String attValue = attributeValueUtil.getAttributeValue(atts, attName);
        manager.addGraphicsAttribute(manager.getCurrentNetwork(), attName, attValue);
    } else {
        manager.setCurrentElement(manager.getCurrentNetwork());
        nextState = attributeValueUtil.handleAttribute(atts);
    }
    if (nextState != ParseState.NONE)
        return nextState;
    return current;
}
Also used : ParseState(org.cytoscape.io.internal.read.xgmml.ParseState)

Example 4 with ParseState

use of org.cytoscape.io.internal.read.xgmml.ParseState in project cytoscape-impl by cytoscape.

the class HandleGraphGraphics method handle.

@Override
public ParseState handle(String tag, Attributes atts, ParseState current) throws SAXException {
    if (atts == null)
        return current;
    manager.attState = current;
    ParseState nextState = current;
    if (tag.equals("graphics")) {
        manager.addGraphicsAttributes(manager.getCurrentNetwork(), atts);
    } else if (tag.equals("att")) {
        String name = atts.getValue(AttributeValueUtil.ATTR_NAME);
        String value = atts.getValue(AttributeValueUtil.ATTR_VALUE);
        if (name != null && value != null)
            manager.addGraphicsAttribute(manager.getCurrentNetwork(), name, value);
    }
    if (nextState != ParseState.NONE)
        return nextState;
    return current;
}
Also used : ParseState(org.cytoscape.io.internal.read.xgmml.ParseState)

Example 5 with ParseState

use of org.cytoscape.io.internal.read.xgmml.ParseState in project cytoscape-impl by cytoscape.

the class HandleNodeAttribute method handle.

@Override
public ParseState handle(String tag, Attributes atts, ParseState current) throws SAXException {
    if (atts == null)
        return current;
    manager.attState = current;
    // the CYS file with redundant <att> tags for 3.0+.
    if (!manager.isSessionFormat() || manager.getDocumentVersion() < 3.0) {
        // Is this a graphics override?
        String name = atts.getValue("name");
        // Check for blank attribute (e.g. surrounding a group)
        if (name == null && atts.getValue("value") == null)
            return current;
        if (manager.getDocumentVersion() < 3.0) {
            if (name.startsWith("node.")) {
                // It is a bypass attribute...
                // Writing locked visual properties as regular <att> tags is deprecated!
                name = name.replace(".", "").toLowerCase();
                String value = atts.getValue("value");
                manager.addGraphicsAttribute(manager.getCurrentNode(), name, value);
            } else if (name.equals("nested_network_id")) {
                // Handle 2.x nested network as network pointer
                final String netId = atts.getValue("value");
                final CyNode node = manager.getCurrentNode();
                // because the first one may be a 2.x group-network
                if (netId != null && !netId.equals(manager.getCache().getNetworkPointerId(node)))
                    manager.getCache().addNetworkPointer(node, netId);
            }
        }
        ParseState nextState = attributeValueUtil.handleAttribute(atts);
        if (nextState != ParseState.NONE)
            return nextState;
    }
    return current;
}
Also used : CyNode(org.cytoscape.model.CyNode) ParseState(org.cytoscape.io.internal.read.xgmml.ParseState)

Aggregations

ParseState (org.cytoscape.io.internal.read.xgmml.ParseState)6 CyNode (org.cytoscape.model.CyNode)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ObjectType (org.cytoscape.io.internal.util.xgmml.ObjectType)1 CyColumn (org.cytoscape.model.CyColumn)1 CyEdge (org.cytoscape.model.CyEdge)1 CyIdentifiable (org.cytoscape.model.CyIdentifiable)1 CyNetwork (org.cytoscape.model.CyNetwork)1 CyRow (org.cytoscape.model.CyRow)1 CyTable (org.cytoscape.model.CyTable)1 VirtualColumnInfo (org.cytoscape.model.VirtualColumnInfo)1