001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }