001: package nl.knowlogy.validation.jsf.renderkit;
002:
003: import java.io.IOException;
004:
005: import javax.faces.component.UIComponent;
006: import javax.faces.component.UIViewRoot;
007: import javax.faces.context.FacesContext;
008: import javax.faces.context.ResponseWriter;
009:
010: import nl.knowlogy.validation.jsf.utils.HTML;
011:
012: public abstract class HtmlRendererUtils {
013:
014: /**
015: * @return true, if the attribute was written
016: * @throws java.io.IOException
017: */
018: public static boolean renderHTMLAttribute(ResponseWriter writer,
019: String componentProperty, String attrName, Object value)
020: throws IOException {
021: if (!HtmlRendererUtils.isDefaultAttributeValue(value)) {
022: // render JSF "styleClass" and "itemStyleClass" attributes as "class"
023: String htmlAttrName = attrName
024: .equals(HTML.STYLE_CLASS_ATTR) ? HTML.CLASS_ATTR
025: : attrName;
026: writer.writeAttribute(htmlAttrName, value,
027: componentProperty);
028: return true;
029: }
030:
031: return false;
032: }
033:
034: /**
035: * @return true, if the attribute was written
036: * @throws java.io.IOException
037: */
038: public static boolean renderHTMLAttribute(ResponseWriter writer,
039: UIComponent component, String componentProperty,
040: String htmlAttrName) throws IOException {
041: Object value = component.getAttributes().get(componentProperty);
042: return renderHTMLAttribute(writer, componentProperty,
043: htmlAttrName, value);
044: }
045:
046: /**
047: * @return true, if an attribute was written
048: * @throws java.io.IOException
049: */
050: public static boolean renderHTMLAttributes(ResponseWriter writer,
051: UIComponent component, String[] attributes)
052: throws IOException {
053: boolean somethingDone = false;
054: for (int i = 0, len = attributes.length; i < len; i++) {
055: String attrName = attributes[i];
056: if (renderHTMLAttribute(writer, component, attrName,
057: attrName)) {
058: somethingDone = true;
059: }
060: }
061: return somethingDone;
062: }
063:
064: public static boolean renderHTMLAttributeWithOptionalStartElement(
065: ResponseWriter writer, UIComponent component,
066: String elementName, String attrName, Object value,
067: boolean startElementWritten) throws IOException {
068: if (!HtmlRendererUtils.isDefaultAttributeValue(value)) {
069: if (!startElementWritten) {
070: writer.startElement(elementName, component);
071: startElementWritten = true;
072: }
073: renderHTMLAttribute(writer, attrName, attrName, value);
074: }
075: return startElementWritten;
076: }
077:
078: public static boolean renderHTMLAttributesWithOptionalStartElement(
079: ResponseWriter writer, UIComponent component,
080: String elementName, String[] attributes) throws IOException {
081: boolean startElementWritten = false;
082: for (int i = 0, len = attributes.length; i < len; i++) {
083: String attrName = attributes[i];
084: Object value = component.getAttributes().get(attrName);
085: if (!HtmlRendererUtils.isDefaultAttributeValue(value)) {
086: if (!startElementWritten) {
087: writer.startElement(elementName, component);
088: startElementWritten = true;
089: }
090: renderHTMLAttribute(writer, attrName, attrName, value);
091: }
092: }
093: return startElementWritten;
094: }
095:
096: public static boolean isDefaultAttributeValue(Object value) {
097: if (value == null) {
098: return true;
099: } else if (value instanceof Boolean) {
100: return !((Boolean) value).booleanValue();
101: } else if (value instanceof Number) {
102: if (value instanceof Integer) {
103: return ((Number) value).intValue() == Integer.MIN_VALUE;
104: } else if (value instanceof Double) {
105: return ((Number) value).doubleValue() == Double.MIN_VALUE;
106: } else if (value instanceof Long) {
107: return ((Number) value).longValue() == Long.MIN_VALUE;
108: } else if (value instanceof Byte) {
109: return ((Number) value).byteValue() == Byte.MIN_VALUE;
110: } else if (value instanceof Float) {
111: return ((Number) value).floatValue() == Float.MIN_VALUE;
112: } else if (value instanceof Short) {
113: return ((Number) value).shortValue() == Short.MIN_VALUE;
114: }
115: }
116: return false;
117: }
118:
119: public static void writeIdIfNecessary(ResponseWriter writer,
120: UIComponent component, FacesContext facesContext)
121: throws IOException {
122: if (component.getId() != null
123: && !component.getId().startsWith(
124: UIViewRoot.UNIQUE_ID_PREFIX)) {
125: writer.writeAttribute(HTML.ID_ATTR, component
126: .getClientId(facesContext), null);
127: }
128: }
129:
130: }
|