KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > string > util > XmlW


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 // Copied from GenerationJava Core Library.
17
//package com.generationjava.web;
18

19 package org.apache.taglibs.string.util;
20
21 import org.apache.commons.lang.StringUtils;
22
23 /**
24  * XML helping static methods.
25  *
26  * @author bayard@generationjava.com
27  * @version 0.4 20010812
28  */

29 final public class XmlW {
30
31     static public String JavaDoc escapeXml(String JavaDoc str) {
32         str = StringUtils.replace(str,"&","&");
33         str = StringUtils.replace(str,"<","&lt;");
34         str = StringUtils.replace(str,">","&gt;");
35         str = StringUtils.replace(str,"\"","&quot;");
36         str = StringUtils.replace(str,"'","&apos;");
37         return str;
38     }
39
40     static public String JavaDoc unescapeXml(String JavaDoc str) {
41         str = StringUtils.replace(str,"&amp;","&");
42         str = StringUtils.replace(str,"&lt;","<");
43         str = StringUtils.replace(str,"&gt;",">");
44         str = StringUtils.replace(str,"&quot;","\"");
45         str = StringUtils.replace(str,"&apos;","'");
46         return str;
47     }
48
49     /**
50      * Remove any xml tags from a String.
51      * Same as HtmlW's method.
52      */

53     static public String JavaDoc removeXml(String JavaDoc str) {
54         int sz = str.length();
55         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(sz);
56         boolean inString = false;
57         boolean inTag = false;
58         for(int i=0; i<sz; i++) {
59             char ch = str.charAt(i);
60             if(ch == '<') {
61                 inTag = true;
62             } else
63             if(ch == '>') {
64                 inTag = false;
65                 continue;
66             }
67             if(!inTag) {
68                 buffer.append(ch);
69             }
70         }
71         return buffer.toString();
72     }
73
74     static public String JavaDoc getContent(String JavaDoc tag, String JavaDoc text) {
75         int idx = XmlW.getIndexOpeningTag(tag, text);
76         if(idx == -1) {
77             return "";
78         }
79         text = text.substring(idx);
80         int end = XmlW.getIndexClosingTag(tag, text);
81         idx = text.indexOf('>');
82         if(idx == -1) {
83             return "";
84         }
85         return text.substring(idx+1, end);
86     }
87
88     static public int getIndexOpeningTag(String JavaDoc tag, String JavaDoc text) {
89         return getIndexOpeningTag(tag, text, 0);
90     }
91     static private int getIndexOpeningTag(String JavaDoc tag, String JavaDoc text, int start) {
92         // consider whitespace?
93
int idx = text.indexOf("<"+tag, start);
94         if(idx == -1) {
95             return -1;
96         }
97         char next = text.charAt(idx+1+tag.length());
98         if( (next == '>') || Character.isWhitespace(next) ) {
99             return idx;
100         } else {
101             return getIndexOpeningTag(tag, text, idx+1);
102         }
103     }
104
105     // Pass in "para" and a string that starts with
106
// <para> and it will return the index of the matching </para>
107
// It assumes well-formed xml. Or well enough.
108
static public int getIndexClosingTag(String JavaDoc tag, String JavaDoc text) {
109         return getIndexClosingTag(tag, text, 0);
110     }
111     static public int getIndexClosingTag(String JavaDoc tag, String JavaDoc text, int start) {
112         String JavaDoc open = "<"+tag;
113         String JavaDoc close = "</"+tag+">";
114 // System.err.println("OPEN: "+open);
115
// System.err.println("CLOSE: "+close);
116
int closeSz = close.length();
117         int nextCloseIdx = text.indexOf(close, start);
118 // System.err.println("first close: "+nextCloseIdx);
119
if(nextCloseIdx == -1) {
120             return -1;
121         }
122         int count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open);
123 // System.err.println("count: "+count);
124
if(count == 0) {
125             return -1; // tag is never opened
126
}
127         int expected = 1;
128         while(count != expected) {
129             nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz);
130             if(nextCloseIdx == -1) {
131                 return -1;
132             }
133             count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open);
134             expected++;
135         }
136         return nextCloseIdx;
137     }
138
139     static public String JavaDoc getAttribute(String JavaDoc attribute, String JavaDoc text) {
140         return getAttribute(attribute, text, 0);
141     }
142     static public String JavaDoc getAttribute(String JavaDoc attribute, String JavaDoc text, int idx) {
143          int close = text.indexOf(">", idx);
144          int attrIdx = text.indexOf(attribute+"=\"", idx);
145          if(attrIdx == -1) {
146              return null;
147          }
148          if(attrIdx > close) {
149              return null;
150          }
151          int attrStartIdx = attrIdx + attribute.length() + 2;
152          int attrCloseIdx = text.indexOf("\"", attrStartIdx);
153          if(attrCloseIdx > close) {
154              return null;
155          }
156          return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
157     }
158
159 }
160
Popular Tags