org.xwiki.rendering.internal.renderer.xwiki20.XWikiSyntaxEscapeHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xwiki-rendering-syntax-xwiki20 Show documentation
Show all versions of xwiki-rendering-syntax-xwiki20 Show documentation
XWiki Rendering - Syntax - XWiki 2.0
/*
* 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.renderer.xwiki20;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.xwiki.rendering.listener.chaining.BlockStateChainingListener;
/**
* Escape characters that would be confused for XWiki wiki syntax if they were not escaped.
*
* @version $Id: 5dc8bec49ad85be737703c1e1b9c3ee218e07484 $
* @since 2.0M3
*/
public class XWikiSyntaxEscapeHandler
{
public static final Pattern STARLISTEND_PATTERN = Pattern.compile("(\\**([:;]*|1+\\.)?\\p{Blank})");
private static final Pattern LIST_PATTERN = Pattern
.compile("\\p{Blank}*((\\*+[:;]*)|([1*]+\\.[:;]*)|([:;]+))\\p{Blank}+");
private static final Pattern QUOTE_PATTERN = Pattern.compile("(\\>+)");
private static final Pattern HEADER_PATTERN = Pattern.compile("\\p{Blank}*(=+)");
private static final Pattern TABLE_PATTERN = Pattern.compile("\\p{Blank}*(\\||!!|!=)");
/**
* Note that we take care to not match if the first character is preceded by an escape (i.e. '~).
*/
private static final Pattern DOUBLE_CHARS_PATTERN = Pattern.compile(
"(? -1) {
// Escape the ":" symbol
accumulatedBuffer.insert(pos + match.length() - 1, '~');
}
}
private void replaceAll(StringBuffer accumulatedBuffer, String match, String replacement)
{
int pos = -replacement.length();
while ((pos + replacement.length() < accumulatedBuffer.length())
&& ((pos = accumulatedBuffer.indexOf(match, pos + replacement.length())) != -1)) {
accumulatedBuffer.replace(pos, pos + match.length(), replacement);
}
}
private void escapeFirstMatchedCharacter(Pattern pattern, StringBuffer accumulatedBuffer)
{
Matcher matcher = pattern.matcher(accumulatedBuffer);
if (matcher.lookingAt()) {
// Escape the first character
accumulatedBuffer.replace(matcher.start(1), matcher.start(1) + 1, ESCAPE_CHAR + matcher.group(1).charAt(0));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy