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     * Created by IntelliJ IDEA.
028     * User: bommel
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        StringBuilder buf = new StringBuilder();
062        for(Map.Entry<String, Object> style :entrySet()) {
063          buf.append(style.getKey());
064          buf.append(":");
065          buf.append(style.getValue());
066          if (style.getValue() instanceof Integer) {
067            buf.append("px; ");
068          } else {
069            buf.append("; ");
070          }
071        }
072        return buf.toString();
073      }
074    }