001: /*
002: ******************************************************************
003: Copyright (c) 200, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit;
038:
039: /**
040: * Performs replacement of one String by another String
041: * within one or more Strings.
042: * This was once required but a code refactoring made it redundant and I don't have
043: * the heart to kill it off...!
044: */
045: public class Replacement {
046: private final char[] ofChars;
047: private final char[] byChars;
048:
049: public Replacement(String ofString, String byString) {
050: this (ofString.toCharArray(), byString == null ? null : byString
051: .toCharArray());
052: }
053:
054: public Replacement(char[] ofChars, char[] byChars) {
055: this .ofChars = ofChars;
056: this .byChars = byChars;
057: }
058:
059: public final String replace(String inString) {
060: StringBuffer buf = replaceAndAppend(inString.toCharArray(),
061: new StringBuffer(inString.length()));
062:
063: return buf.toString();
064: }
065:
066: public final char[] replace(char[] inChars) {
067: StringBuffer buf = replaceAndAppend(inChars, new StringBuffer(
068: inChars.length));
069:
070: char[] replacement = new char[buf.length()];
071: buf.getChars(0, buf.length(), replacement, 0);
072:
073: return replacement;
074: }
075:
076: public final StringBuffer replaceAndAppend(char[] inChars,
077: StringBuffer toBuffer) {
078: int ofPos = 0;
079: int falseStartPos = -1;
080: for (int i = 0; i < inChars.length; ++i) {
081: if (inChars[i] == ofChars[ofPos]) {
082: if (falseStartPos == -1) {
083: falseStartPos = i;
084: }
085: ++ofPos;
086: } else {
087: ofPos = 0;
088: if (falseStartPos != -1) {
089: for (; falseStartPos < i; ++falseStartPos) {
090: toBuffer.append(inChars[falseStartPos]);
091: }
092: falseStartPos = -1;
093: }
094: toBuffer.append(inChars[i]);
095: }
096:
097: if (ofPos == ofChars.length) {
098: if (hasReplaceBy()) {
099: toBuffer.append(byChars);
100: }
101: ofPos = 0;
102: falseStartPos = -1;
103: }
104: }
105:
106: return toBuffer;
107: }
108:
109: public boolean hasReplaceBy() {
110: return byChars != null && byChars.length > 0;
111: }
112: }
|