001    /**
002     * Copyright (c) 2000-present 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                    String name = null;
036    
037                    if (chunkParts.length == 2) {
038                            name = chunkParts[0];
039                            String pattern = chunkParts[1];
040    
041                            if (Validator.isNull(pattern)) {
042                                    throw new IllegalArgumentException("Pattern is null");
043                            }
044    
045                            _pattern = Pattern.compile(pattern);
046                    }
047                    else {
048                            name = chunkParts[0];
049                            _pattern = _defaultPattern;
050                    }
051    
052                    if (Validator.isNull(name)) {
053                            throw new IllegalArgumentException("Name is null");
054                    }
055    
056                    if (name.startsWith(StringPool.PERCENT)) {
057                            name = name.substring(1);
058    
059                            if (Validator.isNull(name)) {
060                                    throw new IllegalArgumentException("Name is null");
061                            }
062    
063                            _raw = true;
064                    }
065                    else {
066                            _raw = false;
067                    }
068    
069                    _name = name;
070    
071                    _token = StringPool.OPEN_CURLY_BRACE.concat(_name).concat(
072                            StringPool.CLOSE_CURLY_BRACE);
073            }
074    
075            public String getName() {
076                    return _name;
077            }
078    
079            public String getPattern() {
080                    return _pattern.toString();
081            }
082    
083            public String getToken() {
084                    return _token;
085            }
086    
087            public boolean isRaw() {
088                    return _raw;
089            }
090    
091            public boolean matches(String parameter) {
092                    Matcher matcher = _pattern.matcher(parameter);
093    
094                    return matcher.matches();
095            }
096    
097            private static final Pattern _defaultPattern = Pattern.compile("[^/\\.]+");
098    
099            private final String _name;
100            private final Pattern _pattern;
101            private final boolean _raw;
102            private final String _token;
103    
104    }