1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet;
16  
17  import com.liferay.portal.kernel.util.MapUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.Validator;
20  
21  import java.util.Map;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * <a href="RoutePart.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Connor McKay
29   * @author Brian Wing Shun Chan
30   */
31  public class RoutePart {
32  
33      public RoutePart(String fragment) {
34          fragment = fragment.substring(1, fragment.length() - 1);
35  
36          if (Validator.isNull(fragment)) {
37              throw new IllegalArgumentException("Fragment is null");
38          }
39  
40          String[] fragmentParts = fragment.split(StringPool.COLON, 2);
41  
42          if (fragmentParts.length == 2) {
43              String pattern = fragmentParts[0];
44  
45              if (Validator.isNull(pattern)) {
46                  throw new IllegalArgumentException("Pattern is null");
47              }
48  
49              _pattern = Pattern.compile(pattern);
50              _name = fragmentParts[1];
51          }
52          else {
53              _pattern = _defaultPattern;
54              _name = fragmentParts[0];
55          }
56  
57          if (Validator.isNull(_name)) {
58              throw new IllegalArgumentException("Name is null");
59          }
60  
61          _fragmentName = StringPool.OPEN_CURLY_BRACE.concat(_name).concat(
62              StringPool.CLOSE_CURLY_BRACE);
63      }
64  
65      public String getFragmentName() {
66          return _fragmentName;
67      }
68  
69      public String getName() {
70          return _name;
71      }
72  
73      public String getPattern() {
74          return _pattern.toString();
75      }
76  
77      public boolean matches(Map<String, ?> parameters) {
78          String value = MapUtil.getString(parameters, _name);
79  
80          if (value == null) {
81              return false;
82          }
83  
84          return matches(value);
85      }
86  
87      public boolean matches(String parameter) {
88          Matcher matcher = _pattern.matcher(parameter);
89  
90          return matcher.matches();
91      }
92  
93      private static Pattern _defaultPattern = Pattern.compile("[a-zA-Z_]+");
94  
95      private String _fragmentName;
96      private String _name;
97      private Pattern _pattern;
98  
99  }