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 org.apache.myfaces.tobago.component.Attributes;
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      public static final String CHAR_NON_BEAKING_SPACE = "\u00a0";
030    
031      public static String generateAttribute(String name, Object value) {
032        String stringValue;
033        if (value == null) {
034          stringValue = null;
035        } else if (value instanceof String) {
036          stringValue = (String) value;
037        } else {
038          stringValue = value.toString();
039        }
040        return (stringValue != null && stringValue.length() > 0)
041            ? name + "=\"" + value + "\""
042            : "";
043      }
044    
045      public static String appendAttribute(UIComponent component, String name,
046          String appendValue) {
047        Object attribute = component.getAttributes().get(name);
048        return attribute != null
049            ? attribute.toString() + " " + appendValue : appendValue;
050      }
051    
052      public static String generateOnchange(UIInput component,
053          FacesContext facesContext) {
054    
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(Attributes.ONCHANGE);
073        if (onchange != null) {
074          return onchange.toString();
075        }
076        return null;
077      }
078    
079    }