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        return parse(tokens, null);
079      }
080    
081    
082    
083      public static LayoutTokens parse(String tokens, LayoutToken defaultToken) {
084        LayoutTokens layoutTokens = new LayoutTokens();
085        if (tokens == null) {
086          layoutTokens.addToken(defaultToken);
087          return layoutTokens;
088        }
089        StringTokenizer tokenizer = new StringTokenizer(tokens, ";");
090    
091        while (tokenizer.hasMoreTokens()) {
092          String token = tokenizer.nextToken().trim();
093          parseToken(token, layoutTokens);
094        }
095        return layoutTokens;
096      }
097    
098      private static void parseToken(String token, LayoutTokens layoutTokens) {
099        try {
100        // TODO optimize me
101          if ("*".equals(token)) {
102            layoutTokens.addToken(new RelativeLayoutToken(1));
103          } else if (token.equals("fixed")) {
104            layoutTokens.addToken(new FixedLayoutToken());
105          } else if (token.equals("minimum")) {
106            layoutTokens.addToken(new MinimumLayoutToken());
107          } else if (token.matches("\\d+px")) {
108            layoutTokens.addToken(new PixelLayoutToken(Integer.parseInt(token.replaceAll("\\D", ""))));
109          } else if (token.matches("^\\d+\\%")) {
110            layoutTokens.addToken(new PercentLayoutToken(Integer.parseInt(token.replaceAll("\\D", ""))));
111          } else if (token.matches("^\\d+\\*")) {
112            layoutTokens.addToken(new RelativeLayoutToken(Integer.parseInt(token.replaceAll("\\D", ""))));
113          } else {
114            LOG.error("Unknown layout token " + token + " ignoring");
115          }
116        } catch (NumberFormatException e) {
117          LOG.error("Error parsing layout token " + token, e);
118        }
119      }
120    
121      public String toString() {
122        StringBuilder str = new StringBuilder();
123        for (LayoutToken token: tokens) {
124          str.append(token);
125          str.append(";");
126        }
127        return str.toString();
128      }
129    
130    
131    }
132