1 16 19 package org.apache.xalan.lib; 20 21 import java.util.StringTokenizer ; 22 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.FactoryConfigurationError ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 27 import org.apache.xpath.NodeSet; 28 29 import org.w3c.dom.Document ; 30 import org.w3c.dom.Element ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.Text ; 34 35 49 public class ExsltStrings extends ExsltBase 50 { 51 80 public static String align(String targetStr, String paddingStr, String type) 81 { 82 if (targetStr.length() >= paddingStr.length()) 83 return targetStr.substring(0, paddingStr.length()); 84 85 if (type.equals("right")) 86 { 87 return paddingStr.substring(0, paddingStr.length() - targetStr.length()) + targetStr; 88 } 89 else if (type.equals("center")) 90 { 91 int startIndex = (paddingStr.length() - targetStr.length()) / 2; 92 return paddingStr.substring(0, startIndex) + targetStr + paddingStr.substring(startIndex + targetStr.length()); 93 } 94 else 96 { 97 return targetStr + paddingStr.substring(targetStr.length()); 98 } 99 } 100 101 104 public static String align(String targetStr, String paddingStr) 105 { 106 return align(targetStr, paddingStr, "left"); 107 } 108 109 117 public static String concat(NodeList nl) 118 { 119 StringBuffer sb = new StringBuffer (); 120 for (int i = 0; i < nl.getLength(); i++) 121 { 122 Node node = nl.item(i); 123 String value = toString(node); 124 125 if (value != null && value.length() > 0) 126 sb.append(value); 127 } 128 129 return sb.toString(); 130 } 131 132 147 public static String padding(double length, String pattern) 148 { 149 if (pattern == null || pattern.length() == 0) 150 return ""; 151 152 StringBuffer sb = new StringBuffer (); 153 int len = (int)length; 154 int numAdded = 0; 155 int index = 0; 156 while (numAdded < len) 157 { 158 if (index == pattern.length()) 159 index = 0; 160 161 sb.append(pattern.charAt(index)); 162 index++; 163 numAdded++; 164 } 165 166 return sb.toString(); 167 } 168 169 172 public static String padding(double length) 173 { 174 return padding(length, " "); 175 } 176 177 198 public static NodeList split(String str, String pattern) 199 { 200 201 202 NodeSet resultSet = new NodeSet(); 203 resultSet.setShouldCacheNodes(true); 204 205 boolean done = false; 206 int fromIndex = 0; 207 int matchIndex = 0; 208 String token = null; 209 210 while (!done && fromIndex < str.length()) 211 { 212 matchIndex = str.indexOf(pattern, fromIndex); 213 if (matchIndex >= 0) 214 { 215 token = str.substring(fromIndex, matchIndex); 216 fromIndex = matchIndex + pattern.length(); 217 } 218 else 219 { 220 done = true; 221 token = str.substring(fromIndex); 222 } 223 224 Document doc = DocumentHolder.m_doc; 225 synchronized (doc) 226 { 227 Element element = doc.createElement("token"); 228 Text text = doc.createTextNode(token); 229 element.appendChild(text); 230 resultSet.addNode(element); 231 } 232 } 233 234 return resultSet; 235 } 236 237 240 public static NodeList split(String str) 241 { 242 return split(str, " "); 243 } 244 245 278 public static NodeList tokenize(String toTokenize, String delims) 279 { 280 281 282 NodeSet resultSet = new NodeSet(); 283 284 if (delims != null && delims.length() > 0) 285 { 286 StringTokenizer lTokenizer = new StringTokenizer (toTokenize, delims); 287 288 Document doc = DocumentHolder.m_doc; 289 synchronized (doc) 290 { 291 while (lTokenizer.hasMoreTokens()) 292 { 293 Element element = doc.createElement("token"); 294 element.appendChild(doc.createTextNode(lTokenizer.nextToken())); 295 resultSet.addNode(element); 296 } 297 } 298 } 299 else 302 { 303 304 Document doc = DocumentHolder.m_doc; 305 synchronized (doc) 306 { 307 for (int i = 0; i < toTokenize.length(); i++) 308 { 309 Element element = doc.createElement("token"); 310 element.appendChild(doc.createTextNode(toTokenize.substring(i, i+1))); 311 resultSet.addNode(element); 312 } 313 } 314 } 315 316 return resultSet; 317 } 318 319 322 public static NodeList tokenize(String toTokenize) 323 { 324 return tokenize(toTokenize, " \t\n\r"); 325 } 326 335 private static class DocumentHolder 336 { 337 private static final Document m_doc; 339 static { 340 try 341 { 342 m_doc =DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 343 } 344 345 catch(ParserConfigurationException pce) 346 { 347 throw new org.apache.xml.utils.WrappedRuntimeException(pce); 348 } 349 350 } 351 } 352 353 } 354 | Popular Tags |