Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.application.resource;
import java.io.IOException;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.context.ResponseWriterWrapper;
import org.primefaces.renderkit.RendererUtils;
import org.primefaces.util.AgentUtils;
import org.primefaces.util.LangUtils;
public class MoveScriptsToBottomResponseWriter extends ResponseWriterWrapper {
private static final String SCRIPT_TAG = "script";
private static final String BODY_TAG = "body";
private static final String HTML_TAG = "html";
private static final String TYPE_ATTRIBUTE = "type";
private final ResponseWriter wrapped;
private final MoveScriptsToBottomState state;
private boolean inScript;
private String scriptType;
private Map includeAttributes;
private StringBuilder inline;
private boolean scriptsRendered;
private boolean writeFouc;
@SuppressWarnings("deprecation") // the default constructor is deprecated in JSF 2.3
public MoveScriptsToBottomResponseWriter(ResponseWriter wrapped, MoveScriptsToBottomState state) {
this.wrapped = wrapped;
this.state = state;
inScript = false;
scriptsRendered = false;
writeFouc = false;
includeAttributes = new LinkedHashMap<>(6);
inline = new StringBuilder(75);
}
@Override
public ResponseWriter getWrapped() {
return wrapped;
}
@Override
public void write(int c) throws IOException {
if (inScript) {
inline.append((char) c);
}
else {
getWrapped().write(c);
}
}
@Override
public void write(char[] cbuf) throws IOException {
if (inScript) {
inline.append(cbuf);
}
else {
getWrapped().write(cbuf);
}
}
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
if (inScript) {
inline.append(cbuf, off, len);
}
else {
getWrapped().write(cbuf, off, len);
}
}
@Override
public void write(String str) throws IOException {
if (inScript) {
inline.append(str);
}
else {
getWrapped().write(str);
}
}
@Override
public void writeText(char[] cbuf, int off, int len) throws IOException {
if (inScript) {
inline.append(cbuf, off, len);
}
else {
getWrapped().writeText(cbuf, off, len);
}
}
@Override
public void writeText(Object text, String property) throws IOException {
if (inScript) {
inline.append(text);
}
else {
getWrapped().writeText(text, property);
}
}
@Override
public void writeText(Object text, UIComponent component, String property) throws IOException {
if (inScript) {
inline.append(text);
}
else {
getWrapped().writeText(text, property);
}
}
@Override
public void writeAttribute(String name, Object value, String property) throws IOException {
if (inScript) {
updateAttributes(name, (String) value);
}
else {
getWrapped().writeAttribute(name, value, property);
}
}
@Override
public void writeURIAttribute(String name, Object value, String property) throws IOException {
if (inScript) {
updateAttributes(name, (String) value);
}
else {
getWrapped().writeURIAttribute(name, value, property);
}
}
@Override
public void startElement(String name, UIComponent component) throws IOException {
if (SCRIPT_TAG.equalsIgnoreCase(name)) {
inScript = true;
scriptType = RendererUtils.SCRIPT_TYPE;
}
else {
writeFouc();
getWrapped().startElement(name, component);
if (BODY_TAG.equalsIgnoreCase(name) && isFirefox()) {
writeFouc = true;
}
}
}
@Override
public void endElement(String name) throws IOException {
if (SCRIPT_TAG.equalsIgnoreCase(name)) {
inScript = false;
state.addInline(scriptType, inline);
if (LangUtils.isNotBlank(includeAttributes.get("src"))) {
state.addInclude(scriptType, includeAttributes);
}
scriptType = null;
includeAttributes.clear();
inline.setLength(0);
}
else if (BODY_TAG.equalsIgnoreCase(name) || (HTML_TAG.equalsIgnoreCase(name) && !scriptsRendered)) {
// write script includes
for (Entry>> entry : state.getIncludes().entrySet()) {
List