org.omnifaces.renderer.InlineResourceRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of omnifaces Show documentation
Show all versions of omnifaces Show documentation
Jakarta Faces 3.0+ utility library
/*
* Copyright OmniFaces
*
* 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.omnifaces.renderer;
import static org.omnifaces.util.FacesLocal.createResource;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import jakarta.faces.application.Resource;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.context.ResponseWriter;
import jakarta.faces.render.Renderer;
import org.omnifaces.resourcehandler.CombinedResourceHandler;
/**
* Base renderer which is to be shared between inline CSS and JS renderers.
*
* @author Bauke Scholtz
* @since 1.2
* @see CombinedResourceHandler
*/
public abstract class InlineResourceRenderer extends Renderer {
// Actions --------------------------------------------------------------------------------------------------------
/**
* Returns true
.
*/
@Override
public boolean getRendersChildren() {
return true;
}
/**
* Obtain the resource, construct a {@link Reader} around it using the character encoding as obtained from the
* response writer and then invoke {@link #startElement(ResponseWriter, UIComponent)},
* {@link #writeResource(Reader, ResponseWriter)} and {@link #endElement(ResponseWriter)} in sequence.
*/
@Override
public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
ResponseWriter writer = context.getResponseWriter();
String name = (String) component.getAttributes().get("name");
String library = (String) component.getAttributes().get("library");
Resource resource = createResource(context, library, name);
startElement(writer, component);
try (Reader reader = new InputStreamReader(resource.getInputStream(), writer.getCharacterEncoding())) {
writeResource(reader, writer);
}
endElement(writer);
}
/**
* Start the element.
* @param writer The response writer.
* @param component The {@link UIComponent} to which this element corresponds.
* @throws IOException When an I/O error occurs.
*/
public abstract void startElement(ResponseWriter writer, UIComponent component) throws IOException;
/**
* Write the resource inline.
* @param reader The reader providing the resource content.
* @param writer The response writer where the resource content has to be written to.
* @throws IOException When an I/O error occurs.
*/
public abstract void writeResource(Reader reader, ResponseWriter writer) throws IOException;
/**
* End the element.
* @param writer The response writer.
* @throws IOException When an I/O error occurs.
*/
public abstract void endElement(ResponseWriter writer) throws IOException;
}