1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.omadm.plugin; 18 19import android.text.TextUtils; 20 21import java.util.Arrays; 22 23public final class DmtPathUtils { 24 25 public static final String ROOTNODE = "__ROOT__"; 26 27 private DmtPathUtils() {} 28 29 /** 30 * Checks whether the given path is valid or not. 31 * 32 * @param path full path. 33 * @return true if this is valid path, otherwise false. 34 */ 35 public static boolean isValidPath(String path) { 36 return (!TextUtils.isEmpty(path)) && (".".equals(path) || path.startsWith("./")); 37 } 38 39 /** 40 * Parses path and returns name of the end node. 41 * 42 * @param nodePath path to the node. 43 * @return node name or null in bad case. 44 */ 45 public static String getNodeName(String nodePath) { 46 String[] data = splitPath(nodePath); 47 return data[1]; 48 } 49 50 /** 51 * Parses passed path and returns name of the first node 52 * after root path. 53 * 54 * @param rootPath root path. 55 * @param nodePath full path to the node. 56 * @return name of the node or null in bad case. 57 */ 58 public static String getSubNodeName(String rootPath, String nodePath) { 59 if (!isValidPath(rootPath) || !isValidPath(nodePath)) { 60 return null; 61 } 62 63 String prefix = rootPath + '/'; 64 65 if (!nodePath.startsWith(prefix)) { 66 return null; 67 } 68 69 int nameEnd = nodePath.indexOf('/', prefix.length()); 70 if (nameEnd == -1) { 71 return nodePath.substring(prefix.length()); 72 } 73 return nodePath.substring(prefix.length(), nameEnd); 74 } 75 76 /** 77 * Parses path and returns name of the end node and its root path. 78 * 79 * @param nodePath path to the node. 80 * @return root path (String[0]) and name node (String[1]). 81 */ 82 public static String[] splitPath(String nodePath) { 83 String[] data = new String[2]; 84 Arrays.fill(data, null); 85 86 if (!isValidPath(nodePath)) { 87 return data; 88 } 89 90 int index = nodePath.lastIndexOf('/'); 91 if (index == -1) { 92 data[1] = nodePath; 93 return data; 94 } 95 96 if (nodePath.length() == 1) { 97 return data; 98 } 99 100 if (index == 0) { 101 data[1] = nodePath.substring(1); 102 return data; 103 } 104 105 if (index == nodePath.length() - 1) { 106 data[0] = nodePath.substring(0, index); 107 return data; 108 } 109 110 data[0] = nodePath.substring(0, index); 111 data[1] = nodePath.substring(index + 1); 112 113 return data; 114 } 115 116 /** 117 * Checks whether the root path is a part of the node path. 118 * 119 * @param rootPath root path. 120 * @param nodePath path to some node. 121 * @return true if these paths are equal or nodePath starts with rootPath. 122 */ 123 public static boolean isSubPath(String rootPath, String nodePath) { 124 if (!isValidPath(rootPath) || !isValidPath(nodePath)) { 125 return false; 126 } 127 128 if (nodePath.equals(rootPath)) { 129 return true; 130 } 131 132 return nodePath.startsWith(rootPath + '/'); 133 } 134 135 public static String toRelativePath(String rootPath, String path) { 136 String r = path; 137 138 // deal with the root path of plugin tree 139 if (rootPath.equals(path) || path.isEmpty()) { 140 r = ROOTNODE; 141 } 142 else if (path.startsWith(rootPath)) { 143 r = path.substring(rootPath.length() + 1); 144 } 145// Log.i(TAG, "'" + path + "' -> '" + r + "'"); 146 return r; 147 } 148 149 /** 150 * convert to the absolute path 151 */ 152 public static String toAbsolutePath(String rootPath, String path) { 153 String a = path; 154 if (path.isEmpty() || path.equals(ROOTNODE)) { 155 a = rootPath; 156 } 157 else if (!path.startsWith(rootPath)) { 158 a = rootPath + '/' + path; 159 } 160// Log.i(TAG, "'" + path + "' -> '" + a + "'"); 161 return a; 162 } 163} 164