
/*
 Copyright 2012-2013, Polyvi Inc. (http://polyvi.github.io/openxface)
 This program is distributed under the terms of the GNU General Public License.

 This file is part of xFace.

 xFace is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 xFace is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with xFace.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.polyvi.xface.util;

import java.io.File;
import java.io.IOException;
import org.apache.cordova.CordovaResourceApi;

import android.net.Uri;
/**
 * 把传入的路径解析为对应的相对于app workspace的路径、sdcard路径或其他绝对路径
 */
public class XPathResolver {
    private String mPath;
    private String mAppWorkspace;

    /**
     * @param path
     *            需要解析的路径
     * @param appWorkspace
     *            app的workspace
     */
    public XPathResolver(String path, String appWorkspace) {
        mPath = path;
        mAppWorkspace = appWorkspace;
    }

    /**
     * 解析路径
     *
     * @param resourceApi
     * @return 路径合法就返回在当前系统中对应的路径，否则就返回null。
     *         路径不合法可能是因为以下原因之一：
     *         1、file://...uri不符合格式或主机不存在，目前只有sdcard host和默认的local host
     *         2、sdcard不可访问
     *         3、相对app workspace的路径不在app workspace之内。
     */
    public String resolve(CordovaResourceApi resourceApi) {
        // 根据协议解析路径
        if (null == mPath) {
            return null;
        }
        Uri uri = Uri.parse(mPath);
        if (null != uri.getScheme()) {
            return resourceApi.remapUri(uri).getPath();
        } else if (mPath.startsWith("/")) {
            // 全路径形式，例如：/mnt/sdcard/mypath
            return mPath;
        } else {
            // 相对app workspace的路径，无'/'开头
            return resolveAppRelativePath(mPath);
        }
    }

    /**
     * 解析相对 app workspace 的路径
     *
     * @param path
     *            要解析的路径
     * @return 路径在app workspace之内，则返回该路径的绝对路径，否则返回null。
     */
    private String resolveAppRelativePath(String path) {
        File file = new File(mAppWorkspace, path);
        String absolutePath = null;
        try {
            absolutePath = file.getCanonicalPath();
            // 路径是否在app workspace 之内
            if (!XFileUtils.isFileAncestorOf(mAppWorkspace, absolutePath)) {
                absolutePath = null;
            }
        } catch (IOException e) {
            absolutePath = null;
            e.printStackTrace();
        }
        return absolutePath;
    }

}

@@ -935,16 +935,17 @@
 et.Uri;%0A
+%0A
 /**%0A * %E6%8A%8A
@@ -1430,31 +1430,16 @@
 %E5%B0%B1%E8%BF%94%E5%9B%9Enull%E3%80%82
-%0A     *        
  %E8%B7%AF%E5%BE%84%E4%B8%8D%E5%90%88%E6%B3%95%E5%8F%AF%E8%83%BD
@@ -1548,31 +1548,16 @@
 card%E4%B8%8D%E5%8F%AF%E8%AE%BF%E9%97%AE
-%0A     *        
  3%E3%80%81%E7%9B%B8%E5%AF%B9app
@@ -2101,32 +2101,678 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * %E8%8E%B7%E5%8F%96Uri%0A     *%0A     * @param resourceApi%0A     * @return%0A     */%0A    public Uri getUri(CordovaResourceApi resourceApi) %7B%0A        if (null == mPath) %7B%0A            return null;%0A        %7D%0A        Uri uri = Uri.parse(mPath);%0A        if (null != uri.getScheme()) %7B%0A            return resourceApi.remapUri(uri);%0A        %7D else if (mPath.startsWith(%22/%22)) %7B%0A            // %E5%85%A8%E8%B7%AF%E5%BE%84%E5%BD%A2%E5%BC%8F%EF%BC%8C%E4%BE%8B%E5%A6%82%EF%BC%9A/mnt/sdcard/mypath%0A            return Uri.parse(XConstant.FILE_SCHEME + mPath);%0A        %7D else %7B%0A            // %E7%9B%B8%E5%AF%B9app workspace%E7%9A%84%E8%B7%AF%E5%BE%84%EF%BC%8C%E6%97%A0'/'%E5%BC%80%E5%A4%B4%0A            return Uri.parse(XConstant.FILE_SCHEME%0A                    + resolveAppRelativePath(mPath));%0A        %7D%0A    %7D%0A%0A
     /**%0A     * %E8%A7%A3

Add getUri method in XPathResolver