KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > xml > XmlUtils


1 package org.jahia.utils.xml;
2
3 import org.apache.commons.lang.StringUtils;
4
5 /* ====================================================================
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 2001 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Apache" and "Apache Software Foundation" and
31  * "Apache Commons" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache",
36  * "Apache Turbine", nor may "Apache" appear in their name, without
37  * prior written permission of the Apache Software Foundation.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58
59 /**
60  * XML helping static methods.
61  *
62  * @author <a HREF="mailto:bayard@apache.org">Henri Yandell</a>
63  * @version 1.0
64  */

65 final public class XmlUtils {
66
67     static public String JavaDoc escapeXml(String JavaDoc str) {
68         str = StringUtils.replace(str,"&","&amp;");
69         str = StringUtils.replace(str,"<","&lt;");
70         str = StringUtils.replace(str,">","&gt;");
71         str = StringUtils.replace(str,"\"","&quot;");
72         str = StringUtils.replace(str,"'","&apos;");
73         return str;
74     }
75
76     static public String JavaDoc unescapeXml(String JavaDoc str) {
77         str = StringUtils.replace(str,"&amp;","&");
78         str = StringUtils.replace(str,"&lt;","<");
79         str = StringUtils.replace(str,"&gt;",">");
80         str = StringUtils.replace(str,"&quot;","\"");
81         str = StringUtils.replace(str,"&apos;","'");
82         return str;
83     }
84
85     /**
86      * Remove any xml tags from a String.
87      * Same as HtmlW's method.
88      */

89     static public String JavaDoc removeXml(String JavaDoc str) {
90         int sz = str.length();
91         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(sz);
92         boolean inString = false;
93         boolean inTag = false;
94         for(int i=0; i<sz; i++) {
95             char ch = str.charAt(i);
96             if(ch == '<') {
97                 inTag = true;
98             } else
99             if(ch == '>') {
100                 inTag = false;
101                 continue;
102             }
103             if(!inTag) {
104                 buffer.append(ch);
105             }
106         }
107         return buffer.toString();
108     }
109
110     static public String JavaDoc getContent(String JavaDoc tag, String JavaDoc text) {
111         int idx = XmlUtils.getIndexOpeningTag(tag, text);
112         if(idx == -1) {
113             return "";
114         }
115         text = text.substring(idx);
116         int end = XmlUtils.getIndexClosingTag(tag, text);
117         idx = text.indexOf('>');
118         if(idx == -1) {
119             return "";
120         }
121         return text.substring(idx+1, end);
122     }
123
124     static public int getIndexOpeningTag(String JavaDoc tag, String JavaDoc text) {
125         return getIndexOpeningTag(tag, text, 0);
126     }
127     static private int getIndexOpeningTag(String JavaDoc tag, String JavaDoc text, int start) {
128         // consider whitespace?
129
int idx = text.indexOf("<"+tag, start);
130         if(idx == -1) {
131             return -1;
132         }
133         char next = text.charAt(idx+1+tag.length());
134         if( (next == '>') || Character.isWhitespace(next) ) {
135             return idx;
136         } else {
137             return getIndexOpeningTag(tag, text, idx+1);
138         }
139     }
140
141     // Pass in "para" and a string that starts with
142
// <para> and it will return the index of the matching </para>
143
// It assumes well-formed xml. Or well enough.
144
static public int getIndexClosingTag(String JavaDoc tag, String JavaDoc text) {
145         return getIndexClosingTag(tag, text, 0);
146     }
147     static public int getIndexClosingTag(String JavaDoc tag, String JavaDoc text, int start) {
148         String JavaDoc open = "<"+tag;
149         String JavaDoc close = "</"+tag+">";
150 // System.err.println("OPEN: "+open);
151
// System.err.println("CLOSE: "+close);
152
int closeSz = close.length();
153         int nextCloseIdx = text.indexOf(close, start);
154 // System.err.println("first close: "+nextCloseIdx);
155
if(nextCloseIdx == -1) {
156             return -1;
157         }
158         int count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open);
159 // System.err.println("count: "+count);
160
if(count == 0) {
161             return -1; // tag is never opened
162
}
163         int expected = 1;
164         while(count != expected) {
165             nextCloseIdx = text.indexOf(close, nextCloseIdx+closeSz);
166             if(nextCloseIdx == -1) {
167                 return -1;
168             }
169             count = StringUtils.countMatches(text.substring(start, nextCloseIdx), open);
170             expected++;
171         }
172         return nextCloseIdx;
173     }
174
175     static public String JavaDoc getAttribute(String JavaDoc attribute, String JavaDoc text) {
176         return getAttribute(attribute, text, 0);
177     }
178     static public String JavaDoc getAttribute(String JavaDoc attribute, String JavaDoc text, int idx) {
179          int close = text.indexOf(">", idx);
180          int attrIdx = text.indexOf(attribute+"=\"", idx);
181          if(attrIdx == -1) {
182              return null;
183          }
184          if(attrIdx > close) {
185              return null;
186          }
187          int attrStartIdx = attrIdx + attribute.length() + 2;
188          int attrCloseIdx = text.indexOf("\"", attrStartIdx);
189          if(attrCloseIdx > close) {
190              return null;
191          }
192          return unescapeXml(text.substring(attrStartIdx, attrCloseIdx));
193     }
194
195 }
196
Popular Tags