13d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby/* 23d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Copyright (C) 2014 The Android Open Source Project 33d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 43d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Licensed under the Apache License, Version 2.0 (the "License"); 53d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * you may not use this file except in compliance with the License. 63d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * You may obtain a copy of the License at 73d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 83d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * http://www.apache.org/licenses/LICENSE-2.0 93d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 103d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Unless required by applicable law or agreed to in writing, software 113d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * distributed under the License is distributed on an "AS IS" BASIS, 123d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * See the License for the specific language governing permissions and 143d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * limitations under the License. 153d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 163d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 173d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hambypackage com.android.omadm.plugin; 183d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 193d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hambyimport android.text.TextUtils; 203d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 213d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hambyimport java.util.Arrays; 223d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 233d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hambypublic final class DmtPathUtils { 243d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 253d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static final String ROOTNODE = "__ROOT__"; 263d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 273d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby private DmtPathUtils() {} 283d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 293d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby /** 303d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Checks whether the given path is valid or not. 313d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 323d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param path full path. 333d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @return true if this is valid path, otherwise false. 343d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 353d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static boolean isValidPath(String path) { 363d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return (!TextUtils.isEmpty(path)) && (".".equals(path) || path.startsWith("./")); 373d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 383d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 393d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby /** 403d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Parses path and returns name of the end node. 413d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 423d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param nodePath path to the node. 433d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @return node name or null in bad case. 443d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 453d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static String getNodeName(String nodePath) { 463d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby String[] data = splitPath(nodePath); 473d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data[1]; 483d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 493d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 503d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby /** 513d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Parses passed path and returns name of the first node 523d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * after root path. 533d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 543d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param rootPath root path. 553d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param nodePath full path to the node. 563d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @return name of the node or null in bad case. 573d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 583d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static String getSubNodeName(String rootPath, String nodePath) { 593d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (!isValidPath(rootPath) || !isValidPath(nodePath)) { 603d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return null; 613d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 623d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 633d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby String prefix = rootPath + '/'; 643d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 653d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (!nodePath.startsWith(prefix)) { 663d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return null; 673d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 683d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 693d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby int nameEnd = nodePath.indexOf('/', prefix.length()); 703d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (nameEnd == -1) { 713d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return nodePath.substring(prefix.length()); 723d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 733d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return nodePath.substring(prefix.length(), nameEnd); 743d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 753d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 763d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby /** 773d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Parses path and returns name of the end node and its root path. 783d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 793d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param nodePath path to the node. 803d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @return root path (String[0]) and name node (String[1]). 813d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 823d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static String[] splitPath(String nodePath) { 833d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby String[] data = new String[2]; 843d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby Arrays.fill(data, null); 853d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 863d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (!isValidPath(nodePath)) { 873d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data; 883d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 893d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 903d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby int index = nodePath.lastIndexOf('/'); 913d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (index == -1) { 923d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby data[1] = nodePath; 933d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data; 943d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 953d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 963d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (nodePath.length() == 1) { 973d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data; 983d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 993d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1003d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (index == 0) { 1013d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby data[1] = nodePath.substring(1); 1023d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data; 1033d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1043d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1053d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (index == nodePath.length() - 1) { 1063d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby data[0] = nodePath.substring(0, index); 1073d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data; 1083d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1093d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1103d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby data[0] = nodePath.substring(0, index); 1113d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby data[1] = nodePath.substring(index + 1); 1123d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1133d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return data; 1143d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1153d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1163d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby /** 1173d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * Checks whether the root path is a part of the node path. 1183d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * 1193d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param rootPath root path. 1203d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @param nodePath path to some node. 1213d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * @return true if these paths are equal or nodePath starts with rootPath. 1223d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 1233d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static boolean isSubPath(String rootPath, String nodePath) { 1243d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (!isValidPath(rootPath) || !isValidPath(nodePath)) { 1253d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return false; 1263d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1273d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1283d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (nodePath.equals(rootPath)) { 1293d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return true; 1303d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1313d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1323d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return nodePath.startsWith(rootPath + '/'); 1333d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1343d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1353d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static String toRelativePath(String rootPath, String path) { 1363d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby String r = path; 1373d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1383d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby // deal with the root path of plugin tree 1393d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (rootPath.equals(path) || path.isEmpty()) { 1403d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby r = ROOTNODE; 1413d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1423d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby else if (path.startsWith(rootPath)) { 1433d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby r = path.substring(rootPath.length() + 1); 1443d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1453d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby// Log.i(TAG, "'" + path + "' -> '" + r + "'"); 1463d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return r; 1473d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1483d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby 1493d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby /** 1503d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby * convert to the absolute path 1513d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby */ 1523d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby public static String toAbsolutePath(String rootPath, String path) { 1533d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby String a = path; 1543d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby if (path.isEmpty() || path.equals(ROOTNODE)) { 1553d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby a = rootPath; 1563d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1573d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby else if (!path.startsWith(rootPath)) { 1583d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby a = rootPath + '/' + path; 1593d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1603d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby// Log.i(TAG, "'" + path + "' -> '" + a + "'"); 1613d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby return a; 1623d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby } 1633d91e7ce47853dc4e6ec7e1fc675c3d1585e3c51Jake Hamby} 164