001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.renderkit;
021
022 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_ONCHANGE;
023
024 import javax.faces.component.UIComponent;
025 import javax.faces.component.UIInput;
026 import javax.faces.context.FacesContext;
027
028 public class HtmlUtils {
029
030 public static final String LAYOUT_ATTRIBUTE_PREFIX = "layout.";
031
032 public static String generateAttribute(String name, Object value) {
033 String stringValue;
034 if (value == null) {
035 stringValue = null;
036 } else if (value instanceof String) {
037 stringValue = (String) value;
038 } else {
039 stringValue = value.toString();
040 }
041 return (stringValue != null && stringValue.length() > 0)
042 ? name + "=\"" + value + "\""
043 : "";
044 }
045
046 public static String appendAttribute(UIComponent component, String name,
047 String appendValue) {
048 Object attribute = component.getAttributes().get(name);
049 return attribute != null
050 ? attribute.toString() + " " + appendValue : appendValue;
051 }
052
053 public static String generateOnchange(UIInput component,
054 FacesContext facesContext) {
055
056 StringBuilder buffer = new StringBuilder();
057 /*Validator[] validators = component.getValidators();
058 for (int i = 0; i < validators.length; i++) {
059 if (validators[i] instanceof LongRangeValidator) {
060 String functionCall = "validateLongRange('"
061 + component.getClientId(facesContext) + "')";
062 if (LOG.isDebugEnabled()) {
063 LOG.debug("validator functionCall: " + functionCall);
064 }
065 buffer.append(functionCall);
066 } else {
067 buffer.append("true");
068 }
069 if (i + 1 < validators.length) { // is not last
070 buffer.append(" && ");
071 }
072 } */
073
074 Object onchange = component.getAttributes().get(ATTR_ONCHANGE);
075 if (onchange != null) { // append the onchange attribute
076 if (buffer.length() > 0) {
077 buffer.append(" && ");
078 }
079 buffer.append(onchange);
080 }
081
082 if (buffer.length() > 0) { // has content ?
083 return buffer.toString();
084 } else {
085 return null;
086 }
087 }
088
089 }