Author: clopes
Date: 2012-08-01 12:26:25 -0700 (Wed, 01 Aug 2012)
New Revision: 30066

Modified:
   
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriter.java
   
core3/impl/trunk/io-impl/impl/src/test/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriterTest.java
Log:
Fixes #1332 (XGMML Export: Long type attributes are not exported).
Fixes #1333 (XGMML Export: boolean attributes saved as [true|false], not [1|0]).

Modified: 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriter.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriter.java
   2012-08-01 18:54:23 UTC (rev 30065)
+++ 
core3/impl/trunk/io-impl/impl/src/main/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriter.java
   2012-08-01 19:26:25 UTC (rev 30066)
@@ -652,13 +652,15 @@
         return new String[]{};
     }
     
-    protected void writeAttributes(CyRow row) throws IOException {
+    protected void writeAttributes(final CyRow row) throws IOException {
                // If it is a Cy Session XGMML, writing the CyRows would be 
redundant,
                // because they are already serialized in .cytable files.
-       CyTable table = row.getTable();
+       final CyTable table = row.getTable();
 
-               for (final CyColumn column : table.getColumns())
-                       writeAttribute(row, column.getName());
+               for (final CyColumn column : table.getColumns()) {
+                       if (!CyIdentifiable.SUID.equals(column.getName()))
+                               writeAttribute(row, column.getName());
+               }
     }
     
     /**
@@ -681,43 +683,51 @@
                if (attType == Double.class) {
                        Double dAttr = row.get(attName, Double.class);
                        writeAttributeXML(attName, ObjectType.REAL, dAttr, 
true);
-               } else {
-                       if (attType == Integer.class) {
-                               Integer iAttr = row.get(attName, Integer.class);
-                               writeAttributeXML(attName, ObjectType.INTEGER, 
iAttr, true);
-                       } else if (attType == String.class) {
-                               String sAttr = row.get(attName, String.class);
-                               // Protect tabs and returns
-                               if (sAttr != null) {
-                                       sAttr = sAttr.replace("\n", "\\n");
-                                       sAttr = sAttr.replace("\t", "\\t");
-                               }
+               } else if (attType == Integer.class) {
+                       Integer iAttr = row.get(attName, Integer.class);
+                       writeAttributeXML(attName, ObjectType.INTEGER, iAttr, 
true);
+               } else if (attType == Long.class) {
+                       Long lAttr = row.get(attName, Long.class);
+                       writeAttributeXML(attName, ObjectType.REAL, lAttr, 
true);
+               } else if (attType == String.class) {
+                       String sAttr = row.get(attName, String.class);
+                       // Protect tabs and returns
+                       if (sAttr != null) {
+                               sAttr = sAttr.replace("\n", "\\n");
+                               sAttr = sAttr.replace("\t", "\\t");
+                       }
 
-                               writeAttributeXML(attName, ObjectType.STRING, 
sAttr, true);
-                       } else if (attType == Boolean.class) {
-                               Boolean bAttr = row.get(attName, Boolean.class);
-                               writeAttributeXML(attName, ObjectType.BOOLEAN, 
bAttr, true);
-                       } else if (attType == List.class) {
-                               final List<?> listAttr = row.getList(attName, 
column.getListElementType());
-                               writeAttributeXML(attName, ObjectType.LIST, 
null, false);
+                       writeAttributeXML(attName, ObjectType.STRING, sAttr, 
true);
+               } else if (attType == Boolean.class) {
+                       Boolean bAttr = row.get(attName, Boolean.class);
+                       writeAttributeXML(attName, ObjectType.BOOLEAN, 
ObjectTypeMap.toXGMMLBoolean(bAttr), true);
+               } else if (attType == List.class) {
+                       final List<?> listAttr = row.getList(attName, 
column.getListElementType());
+                       writeAttributeXML(attName, ObjectType.LIST, null, 
false);
 
-                               if (listAttr != null) {
-                                       depth++;
-                                       // iterate through the list
-                                       for (Object obj : listAttr) {
+                       if (listAttr != null) {
+                               depth++;
+                               // iterate through the list
+                               for (Object obj : listAttr) {
+                                       String sAttr = null;
+                                       
+                                       if (obj instanceof Boolean) {
+                                               sAttr = 
ObjectTypeMap.toXGMMLBoolean((Boolean) obj);
+                                       } else {
                                                // Protect tabs and returns (if 
necessary)
-                                               String sAttr = obj.toString();
+                                               sAttr = obj.toString();
                                                if (sAttr != null) {
                                                        sAttr = 
sAttr.replace("\n", "\\n");
                                                        sAttr = 
sAttr.replace("\t", "\\t");
                                                }
-                                               // set child attribute value & 
label
-                                               writeAttributeXML(attName, 
checkType(obj), sAttr, true);
                                        }
-                                       depth--;
+                                       // set child attribute value & label
+                                       writeAttributeXML(attName, 
checkType(obj), sAttr, true);
                                }
-                               writeAttributeXML(null, null, null, true);
+                               depth--;
                        }
+                       
+                       writeAttributeXML(null, null, null, true);
                }
        }
 
@@ -790,18 +800,19 @@
      * @param obj
      * @return Attribute type in string.
      */
-    private ObjectType checkType(final Object obj) {
-        if (obj.getClass() == String.class) {
+       private ObjectType checkType(final Object obj) {
+        final Class<?> type = obj.getClass();
+               
+               if (type == String.class)
             return ObjectType.STRING;
-        } else if (obj.getClass() == Integer.class) {
+        else if (type == Integer.class)
             return ObjectType.INTEGER;
-        } else if ((obj.getClass() == Double.class) || (obj.getClass() == 
Float.class)) {
+        else if (type == Double.class || type == Float.class || type == 
Long.class)
             return ObjectType.REAL;
-        } else if (obj.getClass() == Boolean.class) {
+        else if (type == Boolean.class)
             return ObjectType.BOOLEAN;
-        } else {
-            return null;
-        }
+
+        return null;
     }
 
     protected String getLabel(CyNetwork network, CyIdentifiable entry) {

Modified: 
core3/impl/trunk/io-impl/impl/src/test/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriterTest.java
===================================================================
--- 
core3/impl/trunk/io-impl/impl/src/test/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriterTest.java
       2012-08-01 18:54:23 UTC (rev 30065)
+++ 
core3/impl/trunk/io-impl/impl/src/test/java/org/cytoscape/io/internal/write/xgmml/GenericXGMMLWriterTest.java
       2012-08-01 19:26:25 UTC (rev 30066)
@@ -5,6 +5,7 @@
 
 import java.awt.Color;
 import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
 
 import javax.xml.xpath.XPathConstants;
 
@@ -48,6 +49,83 @@
        }
        
        @Test
+       public void testIntegerAttributeSavedAsInteger() {
+               net.getRow(net).getTable().createColumn("test_int", 
Integer.class, false);
+               net.getRow(net).set("test_int", Integer.MAX_VALUE);
+               net.getRow(net).getTable().createListColumn("test_list_int", 
Integer.class, false);
+               net.getRow(net).set("test_list_int", Arrays.asList(new 
Integer[]{ 1 }));
+               write(net);
+               assertEquals("integer", 
evalString("/x:graph/x:att[@name=\"test_int\"]/@type"));
+               assertEquals(Integer.MAX_VALUE, 
evalNumber("/x:graph/x:att[@name=\"test_int\"]/@value"));
+               assertEquals("list", 
evalString("/x:graph/x:att[@name=\"test_list_int\"]/@type"));
+               assertEquals("integer", 
evalString("/x:graph/x:att[@name=\"test_list_int\"]/x:att[@value=\"1\"]/@type"));
+       }
+       
+       @Test
+       public void testLongAttributeSavedAsReal() {
+               net.getRow(net).getTable().createColumn("test_long", 
Long.class, false);
+               net.getRow(net).set("test_long", Long.MAX_VALUE);
+               net.getRow(net).getTable().createListColumn("test_list_long", 
Long.class, false);
+               net.getRow(net).set("test_list_long", Arrays.asList(new Long[]{ 
1L }));
+               write(net);
+               assertEquals("real", 
evalString("/x:graph/x:att[@name=\"test_long\"]/@type"));
+               assertEquals(Long.MAX_VALUE, 
evalNumber("/x:graph/x:att[@name=\"test_long\"]/@value"));
+               assertEquals("list", 
evalString("/x:graph/x:att[@name=\"test_list_long\"]/@type"));
+               assertEquals("real", 
evalString("/x:graph/x:att[@name=\"test_list_long\"]/x:att[@value=\"1\"]/@type"));
+       }
+       
+       @Test
+       public void testDoubleAttributeSavedAsReal() {
+               net.getRow(net).getTable().createColumn("test_double", 
Double.class, false);
+               net.getRow(net).set("test_double", Double.MAX_VALUE);
+               net.getRow(net).getTable().createListColumn("test_list_double", 
Double.class, false);
+               net.getRow(net).set("test_list_double", Arrays.asList(new 
Double[]{ 1.2D }));
+               write(net);
+               assertEquals("real", 
evalString("/x:graph/x:att[@name=\"test_double\"]/@type"));
+               assertEquals(Double.MAX_VALUE, new 
Double(evalString("/x:graph/x:att[@name=\"test_double\"]/@value")), 0.0);
+               assertEquals("list", 
evalString("/x:graph/x:att[@name=\"test_list_double\"]/@type"));
+               assertEquals("real", 
evalString("/x:graph/x:att[@name=\"test_list_double\"]/x:att[@value=\"1.2\"]/@type"));
+       }
+       
+       @Test
+       public void testStringAttributeSavedAsString() {
+               net.getRow(net).getTable().createColumn("test_str", 
String.class, false);
+               net.getRow(net).set("test_str", "My String");
+               net.getRow(net).getTable().createListColumn("test_list_str", 
String.class, false);
+               net.getRow(net).set("test_list_str", Arrays.asList(new 
String[]{ "A", "B" }));
+               write(net);
+               assertEquals("string", 
evalString("/x:graph/x:att[@name=\"test_str\"]/@type"));
+               assertEquals("My String", 
evalString("/x:graph/x:att[@name=\"test_str\"]/@value"));
+               assertEquals("list", 
evalString("/x:graph/x:att[@name=\"test_list_str\"]/@type"));
+               assertEquals("string", 
evalString("/x:graph/x:att[@name=\"test_list_str\"]/x:att[@value=\"B\"]/@type"));
+       }
+       
+       @Test
+       public void testBooleanAttributeSavedAsBoolean() {
+               net.getRow(net).getTable().createColumn("test_bool", 
Boolean.class, false);
+               net.getRow(net).set("test_bool", true);
+               net.getRow(net).getTable().createListColumn("test_list_bool", 
Boolean.class, false);
+               net.getRow(net).set("test_list_bool", Arrays.asList(new 
Boolean[]{ true, false }));
+               write(net);
+               // see 
http://www.cs.rpi.edu/research/groups/pb/punin/public_html/XGMML/draft-xgmml-20010628.html#BT
+               assertEquals("boolean", 
evalString("/x:graph/x:att[@name=\"test_bool\"]/@type"));
+               assertEquals("1", 
evalString("/x:graph/x:att[@name=\"test_bool\"]/@value")); // XGMML boolean 
[0|1]
+               assertEquals("list", 
evalString("/x:graph/x:att[@name=\"test_list_bool\"]/@type"));
+               assertEquals("boolean", 
evalString("/x:graph/x:att[@name=\"test_list_bool\"]/x:att[1]/@type"));
+               assertEquals("1", 
evalString("/x:graph/x:att[@name=\"test_list_bool\"]/x:att[1]/@value"));
+               assertEquals("0", 
evalString("/x:graph/x:att[@name=\"test_list_bool\"]/x:att[last()]/@value"));
+       }
+       
+       @Test
+       public void testSUIDAttNotSaved() throws UnsupportedEncodingException {
+               // The SUID should NEVER be saved as an att tag 
+               CyNetwork newNet = 
netFactory.createNetwork(SavePolicy.DO_NOT_SAVE);
+               setRegistered(newNet, false); // It shouldn't make any 
difference either
+               write(newNet);
+               assertEquals(0, evalNumber("count(//x:att[@name=\"SUID\"])"));
+       }
+       
+       @Test
        public void testTopNetworkSavedEvenIfDoNotSavePolicy() throws 
UnsupportedEncodingException {
                // The network that is passed to the writer should always be 
saved, even if its save policy is DO_NOT_SAVE,
                // because it doesn't make sense to prevent an app from 
exporting it if the app explicitly wants to do so.

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to