Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package com.cloudbees.diff;
import com.cloudbees.diff.provider.CmdlineDiffProvider;
import java.io.BufferedReader;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* Utility class for patch application.
*
* @author Martin Entlicher
*/
class Patch extends Reader {
private static final int CONTEXT_DIFF = 0;
private static final int NORMAL_DIFF = 1;
private static final int UNIFIED_DIFF = 2;
private Diff diffs;
private PushbackReader source;
private int currDiff = 0;
private int line = 1;
private String newLine = null; // String, that is used to separate lines
private StringBuffer buff = new StringBuffer();
/** Creates a new instance of Patch */
Patch(Diff diffs, Reader source) {
this.diffs = diffs;
this.source = new PushbackReader(new BufferedReader(source), 1);
}
/**
* Parse the differences.
*
public static Difference[] parse(Reader source) throws IOException {
return parseContextDiff(source);
}
*/
/**
* Parse the differences and corresponding file names.
*/
public static FileDifferences[] parse(Reader source) throws IOException {
List fileDifferences = new ArrayList();
//int pushBackLimit = DIFFERENCE_DELIMETER.length();
//PushbackReader recognizedSource = new PushbackReader(source, pushBackLimit);
Patch.SinglePatchReader patchReader = new Patch.SinglePatchReader(source);
int[] diffType = new int[1];
String[] fileName = new String[2];
while (patchReader.hasNextPatch(diffType, fileName)) {
//System.out.println("Have a next patch of name '"+fileName[0]+"'");
Difference[] diffs = null;
switch (diffType[0]) {
case CONTEXT_DIFF:
diffs = parseContextDiff(patchReader);
break;
case UNIFIED_DIFF:
diffs = parseUnifiedDiff(patchReader);
break;
case NORMAL_DIFF:
diffs = parseNormalDiff(patchReader);
break;
}
if (diffs != null) {
fileDifferences.add(new FileDifferences(fileName[0], fileName[1], diffs));
}
}
return fileDifferences.toArray(new FileDifferences[fileDifferences.size()]);
}
public int read(char[] cbuf, int off, int length) throws java.io.IOException {
if (buff.length() < length) {
doRetrieve(length - buff.length());
}
int ret = Math.min(buff.length(), length);
if (ret == 0) return -1;
String retStr = buff.substring(0, ret);
char[] retChars = retStr.toCharArray();
System.arraycopy(retChars, 0, cbuf, off, ret);
buff.delete(0, ret);
return ret;
}
public void close() throws java.io.IOException {
if (currDiff < diffs.size()) {
throw new IOException("There are " + (diffs.size() - currDiff) + " pending hunks!");
}
source.close();
}
private void doRetrieve(int length) throws IOException {
for (int size = 0; size < length; line++) {
Difference cur = diffs.get(currDiff);
if (currDiff < diffs.size() &&
((Difference.ADD == cur.getType() &&
line == (cur.getFirstStart() + 1)) ||
(Difference.ADD != cur.getType() &&
line == cur.getFirstStart()))) {
if (compareText(source, cur.getFirstText())) {
String text = convertNewLines(cur.getSecondText(), newLine);
buff.append(text);
currDiff++;
} else {
throw new IOException("Patch not applicable.");
}
}
StringBuffer newLineBuffer = null;
if (newLine == null) {
newLineBuffer = new StringBuffer();
}
String lineStr = readLine(source, newLineBuffer);
if (newLineBuffer != null) newLine = newLineBuffer.toString();
if (lineStr == null) break;
buff.append(lineStr);
buff.append(newLine);
}
}
/** Reads a line and returns the char sequence for newline */
private static String readLine(PushbackReader r, StringBuffer nl) throws IOException {
StringBuilder line = new StringBuilder();
int ic = r.read();
if (ic == -1) return null;
char c = (char) ic;
while (c != '\n' && c != '\r') {
line.append(c);
ic = r.read();
if (ic == -1) break;
c = (char) ic;
}
if (nl != null) {
nl.append(c);
}
if (c == '\r') {
try {
ic = r.read();
if (ic != -1) {
c = (char) ic;
if (c != '\n') r.unread(c);
else if (nl != null) nl.append(c);
}
} catch (IOException ioex) {}
}
return line.toString();
}
private static String convertNewLines(String text, String newLine) {
if (text == null) return ""; // NOI18N
if (newLine == null) return text;
StringBuilder newText = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\n') newText.append(newLine);
else if (c == '\r') {
if ((i + 1) < text.length() && text.charAt(i + 1) == '\n') {
i++;
newText.append(newLine);
}
} else newText.append(c);
}
return newText.toString();
}
private boolean compareText(PushbackReader source, String text) throws IOException {
if (text == null || text.length() == 0) return true;
text = adjustTextNL(text);
char[] chars = new char[text.length()];
int pos = 0;
int n;
String readStr = "";
do {
n = source.read(chars, 0, chars.length - pos);
if (n > 0) {
pos += n;
readStr = readStr + new String(chars, 0, n);
}
if (readStr.endsWith("\r")) {
try {
char c = (char) source.read();
if (c != '\n') source.unread(c);
else readStr += c;
} catch (IOException ioex) {}
}
readStr = adjustTextNL(readStr);
pos = readStr.length();
} while (n > 0 && pos < chars.length);
readStr.getChars(0, readStr.length(), chars, 0);
line += numChars('\n', chars);
//System.out.println("Comparing text of the diff:\n'"+text+"'\nWith the read text:\n'"+readStr+"'\n");
//System.out.println(" EQUALS = "+readStr.equals(text));
return readStr.equals(text);
}
/**
* When comparing the two texts, it's important to ignore different line endings.
* This method assures, that only '\n' is used as the line ending.
*/
private String adjustTextNL(String text) {
text = text.replace("\r\n", "\n");
text = text.replace("\n\r", "\n");
text = text.replace("\r", "\n");
return text;
}
private static int numChars(char c, char[] chars) {
int n = 0;
for (char ch : chars) {
if (ch == c) n++;
}
return n;
}
private static final String CONTEXT_MARK1B = "*** ";
// private static final String CONTEXT_MARK1E = " ****";
private static final String CONTEXT_MARK2B = "--- ";
// private static final String CONTEXT_MARK2E = " ----";
private static final String CONTEXT_MARK_DELIMETER = ",";
private static final String DIFFERENCE_DELIMETER = "***************";
// private static final String LINE_PREP = " ";
private static final String LINE_PREP_ADD = "+ ";
private static final String LINE_PREP_REMOVE = "- ";
private static final String LINE_PREP_CHANGE = "! ";
private static Difference[] parseContextDiff(Reader in) throws IOException {
BufferedReader br = new BufferedReader(in);
ArrayList diffs = new ArrayList();
String line = null;
do {
if (line == null || !DIFFERENCE_DELIMETER.equals(line)) {
do {
line = br.readLine();
} while (line != null && !DIFFERENCE_DELIMETER.equals(line));
}
int[] firstInterval = new int[2];
line = br.readLine();
if (line != null && line.startsWith(CONTEXT_MARK1B)) {
try {
readNums(line, CONTEXT_MARK1B.length(), firstInterval);
} catch (NumberFormatException nfex) {
throw new IOException(nfex.getLocalizedMessage());
}
} else continue;
ArrayList