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