Package org.telluriumsource.trump

Source Code of org.telluriumsource.trump.XPathMatcher

package org.telluriumsource.trump;

import org.telluriumsource.crosscut.i18n.ResourceBundle;
import org.telluriumsource.crosscut.i18n.IResourceBundle;


/**
* Match two XPaths and return the common portion
*
* @author Jian Fang (John.Jian.Fang@gmail.com)
*
*         Date: Dec 10, 2008
*/
public class XPathMatcher {
    public final static String DELIMITER = "/";

    public static String match(String src, String dst){
        if(src == null || dst == null)
            return null;

        //try to find the shorter one from the two xpaths
        int minlen = src.split(DELIMITER).length;
        String shorter = src;
        String longer = dst;
        int longlen = dst.split(DELIMITER).length;
        if(minlen > longlen){
            minlen = longlen;
            shorter = dst;
            longer = src;
        }

        String[] shortsplit = shorter.split(DELIMITER);
        String[] longsplit = longer.split(DELIMITER);
        StringBuffer sb = new StringBuffer(minlen);

        for(int i=0; i<minlen; i++){
            if(shortsplit[i].equals(longsplit[i])){
                //match
                if(i>0)
                    sb.append(DELIMITER);
                sb.append(shortsplit[i]);
            }else
                //not match
                break;
        }

        return sb.toString();
    }

    public static String remainingXPath(String original, String prefix){
        if(original == null || prefix == null)
            return original;
        if(original.startsWith(prefix)){
            return original.substring(prefix.length());
        }
        IResourceBundle i18nBundle = new ResourceBundle();
        throw new RuntimeException(i18nBundle.getMessage("XPathMatcher.RunTimeException" , new Object[] {original , prefix}));
    }
}
TOP

Related Classes of org.telluriumsource.trump.XPathMatcher

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.