001    /**
002     * Copyright (c) 2000-2011 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.parsers.bbcode;
016    
017    import java.util.regex.Matcher;
018    import java.util.regex.Pattern;
019    
020    /**
021     * @author Iliyan Peychev
022     */
023    public class BBCodeLexer {
024    
025            public BBCodeLexer(String data) {
026                    _matcher = _pattern.matcher(data);
027            }
028    
029            public int getLastIndex() {
030                    return _matcher.end();
031            }
032    
033            public BBCodeToken getNextBBCodeToken() {
034                    if (!_matcher.find()) {
035                            return null;
036                    }
037    
038                    return new BBCodeToken(
039                            _matcher.group(1), _matcher.group(2), _matcher.group(3),
040                            _matcher.start(), _matcher.end());
041            }
042    
043            private static Pattern _pattern = Pattern.compile(
044                    "(?:\\[((?:[a-z]|\\*){1,16})(?:=([^\\x00-\\x1F\"'()<>\\[\\]]{1,256}))" +
045                            "?\\])|(?:\\[/([a-z]{1,16})\\])",
046                    Pattern.CASE_INSENSITIVE);
047    
048            private Matcher _matcher;
049    
050    }