/
InlineResourceRenderer.java
92 lines (77 loc) · 3.13 KB
/
InlineResourceRenderer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* 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 <code>true</code>.
*/
@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;
}