KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > manager > impl > XMLUtil


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.excalibur.instrument.manager.impl;
21
22 /**
23  * Utility classes useful for working with XML.
24  *
25  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
26  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:25 $
27  * @since 4.1
28  */

29 final class XMLUtil
30 {
31     /*---------------------------------------------------------------
32      * Static Methods
33      *-------------------------------------------------------------*/

34     /**
35      * Replaces one token with another in a string.
36      *
37      * @param str String with tokens to be replaced.
38      * @param oldToken The token to be replaced.
39      * @param newToken The new token value.
40      *
41      * @return A new String that has had its tokens replaced.
42      */

43     static String JavaDoc replaceToken( String JavaDoc str, String JavaDoc oldToken, String JavaDoc newToken )
44     {
45         int len = str.length();
46         int oldLen = oldToken.length();
47         if ( oldLen == 0 )
48         {
49             // Can't replace nothing.
50
return str;
51         }
52         int newLen = newToken.length();
53         int start = 0;
54         int pos;
55         while ( ( pos = str.indexOf( oldToken, start ) ) >= 0 )
56         {
57             String JavaDoc left;
58             String JavaDoc right;
59             int leftLen;
60             int rightLen;
61             
62             // Get the left side of the string
63
leftLen = pos;
64             if ( leftLen == 0 )
65             {
66                 left = "";
67             }
68             else
69             {
70                 left = str.substring( 0, pos );
71             }
72             
73             // Get the right side of the string
74
rightLen = len - pos - oldLen;
75             if ( len - pos - oldLen <= 0 )
76             {
77                 right = "";
78             }
79             else
80             {
81                 right = str.substring( pos + oldLen );
82             }
83             
84             // Rebuild the str variable
85
str = left + newToken + right;
86             len = leftLen + newLen + rightLen;
87             start = leftLen + newLen;
88         }
89         return str;
90     }
91
92     /**
93      * Given an arbitrary text String, generate a new String which can be
94      * safely included in XML content.
95      *
96      * @param value The original String.
97      *
98      * @return A new XML-safe String.
99      */

100     static final String JavaDoc getXMLSafeString( String JavaDoc value )
101     {
102         value = replaceToken( value, "&", "&amp;" ); // Must be done first.
103
value = replaceToken( value, "<", "&lt;" );
104         value = replaceToken( value, ">", "&gt;" );
105         value = replaceToken( value, "\"", "&quot;" );
106         
107         return value;
108     }
109     
110     /*---------------------------------------------------------------
111      * Constructors
112      *-------------------------------------------------------------*/

113     /**
114      * Not instantiable.
115      */

116     private XMLUtil()
117     {
118     }
119 }
120
Popular Tags