01: /*
02: * Created on 20-feb-2006
03: *
04: */
05: package nl.knowlogy.validation.jsf.renderkit;
06:
07: import java.io.IOException;
08: import java.util.ArrayList;
09: import java.util.Iterator;
10: import java.util.List;
11:
12: import javax.faces.component.UIComponent;
13: import javax.faces.context.FacesContext;
14: import javax.faces.render.Renderer;
15:
16: import nl.knowlogy.validation.Message;
17: import nl.knowlogy.validation.MessageType;
18: import nl.knowlogy.validation.jsf.component.field.FieldMessageComponent;
19:
20: import org.apache.commons.logging.Log;
21: import org.apache.commons.logging.LogFactory;
22:
23: /**
24: *
25: * @author ccstaaldui
26: *
27: */
28: public abstract class FieldMessageRenderer extends Renderer {
29:
30: protected final Log log = LogFactory.getLog(getClass());
31:
32: /*
33: * (non-Javadoc)
34: *
35: * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext,
36: * javax.faces.component.UIComponent)
37: */
38: public void encodeBegin(FacesContext context, UIComponent component)
39: throws IOException {
40: if (context == null || component == null) {
41: throw new NullPointerException(""
42: //Util
43: // .getExceptionMessageString(Util.NULL_PARAMETERS_ERROR_MESSAGE_ID));
44: );
45: }
46: if (!component.isRendered()) {
47: if (log.isTraceEnabled()) {
48: log.trace("End encoding component " + component.getId()
49: + " since "
50: + "rendered attribute is set to false ");
51: }
52: return;
53: }
54:
55: List errorMessages = new ArrayList();
56: List errorList = new ArrayList();
57: if (component instanceof FieldMessageComponent) {
58: FieldMessageComponent fieldMesssageComponent = (FieldMessageComponent) component;
59: errorMessages = fieldMesssageComponent.getErrorMessages();
60: errorList = fieldMesssageComponent.getErrorList();
61: }
62:
63: List encounteredErrorMessages = new ArrayList(errorMessages
64: .size());
65:
66: boolean hasErrors = false;
67: for (Iterator iter = errorMessages.iterator(); iter.hasNext();) {
68: String element = (String) iter.next();
69: if (!encounteredErrorMessages.contains(element)) {
70: encounteredErrorMessages.add(element);
71: hasErrors = true;
72: }
73: }
74:
75: for (Iterator iter = errorList.iterator(); iter.hasNext();) {
76: Message error = (Message) iter.next();
77:
78: if (error.getMessageType() == MessageType.ERROR) {
79: hasErrors = true;
80: }
81:
82: String element = error.getMessage();
83: if (!encounteredErrorMessages.contains(element)) {
84: encounteredErrorMessages.add(element);
85: }
86:
87: }
88:
89: if (encounteredErrorMessages.size() > 0) {
90: writeContent(context, component, encounteredErrorMessages,
91: hasErrors);
92: }
93: }
94:
95: public abstract void writeContent(FacesContext context,
96: UIComponent component, List errorList, boolean hasErrors)
97: throws IOException;
98: }
|