KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > xtags > xpath > ValueOfTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
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
17 package org.apache.taglibs.xtags.xpath;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.ServletContext JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29 import org.dom4j.DocumentException;
30 import org.dom4j.Node;
31 import org.dom4j.XPath;
32
33 /** A tag which performs an XPath expression on the current context Node
34   *
35   * @author James Strachan
36   */

37 public class ValueOfTag extends AbstractTag {
38
39     /** Holds value of property xpath. */
40     private XPath xpath;
41     
42
43     public ValueOfTag() {
44     }
45
46     // Tag interface
47
//-------------------------------------------------------------------------
48
public int doStartTag() throws JspException JavaDoc {
49         if ( xpath != null ) {
50             try {
51                 String JavaDoc text = xpath.valueOf( getInputNodes() );
52                 text = encode( text );
53                 pageContext.getOut().print( text );
54             }
55             catch (IOException JavaDoc e) {
56                 handleException(e);
57             }
58         }
59         return SKIP_BODY;
60     }
61
62     public void release() {
63         super.release();
64         xpath = null;
65     }
66
67     // Properties
68
//-------------------------------------------------------------------------
69

70     /** Sets the select XPath expression
71       */

72     public void setSelect(String JavaDoc select) {
73         this.xpath = createXPath( select );
74     }
75
76     /** Sets the XPath selection expression
77       */

78     public void setSelectXPath(XPath xpath) {
79         this.xpath = xpath;
80     }
81
82     
83     // Helper methods
84
//-------------------------------------------------------------------------
85

86     /** Encodes the standard entities in the given string
87      */

88     public static String JavaDoc encode(String JavaDoc text) {
89         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
90         char[] block = text.toCharArray();
91         int size = block.length;
92         int last = 0;
93         for ( int i = 0; i < size; i++) {
94             char ch = block[i];
95             
96             String JavaDoc entity = null;
97             switch ( ch ) {
98                 case '<' :
99                     entity = "&lt;";
100                     break;
101                 case '>' :
102                     entity = "&gt;";
103                     break;
104                 case '&' :
105                     entity = "&amp;";
106                     break;
107                 default :
108                     /* no-op */ ;
109             }
110             if (entity != null) {
111                 buffer.append(block, last, i - last);
112                 buffer.append(entity);
113                 last = i + 1;
114             }
115         }
116         if ( last < size ) {
117             buffer.append(block, last, size - last);
118         }
119         return buffer.toString();
120     }
121 }
122
Popular Tags