diff --git a/model/src/main/java/one/nem/lacerta/model/PublicPath.java b/model/src/main/java/one/nem/lacerta/model/PublicPath.java new file mode 100644 index 00000000..980a3a9d --- /dev/null +++ b/model/src/main/java/one/nem/lacerta/model/PublicPath.java @@ -0,0 +1,68 @@ +package one.nem.lacerta.model; + +import java.util.ArrayList; +import java.util.List; + +public class PublicPath { + /* + * ユーザーが扱うパス(内部パスの代替) + * (時間がないのでInjectされることは考慮しない) + * + * TODO-rca: + * - こわれたパスを検知する + * - バリデーション + */ + + List path = new ArrayList(); + + public PublicPath() { + } + + public PublicPath(List path) { + this.path = path; + } + + private void add(String path) { + this.path.add(path); + } + + private void resolveInternal(String path) { + if (path.equals("..")) { + this.path.remove(this.path.size() - 1); + } else { + add(path); + } + } + + public PublicPath resolve(String path) { + resolveInternal(path); + return this; + } + + public PublicPath resolve(List path) { + for (String p : path) { + resolveInternal(p); + } + return this; + } + + public PublicPath resolve(PublicPath path) { + for (String p : path.getPath()) { + resolveInternal(p); + } + return this; + } + + public PublicPath parent() { + this.path.remove(this.path.size() - 1); + return this; + } + + public List getPath() { + return path; + } + + public String getStringPath() { + return String.join("/", path); + } +}