| /* |
| * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| * |
| * This code is free software; you can redistribute it and/or modify it |
| * under the terms of the GNU General Public License version 2 only, as |
| * published by the Free Software Foundation. Sun designates this |
| * particular file as subject to the "Classpath" exception as provided |
| * by Sun in the LICENSE file that accompanied this code. |
| * |
| * This code is distributed in the hope that it will be useful, but WITHOUT |
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| * version 2 for more details (a copy is included in the LICENSE file that |
| * accompanied this code). |
| * |
| * You should have received a copy of the GNU General Public License version |
| * 2 along with this work; if not, write to the Free Software Foundation, |
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| * |
| * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| * CA 95054 USA or visit www.sun.com if you need additional information or |
| * have any questions. |
| */ |
| |
| package com.sun.xml.internal.ws.encoding.simpletype; |
| |
| /** |
| * |
| * @author WS Development Team |
| */ |
| public class EncoderUtils { |
| public static boolean needsCollapsing(String str) { |
| int len = str.length(); |
| int spanLen = 0; |
| |
| for (int idx = 0; idx < len; ++idx) { |
| if (Character.isWhitespace(str.charAt(idx))) { |
| ++spanLen; |
| } else if (spanLen > 0) { |
| if (spanLen == idx) { |
| // leading whitespace |
| return true; |
| } else { |
| // non-leading, non-trailing whitespace |
| if (str.charAt(idx - spanLen) != ' ') { |
| // first whitespace character is not a space |
| return true; |
| } |
| if (spanLen > 1) { |
| // there is a span of multiple whitespace characters |
| return true; |
| } |
| } |
| |
| spanLen = 0; |
| } |
| } |
| |
| if (spanLen > 0) { |
| // trailing whitespace |
| return true; |
| } |
| |
| return false; |
| } |
| |
| public static String collapseWhitespace(String str) { |
| if (!needsCollapsing(str)) { |
| return str; |
| } |
| |
| // the assumption is that most strings will not need to be collapsed, |
| // so the code below will usually not be reached |
| |
| int len = str.length(); |
| char[] buf = new char[len]; |
| str.getChars(0, len, buf, 0); |
| |
| int leadingWSLen = 0; |
| int trailingWSLen = 0; |
| int spanLen = 0; |
| |
| for (int idx = 0; idx < len; ++idx) { |
| if (Character.isWhitespace(buf[idx])) { |
| ++spanLen; |
| } else if (spanLen > 0) { |
| if (spanLen == idx) { |
| // leading whitespace |
| leadingWSLen = spanLen; |
| } else { |
| // non-leading, non-trailing whitespace |
| |
| // ensure that the first whitespace character is a space |
| int firstWSIdx = idx - spanLen; |
| buf[firstWSIdx] = ' '; |
| |
| if (spanLen > 1) { |
| // remove all but the first whitespace character |
| System.arraycopy( |
| buf, |
| idx, |
| buf, |
| firstWSIdx + 1, |
| len - idx); |
| len -= (spanLen - 1); |
| idx = firstWSIdx + 1; |
| } |
| } |
| |
| spanLen = 0; |
| } |
| } |
| |
| if (spanLen > 0) { |
| // trailing whitespace |
| trailingWSLen = spanLen; |
| } |
| |
| return new String( |
| buf, |
| leadingWSLen, |
| len - leadingWSLen - trailingWSLen); |
| } |
| |
| public static String removeWhitespace(String str) { |
| int len = str.length(); |
| StringBuffer buf = new StringBuffer(); |
| int firstNonWS = 0; |
| int idx = 0; |
| for (; idx < len; ++idx) { |
| if (Character.isWhitespace(str.charAt(idx))) { |
| if (firstNonWS < idx) |
| buf.append(str.substring(firstNonWS, idx)); |
| firstNonWS = idx + 1; |
| } |
| } |
| if (firstNonWS < idx) |
| buf.append(str.substring(firstNonWS, idx)); |
| return buf.toString(); |
| } |
| } |