Author: nbelaevski
Date: 2008-01-09 22:05:33 -0500 (Wed, 09 Jan 2008)
New Revision: 5255
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java
trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java
trunk/framework/impl/src/main/java/org/ajax4jsf/io/parser/FastHtmlParser.java
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/HtmlParser.java
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/nekko/NekkoParser.java
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/tidy/TidyParser.java
trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js
Log:
http://jira.jboss.com/jira/browse/RF-1627
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java 2008-01-10
03:02:51 UTC (rev 5254)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -27,8 +27,8 @@
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.LinkedHashSet;
-import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@@ -444,6 +444,7 @@
}
requestMap.put(SCRIPTS_PARAMETER, scripts);
}
+
Set styles = viewResources.getStyles();
if (styles.size() > 0) {
if (log.isDebugEnabled()) {
@@ -457,6 +458,20 @@
}
requestMap.put(STYLES_PARAMETER, styles);
}
+
+ Set usersStyles = viewResources.getUserStyles();
+ if (usersStyles.size() > 0) {
+ if (log.isDebugEnabled()) {
+ StringBuffer buff = new StringBuffer(
+ "User styles for insert into head : \n");
+ for (Iterator iter = usersStyles.iterator(); iter.hasNext();) {
+ String style = (String) iter.next();
+ buff.append(style).append("\n");
+ }
+ log.debug(buff.toString());
+ }
+ requestMap.put(USER_STYLES_PARAMETER, usersStyles);
+ }
// Mark as processed.
requestMap.put(RESOURCES_PROCESSED, Boolean.TRUE);
// Save viewId for a parser selection
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java 2008-01-10
03:02:51 UTC (rev 5254)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/context/ViewResources.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -39,7 +39,7 @@
renderKit = rkFactory.getRenderKit(context, context
.getViewRoot().getRenderKitId());
processHeadResources(context, root);
- styles.addAll(userStyles);
+ //styles.addAll(userStyles);
scripts.addAll(userScripts);
}
@@ -156,6 +156,13 @@
return styles;
}
+ /**
+ * @return user styles
+ */
+ public Set getUserStyles() {
+ return userStyles;
+ }
+
public void addScript(String scriptUrl) {
scripts.add(scriptUrl);
}
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/io/parser/FastHtmlParser.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/io/parser/FastHtmlParser.java 2008-01-10
03:02:51 UTC (rev 5254)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/io/parser/FastHtmlParser.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -50,6 +50,8 @@
private Set styles;
+ private Set userStyles;
+
private String encoding;
private String doctype;
@@ -62,7 +64,7 @@
boolean haveHtml = false;
boolean haveHead = false;
boolean closingElement = false;
- if (null != scripts || null != styles) {
+ if (null != scripts || null != styles || null != userStyles) {
ParsingStateManager stateManager = ParsingStateManager
.getInstance();
ParserState state = stateManager.getInitialState();
@@ -174,11 +176,21 @@
if (null != styles) {
for (Iterator iter = styles.iterator(); iter.hasNext();) {
String style = (String) iter.next();
- out.write("<link rel='stylesheet' type='text/css'
href='");
+ out.write("<link rel='stylesheet' class='" +
COMPONENT_RESOURCE_LINK_CLASS + "'" +
+ " type='text/css' href='");
out.write(style);
out.write("' />");
}
}
+ if (null != userStyles) {
+ for (Iterator iter = userStyles.iterator(); iter.hasNext();) {
+ String style = (String) iter.next();
+ out.write("<link rel='stylesheet' class='" +
USER_RESOURCE_LINK_CLASS + "'" +
+ " type='text/css' href='");
+ out.write(style);
+ out.write("' />");
+ }
+ }
if (null != scripts) {
for (Iterator iter = scripts.iterator(); iter.hasNext();) {
String script = (String) iter.next();
@@ -223,6 +235,21 @@
this.styles = styles;
}
+ /**
+ * @return Returns the user styles.
+ */
+ public Set getUserStyles() {
+ return userStyles;
+ }
+
+ /**
+ * @param styles
+ * The user styles to set.
+ */
+ public void setUserStyles(Set userStyles) {
+ this.userStyles = userStyles;
+ }
+
public void parseHtml(InputStream input, Writer output) throws IOException {
parse(new InputStreamReader(input, encoding), output);
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java 2008-01-10
03:02:51 UTC (rev 5254)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseXMLFilter.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -293,6 +293,8 @@
.getAttribute(AjaxContext.SCRIPTS_PARAMETER));
parser.setStyles((Set) request
.getAttribute(AjaxContext.STYLES_PARAMETER));
+ parser.setUserStyles((Set) request
+ .getAttribute(AjaxContext.USER_STYLES_PARAMETER));
// Process parsing.
long startTimeMills = System.currentTimeMillis();
servletResponseWrapper.parseContent(output, parser);
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/HtmlParser.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/HtmlParser.java 2008-01-10
03:02:51 UTC (rev 5254)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/HtmlParser.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -59,6 +59,11 @@
*/
public abstract void setStyles(Set styles);
+ /**
+ * @param styles The user styles to set
+ */
+ public abstract void setUserStyles(Set styles);
+
public abstract void setDoctype(String doctype);
/**
@@ -68,4 +73,7 @@
public abstract boolean setMime(String mimeType);
+ public static final String COMPONENT_RESOURCE_LINK_CLASS = "component";
+
+ public static final String USER_RESOURCE_LINK_CLASS = "user";
}
\ No newline at end of file
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/nekko/NekkoParser.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/nekko/NekkoParser.java 2008-01-10
03:02:51 UTC (rev 5254)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/nekko/NekkoParser.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -94,6 +94,8 @@
private Set _styles;
+ private Set _userStyles;
+
private String _viewState;
private String _encoding;
@@ -163,6 +165,7 @@
public void reset() {
_scripts = null;
_styles = null;
+ _userStyles = null;
_viewState = null;
_parser.reset();
_serializer.reset();
@@ -289,6 +292,10 @@
}
+ public void setUserStyles(Set styles) {
+ _userStyles = styles;
+ }
+
/*
* (non-Javadoc)
*
@@ -421,23 +428,37 @@
super.endElement(element, augs);
}
+ private void insertStyle(String style, String className) {
+ QName element = new QName(null, "link", "link", null);
+ XMLAttributes attrs = new XMLAttributesImpl();
+ attrs.addAttribute(new QName(null, "href", "href", null),
+ "CDATA", style);
+ attrs.addAttribute(new QName(null, "type", "type", null),
+ "CDATA", "text/css");
+ attrs.addAttribute(new QName(null, "rel", "rel", null),
+ "CDATA", "stylesheet");
+ attrs.addAttribute(new QName(null, "class", "class", null),
+ "CDATA", className);
+ Augmentations augs = new HTMLAugmentations();
+ super.emptyElement(element, attrs, augs);
+ }
+
private void insertResources() {
headParsed = true;
if (null != _styles) {
for (Iterator iter = _styles.iterator(); iter.hasNext();) {
String style = (String) iter.next();
- QName element = new QName(null, "link", "link", null);
- XMLAttributes attrs = new XMLAttributesImpl();
- attrs.addAttribute(new QName(null, "href", "href", null),
- "CDATA", style);
- attrs.addAttribute(new QName(null, "type", "type", null),
- "CDATA", "text/css");
- attrs.addAttribute(new QName(null, "rel", "rel", null),
- "CDATA", "stylesheet");
- Augmentations augs = new HTMLAugmentations();
- super.emptyElement(element, attrs, augs);
+ insertStyle(style, COMPONENT_RESOURCE_LINK_CLASS);
}
}
+
+ if (null != _userStyles) {
+ for (Iterator iter = _userStyles.iterator(); iter.hasNext();) {
+ String style = (String) iter.next();
+ insertStyle(style, USER_RESOURCE_LINK_CLASS);
+ }
+ }
+
if (null != _scripts) {
for (Iterator iter = _scripts.iterator(); iter.hasNext();) {
String script = (String) iter.next();
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 2008-01-10
03:02:51 UTC (rev 5254)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/tidy/TidyParser.java 2008-01-10
03:05:33 UTC (rev 5255)
@@ -59,6 +59,8 @@
private Set _styles;
+ private Set _userStyles;
+
private String _viewState;
private String _encoding;
@@ -138,7 +140,7 @@
}
// Inserts scripts and styles to head.
- if (null != _scripts || null != _styles || null != _viewState) {
+ if (null != _scripts || null != _styles || null != _userStyles || null != _viewState)
{
// find head
org.w3c.dom.Node head = documentElement
.getElementsByTagName("head").item(0);
@@ -168,9 +170,21 @@
style.setAttribute("type", "text/css");
style.setAttribute("rel", "stylesheet");
style.setAttribute("href", url);
+ style.setAttribute("class", COMPONENT_RESOURCE_LINK_CLASS);
head.insertBefore(style, child);
}
}
+ if (null != _userStyles) {
+ for (Iterator iter = _userStyles.iterator(); iter.hasNext();) {
+ String url = (String) iter.next();
+ Element style = document.createElement("link");
+ style.setAttribute("type", "text/css");
+ style.setAttribute("rel", "stylesheet");
+ style.setAttribute("href", url);
+ style.setAttribute("class", USER_RESOURCE_LINK_CLASS);
+ head.insertBefore(style, child);
+ }
+ }
// Scripts
if (null != _scripts) {
for (Iterator iter = _scripts.iterator(); iter
@@ -385,6 +399,10 @@
public void setStyles(Set styles) {
this._styles = styles;
}
+
+ public void setUserStyles(Set styles) {
+ this._userStyles = styles;
+ }
/*
* (non-Javadoc)
Modified: trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js
===================================================================
--- trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js 2008-01-10 03:02:51 UTC
(rev 5254)
+++ trunk/framework/impl/src/main/javascript/ajaxjsf/JSFAJAX.js 2008-01-10 03:05:33 UTC
(rev 5255)
@@ -392,24 +392,58 @@
},
appendNewHeadElements : function(){
- // Append scripts and styles to head, if not presented in page before.
-
this._appendNewElements("script","src",["type","language","charset"]);
-
this._appendNewElements("link","href",["type","rel","rev","media"]);
+ // Append scripts and styles to head, if not presented in page before.
+
this._appendNewElements("script","src",null,null,["type","language","charset"]);
+
this._appendNewElements("link","href","class",["component","user"],["type","rel","rev","media"],{"class":
"className"});
},
- _appendNewElements : function(tag,href,attributes){
+ _appendNewElements : function(tag,href,role,roles,attributes,mappings){
+ var head =
document.getElementsByTagName("head")[0]||document.documentElement;
var newscripts = this.getElementsByTagName(tag);
var oldscripts = document.getElementsByTagName(tag);
- var head =
document.getElementsByTagName("head")[0]||document.documentElement;
+ var mappedRole = (mappings && mappings[role]) || role;
+
+ var roleAnchors = {};
+ if (roles) {
+ var i = 0;
+
+ for(var j = 0; j < oldscripts.length; j++){
+ var oldscript = oldscripts[j];
+ var scriptRole = oldscript[mappedRole];
+
+ for ( ; i < roles.length && roles[i] != scriptRole; i++) {
+ roleAnchors[roles[i]] = oldscript;
+ }
+
+ if (i == roles.length) {
+ break;
+ }
+ }
+ }
+
for(var i=0 ; i<newscripts.length;i++){
var element = newscripts[i];
var src = element.getAttribute(href);
+ var elementRole;
+
+ if (roles) {
+ elementRole = element.getAttribute(role);
+ }
+
if(src){
var exist = false;
LOG.debug("<"+tag+"> in response with
src="+src);
for(var j = 0 ; j < oldscripts.length; j++){
if(this._noSessionHref(src) ==
this._noSessionHref(oldscripts[j].getAttribute(href))){
LOG.debug("Such element exist in document");
+
+ if (role) {
+ var oldRole = oldscripts[j][mappedRole];
+ if ((!elementRole ^ !oldRole) || (elementRole && oldRole
&& elementRole != oldRole)) {
+ LOG.warn("Roles are different");
+ }
+ }
+
exist = true;
break;
}
@@ -421,8 +455,19 @@
for(var j = 0 ; j < attributes.length; j++){
this._copyAttribute(element,script,attributes[j]);
}
+
+ if (elementRole) {
+ script[mappedRole] = elementRole;
+ }
+
LOG.debug("append element to document");
- head.appendChild(script);
+
+ var anchor = roleAnchors[elementRole];
+ if (anchor && anchor.parentNode) {
+ anchor.parentNode.insertBefore(script, anchor);
+ } else {
+ head.appendChild(script);
+ }
}
}
}