All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xwiki.rendering.internal.parser.wikimodel.WikiModelParserUtils Maven / Gradle / Ivy

There is a newer version: 16.10.0-rc-1
Show newest version
/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.internal.parser.wikimodel;

import java.io.StringReader;

import org.xwiki.rendering.listener.InlineFilterListener;
import org.xwiki.rendering.listener.Listener;
import org.xwiki.rendering.listener.WrappingListener;
import org.xwiki.rendering.parser.ParseException;
import org.xwiki.rendering.parser.StreamParser;
import org.xwiki.rendering.util.ParserUtils;

/**
 * Methods for helping in parsing.
 *
 * @version $Id: 0e77f4a8382979247d228e8aa032ea380853d1f2 $
 * @since 1.8M1
 */
public class WikiModelParserUtils extends ParserUtils
{
    public void parseInline(StreamParser parser, String content, Listener listener) throws ParseException
    {
        parseInline(parser, content, listener, false);
    }
    
    private class PrefixIgnoredInlineFilterListener extends InlineFilterListener 
    {
        private boolean foundWord;

        private boolean foundSpace;

        @Override
        public void onWord(String word)
        {
            if (this.foundWord) {
                super.onWord(word);
            } else {
                this.foundWord = true;
            }
        }

        @Override
        public void onSpace()
        {
            if (this.foundSpace) {
                super.onSpace();
            } else {
                this.foundSpace = true;
            }
        }
    }

    /**
     * @since 6.0RC1
     * @since 5.4.5
     */
    public void parseInline(StreamParser parser, String content, Listener listener, boolean prefix)
        throws ParseException
    {
        if (prefix) {
            WrappingListener inlineFilterListener = new PrefixIgnoredInlineFilterListener();
            inlineFilterListener.setWrappedListener(listener);
            
            parser.parse(new StringReader("wikimarker " + content), inlineFilterListener);
        } else {
            WrappingListener inlineFilterListener = new InlineFilterListener();
            inlineFilterListener.setWrappedListener(listener);
            
            parser.parse(new StringReader(content), inlineFilterListener);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy