6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: ExsltStrings.java,v 1.1.2.1 2005/08/01 02:08:48 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xalan.internal.lib;
|
|
24 |
|
|
25 |
import java.util.StringTokenizer;
|
|
26 |
|
|
27 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
28 |
import javax.xml.parsers.FactoryConfigurationError;
|
|
29 |
import javax.xml.parsers.ParserConfigurationException;
|
|
30 |
|
|
31 |
import com.sun.org.apache.xpath.internal.NodeSet;
|
|
32 |
|
|
33 |
import org.w3c.dom.Document;
|
|
34 |
import org.w3c.dom.Element;
|
|
35 |
import org.w3c.dom.Node;
|
|
36 |
import org.w3c.dom.NodeList;
|
|
37 |
import org.w3c.dom.Text;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* This class contains EXSLT strings extension functions.
|
|
41 |
*
|
|
42 |
* It is accessed by specifying a namespace URI as follows:
|
|
43 |
* <pre>
|
|
44 |
* xmlns:str="http://exslt.org/strings"
|
|
45 |
* </pre>
|
|
46 |
* The documentation for each function has been copied from the relevant
|
|
47 |
* EXSLT Implementer page.
|
|
48 |
*
|
|
49 |
* @see <a href="http://www.exslt.org/">EXSLT</a>
|
|
50 |
|
|
51 |
* @xsl.usage general
|
|
52 |
*/
|
|
53 |
public class ExsltStrings extends ExsltBase
|
|
54 |
{
|
|
55 |
/**
|
|
56 |
* The str:align function aligns a string within another string.
|
|
57 |
* <p>
|
|
58 |
* The first argument gives the target string to be aligned. The second argument gives
|
|
59 |
* the padding string within which it is to be aligned.
|
|
60 |
* <p>
|
|
61 |
* If the target string is shorter than the padding string then a range of characters
|
|
62 |
* in the padding string are repaced with those in the target string. Which characters
|
|
63 |
* are replaced depends on the value of the third argument, which gives the type of
|
|
64 |
* alignment. It can be one of 'left', 'right' or 'center'. If no third argument is
|
|
65 |
* given or if it is not one of these values, then it defaults to left alignment.
|
|
66 |
* <p>
|
|
67 |
* With left alignment, the range of characters replaced by the target string begins
|
|
68 |
* with the first character in the padding string. With right alignment, the range of
|
|
69 |
* characters replaced by the target string ends with the last character in the padding
|
|
70 |
* string. With center alignment, the range of characters replaced by the target string
|
|
71 |
* is in the middle of the padding string, such that either the number of unreplaced
|
|
72 |
* characters on either side of the range is the same or there is one less on the left
|
|
73 |
* than there is on the right.
|
|
74 |
* <p>
|
|
75 |
* If the target string is longer than the padding string, then it is truncated to be
|
|
76 |
* the same length as the padding string and returned.
|
|
77 |
*
|
|
78 |
* @param targetStr The target string
|
|
79 |
* @param paddingStr The padding string
|
|
80 |
* @param type The type of alignment
|
|
81 |
*
|
|
82 |
* @return The string after alignment
|
|
83 |
*/
|
|
84 |
public static String align(String targetStr, String paddingStr, String type)
|
|
85 |
{
|
|
86 |
if (targetStr.length() >= paddingStr.length())
|
|
87 |
return targetStr.substring(0, paddingStr.length());
|
|
88 |
|
|
89 |
if (type.equals("right"))
|
|
90 |
{
|
|
91 |
return paddingStr.substring(0, paddingStr.length() - targetStr.length()) + targetStr;
|
|
92 |
}
|
|
93 |
else if (type.equals("center"))
|
|
94 |
{
|
|
95 |
int startIndex = (paddingStr.length() - targetStr.length()) / 2;
|
|
96 |
return paddingStr.substring(0, startIndex) + targetStr + paddingStr.substring(startIndex + targetStr.length());
|
|
97 |
}
|
|
98 |
// Default is left
|
|
99 |
else
|
|
100 |
{
|
|
101 |
return targetStr + paddingStr.substring(targetStr.length());
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* See above
|
|
107 |
*/
|
|
108 |
public static String align(String targetStr, String paddingStr)
|
|
109 |
{
|
|
110 |
return align(targetStr, paddingStr, "left");
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* The str:concat function takes a node set and returns the concatenation of the
|
|
115 |
* string values of the nodes in that node set. If the node set is empty, it returns
|
|
116 |
* an empty string.
|
|
117 |
*
|
|
118 |
* @param nl A node set
|
|
119 |
* @return The concatenation of the string values of the nodes in that node set
|
|
120 |
*/
|
|
121 |
public static String concat(NodeList nl)
|
|
122 |
{
|
|
123 |
StringBuffer sb = new StringBuffer();
|
|
124 |
for (int i = 0; i < nl.getLength(); i++)
|
|
125 |
{
|
|
126 |
Node node = nl.item(i);
|
|
127 |
String value = toString(node);
|
|
128 |
|
|
129 |
if (value != null && value.length() > 0)
|
|
130 |
sb.append(value);
|
|
131 |
}
|
|
132 |
|
|
133 |
return sb.toString();
|
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
|
137 |
* The str:padding function creates a padding string of a certain length.
|
|
138 |
* The first argument gives the length of the padding string to be created.
|
|
139 |
* The second argument gives a string to be used to create the padding. This
|
|
140 |
* string is repeated as many times as is necessary to create a string of the
|
|
141 |
* length specified by the first argument; if the string is more than a character
|
|
142 |
* long, it may have to be truncated to produce the required length. If no second
|
|
143 |
* argument is specified, it defaults to a space (' '). If the second argument is
|
|
144 |
* an empty string, str:padding returns an empty string.
|
|
145 |
*
|
|
146 |
* @param length The length of the padding string to be created
|
|
147 |
* @param pattern The string to be used as pattern
|
|
148 |
*
|
|
149 |
* @return A padding string of the given length
|
|
150 |
*/
|
|
151 |
public static String padding(double length, String pattern)
|
|
152 |
{
|
|
153 |
if (pattern == null || pattern.length() == 0)
|
|
154 |
return "";
|
|
155 |
|
|
156 |
StringBuffer sb = new StringBuffer();
|
|
157 |
int len = (int)length;
|
|
158 |
int numAdded = 0;
|
|
159 |
int index = 0;
|
|
160 |
while (numAdded < len)
|
|
161 |
{
|
|
162 |
if (index == pattern.length())
|
|
163 |
index = 0;
|
|
164 |
|
|
165 |
sb.append(pattern.charAt(index));
|
|
166 |
index++;
|
|
167 |
numAdded++;
|
|
168 |
}
|
|
169 |
|
|
170 |
return sb.toString();
|
|
171 |
}
|
|
172 |
|
|
173 |
/**
|
|
174 |
* See above
|
|
175 |
*/
|
|
176 |
public static String padding(double length)
|
|
177 |
{
|
|
178 |
return padding(length, " ");
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* The str:split function splits up a string and returns a node set of token
|
|
183 |
* elements, each containing one token from the string.
|
|
184 |
* <p>
|
|
185 |
* The first argument is the string to be split. The second argument is a pattern
|
|
186 |
* string. The string given by the first argument is split at any occurrence of
|
|
187 |
* this pattern. For example:
|
|
188 |
* <pre>
|
|
189 |
* str:split('a, simple, list', ', ') gives the node set consisting of:
|
|
190 |
*
|
|
191 |
* <token>a</token>
|
|
192 |
* <token>simple</token>
|
|
193 |
* <token>list</token>
|
|
194 |
* </pre>
|
|
195 |
* If the second argument is omitted, the default is the string ' ' (i.e. a space).
|
|
196 |
*
|
|
197 |
* @param str The string to be split
|
|
198 |
* @param pattern The pattern
|
|
199 |
*
|
|
200 |
* @return A node set of split tokens
|
|
201 |
*/
|
|
202 |
public static NodeList split(String str, String pattern)
|
|
203 |
{
|
|
204 |
|
|
205 |
|
|
206 |
NodeSet resultSet = new NodeSet();
|
|
207 |
resultSet.setShouldCacheNodes(true);
|
|
208 |
|
|
209 |
boolean done = false;
|
|
210 |
int fromIndex = 0;
|
|
211 |
int matchIndex = 0;
|
|
212 |
String token = null;
|
|
213 |
|
|
214 |
while (!done && fromIndex < str.length())
|
|
215 |
{
|
|
216 |
matchIndex = str.indexOf(pattern, fromIndex);
|
|
217 |
if (matchIndex >= 0)
|
|
218 |
{
|
|
219 |
token = str.substring(fromIndex, matchIndex);
|
|
220 |
fromIndex = matchIndex + pattern.length();
|
|
221 |
}
|
|
222 |
else
|
|
223 |
{
|
|
224 |
done = true;
|
|
225 |
token = str.substring(fromIndex);
|
|
226 |
}
|
|
227 |
|
|
228 |
Document doc = DocumentHolder.m_doc;
|
|
229 |
synchronized (doc)
|
|
230 |
{
|
|
231 |
Element element = doc.createElement("token");
|
|
232 |
Text text = doc.createTextNode(token);
|
|
233 |
element.appendChild(text);
|
|
234 |
resultSet.addNode(element);
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
return resultSet;
|
|
239 |
}
|
|
240 |
|
|
241 |
/**
|
|
242 |
* See above
|
|
243 |
*/
|
|
244 |
public static NodeList split(String str)
|
|
245 |
{
|
|
246 |
return split(str, " ");
|
|
247 |
}
|
|
248 |
|
|
249 |
/**
|
|
250 |
* The str:tokenize function splits up a string and returns a node set of token
|
|
251 |
* elements, each containing one token from the string.
|
|
252 |
* <p>
|
|
253 |
* The first argument is the string to be tokenized. The second argument is a
|
|
254 |
* string consisting of a number of characters. Each character in this string is
|
|
255 |
* taken as a delimiting character. The string given by the first argument is split
|
|
256 |
* at any occurrence of any of these characters. For example:
|
|
257 |
* <pre>
|
|
258 |
* str:tokenize('2001-06-03T11:40:23', '-T:') gives the node set consisting of:
|
|
259 |
*
|
|
260 |
* <token>2001</token>
|
|
261 |
* <token>06</token>
|
|
262 |
* <token>03</token>
|
|
263 |
* <token>11</token>
|
|
264 |
* <token>40</token>
|
|
265 |
* <token>23</token>
|
|
266 |
* </pre>
|
|
267 |
* If the second argument is omitted, the default is the string '	

 '
|
|
268 |
* (i.e. whitespace characters).
|
|
269 |
* <p>
|
|
270 |
* If the second argument is an empty string, the function returns a set of token
|
|
271 |
* elements, each of which holds a single character.
|
|
272 |
* <p>
|
|
273 |
* Note: This one is different from the tokenize extension function in the Xalan
|
|
274 |
* namespace. The one in Xalan returns a set of Text nodes, while this one wraps
|
|
275 |
* the Text nodes inside the token Element nodes.
|
|
276 |
*
|
|
277 |
* @param toTokenize The string to be tokenized
|
|
278 |
* @param delims The delimiter string
|
|
279 |
*
|
|
280 |
* @return A node set of split token elements
|
|
281 |
*/
|
|
282 |
public static NodeList tokenize(String toTokenize, String delims)
|
|
283 |
{
|
|
284 |
|
|
285 |
|
|
286 |
NodeSet resultSet = new NodeSet();
|
|
287 |
|
|
288 |
if (delims != null && delims.length() > 0)
|
|
289 |
{
|
|
290 |
StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
|
|
291 |
|
|
292 |
Document doc = DocumentHolder.m_doc;
|
|
293 |
synchronized (doc)
|
|
294 |
{
|
|
295 |
while (lTokenizer.hasMoreTokens())
|
|
296 |
{
|
|
297 |
Element element = doc.createElement("token");
|
|
298 |
element.appendChild(doc.createTextNode(lTokenizer.nextToken()));
|
|
299 |
resultSet.addNode(element);
|
|
300 |
}
|
|
301 |
}
|
|
302 |
}
|
|
303 |
// If the delimiter is an empty string, create one token Element for
|
|
304 |
// every single character.
|
|
305 |
else
|
|
306 |
{
|
|
307 |
|
|
308 |
Document doc = DocumentHolder.m_doc;
|
|
309 |
synchronized (doc)
|
|
310 |
{
|
|
311 |
for (int i = 0; i < toTokenize.length(); i++)
|
|
312 |
{
|
|
313 |
Element element = doc.createElement("token");
|
|
314 |
element.appendChild(doc.createTextNode(toTokenize.substring(i, i+1)));
|
|
315 |
resultSet.addNode(element);
|
|
316 |
}
|
|
317 |
}
|
|
318 |
}
|
|
319 |
|
|
320 |
return resultSet;
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* See above
|
|
325 |
*/
|
|
326 |
public static NodeList tokenize(String toTokenize)
|
|
327 |
{
|
|
328 |
return tokenize(toTokenize, " \t\n\r");
|
|
329 |
}
|
|
330 |
/**
|
|
331 |
* This class is not loaded until first referenced (see Java Language
|
|
332 |
* Specification by Gosling/Joy/Steele, section 12.4.1)
|
|
333 |
*
|
|
334 |
* The static members are created when this class is first referenced, as a
|
|
335 |
* lazy initialization not needing checking against null or any
|
|
336 |
* synchronization.
|
|
337 |
*
|
|
338 |
*/
|
|
339 |
private static class DocumentHolder
|
|
340 |
{
|
|
341 |
// Reuse the Document object to reduce memory usage.
|
|
342 |
private static final Document m_doc;
|
|
343 |
static {
|
|
344 |
try
|
|
345 |
{
|
|
346 |
m_doc =DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
|
347 |
}
|
|
348 |
|
|
349 |
catch(ParserConfigurationException pce)
|
|
350 |
{
|
|
351 |
throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
|
|
352 |
}
|
|
353 |
|
|
354 |
}
|
|
355 |
}
|
|
356 |
|
|
357 |
}
|