Changeset 13910


Ignore:
Timestamp:
Oct 30, 2019 2:21:41 PM (3 weeks ago)
Author:
Jesper Mattsson
Message:

Rewrote BaseNode.cropAbsolutePath() to a reasonable implementation. (#5866)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/ModelicaFlatTree/src/jastadd/HtmlSourcePrettyPrint.jrag

    r13246 r13910  
    11import java.io.File;
     2import java.nio.file.Path;
     3import java.nio.file.Paths;
     4import java.nio.file.InvalidPathException;
    25
    36import beaver.Symbol;
     
    276279    }
    277280   
    278     private static String BaseNode.cropAbsolutePath(String path){
    279         String p = path;
    280         String dir = System.getProperty("user.dir");
    281         try {
    282             if (p.substring(0, dir.lastIndexOf("\\")).equals(dir.substring(0, dir.lastIndexOf("\\")))) {
    283                 p = p.substring(dir.lastIndexOf("\\") + 1);
    284             }
    285         } catch (IndexOutOfBoundsException e) {}
    286         return p;
     281    private static String BaseNode.cropAbsolutePath(String path) {
     282        try {
     283            Path p = Paths.get(path);
     284            if (p.isAbsolute()) {
     285                Path cwd = Paths.get("").toAbsolutePath();
     286                if (p.startsWith(cwd)) {
     287                    p = cwd.relativize(p);
     288                }
     289            }
     290            return p.toString();
     291        } catch (InvalidPathException e) {
     292            // Incoming path is invalid, so we can't really process it
     293            return path;
     294        }
    287295    }
    288296   
Note: See TracChangeset for help on using the changeset viewer.