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