/**
 * 
 */
package net.sf.taverna.t2.lang.ui;

import java.util.regex.Pattern;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

/**
 * @author alanrw
 *
 */
public class SanitisingDocumentFilter extends DocumentFilter {
	public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
		
		fb.insertString(offset, sanitiseString(string), attr);
	}
	
	public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
		      String text, javax.swing.text.AttributeSet attr)

		      throws BadLocationException {
		           fb.insertString(offset, sanitiseString(text), attr);   
		 }
	
	private static String sanitiseString(String text) {
		String result = text;
		if (Pattern.matches("\\w++", text) == false) {
			result = "";
			for (char c : text.toCharArray()) {
				if (Character.isLetterOrDigit(c) || c == '_') {
					result += c;
				} else {
					result += "_";
				}
			}
		}
		return result;		
	}
}

Document filter for names