[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/util ...
Christian Bauer
christian at hibernate.org
Wed Mar 7 13:37:35 EST 2007
User: cbauer
Date: 07/03/07 13:37:35
Added: examples/wiki/src/main/org/jboss/seam/wiki/util Hash.java
Diff.java
Log:
Moved to hot-redeploy WAR build structure
Revision Changes Path
1.1 date: 2007/03/07 18:37:35; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/util/Hash.java
Index: Hash.java
===================================================================
package org.jboss.seam.wiki.util;
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.AutoCreate;
/**
* Not reall save, should use a random salt, prepended later on the digest.
* Should also iterate the hashing a few thousand times to make brute force
* attacks more difficult. Basically, implement user password encryption with
* the same technique as on a typical Linux distribution.
*
* TODO: Make this more secure - before releasing to public and breaking all stored passwords!
*/
@Name("hashUtil")
@AutoCreate
public class Hash {
String hashFunction = "MD5";
String charset = "UTF-8";
public String hash(String text) {
try {
MessageDigest md = MessageDigest.getInstance(hashFunction);
md.update(text.getBytes(charset));
byte[] raw = md.digest();
return new String(Hex.encodeHex(raw));
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getHashFunction() {
return hashFunction;
}
public void setHashFunction(String hashFunction) {
this.hashFunction = hashFunction;
}
}
1.1 date: 2007/03/07 18:37:35; author: cbauer; state: Exp;jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/util/Diff.java
Index: Diff.java
===================================================================
package org.jboss.seam.wiki.util;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
/**
* String comparison and diff algorithms with customizable result rendering.
* <p>
* TODO: Support new methods for word and character diff
* <p>
* @author Christian Bauer
*/
public abstract class Diff {
/**
* Compares two strings, left and ride side, renders a new string with custom boundary markers
* for deleted and added lines.
*
* @param x The "left" side of the comparison.
* @param y The "right" side of the comparison.
* @param ignoreStrings These strings are ignored and not marked as different.
* @return String A result with all deletions and modifications highlighted with custom boundary markers.
*/
public String[] renderDiff(String[] x, String[] y, String... ignoreStrings) {
List<String> ignoreList = Arrays.asList(ignoreStrings);
int M = x.length;
int N = y.length;
String[] result = new String[M + N]; // N + M? Not nice but safe
int k = 0;
int[][] opt = new int[M + 1][N + 1];
for (int i = M - 1; i >= 0; i--) {
for (int j = N - 1; j >= 0; j--) {
if (x[i].equals(y[j]))
opt[i][j] = opt[i + 1][j + 1] + 1;
else
opt[i][j] = Math.max(opt[i + 1][j], opt[i][j + 1]);
}
}
int i = 0, j = 0;
while (i < M && j < N) {
if (x[i].equals(y[j])) {
result[k++] = (x[i]);
i++;
j++;
} else if (opt[i + 1][j] >= opt[i][j + 1]) {
if (ignoreList.contains(x[i]) || "".equals(x[i]) ) {
result[k++] = x[i++];
} else {
result[k++] = getDeletionStartMarker() + x[i++] + getDeletionEndMarker();
}
} else {
if (ignoreList.contains(y[j]) || "".equals(y[j]) ) {
result[k++] = y[j++];
} else {
result[k++] = getAdditionStartMarker() + y[j++] + getAdditionEndMarker();
}
}
}
while (i < M || j < N) {
if (j == N) {
if (ignoreList.contains(x[i]) || "".equals(x[i]) ) {
result[k++] = x[i++];
} else {
result[k++] = getDeletionStartMarker() + x[i++] + getDeletionEndMarker();
}
} else if (i == M) {
if (ignoreList.contains(y[j]) || "".equals(y[j]) ) {
result[k++] = y[j++];
} else {
result[k++] = getAdditionStartMarker() + y[j++] + getAdditionEndMarker();
}
}
}
return result;
}
public static String renderWithDelimiter(String[] strings, String delimiter) {
StringBuilder diff = new StringBuilder();
for (String s: strings) {
if (s != null) {
diff.append(s);
diff.append(delimiter);
}
}
return diff.toString();
}
protected abstract String getDeletionStartMarker();
protected abstract String getDeletionEndMarker();
protected abstract String getAdditionStartMarker();
protected abstract String getAdditionEndMarker();
}
More information about the jboss-cvs-commits
mailing list