001    package org.apache.myfaces.tobago.renderkit.html;
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.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    /*
027     * Date: Nov 24, 2006
028     * Time: 10:07:33 PM
029     */
030    public class HtmlStyleMap extends HashMap<String, Object> {
031    
032      private static final Log LOG = LogFactory.getLog(HtmlStyleMap.class);
033      private static final long serialVersionUID = 342607693971417143L;
034    
035      public Object put(String s, Object o) {
036        if (o instanceof String && (s.equals("height") || s.equals("width"))) {
037          String str = (String) o;
038          if (str.endsWith("px")) {
039            LOG.error("", new Exception());
040            o = Integer.parseInt(str.substring(0, str.length() - 2));
041          }
042        }
043        return super.put(s, o);
044      }
045    
046      public Integer getInt(Object o) {
047        Object obj = get(o);
048        if (obj instanceof Integer) {
049          return (Integer) obj;
050        }
051        if (obj == null) {
052          return null;
053        }
054        LOG.error("", new Exception());
055        return Integer.parseInt(obj.toString());
056      }
057    
058      public String toString() {
059        if (entrySet().isEmpty()) {
060          return null;
061        }
062        StringBuilder buf = new StringBuilder();
063        for (Map.Entry<String, Object> style : entrySet()) {
064          buf.append(style.getKey());
065          buf.append(":");
066          buf.append(style.getValue());
067          if (style.getValue() instanceof Integer && !"z-index".equals(style.getKey())) {
068            buf.append("px; ");
069          } else {
070            buf.append("; ");
071          }
072        }
073        return buf.toString();
074      }
075    }