Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XCOMMONS-2534: Provide a method to escape content passed to wiki macro list parameters #285

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xwiki.stability.Unstable;
import org.xwiki.xml.XMLUtils;

/**
Expand Down Expand Up @@ -66,6 +67,8 @@ public class EscapeTool extends org.apache.velocity.tools.generic.EscapeTool
/** And sign. */
private static final String AND = "&";

private XWiki21EscapeTool xWiki21EscapeTool = new XWiki21EscapeTool(this);

/**
* Change the default key defined in {@link org.apache.velocity.tools.generic.EscapeTool}.
*/
Expand Down Expand Up @@ -273,4 +276,14 @@ public String url(Object string)
}
return encodedURL;
}

/**
* @return an instance of escapetool specific for {@code xwiki/21}
* @since 14.9RC1
*/
@Unstable
public XWiki21EscapeTool getXwiki21()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: it's XWiki:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not if we want to be able to write $escapetool.xwiki21.parameterArray in velocity right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed (https://velocity.apache.org/engine/2.0/user-guide.html#property-lookup-rules), getxwiki21() would work too but it's not better.

Copy link
Member

@vmassol vmassol Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just put a comment to explain it's not a typo but that it's for being able to write $escapetool.xwiki21. from Velocity.

{
return this.xWiki21EscapeTool;
}
}
@@ -0,0 +1,70 @@
/*
* 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.velocity.tools;

import org.apache.commons.lang3.StringUtils;
import org.xwiki.stability.Unstable;

/**
* @version $Id$
* @since 14.9RC1
*/
@Unstable
public class XWiki21EscapeTool
{
private EscapeTool escapeTool;

/**
* Default constructor.
*
* @param escapeTool the main escape tool
*/
public XWiki21EscapeTool(EscapeTool escapeTool)
{
this.escapeTool = escapeTool;
}

/**
* Escape values to safely use them as parameters of a xwiki macro expecting an array or a collection.
*
* @param values the values to escape
* @return the escaped value
*/
public String parameterArray(String... values)
{
String[] ret = new String[values.length];
for (int valuesIndex = 0; valuesIndex < values.length; valuesIndex++) {
ret[valuesIndex] = String.valueOf(parameterArray(values[valuesIndex]));
}
return StringUtils.join(ret, ",");
}

private char[] parameterArray(String value)
{
String javaEscaped = String.format("\"%s\"", this.escapeTool.java(value));
char[] xwiki21Escaped = new char[javaEscaped.length() * 2];
for (int i = 0; i < javaEscaped.length(); i++) {
// TODO: duplicated?
xwiki21Escaped[i * 2] = '~';
xwiki21Escaped[(i * 2) + 1] = javaEscaped.charAt(i);
}
return xwiki21Escaped;
}
}
@@ -0,0 +1,45 @@
/*
* 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.velocity.tools;

import org.junit.jupiter.api.Test;
import org.xwiki.test.junit5.mockito.ComponentTest;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Test of {@link XWiki21EscapeTool}.
*
* @version $Id$
* @since 14.9RC1
*/
@ComponentTest
class XWiki21EscapeToolTest
{
private final XWiki21EscapeTool xWiki21EscapeTool = new XWiki21EscapeTool(new EscapeTool());

@Test
void parameterArray()
{
assertEquals("~\"~a~\"", this.xWiki21EscapeTool.parameterArray("a"));
assertEquals("~\"~a~\",~\"~b~\"", this.xWiki21EscapeTool.parameterArray("a", "b"));
assertEquals("~\"~a~\\~\"~\",~\"~b~\"", this.xWiki21EscapeTool.parameterArray("a\"", "b"));
}
}