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

WICKET-7036: MarkupFactory supports IMarkupFilters #564

Open
wants to merge 2 commits into
base: wicket-9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package org.apache.wicket.markup;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.wicket.Application;
import org.apache.wicket.MarkupContainer;
Expand Down Expand Up @@ -61,6 +65,8 @@ public static MarkupFactory get()
return Application.get().getMarkupSettings().getMarkupFactory();
}

private final Map<IMarkupFilter, Class<? extends IMarkupFilter>> additionalMarkupFilters = new LinkedHashMap<>();

/**
* Construct.
*/
Expand Down Expand Up @@ -108,14 +114,25 @@ public IMarkupLoader getMarkupLoader()
public MarkupParser newMarkupParser(final MarkupResourceStream resource)
{
// Markup parsers can not be re-used
return new MarkupParser(newXmlPullParser(), resource)
final MarkupParser markupParser = new MarkupParser(newXmlPullParser(), resource)
{
@Override
protected IMarkupFilter onAppendMarkupFilter(final IMarkupFilter filter)
{
return MarkupFactory.this.onAppendMarkupFilter(filter);
}
};

additionalMarkupFilters.entrySet().forEach((entry) -> {
final IMarkupFilter markupFilter = entry.getKey();
final Class<? extends IMarkupFilter> beforeFilter = entry.getValue();
if (beforeFilter == null) {
markupParser.add(markupFilter);
} else {
markupParser.add(markupFilter, beforeFilter);
}
});
return markupParser;
}

/**
Expand Down Expand Up @@ -454,4 +471,50 @@ public final Markup loadMarkup(final MarkupContainer container,
// Markup not found. Errors should throw a Wicket exception
return null;
}

/**
* Adds an additional {@link IMarkupFilter} to {@link MarkupParser}.
* @param markupFilter added markupFilter
*/
public void addAdditionalMarkupFilter(final IMarkupFilter markupFilter) {
addAdditionalMarkupFilter(markupFilter, null);
}

/**
* Adds an additional {@link IMarkupFilter} to {@link MarkupParser}.
* @param markupFilter added markupFilter
* @param beforeFilterClass add filter before given beforeFilterClass
*/
public void addAdditionalMarkupFilter(final IMarkupFilter markupFilter, final Class<? extends IMarkupFilter> beforeFilterClass) {
if (markupFilter != null) {
additionalMarkupFilters.put(markupFilter, beforeFilterClass);
}
}

/**
* Remove an additional {@link IMarkupFilter}.
* @param markupFilter removedMarkupFilter
*/
public void removeAdditionalMarkupFilter(final IMarkupFilter markupFilter) {
if (markupFilter !=null) {
additionalMarkupFilters.remove(markupFilter);
}
}

/**
* Remove additional {@link IMarkupFilter}s
* @param markupFilterClass filter-class
*/
public void removeAdditionalMarkupFilters(final Class<? extends IMarkupFilter> markupFilterClass) {
if (markupFilterClass != null) {
final Set<IMarkupFilter> removedMarkupFilters = additionalMarkupFilters
.keySet()
.stream()
.filter(e -> markupFilterClass.isAssignableFrom(e.getClass()))
.collect(Collectors.toSet());
removedMarkupFilters
.stream()
.forEach(additionalMarkupFilters::remove);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.wicket.markup;

import java.text.ParseException;

import org.apache.wicket.markup.parser.AbstractMarkupFilter;
import org.apache.wicket.markup.parser.IMarkupFilter;
import org.apache.wicket.util.resource.StringResourceStream;
import org.apache.wicket.util.tester.WicketTestCase;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @author Hans Hosea Schaefer
*/
class MarkupFactoryTest extends WicketTestCase {

private IMarkupFilter dummyMarkupFilter = new DummyMarkupFilter() {};
private IMarkupFilter secondMarkupFilter = new DummyMarkupFilter() {};

@Test
public void testAdditionalMarkupFiltersAdded() {
final MarkupFactory markupFactory = new MarkupFactory();
markupFactory.addAdditionalMarkupFilter(secondMarkupFilter);
markupFactory.addAdditionalMarkupFilter(dummyMarkupFilter, secondMarkupFilter.getClass());

final MarkupParser markupParser = markupFactory.newMarkupParser(new MarkupResourceStream(new StringResourceStream("<hello/>")));
final MarkupParser.MarkupFilterList markupFilters = markupParser.getMarkupFilters();
Assertions.assertTrue(markupFilters.contains(secondMarkupFilter));
Assertions.assertTrue(markupFilters.contains(dummyMarkupFilter));
}

@Test
public void testAdditionalMarkupFiltersAddedAndOneRemoved() {
final MarkupFactory markupFactory = new MarkupFactory();
markupFactory.addAdditionalMarkupFilter(secondMarkupFilter);
markupFactory.addAdditionalMarkupFilter(dummyMarkupFilter, secondMarkupFilter.getClass());
markupFactory.removeAdditionalMarkupFilter(secondMarkupFilter);

final MarkupParser markupParser = markupFactory.newMarkupParser(new MarkupResourceStream(new StringResourceStream("<hello/>")));
final MarkupParser.MarkupFilterList markupFilters = markupParser.getMarkupFilters();
Assertions.assertFalse(markupFilters.contains(secondMarkupFilter));
Assertions.assertTrue(markupFilters.contains(dummyMarkupFilter));
}

@Test
public void testAdditionalMarkupFiltersAddedAndOneRemovedByClass() {
final MarkupFactory markupFactory = new MarkupFactory();
markupFactory.addAdditionalMarkupFilter(secondMarkupFilter);
markupFactory.addAdditionalMarkupFilter(dummyMarkupFilter, secondMarkupFilter.getClass());
markupFactory.removeAdditionalMarkupFilters(secondMarkupFilter.getClass());

final MarkupParser markupParser = markupFactory.newMarkupParser(new MarkupResourceStream(new StringResourceStream("<hello/>")));
final MarkupParser.MarkupFilterList markupFilters = markupParser.getMarkupFilters();
Assertions.assertFalse(markupFilters.contains(secondMarkupFilter));
Assertions.assertTrue(markupFilters.contains(dummyMarkupFilter));
}

@Test
public void testAdditionalMarkupFiltersAddedAndAllRemovedByClass() {
final MarkupFactory markupFactory = new MarkupFactory();
markupFactory.addAdditionalMarkupFilter(secondMarkupFilter);
markupFactory.addAdditionalMarkupFilter(dummyMarkupFilter, secondMarkupFilter.getClass());
markupFactory.removeAdditionalMarkupFilters(IMarkupFilter.class);

final MarkupParser markupParser = markupFactory.newMarkupParser(new MarkupResourceStream(new StringResourceStream("<hello/>")));
final MarkupParser.MarkupFilterList markupFilters = markupParser.getMarkupFilters();
Assertions.assertFalse(markupFilters.contains(secondMarkupFilter));
Assertions.assertFalse(markupFilters.contains(dummyMarkupFilter));
}


static abstract class DummyMarkupFilter extends AbstractMarkupFilter {
protected MarkupElement onComponentTag(final ComponentTag tag) throws ParseException {
return null;
}
}
}