Html Dimensions : HTML Output « Servlets « Java






Html Dimensions

 

/**
 * 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.
 */
// Revised from ajax4jsf

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.regex.Pattern;

public class HtmlDimensions {
  
  private static final Pattern PATTERN_NUMERIC = Pattern.compile("^[+-]?\\d+(\\.\\d+)?$");
  private static final Pattern PATTERN_PX = Pattern.compile("^[+-]?\\d+(\\.\\d+)?px$");
  private static final Pattern PATTERN_PCT = Pattern.compile("^[+-]?\\d+(\\.\\d+)?%$");
  
  private static final NumberFormat numericFormat = new DecimalFormat();
  private static final DecimalFormat pxFormat = new DecimalFormat();
  private static final NumberFormat pctFormat = NumberFormat.getPercentInstance();
  
  static{
    pxFormat.setPositiveSuffix("px");
    pxFormat.setNegativeSuffix("px");
  }
  public static Double decode(String size){
    // TODO - handle px,ex,pt enc suffixes.
    double d = 0;
    try {
      if(size != null){
        if(PATTERN_NUMERIC.matcher(size).matches()){
          synchronized(numericFormat){
            d = numericFormat.parse(size).doubleValue();
          }
        } else if(PATTERN_PX.matcher(size).matches()){
          synchronized (pxFormat) {
            d = pxFormat.parse(size).doubleValue();
          }
        } else if(PATTERN_PCT.matcher(size).matches()){
          synchronized (pctFormat) {
            d = pctFormat.parse(size).doubleValue();
          }
        }
      }
    } catch (ParseException e) {
      throw new IllegalArgumentException(e.getMessage());
    }
    return new Double(d);
  }
  
  public static String formatPx(Double value){
    return (value.intValue() + "px");
  }
  public static String formatPct(Double value){
    String v = "";
    synchronized (pctFormat) {
      v = pctFormat.format(value.doubleValue());
    }
    return v;
  }
}

   
  








Related examples in the same category

1.Servlet Output HTML Demo
2.Servlet Display Static HTML
3.Prints a conversion table of miles per gallon to kilometers per liter
4.Servlet: Print Table
5.Html utilities
6.Html Parse Servlet
7.Escape and unescape string
8.Escapes newlines, tabs, backslashes, and quotes in the specified string
9.Web Calendar
10.HTML Helper
11.Escape HTML
12.Convert HTML to text
13.Text To HTML
14.Unescape HTML
15.Java object representations of the HTML table structure
16.Entity Decoder
17.Format a color to HTML RGB color format (e.g. #FF0000 for Color.red)
18.Definitions of HTML character entities and conversions between unicode characters and HTML character entities
19.Encode special characters and do formatting for HTML output
20.HTML color names
21.Utility methods for dealing with HTML
22.Filter the specified message string for characters that are sensitive in HTML
23.A collection of all character entites defined in the HTML4 standard.
24.Decode an HTML color string like '#F567BA;' into a Color
25.Normalize Post Data
26.Get HTML Color String from Java Color object
27.HTML Decoder
28.HTML Parser
29.HTML color and Java Color
30.HTML form Utilites
31.break Lines with HTML
32.insert HTML block dynamically
33.Convert an integer to an HTML RGB value
34.Convert to HTML string