This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementations for customizing OutputContext
Introduce OutputContextCustomizer, TemplateVariableBindingCustomizer, WebOutputContextInitializer
- Loading branch information
Showing
12 changed files
with
551 additions
and
237 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
grails-taglib/src/main/groovy/org/grails/taglib/TemplateVariableBindingCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2022-2023 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.grails.taglib; | ||
|
||
/** | ||
* Callback interface that can be used to customize a {@link AbstractTemplateVariableBinding}. | ||
* | ||
* @author Michael Yan | ||
* @since 2022.0.0 | ||
* @see AbstractTemplateVariableBinding | ||
*/ | ||
public interface TemplateVariableBindingCustomizer { | ||
|
||
/** | ||
* Callback to customize a {@link AbstractTemplateVariableBinding} instance. | ||
* @param binding the Output Context to customize | ||
*/ | ||
void customize(AbstractTemplateVariableBinding binding); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
grails-taglib/src/main/groovy/org/grails/taglib/encoder/AbstractOutputContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2022-2023 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.grails.taglib.encoder; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
import org.grails.taglib.AbstractTemplateVariableBinding; | ||
import org.grails.taglib.TemplateVariableBindingCustomizer; | ||
|
||
/** | ||
* Abstract {@link OutputContext} to support customizing | ||
* | ||
* @author Michael Yan | ||
* @since 2022.0.0 | ||
* @see TemplateVariableBindingCustomizer | ||
*/ | ||
public abstract class AbstractOutputContext implements OutputContext { | ||
|
||
protected Set<TemplateVariableBindingCustomizer> variableBindingCustomizers = new LinkedHashSet<>(); | ||
|
||
public void addVariableBindingCustomizer(TemplateVariableBindingCustomizer... customizers) { | ||
Assert.notNull(customizers, "Customizers must not be null"); | ||
this.variableBindingCustomizers.addAll(Arrays.asList(customizers)); | ||
} | ||
|
||
public void setVariableBindingCustomizers(Collection<? extends TemplateVariableBindingCustomizer> customizers) { | ||
Assert.notNull(customizers, "Customizers must not be null"); | ||
this.variableBindingCustomizers = new LinkedHashSet<>(customizers); | ||
} | ||
|
||
public Collection<TemplateVariableBindingCustomizer> getVariableBindingCustomizers() { | ||
return this.variableBindingCustomizers; | ||
} | ||
|
||
protected void customizeTemplateVariableBinding(AbstractTemplateVariableBinding binding) { | ||
for (TemplateVariableBindingCustomizer customizer : getVariableBindingCustomizers()) { | ||
customizer.customize(binding); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
grails-taglib/src/main/groovy/org/grails/taglib/encoder/AbstractOutputContextLookup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022-2023 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.grails.taglib.encoder; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Abstract {@link OutputContextLookup} to support customizing | ||
* | ||
* @author Michael Yan | ||
* @since 2022.0.0 | ||
* @see OutputContextCustomizer | ||
*/ | ||
public abstract class AbstractOutputContextLookup implements OutputContextLookup { | ||
|
||
protected Set<OutputContextCustomizer> customizers = new LinkedHashSet<>(); | ||
|
||
public void addContextCustomizers(OutputContextCustomizer... customizers) { | ||
Assert.notNull(customizers, "Customizers must not be null"); | ||
this.customizers.addAll(Arrays.asList(customizers)); | ||
} | ||
|
||
public void setContextCustomizers(Collection<? extends OutputContextCustomizer> customizers) { | ||
Assert.notNull(customizers, "Customizers must not be null"); | ||
this.customizers = new LinkedHashSet<>(customizers); | ||
} | ||
|
||
public Collection<OutputContextCustomizer> getContextCustomizers() { | ||
return this.customizers; | ||
} | ||
|
||
protected void customizeOutputContext(OutputContext outputContext) { | ||
for (OutputContextCustomizer customizer : getContextCustomizers()) { | ||
customizer.customize(outputContext); | ||
} | ||
} | ||
|
||
} |
105 changes: 105 additions & 0 deletions
105
grails-taglib/src/main/groovy/org/grails/taglib/encoder/DefaultOutputContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2015-2023 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.grails.taglib.encoder; | ||
|
||
import java.io.Writer; | ||
|
||
import grails.core.GrailsApplication; | ||
import grails.util.Holders; | ||
|
||
import org.grails.encoder.DefaultEncodingStateRegistry; | ||
import org.grails.encoder.EncodingStateRegistry; | ||
import org.grails.taglib.AbstractTemplateVariableBinding; | ||
import org.grails.taglib.TemplateVariableBinding; | ||
|
||
/** | ||
* Default implementation for {@link OutputContext} | ||
* | ||
* @author Lari Hotari | ||
* @since 3.0 | ||
*/ | ||
public class DefaultOutputContext extends AbstractOutputContext { | ||
|
||
private OutputEncodingStack outputEncodingStack; | ||
|
||
private Writer currentWriter; | ||
|
||
private AbstractTemplateVariableBinding binding; | ||
|
||
private EncodingStateRegistry encodingStateRegistry = new DefaultEncodingStateRegistry(); | ||
|
||
public DefaultOutputContext() { | ||
|
||
} | ||
|
||
@Override | ||
public EncodingStateRegistry getEncodingStateRegistry() { | ||
return this.encodingStateRegistry; | ||
} | ||
|
||
@Override | ||
public void setCurrentOutputEncodingStack(OutputEncodingStack outputEncodingStack) { | ||
this.outputEncodingStack = outputEncodingStack; | ||
} | ||
|
||
@Override | ||
public OutputEncodingStack getCurrentOutputEncodingStack() { | ||
return this.outputEncodingStack; | ||
} | ||
|
||
@Override | ||
public Writer getCurrentWriter() { | ||
return this.currentWriter; | ||
} | ||
|
||
@Override | ||
public void setCurrentWriter(Writer currentWriter) { | ||
this.currentWriter = currentWriter; | ||
} | ||
|
||
@Override | ||
public AbstractTemplateVariableBinding createAndRegisterRootBinding() { | ||
this.binding = new TemplateVariableBinding(); | ||
customizeTemplateVariableBinding(this.binding); | ||
return this.binding; | ||
} | ||
|
||
@Override | ||
public AbstractTemplateVariableBinding getBinding() { | ||
return this.binding; | ||
} | ||
|
||
@Override | ||
public void setBinding(AbstractTemplateVariableBinding binding) { | ||
this.binding = binding; | ||
} | ||
|
||
@Override | ||
public GrailsApplication getGrailsApplication() { | ||
return Holders.findApplication(); | ||
} | ||
|
||
@Override | ||
public void setContentType(String contentType) { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public boolean isContentTypeAlreadySet() { | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.