Simple utility class for CSS style formatting : JavaServer Faces « J2EE « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » J2EE » JavaServer Faces 




Simple utility class for CSS style formatting
 

/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
 * Simple utility class for CSS style formatting
 * Current version isn't thread-safe and doesn't provide any validation
 
 * Usage is simle
 * <code>
 *  CSSFormat format = new CSSFormat();
 *  format.add("background-position", "top left");
 *  format.addURL("background-image", "/images/corner.gif");
 *  responseWriter.writeAttribute("style", format, null);
 * </code>
 
 @author Maksim Kaszynski
 *
 */
public class CSSFormat {

  
  /**
   * Surrounds given URL with <code>url()</code>
   @param url
   @return
   */
  public static String url(String url){
    StringBuffer buf = new StringBuffer(url.length() 7);
    buf.append("url(").append(url).append(")");
    return buf.toString();
  }
  /**
   * Formats property-value pair in CSS fashion
   @param property
   @param value
   @return
   */
  public static String propertyValue(String property, String value){
    return formatPropertyValue(property, value);
  }
  
  private static String formatPropertyValue(Object property, Object value){
    StringBuffer buf = new StringBuffer();
    buf.append(property).append(": ").append(value).append(";");
    return buf.toString();
  }
  
  private Map properties;
  /**
   * Constructs an empty CSSFormat object
   */
  public CSSFormat() {
    properties = new HashMap();
  }
  /**
   * Constructs CSSFormat object 
   * and fills it with given parameters
   @param property
   @param value
   */
  public CSSFormat(String property, String value){
    properties = new HashMap(3);
    add(property, value);
    
  }
  /**
   * Adds property. If such property already exists, 
   * its value is replaced with new one
   @param property
   @param value
   @return itself
   */
  public CSSFormat add(String property, String value){
    properties.put(property, value);
    return this;
  }
  /**
   * adds a property with URL value
   * given value is wrapped in <code>url()</code> clause
   @param property
   @param url
   @return itself
   */
  public CSSFormat addURL(String property, String url){
    properties.put(property, url(url));
    return this;
  }
  /**
   * Concatenates all properties with their values to produce single-line CSS output 
   */
  public String toString() {
    return concatenate(null);
  }
  /**
   * Concatenates all properties with their values to produce CSS output
   @param separator - custom string to be inserted between properties
   @return
   */
  public String concatenate(String separator){
    StringBuffer output = new StringBuffer();
    for(Iterator iter = properties.entrySet().iterator(); iter.hasNext();){
      Map.Entry entry = (Map.Entryiter.next();
      output.append(formatPropertyValue(entry.getKey(), entry.getValue()));
      if(separator != null && iter.hasNext()){
        output.append(separator);
      }
    }
    return output.toString();
  }
}

   
  














Related examples in the same category
1.Change Bean Property
2.Hello World JSF
3.JavaServer Faces
4.jsf jpa war
5.Gathers all select items from specified component's children
6.Converts UISelectMany submitted value to converted value
7.Common static utility methods that help in implementing JSF tags
8.Jsf Utility
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.