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.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})(?:[=\\s]([^\\x00-\\x1F'<>\\[\\]]" +
045                            "{1,2083}))?\\])|(?:\\[\\/([a-z]{1,16})\\])",
046                    Pattern.CASE_INSENSITIVE);
047    
048            private Matcher _matcher;
049    
050    }