From 59d7bcdee6eb7b2c10efcb8eddc1912fe9abe958 Mon Sep 17 00:00:00 2001 From: "nor..67" Date: Wed, 10 Aug 2011 11:09:45 +0000 Subject: [PATCH] - globally reduce asset paths in AssetKey constructor so that no key type can produce relative elements in the asset name git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7996 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- engine/src/core/com/jme3/asset/AssetKey.java | 45 ++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/engine/src/core/com/jme3/asset/AssetKey.java b/engine/src/core/com/jme3/asset/AssetKey.java index 690d7a328..e06b0e82e 100644 --- a/engine/src/core/com/jme3/asset/AssetKey.java +++ b/engine/src/core/com/jme3/asset/AssetKey.java @@ -38,6 +38,9 @@ import com.jme3.export.InputCapsule; import com.jme3.export.OutputCapsule; import com.jme3.export.Savable; import java.io.IOException; +import java.util.LinkedList; +import java.util.logging.Level; +import java.util.logging.Logger; /** * AssetKey is a key that is used to @@ -51,8 +54,8 @@ public class AssetKey implements Savable { protected transient String extension; public AssetKey(String name){ - this.name = name; - this.extension = getExtension(name); + this.name = reducePath(name); + this.extension = getExtension(this.name); } public AssetKey(){ @@ -138,6 +141,42 @@ public class AssetKey implements Savable { return false; } + /** + * Removes all relative elements of a path (A/B/../C.png and A/./C.png). + * @param path The path containing relative elements + * @return A path without relative elements + */ + public static String reducePath(String path) { + if (path == null || path.indexOf("./") == -1) { + return path; + } + String[] parts = path.split("/"); + LinkedList list = new LinkedList(); + for (int i = 0; i < parts.length; i++) { + String string = parts[i]; + if (string.length() == 0 || string.equals(".")) { + //do nothing + } else if (string.equals("..")) { + if (list.size() > 0) { + list.removeLast(); + } else { + throw new IllegalStateException("Relative path is outside assetmanager root!"); + } + } else { + list.add(string); + } + } + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < list.size(); i++) { + String string = list.get(i); + if (i != 0) { + builder.append("/"); + } + builder.append(string); + } + return builder.toString(); + } + @Override public boolean equals(Object other){ if (!(other instanceof AssetKey)){ @@ -163,7 +202,7 @@ public class AssetKey implements Savable { public void read(JmeImporter im) throws IOException { InputCapsule ic = im.getCapsule(this); - name = ic.readString("name", null); + name = reducePath(ic.readString("name", null)); extension = getExtension(name); }