Author: noelr
Date: 2009-07-09 06:32:12 -0700 (Thu, 09 Jul 2009)
New Revision: 17279
Added:
cytoscape/trunk/testData/implicitStringArrayEnc.attribute
Modified:
cytoscape/trunk/src/cytoscape/data/readers/CyAttributesReader.java
cytoscape/trunk/src/cytoscape/data/writers/CyAttributesWriter.java
cytoscape/trunk/tests/cytoscape/data/readers/CyAttributesReaderTest.java
cytoscape/trunk/tests/cytoscape/data/writers/CyAttributesWriterTest.java
Log:
Fixes, tests and data for bug 1990
Modified: cytoscape/trunk/src/cytoscape/data/readers/CyAttributesReader.java
===================================================================
--- cytoscape/trunk/src/cytoscape/data/readers/CyAttributesReader.java
2009-07-09 08:28:47 UTC (rev 17278)
+++ cytoscape/trunk/src/cytoscape/data/readers/CyAttributesReader.java
2009-07-09 13:32:12 UTC (rev 17279)
@@ -44,7 +44,10 @@
import cytoscape.data.CyAttributes;
import cytoscape.data.attr.MultiHashMapDefinition;
+import cytoscape.data.writers.CyAttributesWriter;
import cytoscape.logger.CyLogger;
+import java.net.URLDecoder;
+import java.text.MessageFormat;
// I hate writing parsing code. Grumble grumble grumble.
@@ -52,6 +55,22 @@
*
*/
public class CyAttributesReader {
+ public static final String DECODE_PROPERTY = "cytoscape.decode.attributes";
+ private static final String badDecodeMessage =
+ "Trouble when decoding attribute value, first occurence line no.
{0}" +
+ "\nIgnore if attributes file was created before 2.6.3 or wasn't
creatad by Cytoscape." +
+ "\nUse -Dcytoscape.decode.attributes=false when starting Cytoscape
to turn off decoding.";
+
+ private boolean badDecode;
+
+ private int lineNum;
+ private boolean doDecoding;
+
+ public CyAttributesReader() {
+ lineNum = 0;
+ doDecoding = Boolean.valueOf(System.getProperty(DECODE_PROPERTY,
"true"));
+ }
+
/**
* DOCUMENT ME!
*
@@ -60,10 +79,24 @@
*
* @throws IOException DOCUMENT ME!
*/
- public static void loadAttributes(CyAttributes cyAttrs, Reader fileIn)
+ public static void loadAttributes(CyAttributes cyAttrs, Reader fileIn)
throws IOException {
+ CyAttributesReader ar = new CyAttributesReader();
+ ar.loadAttributesInternal(cyAttrs, fileIn);
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @param cyAttrs DOCUMENT ME!
+ * @param fileIn DOCUMENT ME!
+ *
+ * @throws IOException DOCUMENT ME!
+ */
+ public void loadAttributesInternal(CyAttributes cyAttrs, Reader fileIn)
throws IOException {
- int lineNum = 0;
+ badDecode = false;
+
try {
final BufferedReader reader;
@@ -140,9 +173,11 @@
}
int inx = line.indexOf('=');
- final String key = line.substring(0,
inx).trim();
+ String key = line.substring(0, inx).trim();
String val = line.substring(inx + 1).trim();
+ key = decodeString(key);
+
if (firstLine && val.startsWith("(")) {
list = true;
}
@@ -152,51 +187,15 @@
val = val.substring(1).trim();
val = val.substring(0, val.length() -
1).trim();
- // Home-grown parsing (ughh) to handle
escape sequences.
+ String[] elms = val.split("::");
final ArrayList elmsBuff = new
ArrayList();
- while (val.length() > 0) {
- final StringBuilder elmBuff =
new StringBuilder();
- int inx2;
+ for (String vs : elms) {
+ vs = decodeString(vs);
+ vs = decodeSlashEscapes(vs);
+ elmsBuff.add(vs);
+ }
- for (inx2 = 0; inx2 <
val.length(); inx2++) {
- char ch =
val.charAt(inx2);
-
- if (ch == ':') {
- inx2++;
- inx2++;
-
- break;
- } else if (ch == '\\') {
- if ((inx2 + 1)
< val.length()) {
- inx2++;
-
- char
ch2 = val.charAt(inx2);
-
- if (ch2
== 'n') {
-
elmBuff.append('\n');
- } else
if (ch2 == 't') {
-
elmBuff.append('\t');
- } else
if (ch2 == 'b') {
-
elmBuff.append('\b');
- } else
if (ch2 == 'r') {
-
elmBuff.append('\r');
- } else
if (ch2 == 'f') {
-
elmBuff.append('\f');
- } else {
-
elmBuff.append(ch2);
- }
- } else {
- /* val
ends in '\' - just ignore it. */ }
- } else {
-
elmBuff.append(ch);
- }
- }
-
- elmsBuff.add(new
String(elmBuff));
- val = val.substring(inx2);
- }
-
if (firstLine) {
if (type < 0) {
while (true) {
@@ -244,42 +243,9 @@
cyAttrs.setListAttribute(key,
attributeName, elmsBuff);
} else { // Not a list.
- // Do the escaping thing.
+ val = decodeString(val);
+ val = decodeSlashEscapes(val);
- final StringBuilder elmBuff = new
StringBuilder();
- int inx2;
-
- for (inx2 = 0; inx2 < val.length();
inx2++) {
- char ch = val.charAt(inx2);
-
- if (ch == '\\') {
- if ((inx2 + 1) <
val.length()) {
- inx2++;
-
- char ch2 =
val.charAt(inx2);
-
- if (ch2 == 'n')
{
-
elmBuff.append('\n');
- } else if (ch2
== 't') {
-
elmBuff.append('\t');
- } else if (ch2
== 'b') {
-
elmBuff.append('\b');
- } else if (ch2
== 'r') {
-
elmBuff.append('\r');
- } else if (ch2
== 'f') {
-
elmBuff.append('\f');
- } else {
-
elmBuff.append(ch2);
- }
- } else {
- /* val ends in
'\' - just ignore it. */ }
- } else {
- elmBuff.append(ch);
- }
- }
-
- val = new String(elmBuff);
-
if (firstLine) {
if (type < 0) {
while (true) {
@@ -331,4 +297,64 @@
throw new IOException(message);
}
}
+
+ private String decodeString(String in) throws IOException {
+ if (doDecoding) {
+ try {
+ in = URLDecoder.decode(in, CyAttributesWriter.ENCODING_SCHEME);
+ }
+ catch (IllegalArgumentException iae) {
+ if (!badDecode) {
+
CyLogger.getLogger(CyAttributesReader.class).info(MessageFormat.format(badDecodeMessage,
lineNum), iae);
+ badDecode = true;
+ }
+ }
+ }
+
+ return in;
+ }
+
+ private static String decodeSlashEscapes(String in) {
+ final StringBuilder elmBuff = new StringBuilder();
+ int inx2;
+
+ for (inx2 = 0; inx2 < in.length(); inx2++) {
+ char ch = in.charAt(inx2);
+
+ if (ch == '\\') {
+ if ((inx2 + 1) < in.length()) {
+ inx2++;
+
+ char ch2 = in.charAt(inx2);
+
+ if (ch2 == 'n') {
+ elmBuff.append('\n');
+ } else if (ch2 == 't') {
+ elmBuff.append('\t');
+ } else if (ch2 == 'b') {
+ elmBuff.append('\b');
+ } else if (ch2 == 'r') {
+ elmBuff.append('\r');
+ } else if (ch2 == 'f') {
+ elmBuff.append('\f');
+ } else {
+ elmBuff.append(ch2);
+ }
+ } else {
+ /* val ends in '\' - just ignore it. */ }
+ } else {
+ elmBuff.append(ch);
+ }
+ }
+
+ return elmBuff.toString();
+ }
+
+ public boolean isDoDecoding() {
+ return doDecoding;
+ }
+
+ public void setDoDecoding(boolean doDec) {
+ doDecoding = doDec;
+ }
}
Modified: cytoscape/trunk/src/cytoscape/data/writers/CyAttributesWriter.java
===================================================================
--- cytoscape/trunk/src/cytoscape/data/writers/CyAttributesWriter.java
2009-07-09 08:28:47 UTC (rev 17278)
+++ cytoscape/trunk/src/cytoscape/data/writers/CyAttributesWriter.java
2009-07-09 13:32:12 UTC (rev 17279)
@@ -40,8 +40,10 @@
import cytoscape.data.attr.MultiHashMapDefinition;
import java.io.IOException;
+import java.io.UnsupportedEncodingException;
import java.io.Writer;
+import java.net.URLEncoder;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
@@ -56,9 +58,13 @@
*
*/
public static String newline = System.getProperty("line.separator");
+ public static final String ENCODE_PROPERTY = "cytoscape.encode.attributes";
+
+ public static final String ENCODING_SCHEME = "UTF-8";
private final CyAttributes cyAttributes;
private final String attributeName;
private Writer fileWriter;
+ private boolean doEncoding;
/**
* Creates a new CyAttributesWriter2 object.
@@ -72,7 +78,8 @@
this.cyAttributes = attributes;
this.attributeName = attributeName;
this.fileWriter = fileWriter;
- }
+ doEncoding = Boolean.valueOf(System.getProperty(ENCODE_PROPERTY,
"true"));
+ }
/**
* Write out the state for the given attributes
@@ -113,6 +120,7 @@
String key;
Object value;
Iterator objIt;
+ String vs;
StringBuilder result = new StringBuilder();
while (keys.hasNext()) {
@@ -123,24 +131,41 @@
else
value = cyAttributes.getAttribute(key,
attributeName);
+ key = encodeString(key);
if (value != null) {
if (value instanceof List) {
result.append(key + " = ");
if (((Collection) value).size()
> 0) {
+ Object o;
+
objIt = ((Collection)
value).iterator();
- result.append("(" +
objIt.next());
+ result.append("(");
+ o = objIt.next();
+ vs = o.toString();
+ vs = slashEncodeString(vs);
+ vs = encodeString(vs);
+ result.append(vs);
- while (objIt.hasNext())
-
result.append("::" + objIt.next());
+ while (objIt.hasNext())
{
+ o = objIt.next();
+ vs = o.toString();
+ vs = slashEncodeString(vs);
+ vs = encodeString(vs);
+
result.append("::" + vs);
+ }
result.append(")" +
newline);
fileWriter.write(result.toString());
result = new
StringBuilder();
}
- } else
- fileWriter.write(key + " = " +
value + newline);
+ } else {
+ vs = value.toString();
+ vs = slashEncodeString(vs);
+ vs = encodeString(vs);
+ fileWriter.write(key + " = " +
vs + newline);
+ }
}
}
@@ -150,4 +175,63 @@
fileWriter.close();
fileWriter = null;
}
+
+ private String encodeString(String in) throws UnsupportedEncodingException
{
+ if (doEncoding) {
+ in = URLEncoder.encode(in, ENCODING_SCHEME);
+ }
+
+ return in;
+ }
+
+ private static String slashEncodeString(String in) {
+ StringBuilder sb;
+
+ sb = new StringBuilder(in.length());
+ for (int i = 0; i < in.length(); i++) {
+ char c;
+
+ c = in.charAt(i);
+ switch (c) {
+ case '\n': {
+ sb.append("\\n");
+ break;
+ }
+ case '\t': {
+ sb.append("\\t");
+ break;
+ }
+ case '\b': {
+ sb.append("\\b");
+ break;
+ }
+ case '\r': {
+ sb.append("\\r");
+ break;
+ }
+ case '\f': {
+ sb.append("\\f");
+ break;
+ }
+ case '\\': {
+ sb.append("\\\\");
+ break;
+ }
+ default : {
+ sb.append(c);
+ break;
+ }
+ }
+ }
+
+ return sb.toString();
+ }
+
+ public boolean isDoEncoding() {
+ return doEncoding;
+ }
+
+ public void setDoEncoding(boolean doEnc) {
+ doEncoding = doEnc;
+ }
}
Added: cytoscape/trunk/testData/implicitStringArrayEnc.attribute
===================================================================
--- cytoscape/trunk/testData/implicitStringArrayEnc.attribute
(rev 0)
+++ cytoscape/trunk/testData/implicitStringArrayEnc.attribute 2009-07-09
13:32:12 UTC (rev 17279)
@@ -0,0 +1,6 @@
+GO_molecular_function_level_4
+AP1G1 = (intracellular::clathrin+adaptor::intracellular+transporter)
+HSD17B2 = (membrane::intracellular)
+E2F4 = (DNA+binding)
+CDH3 = (cell+adhesion+molecule)
+
Modified:
cytoscape/trunk/tests/cytoscape/data/readers/CyAttributesReaderTest.java
===================================================================
--- cytoscape/trunk/tests/cytoscape/data/readers/CyAttributesReaderTest.java
2009-07-09 08:28:47 UTC (rev 17278)
+++ cytoscape/trunk/tests/cytoscape/data/readers/CyAttributesReaderTest.java
2009-07-09 13:32:12 UTC (rev 17279)
@@ -142,14 +142,11 @@
assertEquals(3.7, value.doubleValue(), 0.01);
}
- /**
- * Testing Reading of Simple Lists.
- * @throws IOException IO Errors.
- */
- public void testReadSimpleLists() throws IOException {
+ private void checkReadSimpleLists(String fileToRead, char sep) throws
IOException
+ {
String attributeName = "GO_molecular_function_level_4";
CyAttributes cyAttributes = new CyAttributesImpl();
- File file = new File("testData/implicitStringArray.attribute");
+ File file = new File(fileToRead);
FileReader reader = new FileReader(file);
CyAttributesReader.loadAttributes(cyAttributes, reader);
@@ -163,18 +160,72 @@
String value = (String) list.get(0);
assertEquals("intracellular", value);
value = (String) list.get(1);
- assertEquals("clathrin adaptor", value);
+ assertEquals("clathrin" + sep + "adaptor", value);
value = (String) list.get(2);
- assertEquals("intracellular transporter", value);
+ assertEquals("intracellular" + sep + "transporter", value);
// Test the Last List
list = cyAttributes.getListAttribute("CDH3", attributeName);
assertEquals(1, list.size());
value = (String) list.get(0);
- assertEquals("cell adhesion molecule", value);
+ assertEquals("cell" + sep + "adhesion" + sep + "molecule",
value);
+ }
+
+ /**
+ * Testing Reading of Simple Lists.
+ * @throws IOException IO Errors.
+ */
+ public void testReadSimpleLists1() throws IOException {
+ System.clearProperty(CyAttributesReader.DECODE_PROPERTY);
+ checkReadSimpleLists("testData/implicitStringArray.attribute", ' ');
}
/**
+ * Testing Reading of Simple Lists.
+ * @throws IOException IO Errors.
+ */
+ public void testReadSimpleLists2() throws IOException {
+ System.clearProperty(CyAttributesReader.DECODE_PROPERTY);
+ checkReadSimpleLists("testData/implicitStringArrayEnc.attribute", ' ');
+ }
+
+ /**
+ * Testing Reading of Simple Lists.
+ * @throws IOException IO Errors.
+ */
+ public void testReadSimpleLists3() throws IOException {
+ System.setProperty(CyAttributesReader.DECODE_PROPERTY, "false");
+ checkReadSimpleLists("testData/implicitStringArray.attribute", ' ');
+ }
+
+ /**
+ * Testing Reading of Simple Lists.
+ * @throws IOException IO Errors.
+ */
+ public void testReadSimpleLists4() throws IOException {
+ System.setProperty(CyAttributesReader.DECODE_PROPERTY, "false");
+ checkReadSimpleLists("testData/implicitStringArrayEnc.attribute", '+');
+ }
+
+ /**
+ * Testing Reading of Simple Lists.
+ * @throws IOException IO Errors.
+ */
+ public void testReadSimpleLists5() throws IOException {
+ System.setProperty(CyAttributesReader.DECODE_PROPERTY, "true");
+ checkReadSimpleLists("testData/implicitStringArray.attribute", ' ');
+ }
+
+ /**
+ * Testing Reading of Simple Lists.
+ * @throws IOException IO Errors.
+ */
+ public void testReadSimpleLists6() throws IOException {
+ System.setProperty(CyAttributesReader.DECODE_PROPERTY, "true");
+ checkReadSimpleLists("testData/implicitStringArrayEnc.attribute", ' ');
+ }
+
+ /**
* Runs just this one unit test.
*/
public static void main(String[] args) {
Modified:
cytoscape/trunk/tests/cytoscape/data/writers/CyAttributesWriterTest.java
===================================================================
--- cytoscape/trunk/tests/cytoscape/data/writers/CyAttributesWriterTest.java
2009-07-09 08:28:47 UTC (rev 17278)
+++ cytoscape/trunk/tests/cytoscape/data/writers/CyAttributesWriterTest.java
2009-07-09 13:32:12 UTC (rev 17279)
@@ -41,13 +41,10 @@
import cytoscape.data.readers.CyAttributesReader;
-import cytoscape.data.writers.CyAttributesWriter;
-
import junit.framework.TestCase;
import java.io.File;
import java.io.FileReader;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
@@ -108,16 +105,12 @@
System.out.println("################## CyAttributesWriter2 Test
end #######################");
}
- /**
- * Tests Writing out of Lists.
- *
- * @throws IOException
- * IO Error.
- */
- public void testWriteSimpleLists() throws IOException {
-
+ private void checkSimpleLists(char sep) throws IOException
+ {
+ StringWriter writer = new StringWriter();
+
System.out.println("################## CyAttributesWriter2 List
Test start #######################");
-
+
CyAttributes cyAttributes = new CyAttributesImpl();
File file = new File("testData/implicitStringArray.attribute");
FileReader reader = new FileReader(file);
@@ -131,7 +124,6 @@
cyAttributes.setListAttribute("ABC_123",
"GO_molecular_function_level_4", list);
- StringWriter writer = new StringWriter();
CyAttributesWriter writer2 = new
CyAttributesWriter(cyAttributes, "GO_molecular_function_level_4", writer);
writer2.writeAttributes();
@@ -143,7 +135,7 @@
System.out.println("Line = " + line);
allLines.add(line);
}
-
+
if(writer != null) {
writer.close();
writer = null;
@@ -156,19 +148,51 @@
assertEquals(allLines.size(), lines.length);
assertTrue(allLines.contains("GO_molecular_function_level_4
(class=java.lang.String)"));
assertTrue(allLines.contains("HSD17B2 =
(membrane::intracellular)"));
- assertTrue(allLines.contains("E2F4 = (DNA binding)"));
- assertTrue(allLines.contains("AP1G1 = (intracellular::clathrin
adaptor::intracellular "
- + "transporter)"));
+ assertTrue(allLines.contains("E2F4 = (DNA" + sep + "binding)"));
+ assertTrue(allLines.contains("AP1G1 = (intracellular::clathrin"
+ sep + "adaptor::intracellular" + sep + "transporter)"));
assertTrue(allLines.contains("ABC_123 =
(Apple::Orange::Banana)"));
- assertTrue(allLines.contains("CDH3 = (cell adhesion
molecule)"));
+ assertTrue(allLines.contains("CDH3 = (cell" + sep + "adhesion"
+ sep + "molecule)"));
allLines = null;
lines = null;
-
+
System.out.println("################## CyAttributesWriter2 List
Test end #######################");
+ }
+
+ /**
+ * Tests Writing out of Lists.
+ *
+ * @throws IOException
+ * IO Error.
+ */
+ public void testWriteSimpleLists1() throws IOException {
+ System.clearProperty(CyAttributesWriter.ENCODE_PROPERTY);
+ checkSimpleLists('+');
}
/**
+ * Tests Writing out of Lists.
+ *
+ * @throws IOException
+ * IO Error.
+ */
+ public void testWriteSimpleLists2() throws IOException {
+ System.setProperty(CyAttributesWriter.ENCODE_PROPERTY, "false");
+ checkSimpleLists(' ');
+ }
+
+ /**
+ * Tests Writing out of Lists.
+ *
+ * @throws IOException
+ * IO Error.
+ */
+ public void testWriteSimpleLists3() throws IOException {
+ System.setProperty(CyAttributesWriter.ENCODE_PROPERTY, "true");
+ checkSimpleLists('+');
+ }
+
+ /**
* Runs just this one unit test.
*/
public static void main(String[] args) {
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---