FileDocCategorySizeDatePackage
XPathHelper.javaAPI DocGlassfish v2 API16770Fri May 04 22:24:38 BST 2007com.sun.enterprise.config.serverbeans.validation

XPathHelper

public abstract class XPathHelper extends Object
This class provides helper methods for xpath handling in ConfigBeans space $Id: XPathHelper.java,v 1.4 2007/05/05 05:24:37 tcfujii Exp $
author
alexkrav $Log: XPathHelper.java,v $ Revision 1.4 2007/05/05 05:24:37 tcfujii CDDL+GPL header updates. Revision 1.3 2005/12/25 03:44:26 tcfujii Updated copyright text and year. Revision 1.2 2005/06/27 21:18:17 tcfujii Issue number: CDDL header updates. Revision 1.1 2005/06/17 17:09:58 kravtch MS3: Config Validator's infrastructure changes: - new "VALIDATE" ConfigContextEvent type introduced for "static" validation; - host "static" validation as ConfigContext listener for "VALIDATE" events; - new namespace "http://www.sun.com/ias/validation" support for RelaxNG schemas extension: - "name-domains" metadata support for test uniquiness/references; - new "belongs-to"/"references-to"/"type"/"**-than" schema attributes; - most rng element definitions and xslt scripts changed; - new base validation classes to perform generic validation; - custom validation test classes cleaned from performing generic validation cases; - ConfigBeans and domain dtd re-generated; - Schematron(scripts and validation) and Relax NG (validator only) not used for validation any more; Submitted by: kravtch Tests passed QLT/EE + devtests Modules affected: appserver-commons; admin/validator; admin-core/config-api

Fields Summary
Constructors Summary
Methods Summary
private static voidaddAttributeToList(java.lang.String attrName, com.sun.enterprise.config.ConfigBean cb, javax.management.AttributeList list, java.lang.String xpath)

        String attrValue = getBeanAttributeValue(cb, attrName);
        if(attrValue!=null)
        {
//System.out.println("   --------->>>addAttributeToList: "+xpath+"/@"+attrName + "=" + attrValue );        
            list.add(new Attribute(xpath+"/@"+attrName, attrValue)); 
        }
    
public static java.lang.StringconvertName(java.lang.String name)
Convert a DTD name into a bean name: Any - or _ character is removed. The letter following - and _ is changed to be upper-case. If the user mixes upper-case and lower-case, the case is not changed. If the Word is entirely in upper-case, the word is changed to lower-case (except the characters following - and _) The first letter is always upper-case.

        CharacterIterator  ci;
        StringBuffer    n = new StringBuffer();
        boolean    up = true;
        boolean    keepCase = false;
        char    c;
        
        // hack need for schema2beans limitation.
        if(name.equals("property")) {
            name = "element-property";
        }
        
        ci = new StringCharacterIterator(name);
        c = ci.first();
        
        // If everything is uppercase, we'll lowercase the name.
        while (c != CharacterIterator.DONE) {
            if (Character.isLowerCase(c)) {
                keepCase = true;
                break;
            }
            c = ci.next();
        }
        
        c = ci.first();
        while (c != CharacterIterator.DONE) {
            if (c == '-" || c == '_")
                up = true;
            else {
                if (up)
                    c = Character.toUpperCase(c);
                else
                    if (!keepCase)
                        c = Character.toLowerCase(c);
                n.append(c);
                up = false;
            }
            c = ci.next();
        }
        return n.toString();
    
public static java.lang.String[]extractTokens(java.lang.String xpath)

        ArrayList arr = new ArrayList();
        int[] cuts =  getElemDelimitersPositions(xpath);
        int iTokenPos = 0;
        for(int i=0; i<cuts.length; i++)
        {
            if(iTokenPos==cuts[i]) //empty element
            {
                arr.add("");
            }
            else
            {
                arr.add(xpath.substring(iTokenPos, cuts[i]));
            }
            iTokenPos =  cuts[i] +1;
        }
        if(iTokenPos<xpath.length()-1)
        {
            arr.add(xpath.substring(iTokenPos));
        }
        return (String[])arr.toArray(new String[arr.size()]);
    
public static java.lang.StringgetBeanAttributeValue(com.sun.enterprise.config.ConfigBean cb, java.lang.String name)

        try{
            return cb.getAttributeValue(convertName(name));
        } catch(Throwable t){
            return null;
        }
    
public static int[]getElemDelimitersPositions(java.lang.String xpath)

         return getElemDelimitersPositions(xpath, -1);
    
public static int[]getElemDelimitersPositions(java.lang.String xpath, int maxDeep)

        int[] arr = new int[100];
        int length = 0;
        char[] buf = xpath.toCharArray();
        int i=0;
        int iNext;
        while(maxDeep!=0 && (iNext=getNextElemDelimiterPos(buf, i))>=0)
        {
            //add token boundary to list
            arr[length++] = iNext; 
            maxDeep--;
            i = iNext+1;    
        }
        int[] ret_arr = new int[length];
        for(i=0; i<length; i++)
            ret_arr[i] = arr[i];
        return ret_arr;
    
private static com.sun.enterprise.config.ConfigBeangetNextBean(com.sun.enterprise.config.ConfigBean cb, java.lang.String name)

        try{
            if("..".equals(name))
               return (ConfigBean)cb.parent();
            return (ConfigBean)cb.getValue(convertName(name));
        } catch(Throwable t){
            return null;
        }
    
private static com.sun.enterprise.config.ConfigBean[]getNextBeans(com.sun.enterprise.config.ConfigBean cb, java.lang.String name)

        try{
            if("..".equals(name))
            {
               ConfigBean parent = (ConfigBean)cb.parent();
               if(parent==null)
                   return null;
               ConfigBean grand = (ConfigBean)parent.parent();
               if(grand==null)
                   return new ConfigBean[]{parent};
               return (ConfigBean[])grand.getValue(parent.name());
            }
            return (ConfigBean[])cb.getValues(convertName(name));
        } catch(Throwable t){
            return null;
        }
    
private static intgetNextElemDelimiterPos(char[] buf, int iFrom)

        int i = iFrom;
        while(i>=0 && i<buf.length)
        {
            if(buf[i]=='/")
                return i;
            if(buf[i]=='\'" || buf[i]=='"")
            {
                i = indexOf(buf, i+1, buf[i]); //in suggestion that there is no similar symbols inside 
                if(i>0)
                   i++;
                continue;
            }
            i++;
        }
        return -1;
    
public static intgetNumberOfElemTokens(java.lang.String xpath)
This method returns number of elements in given xpath (depth)

        int[] cuts =  getElemDelimitersPositions(xpath);
        int iTokens = cuts.length;
        if(iTokens>0 && cuts[0]==0)
            iTokens--;
        if(cuts.length>0 && cuts[cuts.length-1]<xpath.length()-1)
           iTokens++;
        return iTokens;
    
public static java.lang.StringgetXPathPrefix(java.lang.String xpath, int numberOfTokensInPrefix)

        if(numberOfTokensInPrefix<=0)
            return "";
        int[] cuts =  getElemDelimitersPositions(xpath);
        int delimIdx = numberOfTokensInPrefix;
//System.out.println("$$getXPathPrefix() xpath="+xpath + "   numberOfTokensInPrefix=" + numberOfTokensInPrefix);
        if(cuts.length>0 && cuts[0]!=0)
            delimIdx--;
        if(delimIdx<cuts.length)
            return xpath.substring(0, cuts[delimIdx]);
        if(delimIdx==cuts.length && cuts[cuts.length-1]<xpath.length()-1)
            return xpath;
        return "";
    
private static intindexOf(char[] buf, int iFrom, char chr)

        for(int i=iFrom; i<buf.length; i++)
        {
            if(buf[i]==chr)
                return i;
        }
        return -1;
    
public static booleanisAbsoluteXPath(java.lang.String xpath)

        return (xpath!=null && xpath.startsWith("/"));
    
private static voidnavigate(com.sun.enterprise.config.ConfigBean cb, java.lang.String[] tokens, int iCurrentToken, java.lang.String prefixFilter, javax.management.AttributeList list)

        if(cb==null || tokens==null || tokens.length<=iCurrentToken)
            return;
        String xpath = cb.getXPath();
        if(xpath==null)
            return;
        
        //stop navigation according to prefix filter
        if(prefixFilter!=null && prefixFilter.length()>0)
        {  
            if(xpath.length()>=prefixFilter.length())
            {
                if(!xpath.startsWith(prefixFilter))
                    return;
            }
            else
            {
                if(!prefixFilter.startsWith(xpath))
                    return;
            }
        }
        
//System.out.println( "###>>navigate iCurrentToken=" + iCurrentToken + " cb=" + xpath );        
        //*********************
        //1. Maybe this is it
        if(iCurrentToken==tokens.length-1)
        {
            list.add(new Attribute(xpath, cb));
            return;
        }
        
        //goto the next token
        int iNextToken = iCurrentToken+1;
        String nextToken = tokens[iNextToken];
        //*********************
        //2. Maybe next token is attribute name
        if(iNextToken==(tokens.length-1) && nextToken.startsWith("@"))
        {
//            try {
                if("@*".equals(nextToken)) //all names
                {
                    String[] names = cb.getAttributeNames();
                    if(names.length>0)
                        for(int i=0; i<names.length; i++)
                            addAttributeToList(names[i], cb, list, xpath);
                }
                else //only one attrname
                    addAttributeToList(nextToken.substring(1), cb, list, xpath);
//            } catch (ConfigException ce) {
//            }
            return;     
        }
        //*********************************************
        //3. what if "//"-wildcard or "*" (all children)
        if(nextToken.length()==0 ||
           "*".equals(nextToken))
        {
            ConfigBean[] children = cb.getAllChildBeans();
            if(children!=null)
                for(int i=0; i<children.length; i++)
                {
                    if(nextToken.length()==0)
                        navigate(children[i], tokens, iCurrentToken , prefixFilter, list);
                    
                    navigate(children[i], tokens, iNextToken , prefixFilter, list);
                }
            return;
        }
        
        //************************************************
        //4. looks like new element. First, non-indexed
        if(!nextToken.endsWith("]"))
        {
            ConfigBean cbNext = getNextBean(cb, nextToken);
            if(cbNext!=null)
                navigate((ConfigBean)cbNext, tokens, iNextToken , prefixFilter, list);
            return;
        }
        
        //get index.
        int indexStartPos = nextToken.indexOf("[");
        String indexStr = nextToken.substring(indexStartPos+1, nextToken.length()-1);
        String baseName = nextToken.substring(0, indexStartPos);

        //************************************************
        //5. [*]?
        if("*".equals(indexStr)) //all of kind
        {
            ConfigBean[] beans = getNextBeans(cb, baseName);
            if(beans!=null)
            {
                for(int i=0; i<beans.length; i++)
                {
                    navigate(beans[i], tokens, iNextToken , prefixFilter, list);
                }
            }
            return;
        }
        
        //************************************************
        //5. [@xxx='yyy']?
        if(indexStr.startsWith("@") && indexStr.startsWith("'")) //all of kind
        {
            String attrName  = indexStr.substring(1, indexStr.indexOf('="));
            String attrValue = indexStr.substring(indexStr.indexOf("='")+2, indexStr.length()-2);
            ConfigBean[] beans = getNextBeans(cb, baseName);
            if(beans!=null)
            {
                for(int i=0; i<beans.length; i++)
                {
                    String value = getBeanAttributeValue(beans[i], convertName(attrName));
                    if(value==null)
                        continue;
                    if(attrValue.equals("*") || attrValue.equals(value))
                        navigate(beans[i], tokens, iNextToken , prefixFilter, list);
                }
            }
            return;
        }
        //6 [number] not yet
    
static javax.management.AttributeListresolve(com.sun.enterprise.config.ConfigContext ctx, java.util.ArrayList xpathes, java.lang.String onlyPrefix)

        AttributeList merge_list = null;
        if(xpathes!=null)
        {
            merge_list = new AttributeList();
            for(int i=0; i<xpathes.size(); i++)
            {
                AttributeList list = resolve(ctx, (String)xpathes.get(i), onlyPrefix);
                if(list!=null)
                    merge_list.addAll(list);
            }
        }
        return merge_list;
    
static javax.management.AttributeListresolve(com.sun.enterprise.config.ConfigContext ctx, java.lang.String xpath, java.lang.String prefixFilter)

        if(xpath==null)
            return null;
        String[] elems  = extractTokens(xpath);
        if(elems.length==0 || elems[0].length()!=0)           
           return null;
        ConfigBean cb = null;
        try {
            cb = ctx.getRootConfigBean();
        } catch (ConfigException ce){
        }
        AttributeList list = new AttributeList();
        navigate(cb, elems, 1, prefixFilter, list);
        return list;