001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.regex.Matcher;
018    import java.util.regex.Pattern;
019    
020    /**
021     * @author Connor McKay
022     * @author Brian Wing Shun Chan
023     */
024    public class StringParserFragment {
025    
026            public StringParserFragment(String chunk) {
027                    chunk = chunk.substring(1, chunk.length() - 1);
028    
029                    if (Validator.isNull(chunk)) {
030                            throw new IllegalArgumentException("Fragment is null");
031                    }
032    
033                    String[] chunkParts = chunk.split(StringPool.COLON, 2);
034    
035                    if (chunkParts.length == 2) {
036                            _name = chunkParts[0];
037                            String pattern = chunkParts[1];
038    
039                            if (Validator.isNull(pattern)) {
040                                    throw new IllegalArgumentException("Pattern is null");
041                            }
042    
043                            _pattern = Pattern.compile(pattern);
044                    }
045                    else {
046                            _name = chunkParts[0];
047                            _pattern = _defaultPattern;
048                    }
049    
050                    if (Validator.isNull(_name)) {
051                            throw new IllegalArgumentException("Name is null");
052                    }
053    
054                    if (_name.startsWith(StringPool.PERCENT)) {
055                            _name = _name.substring(1);
056    
057                            if (Validator.isNull(_name)) {
058                                    throw new IllegalArgumentException("Name is null");
059                            }
060    
061                            _raw = true;
062                    }
063    
064                    _token = StringPool.OPEN_CURLY_BRACE.concat(_name).concat(
065                            StringPool.CLOSE_CURLY_BRACE);
066            }
067    
068            public String getName() {
069                    return _name;
070            }
071    
072            public String getPattern() {
073                    return _pattern.toString();
074            }
075    
076            public String getToken() {
077                    return _token;
078            }
079    
080            public boolean isRaw() {
081                    return _raw;
082            }
083    
084            public boolean matches(String parameter) {
085                    Matcher matcher = _pattern.matcher(parameter);
086    
087                    return matcher.matches();
088            }
089    
090            private static Pattern _defaultPattern = Pattern.compile("[^/\\.]+");
091    
092            private String _name;
093            private Pattern _pattern;
094            private boolean _raw;
095            private String _token;
096    
097    }