Author: nbelaevski
Date: 2008-11-26 10:31:40 -0500 (Wed, 26 Nov 2008)
New Revision: 11397
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java
Log:
https://jira.jboss.org/jira/browse/RF-4714
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java 2008-11-26
15:19:06 UTC (rev 11396)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java 2008-11-26
15:31:40 UTC (rev 11397)
@@ -23,8 +23,12 @@
import java.io.IOException;
import java.io.StringWriter;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Set;
@@ -62,6 +66,7 @@
import org.richfaces.skin.Skin;
import org.richfaces.skin.SkinFactory;
import org.richfaces.skin.SkinNotFoundException;
+import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
/**
@@ -87,7 +92,7 @@
private RenderKit renderKit;
- private Node[] result = null;
+ private Node[] headNodes = null;
private static final String EXTENDED_SKINNING_ON_NO_SCRIPTS_INFO_KEY =
ViewResources.class.getName() + "EXTENDED_SKINNING_ON_NO_SCRIPTS_INFO_KEY";
@@ -148,16 +153,69 @@
this.extendedSkinningAllowed = extendedSkinningAllowed;
}
- public Node[] getHeadEvents() {
- Node[] componentResourceNodes = componentWriter.getNodes();
- Node[] userResourceNodes = userWriter.getNodes();
+ private static String SCRIPT = HTML.SCRIPT_ELEM;
+ private static String SCRIPT_UC = SCRIPT.toUpperCase(Locale.US);
+
+ private static String SRC = HTML.src_ATTRIBUTE;
+ private static String SRC_UC = SRC.toUpperCase(Locale.US);
- result = new Node[componentResourceNodes.length + userResourceNodes.length];
- System.arraycopy(componentResourceNodes, 0, result, 0, componentResourceNodes.length);
- System.arraycopy(userResourceNodes, 0, result, componentResourceNodes.length,
userResourceNodes.length);
-
- return result;
+ private void mergeHeadResourceNode(List<Node> nodes, Set<String>
renderedScripts, Node node) {
+ boolean shouldAdd = true;
+
+ String nodeName = node.getNodeName();
+ if (SCRIPT.equals(nodeName) || SCRIPT_UC.equals(nodeName)) {
+ if (node.getFirstChild() == null) {
+ //no text content etc.
+
+ NamedNodeMap attributes = node.getAttributes();
+ if (attributes != null) {
+ Node item = attributes.getNamedItem(SRC);
+ if (item == null) {
+ attributes.getNamedItem(SRC_UC);
+ }
+
+ if (item != null) {
+ String src = item.getNodeValue();
+ if (src != null) {
+ if (renderedScripts.contains(src)) {
+ shouldAdd = false;
+ } else {
+ renderedScripts.add(src);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (shouldAdd) {
+ nodes.add(node);
+ }
}
+
+ private Node[] mergeHeadResourceNodes() {
+ List<Node> result = new ArrayList<Node>();
+
+ Set<String> scripts = new HashSet<String>();
+
+ for (Node node : componentWriter.getNodes()) {
+ mergeHeadResourceNode(result, scripts, node);
+ }
+
+ for (Node node : userWriter.getNodes()) {
+ mergeHeadResourceNode(result, scripts, node);
+ }
+
+ return result.toArray(new Node[result.size()]);
+ }
+
+ public Node[] getHeadEvents() {
+ if (headNodes == null) {
+ headNodes = mergeHeadResourceNodes();
+ }
+
+ return headNodes;
+ }
private static final Log log = LogFactory.getLog(ViewResources.class);
@@ -374,7 +432,9 @@
skinExtendedStyleSheetUri = (String) skin.getParameter(context,
Skin.extendedStyleSheet);
} catch (SkinNotFoundException e) {
- log.warn("Current Skin is not found", e);
+ if (log.isWarnEnabled()) {
+ log.warn("Current Skin is not found", e);
+ }
}
resourceBuilder = InternetResourceBuilder.getInstance();
@@ -519,11 +579,13 @@
stringWriter.flush();
- log.info("Extended skinning is on and NONE scripts loading strategy was
detected. " +
- "Do not forget that one of " +
InternetResourceBuilder.SKINNING_SCRIPT + " or " +
- InternetResourceBuilder.COMMON_FRAMEWORK_SCRIPT + " resources should be
presented " +
- "on the page together with the following code: \n" +
stringWriter.getBuffer().toString() +
- "\nfor extended level of skinning to work.");
+ if (log.isInfoEnabled()) {
+ log.info("Extended skinning is on and NONE scripts loading strategy was
detected. " +
+ "Do not forget that one of " +
InternetResourceBuilder.SKINNING_SCRIPT + " or " +
+ InternetResourceBuilder.COMMON_FRAMEWORK_SCRIPT + " resources should be
presented " +
+ "on the page together with the following code: \n" +
stringWriter.getBuffer().toString() +
+ "\nfor extended level of skinning to work.");
+ }
} finally {
if (writer != null) {
context.setResponseWriter(writer);