KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > util > ConfigXPathHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.util;
25
26 //import org.w3c.dom.*;
27
//import org.xml.sax.*;
28

29 //import java.util.logging.Logger;
30
//import java.util.logging.Level;
31
//import com.sun.logging.LogDomains;
32

33
34 /**
35  * Helper methods are provided to generate valid xpaths
36  * that are needed by config api.
37  */

38 public final class ConfigXPathHelper {
39     public static final String JavaDoc XPATH_SEPARATOR = "/";
40     
41     /**
42      * @param childTagName
43      * @param nameId
44      * @param valueId
45      *
46      */

47     public static String JavaDoc getAbsoluteIdXpathExpression(String JavaDoc childTagName, String JavaDoc nameId, String JavaDoc valueId) {
48         if(childTagName.startsWith(XPATH_SEPARATOR))
49             return childTagName + "[@" + nameId + "='" + valueId + "']";
50         else
51             return XPATH_SEPARATOR + childTagName + "[@" + nameId + "='" + valueId + "']";
52     }
53
54     private static char SEPARATOR_CHAR = '/';
55     private static char OPENBRACKET_CHAR = '[';
56     private static char CLOSEBRACKET_CHAR = ']';
57     private static char ESCAPE_CHAR = '\\';
58   
59     /*
60      * returns element's tag name extracting from its xpath
61      */

62     public static String JavaDoc getLastNodeName(String JavaDoc xPath) {
63         //FIXME: it is supposed that open bracket should be escaped if its part of name or value
64
// FIXME no error handling
65

66         int idx = xPath.length()-1;
67         char ch;
68         int idxEnd = -1;
69         if(idx>=0 && (ch=xPath.charAt(idx))==CLOSEBRACKET_CHAR) {
70             idxEnd = bypassBrackets(xPath, --idx);
71             idx = idxEnd;
72         }
73      
74         while(idx>=0 && ((ch=xPath.charAt(idx))!=SEPARATOR_CHAR || isEscapedChar(xPath,idx))) {
75                 idx--;
76         }
77         idx++;
78         if(idxEnd<=0 || idxEnd==xPath.length()-1)
79             return xPath.substring(idx);
80         else
81             return xPath.substring(idx, idxEnd+1);
82     }
83     
84     /*
85      * returns parent XPath for givend child's xpath
86      * correcly bypasses bracketed values with possible escaping inside of quoted values
87      */

88     public static String JavaDoc getParentXPath(String JavaDoc xPath) {
89         //FIXME: it is supposed that open bracket should be escaped if its part of name or value
90
// FIXME no error handling
91

92         int idx = xPath.length()-1;
93         char ch;
94         if(idx>=0 && (ch=xPath.charAt(idx))==CLOSEBRACKET_CHAR) {
95             idx = bypassBrackets(xPath, --idx);
96         }
97      
98         while(idx>=0 && ((ch=xPath.charAt(idx))!=SEPARATOR_CHAR || isEscapedChar(xPath,idx))) {
99                 idx--;
100         }
101         if(idx<=0)
102             return "/";
103         return xPath.substring(0, idx);
104     }
105     
106     private static int bypassBrackets(String JavaDoc xPath, int idx) {
107         char ch;
108         while(idx>=0 && ((ch=xPath.charAt(idx))!=OPENBRACKET_CHAR || isEscapedChar(xPath,idx))) {
109             idx--;
110         }
111         return idx-1;
112     }
113     private static boolean isEscapedChar(String JavaDoc xPath, int idx) {
114         if(idx<=0)
115             return false;
116         int count = 0;
117         while(--idx>=0 && xPath.charAt(idx)==ESCAPE_CHAR)
118             count++;
119         return ((count%2)==1);
120     }
121 }
122
Popular Tags