001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.encoding.simpletype;
027:
028: /**
029: *
030: * @author WS Development Team
031: */
032: public class EncoderUtils {
033: public static boolean needsCollapsing(String str) {
034: int len = str.length();
035: int spanLen = 0;
036:
037: for (int idx = 0; idx < len; ++idx) {
038: if (Character.isWhitespace(str.charAt(idx))) {
039: ++spanLen;
040: } else if (spanLen > 0) {
041: if (spanLen == idx) {
042: // leading whitespace
043: return true;
044: } else {
045: // non-leading, non-trailing whitespace
046: if (str.charAt(idx - spanLen) != ' ') {
047: // first whitespace character is not a space
048: return true;
049: }
050: if (spanLen > 1) {
051: // there is a span of multiple whitespace characters
052: return true;
053: }
054: }
055:
056: spanLen = 0;
057: }
058: }
059:
060: if (spanLen > 0) {
061: // trailing whitespace
062: return true;
063: }
064:
065: return false;
066: }
067:
068: public static String collapseWhitespace(String str) {
069: if (!needsCollapsing(str)) {
070: return str;
071: }
072:
073: // the assumption is that most strings will not need to be collapsed,
074: // so the code below will usually not be reached
075:
076: int len = str.length();
077: char[] buf = new char[len];
078: str.getChars(0, len, buf, 0);
079:
080: int leadingWSLen = 0;
081: int trailingWSLen = 0;
082: int spanLen = 0;
083:
084: for (int idx = 0; idx < len; ++idx) {
085: if (Character.isWhitespace(buf[idx])) {
086: ++spanLen;
087: } else if (spanLen > 0) {
088: if (spanLen == idx) {
089: // leading whitespace
090: leadingWSLen = spanLen;
091: } else {
092: // non-leading, non-trailing whitespace
093:
094: // ensure that the first whitespace character is a space
095: int firstWSIdx = idx - spanLen;
096: buf[firstWSIdx] = ' ';
097:
098: if (spanLen > 1) {
099: // remove all but the first whitespace character
100: System.arraycopy(buf, idx, buf, firstWSIdx + 1,
101: len - idx);
102: len -= (spanLen - 1);
103: idx = firstWSIdx + 1;
104: }
105: }
106:
107: spanLen = 0;
108: }
109: }
110:
111: if (spanLen > 0) {
112: // trailing whitespace
113: trailingWSLen = spanLen;
114: }
115:
116: return new String(buf, leadingWSLen, len - leadingWSLen
117: - trailingWSLen);
118: }
119:
120: public static String removeWhitespace(String str) {
121: int len = str.length();
122: StringBuffer buf = new StringBuffer();
123: int firstNonWS = 0;
124: int idx = 0;
125: for (; idx < len; ++idx) {
126: if (Character.isWhitespace(str.charAt(idx))) {
127: if (firstNonWS < idx)
128: buf.append(str.substring(firstNonWS, idx));
129: firstNonWS = idx + 1;
130: }
131: }
132: if (firstNonWS < idx)
133: buf.append(str.substring(firstNonWS, idx));
134: return buf.toString();
135: }
136: }
|