001 package org.apache.myfaces.tobago.component; 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.StringTokenizer; 024 import java.util.List; 025 import java.util.ArrayList; 026 027 /* 028 * Date: May 2, 2007 029 * Time: 1:11:25 PM 030 */ 031 public class LayoutTokens { 032 private static final Log LOG = LogFactory.getLog(LayoutTokens.class); 033 034 private List<LayoutToken> tokens = new ArrayList<LayoutToken>(); 035 036 public int getSize() { 037 return tokens.size(); 038 } 039 040 public void set(int index, LayoutToken token) { 041 tokens.set(index, token); 042 } 043 044 public boolean isEmpty() { 045 return getSize() == 0; 046 } 047 048 public LayoutToken get(int index) { 049 return tokens.get(index); 050 } 051 052 public void shrinkSizeTo(int size) { 053 for (int i = getSize()-1; i >= size; i--) { 054 tokens.remove(i); 055 } 056 } 057 058 public void ensureSize(int size, LayoutToken token) { 059 for (int index = getSize(); index < size; index++) { 060 addToken(token); 061 } 062 } 063 064 private void addToken(LayoutToken token) { 065 tokens.add(token); 066 } 067 068 public static LayoutTokens parse(String[] tokens) { 069 LayoutTokens layoutTokens = new LayoutTokens(); 070 071 for (String token: tokens) { 072 parseToken(token, layoutTokens); 073 } 074 return layoutTokens; 075 } 076 077 public static LayoutTokens parse(String tokens) { 078 LayoutTokens layoutTokens = new LayoutTokens(); 079 if (tokens == null) { 080 return layoutTokens; 081 } 082 StringTokenizer tokenizer = new StringTokenizer(tokens, ";"); 083 084 while (tokenizer.hasMoreTokens()) { 085 String token = tokenizer.nextToken().trim(); 086 parseToken(token, layoutTokens); 087 } 088 return layoutTokens; 089 } 090 091 private static void parseToken(String token, LayoutTokens layoutTokens) { 092 try { 093 // TODO optimize me 094 if ("*".equals(token)) { 095 layoutTokens.addToken(new RelativeLayoutToken(1)); 096 } else if (token.equals("fixed")) { 097 layoutTokens.addToken(new FixedLayoutToken()); 098 } else if (token.equals("minimum")) { 099 layoutTokens.addToken(new MinimumLayoutToken()); 100 } else if (token.matches("\\d+px")) { 101 layoutTokens.addToken(new PixelLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")))); 102 } else if (token.matches("^\\d+\\%")) { 103 layoutTokens.addToken(new PercentLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")))); 104 } else if (token.matches("^\\d+\\*")) { 105 layoutTokens.addToken(new RelativeLayoutToken(Integer.parseInt(token.replaceAll("\\D", "")))); 106 } else { 107 LOG.error("Unknown layout token " + token + " ignoring"); 108 } 109 } catch (NumberFormatException e) { 110 LOG.error("Error parsing layout token " + token, e); 111 } 112 } 113 114 public String toString() { 115 StringBuilder str = new StringBuilder(); 116 for (LayoutToken token: tokens) { 117 str.append(token); 118 str.append(";"); 119 } 120 return str.toString(); 121 } 122 123 124 } 125