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.component;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.myfaces.tobago.util.LayoutUtil;
025    
026    import java.util.StringTokenizer;
027    import java.util.List;
028    import java.util.ArrayList;
029    
030    /*
031     * Date: May 2, 2007
032     * Time: 1:11:25 PM
033     */
034    public class LayoutTokens {
035    
036      private static final Log LOG = LogFactory.getLog(LayoutTokens.class);
037    
038      private List<LayoutToken> tokens = new ArrayList<LayoutToken>();
039    
040      public int getSize() {
041        return tokens.size();
042      }
043    
044      public void set(int index, LayoutToken token) {
045        tokens.set(index, token);
046      }
047    
048      public boolean isEmpty() {
049        return getSize() == 0;
050      }
051    
052      public LayoutToken get(int index) {
053        return tokens.get(index);
054      }
055    
056      public void shrinkSizeTo(int size) {
057        for (int i = getSize() - 1; i >= size; i--) {
058          tokens.remove(i);
059        }
060      }
061    
062      public void ensureSize(int size, LayoutToken token) {
063        for (int index = getSize(); index < size; index++) {
064          addToken(token);
065        }
066      }
067    
068      public void addToken(LayoutToken token) {
069        tokens.add(token);
070      }
071    
072      public static LayoutTokens parse(String[] tokens) {
073        LayoutTokens layoutTokens = new LayoutTokens();
074        for (String token : tokens) {
075          parseToken(token, layoutTokens);
076        }
077        return layoutTokens;
078      }
079    
080      public static LayoutTokens parse(String tokens) {
081        return parse(tokens, null);
082      }
083    
084      public static LayoutTokens parse(String tokens, LayoutToken defaultToken) {
085        LayoutTokens layoutTokens = new LayoutTokens();
086        if (tokens == null) {
087          layoutTokens.addToken(defaultToken);
088          return layoutTokens;
089        }
090        StringTokenizer tokenizer = new StringTokenizer(tokens, ";");
091    
092        while (tokenizer.hasMoreTokens()) {
093          String token = tokenizer.nextToken().trim();
094          parseToken(token, layoutTokens);
095        }
096        return layoutTokens;
097      }
098    
099      private static void parseToken(String token, LayoutTokens layoutTokens) {
100        LayoutToken layoutToken = parseToken(token);
101        if (layoutToken != null) {
102          layoutTokens.addToken(layoutToken);
103        }
104      }
105    
106      public static LayoutToken parseToken(String token) {
107        try {
108          if ("*".equals(token)) {
109            return RelativeLayoutToken.DEFAULT_INSTANCE;
110          } else if (token.equals("fixed")) {
111            return FixedLayoutToken.INSTANCE;
112          } else if (token.equals("minimum")) {
113            return new MinimumLayoutToken();
114          } else if (isPixelToken(token)) {
115            return new PixelLayoutToken(Integer.parseInt(LayoutUtil.removeSuffix(token, PixelLayoutToken.SUFFIX)));
116          } else if (isPercentToken(token)) {
117            return new PercentLayoutToken(Integer.parseInt(LayoutUtil.removeSuffix(token, PercentLayoutToken.SUFFIX)));
118          } else if (isRelativeToken(token)) {
119            return new RelativeLayoutToken(Integer.parseInt(LayoutUtil.removeSuffix(token, RelativeLayoutToken.SUFFIX)));
120          } else {
121            LOG.error("Ignoring unknown layout token '" + token + "'");
122          }
123        } catch (NumberFormatException e) {
124          LOG.error("Error parsing layout token '" + token + "'", e);
125        }
126        return null;
127      }
128    
129      static boolean isPixelToken(String token) {
130        return LayoutUtil.isNumberAndSuffix(token, PixelLayoutToken.SUFFIX);
131      }
132    
133      static boolean isPercentToken(String token) {
134        return LayoutUtil.isNumberAndSuffix(token, PercentLayoutToken.SUFFIX);
135      }
136    
137      static boolean isRelativeToken(String token) {
138        return LayoutUtil.isNumberAndSuffix(token, RelativeLayoutToken.SUFFIX);
139      }
140    
141      public String toString() {
142        StringBuilder str = new StringBuilder();
143        for (LayoutToken token : tokens) {
144          str.append(token);
145          str.append(";");
146        }
147        return str.toString();
148      }
149    
150    }
151