XStream
  1. XStream
  2. XSTR-17

java.io.File don't get converted correctly

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      When a File gets xstream-ized, it's path attribute is stored. But this is without taking in account that this path might be relative or absolute.

      One solution around this might be to store the boolean returned from the isAbsolute() method, but this doesn't sound safe since the relative root of the File will change if the user app is launched from a different place for instance.

      Another solution would be to use the getAbsolutePath method, instead of the path attribute to store the path of the File. But again, this might cause problems if an application would rely on the fact that the path it uses is relative.

      I suppose both solutions combined could also be an alternative, but it's not completely safe either I think. (Although I can't think of a decent example that would confirm this)

      Another but hacky solution would be to store, if the path is relative, a substring of the absolute path that would represent the relative root to the file. (Hacky in the sense that it would "simply" and dumbly manipulate Strings without being os-specifities-aware..)

      Unfortunately, I don't know of a safe way to solve this correctly. (AFAIK, you can't get the relative root of a path from File)

      Here's a little testcase that demonstrates the issue:

      public class XStreamFileTest extends junit.framework.TestCase {

      public void testFile() throws IOException

      { XStream xstream = new XStream(); // use temp file just to be independent from OS specific directory layout File f = File.createTempFile("blah", ".tmp"); String xml = xstream.toXML(f); File g = (File) xstream.fromXML(xml); assertEquals(f.getPath(), g.getPath()); assertEquals(f.getAbsolutePath(), g.getAbsolutePath()); assertEquals(f, g); }

      }

      I'll provide a patch that fixes the issues by using the getAbsolutePath, which is OS dependent (delegates to FileSystem implementation).

      1. FileAbsolutePathConverter.java
        1.0 kB
        Grégory Joseph (old account)
      2. FileConverter.java
        0.7 kB
        Grégory Joseph (old account)
      3. xstream-fileconverter.patch
        3 kB
        Grégory Joseph (old account)
      4. xstream-fileconverter.patch
        2 kB
        Grégory Joseph (old account)

        People

        • Assignee:
          Unassigned
          Reporter:
          Grégory Joseph (old account)
        • Votes:
          0 Vote for this issue
          Watchers:
          1 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: