01: package com.sun.portal.admin.console.sra.validators;
02:
03: import java.io.Serializable;
04:
05: import javax.faces.application.FacesMessage;
06: import javax.faces.component.UIComponent;
07: import javax.faces.component.EditableValueHolder;
08: import javax.faces.context.FacesContext;
09: import javax.faces.validator.Validator;
10: import javax.faces.validator.ValidatorException;
11: import java.text.StringCharacterIterator;
12: import java.text.CharacterIterator;
13: import com.sun.portal.admin.console.sra.ISraBean;
14: import com.sun.portal.admin.console.sra.utils.Util;
15:
16: public class HostDomainNameValidator implements Validator, Serializable {
17:
18: public void validate(FacesContext context, UIComponent component,
19: Object value) throws ValidatorException {
20: EditableValueHolder peer = (EditableValueHolder) component;
21:
22: if (value instanceof String[]) {
23: String[] hostArray = (String[]) value;
24: for (int i = 0; i < hostArray.length; i++) {
25: String hostNameValue = (String) hostArray[i];
26: checkHostName(peer, hostNameValue);
27: }
28: } else if (value instanceof String) {
29: String hostNameValue = (String) value;
30: checkHostName(peer, hostNameValue);
31: }
32: }
33:
34: protected void checkHostName(EditableValueHolder peer,
35: String hostNameValue) {
36: if (!peer.isValid()
37: || hostNameValue.equals(ISraBean.EMPTY_STRING)
38: || hostNameValue == null)
39: return;
40:
41: StringCharacterIterator iter = new StringCharacterIterator(
42: hostNameValue);
43: for (char c = iter.first(); c != CharacterIterator.DONE; c = iter
44: .next()) {
45: if (!isAcceptableCharacter(c)) {
46: notASCIICharactersMessage();
47: break;
48: }
49: }
50: }
51:
52: private void notASCIICharactersMessage() {
53: FacesMessage message = new FacesMessage();
54: String err_message = Util.getLocalizedMessageForKey(
55: ISraBean.RB_NAME, "hostname.invalid.characters.error");
56: message.setDetail(err_message);
57: message.setSummary(err_message);
58: message.setSeverity(FacesMessage.SEVERITY_ERROR);
59: throw new ValidatorException(message);
60: }
61:
62: private boolean isAcceptableCharacter(char ch) {
63: boolean isAcceptable = true;
64:
65: if (!Character.isLetterOrDigit(ch) && ch != '-' && ch != '_'
66: && ch != '.')
67: isAcceptable = false;
68:
69: return isAcceptable;
70: }
71: }
|