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