Author: nbelaevski
Date: 2009-03-11 13:28:17 -0400 (Wed, 11 Mar 2009)
New Revision: 12930
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/tidy/TidyParser.java
Log:
https://jira.jboss.org/jira/browse/RF-3717
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/tidy/TidyParser.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/tidy/TidyParser.java 2009-03-11
17:15:54 UTC (rev 12929)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/tidy/TidyParser.java 2009-03-11
17:28:17 UTC (rev 12930)
@@ -26,7 +26,8 @@
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
-import java.util.LinkedList;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Properties;
import org.ajax4jsf.Messages;
@@ -144,6 +145,32 @@
}
}
+ private static final class NodeVisitor {
+ private List<org.w3c.dom.Node> viewStateSpans = new
ArrayList<org.w3c.dom.Node>();
+ private org.w3c.dom.Node head = null;
+
+ private boolean isViewState(Element element) {
+ return AjaxViewHandler.STATE_MARKER_KEY.equals(element.getAttribute("id"))
&&
+ AjaxViewHandler.STATE_MARKER_KEY.equals(element.getAttribute("name"));
+ }
+
+ private void traverse(org.w3c.dom.Node node) {
+ String nodeName = node.getNodeName();
+
+ if (head == null /* first head node only */ &&
"head".equals(nodeName)) {
+ head = node;
+ } else if ("span".equals(nodeName) && isViewState((Element) node))
{
+ viewStateSpans.add(node);
+ } else {
+ org.w3c.dom.Node child = node.getFirstChild();
+ while (child != null) {
+ traverse(child);
+ child = child.getNextSibling();
+ }
+ }
+ }
+ }
+
/*
* (non-Javadoc)
*
@@ -156,59 +183,52 @@
if (null != document) {
Element documentElement = document.getDocumentElement();
if (null != documentElement) {
+ NodeVisitor nodeVisitor = new NodeVisitor();
+ nodeVisitor.traverse(documentElement);
// Replace state elements with real stored.
-
- NodeList inputs = documentElement.getElementsByTagName("span");
- for (int i = 0; i < inputs.getLength(); i++) {
- Element node = (Element) inputs.item(i);
- if (AjaxViewHandler.STATE_MARKER_KEY.equals(node
- .getAttribute("id"))
- && AjaxViewHandler.STATE_MARKER_KEY.equals(node
- .getAttribute("name"))) {
- // State marker - replace with real.
- org.w3c.dom.Node parentNode = node.getParentNode();
- if (null != _viewState) {
- parentNode.replaceChild(document
- .createCDATASection(_viewState), node);
- } else {
- // Remove marker element, but keep it content.
- if (node.hasChildNodes()) {
- org.w3c.dom.Node nextSibling = node
- .getNextSibling();
- NodeList childNodes = node.getChildNodes();
- // Copy all nodes by temporary array ( since
- // moving nodes in iteration
- // modify NodeList with side effects.
- org.w3c.dom.Node[] childArray = new org.w3c.dom.Node[childNodes
- .getLength()];
- for (int j = 0; j < childArray.length; j++) {
- childArray[j] = childNodes.item(j);
- }
- for (int j = 0; j < childArray.length; j++) {
- parentNode.insertBefore(childArray[j],
- nextSibling);
- }
+
+ List<org.w3c.dom.Node> viewStateSpans = nodeVisitor.viewStateSpans;
+ for (org.w3c.dom.Node node : viewStateSpans) {
+ // State marker - replace with real.
+ org.w3c.dom.Node parentNode = node.getParentNode();
+ if (null != _viewState) {
+ parentNode.replaceChild(document
+ .createCDATASection(_viewState), node);
+ } else {
+ // Remove marker element, but keep it content.
+ if (node.hasChildNodes()) {
+ org.w3c.dom.Node nextSibling = node
+ .getNextSibling();
+ NodeList childNodes = node.getChildNodes();
+ // Copy all nodes by temporary array ( since
+ // moving nodes in iteration
+ // modify NodeList with side effects.
+ org.w3c.dom.Node[] childArray = new org.w3c.dom.Node[childNodes
+ .getLength()];
+ for (int j = 0; j < childArray.length; j++) {
+ childArray[j] = childNodes.item(j);
}
- parentNode.removeChild(node);
+ for (int j = 0; j < childArray.length; j++) {
+ parentNode.insertBefore(childArray[j],
+ nextSibling);
+ }
}
+ parentNode.removeChild(node);
}
}
-
+
// Inserts scripts and styles to head.
if ((null != headEvents && headEvents.length > 0) || null != _viewState)
{
// find head
- org.w3c.dom.Node head = documentElement
- .getElementsByTagName("head").item(0);
+ org.w3c.dom.Node head = nodeVisitor.head;
// Insert empty if not found
if (null == head) {
head = document.createElement("head");
documentElement.insertBefore(head, documentElement
.getFirstChild());
}
- NodeList headChildren = head.getChildNodes();
- org.w3c.dom.Node child = null;
- for (int i = 0; i < headChildren.getLength(); i++) {
- child = headChildren.item(i);
+ org.w3c.dom.Node child = head.getFirstChild();
+ while (child != null) {
if (child instanceof Element) {
String nodeName = ((Element) child).getNodeName();
if (!("title".equalsIgnoreCase(nodeName) || "base"
@@ -216,7 +236,8 @@
break;
}
}
- child = null;
+
+ child = child.getNextSibling();
}
if (headEvents != null) {
@@ -226,6 +247,7 @@
}
}
}
+
if (null != output) {
tidy.pprint(document, output);
}