From richfaces-svn-commits at lists.jboss.org Wed Sep 8 15:14:50 2010 Content-Type: multipart/mixed; boundary="===============8486541039917368506==" MIME-Version: 1.0 From: richfaces-svn-commits at lists.jboss.org To: richfaces-svn-commits at lists.jboss.org Subject: [richfaces-svn-commits] JBoss Rich Faces SVN: r19137 - in trunk/core: api/src/test/java/org/ajax4jsf/javascript and 5 other directories. Date: Wed, 08 Sep 2010 15:14:50 -0400 Message-ID: <201009081914.o88JEo7Y019812@svn01.web.mwc.hst.phx2.redhat.com> --===============8486541039917368506== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Author: nbelaevski Date: 2010-09-08 15:14:49 -0400 (Wed, 08 Sep 2010) New Revision: 19137 Added: trunk/core/api/src/test/java/org/richfaces/renderkit/ trunk/core/api/src/test/java/org/richfaces/renderkit/util/ trunk/core/api/src/test/java/org/richfaces/renderkit/util/IdSplitBuilder= Test.java Removed: trunk/core/impl/src/main/java/org/richfaces/renderkit/util/AjaxRendererU= tils.java trunk/core/impl/src/main/java/org/richfaces/renderkit/util/IdSplitBuilde= r.java trunk/core/impl/src/main/java/org/richfaces/renderkit/util/RendererUtils= .java trunk/core/impl/src/test/java/org/richfaces/renderkit/util/ Modified: trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java trunk/core/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java Log: RF-7560 - synchronized unmerged files Modified: trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.ja= va =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java 201= 0-09-08 17:07:06 UTC (rev 19136) +++ trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSEncoder.java 201= 0-09-08 19:14:49 UTC (rev 19137) @@ -31,13 +31,14 @@ = // private char APOSTROPHE[] =3D { '\\', '\'' }; private static final char[] ENCODE_HEX =3D "0123456789ABCDEF".toCharAr= ray(); - private static final char[] ENCODE_APOS =3D "\\'".toCharArray(); private static final char[] ENCODE_QUOT =3D "\\\"".toCharArray(); private static final char[] ENCODE_LF =3D "\\n".toCharArray(); + private static final char[] ENCODE_BC =3D "\\b".toCharArray(); private static final char[] ENCODE_FF =3D "\\f".toCharArray(); private static final char[] ENCODE_CR =3D "\\r".toCharArray(); private static final char[] ENCODE_TAB =3D "\\t".toCharArray(); private static final char[] ENCODE_BS =3D "\\\\".toCharArray(); + private static final char[] ENCODE_FS =3D "\\/".toCharArray(); = // private static final char ENCODE_ESC[] =3D "\\e".toCharArray(); = @@ -47,32 +48,34 @@ public JSEncoder() {} = /** - * Return true or false wether this encoding can encode the specified + * Return true or false whether this encoding/format can encode the sp= ecified * character or not. *
* This method will return true for the following character range:
*
- *
*
StringBuffer
with Javascript code
- */
- public static StringBuffer buildOnClick(UIComponent uiComponent, Faces=
Context facesContext) {
- return buildOnClick(uiComponent, facesContext, false);
- }
-
- /**
- * Build JavaScript onclick event for given component
- *
- * @param uiComponent -
- * component for build event
- * @param facesContext
- * @param omitDefaultActionUrl - default action URL is not encoded if =
parameter is true
- * @return StringBuffer
with Javascript code
- */
- public static StringBuffer buildOnClick(UIComponent uiComponent, Faces=
Context facesContext,
- boolean omitDefaultActionUrl) {
- return buildOnEvent(uiComponent, facesContext, HTML.ONCLICK_ATTRIB=
UTE, omitDefaultActionUrl);
- }
-
- /**
- * Build JavaScript event for component
- *
- * @param uiComponent -
- * component for build event
- * @param facesContext
- * @param eventName -
- * name of event
- * @return StringBuffer
with Javascript code
- */
- public static StringBuffer buildOnEvent(UIComponent uiComponent, Faces=
Context facesContext, String eventName) {
- return buildOnEvent(uiComponent, facesContext, eventName, false);
- }
-
- /**
- * Build JavaScript event for component
- *
- * @param uiComponent -
- * component for build event
- * @param facesContext
- * @param eventName -
- * name of event
- * @param omitDefaultActionUrl - default action URL is not encoded if =
parameter is true
- * @return StringBuffer
with Javascript code
- */
- public static StringBuffer buildOnEvent(UIComponent uiComponent, Faces=
Context facesContext, String eventName,
- boolean omitDefaultActionUrl) {
- StringBuffer onEvent =3D new StringBuffer();
-
-// if (null !=3D eventName) {
-// String commandOnEvent =3D (String) uiComponent.getAttributes()=
.get(
-// eventName);
-// if (commandOnEvent !=3D null) {
-// onEvent.append(commandOnEvent);
-// onEvent.append(';');
-// }
-// }
-// JSFunction ajaxFunction =3D buildAjaxFunction(uiComponent, facesCo=
ntext);
-// // Create formal parameter for non-input elements ???
-// // Link Control pseudo-object
-// // Options map. Possible options for function call :
-// // control - name of form control for submit.
-// // name - name for link control \
-// // value - value of control. - possible replace by parameters ?
-// // single true/false - submit all form or only one control.
-// // affected - array of element's ID for update on responce.
-// // oncomplete - function for call after complete request.
-// // status - id of request status component.
-// // parameters - map of parameters name/value for append on request.
-// // ..........
-// ajaxFunction.addParameter(buildEventOptions(facesContext, uiCompon=
ent, omitDefaultActionUrl));
-//
-// // appendAjaxSubmitParameters(facesContext, uiComponent, onEvent);
-// ajaxFunction.appendScript(onEvent);
-// if (uiComponent instanceof AjaxSupport) {
-// AjaxSupport support =3D (AjaxSupport) uiComponent;
-// if (support.isDisableDefault()) {
-// onEvent.append("; return false;");
-// }
-// }
-// LOG.debug(Messages.getMessage(Messages.BUILD_ONCLICK_INFO, uiCompo=
nent
-// .getId(), onEvent.toString()));
- return onEvent;
- }
-
- public static AjaxEventOptions buildEventOptions(FacesContext facesCon=
text, UIComponent component) {
- return buildEventOptions(facesContext, component, null);
- }
-
- public static AjaxEventOptions buildEventOptions(FacesContext facesCon=
text, UIComponent component,
- AjaxClientBehavior aj=
axBehavior) {
- AjaxEventOptions ajaxEventOptions =3D new AjaxEventOptions();
- Mapnull
- */
- public static String getAjaxStatus(UIComponent component) {
- String statusId;
-
- if (component instanceof AjaxComponent) {
- statusId =3D ((AjaxComponent) component).getStatus();
- } else {
- statusId =3D (String) component.getAttributes().get(STATUS_ATT=
R_NAME);
- }
-
- return statusId;
-
-// if (null !=3D statusId) {
-// UIComponent status =3D RendererUtils.getInstance().
-// findComponentFor(component, statusId);
-//
-// if (null !=3D status) {
-// statusId =3D status
-// .getClientId(FacesContext.getCurrentInstance());
-// } else {
-// LOG.warn(Messages.getMessage(
-// Messages.AJAX_STATUS_COMPONENT_NOT_FOWND_WARNING,
-// component.getId()));
-// }
-// }
-// return statusId;
- }
-
- public static String getQueueId(UIComponent component) {
- return (String) component.getAttributes().get(QUEUE_ID_ATTRIBUTE);
- }
- =
- public static JSFunctionDefinition buildAjaxOncomplete(String body) {
- JSFunctionDefinition function =3D new JSFunctionDefinition("reques=
t", "event", "data");
-
- function.addToBody(body);
-
- return function;
- }
-
- public static JSFunctionDefinition buildAjaxOnBeforeDomUpdate(String b=
ody) {
- JSFunctionDefinition function =3D new JSFunctionDefinition("reques=
t", "event", "data");
-
- function.addToBody(body);
-
- return function;
- }
-
- /**
- * Get function name for call on completed ajax request.
- *
- * @param component for wich calculate function name
- * @return name of JavaScript function or null
- */
- //TODO nick - refactor - remove this method?
- public static String getAjaxOncomplete(UIComponent component) {
- if (component instanceof AjaxComponent) {
- return ((AjaxComponent) component).getOncomplete();
- }
-
- return (String) component.getAttributes().get(ONCOMPLETE_ATTR_NAME=
);
- }
-
- /**
- * Get function name for call before update DOM.
- *
- * @param component for wich calculate function name
- * @return name of JavaScript function or null
- */
- //TODO nick - refactor - remove this method?
- public static String getAjaxOnBeforeDomUpdate(UIComponent component) {
- if (component instanceof AjaxComponent) {
- return ((AjaxComponent) component).getOnbeforedomupdate();
- }
-
- return (String) component.getAttributes().get(ONBEFOREDOMUPDATE_AT=
TR_NAME);
- }
-
- //TODO nick - refactor - remove this method?
- public static String getAjaxOnBegin(UIComponent component) {
- if (component instanceof AjaxComponent) {
- return ((AjaxComponent) component).getOnbegin();
- }
-
- return (String) component.getAttributes().get(ONBEGIN_ATTR_NAME);
- }
-
- /**
- * @param component
- * @return
- * @since 4.0
- */
- public static Object getAjaxData(UIComponent component) {
- if (component instanceof AjaxComponent) {
- return ((AjaxComponent) component).getData();
- }
-
- return component.getAttributes().get(DATA_ATTR_NAME);
- }
-
- /**
- * Calculate, must be component render only given areas, or all sended=
from
- * server.
- *
- * @param component
- * @return true
if client must render ONLY given areas.
- */
- public static boolean isAjaxLimitRender(UIComponent component) {
- boolean result =3D false;
-
- if (component instanceof AjaxComponent) {
- result =3D ((AjaxComponent) component).isLimitRender();
- } else {
- try {
- result =3D ((Boolean) component.getAttributes().get(LIMITR=
ENDER_ATTR_NAME)).booleanValue();
- } catch (NullPointerException e) {
-
- // NullPointer - ignore ...
- } catch (ClassCastException e1) {
-
- // not Boolean - false ...
- }
- }
-
- return result;
- }
-
- /**
- * Replacement for buggy in MyFaces RendererUtils
- *
- * @param component
- * @return
- */
- public static String getAbsoluteId(UIComponent component) {
- if (component =3D=3D null) {
- throw new NullPointerException(Messages.getMessage(Messages.CO=
MPONENT_NULL_ERROR_2));
- }
-
- StringBuffer idBuf =3D new StringBuffer();
-
- idBuf.append(component.getId());
-
- UIComponent parent =3D component;
-
- while ((parent =3D parent.getParent()) !=3D null) {
- if (parent instanceof NamingContainer) {
- idBuf.insert(0, NamingContainer.SEPARATOR_CHAR);
- idBuf.insert(0, parent.getId());
- }
- }
-
- idBuf.insert(0, NamingContainer.SEPARATOR_CHAR);
- LOG.debug(Messages.getMessage(Messages.CALCULATE_COMPONENT_ID_INFO=
, component.getId(), idBuf.toString()));
-
- return idBuf.toString();
- }
-
- /**
- * Find nested form for given component
- *
- * @param component
- * @return nested UIForm
component, or null
- */
- public static UIComponent getNestingForm(UIComponent component) {
- UIComponent parent =3D component;
-
- // Search enclosed UIForm or ADF UIXForm component
- while ((parent !=3D null) && !(parent instanceof UIForm)
- && !("org.apache.myfaces.trinidad.Form".equals(parent.getFamil=
y()))
- && !("oracle.adf.Form".equals(parent.getFamily()))) {
- parent =3D parent.getParent();
- }
-
- return parent;
- }
-
- protected static String getAjaxActionUrl(FacesContext facesContext) {
- return AjaxContext.getCurrentInstance(facesContext).getAjaxActionU=
RL(facesContext);
- }
-
-// /**
-// * Encode rendered areas as special HTML tag ( span in current relea=
se )
-// *
-// * @param context
-// * @param component
-// * @throws IOException
-// */
-// public static void encodeAreas(FacesContext context, UIComponent com=
ponent) throws IOException {
-// AjaxContext ajaxContext =3D AjaxContext.getCurrentInstance(conte=
xt);
-// ExternalContext externalContext =3D context.getExternalContext();
-// MapfunctionCode= object into: - *
function(event) { - * functionCode - * }- */ - EVENT_HANDLER { - @Override - Object wrap(Object o) { - return new JSFunctionDefinition("event").addToBody(o); - } - }; - - /** - * Method that does the wrapping - * - * @param o object to wrap - * @return wrapped object - */ - abstract Object wrap(Object o); - } - - /** - * Use this method to get singleton instance of RendererUtils - * @return singleton instance - */ - public static RendererUtils getInstance() { - return INSTANCE; - } - - /** - * Encode id attribute with clientId component property - * - * @param context - * @param component - * @throws IOException - */ - public void encodeId(FacesContext context, UIComponent component) thro= ws IOException { - encodeId(context, component, HTML.ID_ATTRIBUTE); - } - - /** - * Encode clientId to custom attribute ( for example, to control name ) - * - * @param context - * @param component - * @param attribute - * @throws IOException - */ - public void encodeId(FacesContext context, UIComponent component, Stri= ng attribute) throws IOException { - String clientId =3D null; - - try { - clientId =3D component.getClientId(context); - } catch (Exception e) { - - // just ignore if clientId wasn't inited yet - } - - if (null !=3D clientId) { - context.getResponseWriter().writeAttribute(attribute, clientId, - (String) getComponentAttributeName(attribute)); - } - } - - /** - * Encode id attribute with clientId component property. Encoded only = if id - * not auto generated. - * - * @param context - * @param component - * @throws IOException - */ - public void encodeCustomId(FacesContext context, UIComponent component= ) throws IOException { - if (hasExplicitId(component)) { - context.getResponseWriter().writeAttribute(HTML.ID_ATTRIBUTE, = component.getClientId(context), - HTML.ID_ATTRIBUTE); - } - } - - public Map
attributeVal
=
is
- * an instance of a wrapper for a primitive type and its value=
is
- * equal to the default value for that type as given in the sp=
ec.
- */
- public boolean shouldRenderAttribute(Object attributeVal) {
- if (null =3D=3D attributeVal) {
- return false;
- } else if ((attributeVal instanceof Boolean)
- && ((Boolean) attributeVal).booleanValue() =3D=3D Boole=
an.FALSE.booleanValue()) {
- return false;
- } else if (attributeVal.toString().length() =3D=3D 0) {
- return false;
- } else {
- return isValidProperty(attributeVal);
- }
- }
-
- public boolean shouldRenderAttribute(String attributeName, Object attr=
ibuteVal) {
- if (REQUIRED_ATTRIBUTES.contains(attributeName)) {
- if (attributeVal =3D=3D null) {
- return false;
- }
- } else {
- return shouldRenderAttribute(attributeVal);
- }
-
- return true;
- }
-
- /**
- * Test for valid value of property. by default, for non-setted proper=
ties
- * with Java primitive types of JSF component return appropriate MIN_V=
ALUE .
- *
- * @param property -
- * value of property returned from
- * {@link UIComponent#getAttributes()}
- * @return true for setted property, false otherthise.
- */
- public boolean isValidProperty(Object property) {
- if (null =3D=3D property) {
- return false;
- } else if ((property instanceof Integer) && ((Integer) property).i=
ntValue() =3D=3D Integer.MIN_VALUE) {
- return false;
- } else if ((property instanceof Double) && ((Double) property).dou=
bleValue() =3D=3D Double.MIN_VALUE) {
- return false;
- } else if ((property instanceof Character) && ((Character) propert=
y).charValue() =3D=3D Character.MIN_VALUE) {
- return false;
- } else if ((property instanceof Float) && ((Float) property).float=
Value() =3D=3D Float.MIN_VALUE) {
- return false;
- } else if ((property instanceof Short) && ((Short) property).short=
Value() =3D=3D Short.MIN_VALUE) {
- return false;
- } else if ((property instanceof Byte) && ((Byte) property).byteVal=
ue() =3D=3D Byte.MIN_VALUE) {
- return false;
- } else if ((property instanceof Long) && ((Long) property).longVal=
ue() =3D=3D Long.MIN_VALUE) {
- return false;
- }
-
- return true;
- }
-
- /**
- * Checks if the argument passed in is empty or not.
- * Object is empty if it is: null
true
if the argument is empty, false otherwise
- */
- public boolean isEmpty(Object o) {
- if (null =3D=3D o) {
- return true;
- }
-
- if (o instanceof String) {
- return 0 =3D=3D ((String) o).length();
- }
-
- if (o instanceof Collection>) {
- return ((Collection>) o).isEmpty();
- }
-
- if (o instanceof Map, ?>) {
- return ((Map, ?>) o).isEmpty();
- }
-
- if (o.getClass().isArray()) {
- return Array.getLength(o) =3D=3D 0;
- }
-
- return false;
- }
-
- /**
- * Puts value into map under specified key if the value is not empty a=
nd not default.
- * Performs optional value wrapping.
- *
- * @param hash
- * @param name
- * @param value
- * @param defaultValue
- * @param wrapper
- *
- * @since 3.3.2
- */
- public void addToScriptHash(Map hash, String name, Obj=
ect value, String defaultValue,
- ScriptHashVariableWrapper wrapper) {
- ScriptHashVariableWrapper wrapperOrDefault =3D (wrapper !=3D null)=
? wrapper : ScriptHashVariableWrapper.DEFAULT;
-
- if (isValidProperty(value) && !isEmpty(value)) {
- if (!isEmpty(defaultValue)) {
- if (!defaultValue.equals(value.toString())) {
- hash.put(name, wrapperOrDefault.wrap(value));
- }
- } else {
- if (!(value instanceof Boolean) || ((Boolean) value).boole=
anValue()) {
- hash.put(name, wrapperOrDefault.wrap(value));
- }
- }
- }
- }
-
- /**
- * Puts value into map under specified key if the value is not empty a=
nd not default.
- * Performs optional value wrapping.
- *
- * @param hash
- * @param name
- * @param value
- * @param defaultValue
- *
- * @since 3.3.2
- */
- public void addToScriptHash(Map hash, String name, Obj=
ect value, String defaultValue) {
- addToScriptHash(hash, name, value, defaultValue, null);
- }
-
- /**
- * Puts value into map under specified key if the value is not empty a=
nd not default.
- * Performs optional value wrapping.
- *
- * @param hash
- * @param name
- * @param value
- *
- * @since 3.3.2
- */
- public void addToScriptHash(Map hash, String name, Obj=
ect value) {
- addToScriptHash(hash, name, value, null, null);
- }
-
- /**
- * Convert HTML attribute name to component property name.
- *
- * @param key
- * @return
- */
- protected Object getComponentAttributeName(Object key) {
- Object converted =3D SUBSTITUTIONS.get(key);
-
- if (null =3D=3D converted) {
- return key;
- } else {
- return converted;
- }
- }
-
- /**
- * Convert attribute value to proper object. For known html boolean
- * attributes return name for true value, otherthise - null. For non-b=
oolean
- * attributes return same value.
- *
- * @param name
- * attribute name.
- * @param value
- * @return
- */
- protected Object attributeValue(String name, Object value) {
- if (null =3D=3D value || Arrays.binarySearch(HTML.PASS_THRU_BOOLEA=
N, name) < 0) {
- return value;
- }
-
- boolean checked;
-
- if (value instanceof Boolean) {
- checked =3D ((Boolean) value).booleanValue();
- } else {
- checked =3D Boolean.parseBoolean(value.toString());
- }
-
- return checked ? name : null;
- }
-
- /**
- * Get boolean value of logical attribute
- *
- * @param component
- * @param name
- * attribute name
- * @return true if attribute is equals Boolean.TRUE or String "true" ,=
false
- * otherwise.
- */
- public boolean isBooleanAttribute(UIComponent component, String name) {
- Object attrValue =3D component.getAttributes().get(name);
- boolean result =3D false;
-
- if (null !=3D attrValue) {
- if (attrValue instanceof String) {
- result =3D "true".equalsIgnoreCase((String) attrValue);
- } else {
- result =3D Boolean.TRUE.equals(attrValue);
- }
- }
-
- return result;
- }
-
- /**
- * Return converted value for {@link javax.faces.component.ValueHolder=
} as
- * String, perform nessesary convertions.
- *
- * @param context
- * @param component
- * @return
- */
- public String getValueAsString(FacesContext context, UIComponent compo=
nent) {
-
- // First - get submitted value for input components
- if (component instanceof EditableValueHolder) {
- EditableValueHolder input =3D (EditableValueHolder) component;
- String submittedValue =3D (String) input.getSubmittedValue();
-
- if (null !=3D submittedValue) {
- return submittedValue;
- }
- }
-
- // If no submitted value presented - convert same for UIInput/UIOu=
tput
- if (component instanceof ValueHolder) {
- return formatValue(context, component, ((ValueHolder) componen=
t).getValue());
- } else {
- throw new IllegalArgumentException(
- Messages.getMessage(Messages.CONVERTING_NON_VALUE_HOLDER_C=
OMPONENT_ERROR, component.getId()));
- }
- }
-
- /**
- * Convert any object value to string. If component instance of
- * {@link ValueHolder } got {@link Converter} for formatting. If not,
- * attempt to use converter based on value type.
- *
- * @param context
- * @param component
- * @return
- */
- public String formatValue(FacesContext context, UIComponent component,=
Object value) {
- if (value instanceof String) {
- return (String) value;
- }
-
- Converter converter =3D null;
-
- if (component instanceof ValueHolder) {
- ValueHolder holder =3D (ValueHolder) component;
-
- converter =3D holder.getConverter();
- }
-
- if ((null =3D=3D converter) && (null !=3D value)) {
- try {
- converter =3D context.getApplication().createConverter(val=
ue.getClass());
- } catch (FacesException e) {
-
- // TODO - log converter exception.
- }
- }
-
- if (null =3D=3D converter) {
- if (null !=3D value) {
- return value.toString();
- }
- } else {
- return converter.getAsString(context, component, value);
- }
-
- return "";
- }
-
- public String encodePx(String value) {
- return HtmlDimensions.formatPx(HtmlDimensions.decode(value));
- }
-
- /**
- * formats given value to
- *
- * @param value
- *
- * @return
- */
- public String encodePctOrPx(String value) {
- if (value.indexOf('%') > 0) {
- return value;
- } else {
- return encodePx(value);
- }
- }
-
- /**
- * Find nested form for given component
- *
- * @param component
- * @return nested UIForm
component, or null
- */
- public UIForm getNestingForm(FacesContext context, UIComponent compone=
nt) {
- UIComponent parent =3D component.getParent();
-
- while ((parent !=3D null) && !(parent instanceof UIForm)) {
- parent =3D parent.getParent();
- }
-
- UIForm nestingForm =3D null;
-
- if (parent !=3D null) {
-
- // link is nested inside a form
- nestingForm =3D (UIForm) parent;
- }
-
- return nestingForm;
- }
-
- /**
- * @param context
- * @param component
- * @return
- * @throws IOException
- */
- public void encodeBeginFormIfNessesary(FacesContext context, UICompone=
nt component) throws IOException {
- UIForm form =3D getNestingForm(context, component);
-
- if (null =3D=3D form) {
- ResponseWriter writer =3D context.getResponseWriter();
- String clientId =3D component.getClientId(context) + DUMMY_FOR=
M_ID;
-
- encodeBeginForm(context, component, writer, clientId);
-
- // writer.writeAttribute(HTML.STYLE_ATTRIBUTE, "margin:0;
- // padding:0;", null);
- }
- }
-
- /**
- * @param context
- * @param component
- * @param writer
- * @param clientId
- * @throws IOException
- */
- public void encodeBeginForm(FacesContext context, UIComponent componen=
t, ResponseWriter writer, String clientId)
- throws IOException {
-
- String actionURL =3D getActionUrl(context);
- String encodeActionURL =3D context.getExternalContext().encodeActi=
onURL(actionURL);
-
- writer.startElement(HTML.FORM_ELEMENT, component);
- writer.writeAttribute(HTML.ID_ATTRIBUTE, clientId, null);
- writer.writeAttribute(HTML.NAME_ATTRIBUTE, clientId, null);
- writer.writeAttribute(HTML.METHOD_ATTRIBUTE, "post", null);
- writer.writeAttribute(HTML.STYLE_ATTRIBUTE, "margin:0; padding:0; =
display: inline;", null);
- writer.writeURIAttribute(HTML.ACTION_ATTRIBUTE, encodeActionURL, "=
action");
- }
-
- /**
- * @param context
- * @param component
- * @throws IOException
- */
- public void encodeEndFormIfNessesary(FacesContext context, UIComponent=
component) throws IOException {
- UIForm form =3D getNestingForm(context, component);
-
- if (null =3D=3D form) {
- ResponseWriter writer =3D context.getResponseWriter();
-
- // TODO - hidden form parameters ?
- encodeEndForm(context, writer);
- }
- }
-
- /**
- * @param context
- * @param writer
- * @throws IOException
- */
- public void encodeEndForm(FacesContext context, ResponseWriter writer)=
throws IOException {
- AjaxRendererUtils.writeState(context);
- writer.endElement(HTML.FORM_ELEMENT);
- }
-
- /**
- * @param facesContext
- * @return String A String representing the action URL
- */
- public String getActionUrl(FacesContext facesContext) {
- ViewHandler viewHandler =3D facesContext.getApplication().getViewH=
andler();
- String viewId =3D facesContext.getViewRoot().getViewId();
-
- return viewHandler.getActionURL(facesContext, viewId);
- }
-
- /**
- * Simplified version of {@link encodeId}
- *
- * @param context
- * @param component
- * @return client id of current component
- */
- public String clientId(FacesContext context, UIComponent component) {
- String clientId =3D "";
-
- try {
- clientId =3D component.getClientId(context);
- } catch (Exception e) {
-
- // just ignore
- }
-
- return clientId;
- }
-
- /**
- * Wtrie JavaScript with start/end elements and type.
- *
- * @param context
- * @param component
- * @param script
- */
- public void writeScript(FacesContext context, UIComponent component, O=
bject script) throws IOException {
- ResponseWriter writer =3D context.getResponseWriter();
-
- writer.startElement(HTML.SCRIPT_ELEM, component);
- writer.writeAttribute(HTML.TYPE_ATTR, "text/javascript", "type");
- writer.writeText(script, null);
- writer.endElement(HTML.SCRIPT_ELEM);
- }
-
- /**
- * @param ids
- * @param keyword
- * @return
- * @since 4.0
- */
- private static boolean checkKeyword(Collection ids, String key=
word) {
- if (ids.contains(keyword)) {
- if (ids.size() !=3D 1) {
- //TODO log
- }
-
- return true;
- }
-
- return false;
- }
-
- public String getPredefinedMetaComponentId(FacesContext facesContext, =
UIComponent component, String id) {
-
- if (AjaxRendererUtils.ALL.equals(id)) {
- return AjaxRendererUtils.ALL;
- } else if (AjaxRendererUtils.NONE.equals(id)) {
- return AjaxRendererUtils.NONE;
- } else if (AjaxRendererUtils.THIS.equals(id)) {
- return component.getClientId(facesContext);
- } else if (AjaxRendererUtils.FORM.equals(id)) {
- UIForm nestingForm =3D getNestingForm(facesContext, component);
- if (nestingForm !=3D null) {
- return nestingForm.getClientId(facesContext);
- } else {
- //TODO nick - log warning for missing form
- }
- }
-
- return null;
- }
-
- /**
- * @param context
- * @param component
- * @param shortIds
- * @since 4.0
- * @return
- */
- public Collection findComponentsFor(FacesContext context, UICo=
mponent component,
- Collection shortIds) {
-
- // TODO - implement
- // TODO add support for @*
- Set result =3D new LinkedHashSet(shortIds.size());
-
- if (checkKeyword(shortIds, AjaxRendererUtils.ALL)) {
- result.add(AjaxRendererUtils.ALL);
- } else if (checkKeyword(shortIds, AjaxRendererUtils.NONE)) {
- //do nothing, use empty set
- } else {
- ComponentIdResolver locator =3D new ComponentIdResolver(contex=
t);
-
- for (String id : shortIds) {
- String predefinedMetaComponentId =3D getPredefinedMetaComp=
onentId(context, component, id);
- if (predefinedMetaComponentId !=3D null) {
- if (AjaxRendererUtils.GLOBAL_META_COMPONENTS.contains(=
predefinedMetaComponentId)) {
- result.clear();
- result.add(predefinedMetaComponentId);
- break;
- } else {
- result.add(predefinedMetaComponentId);
- continue;
- }
- }
-
- locator.addId(id);
- }
-
- locator.resolve(component);
-
- result.addAll(locator.getResolvedIds());
- }
-
- return result;
- }
-
- public UIComponent findComponentFor(FacesContext context, UIComponent =
component, String id) {
- return findComponentFor(component, id);
- }
-
- /**
- * @param component
- * @param id
- * @return
- */
- public UIComponent findComponentFor(UIComponent component, String id) {
- if (id =3D=3D null) {
- throw new NullPointerException("id is null!");
- }
-
- if (id.length() =3D=3D 0) {
- return null;
- }
-
- UIComponent target =3D null;
- UIComponent parent =3D component;
- UIComponent root =3D component;
-
- while ((null =3D=3D target) && (null !=3D parent)) {
- target =3D parent.findComponent(id);
- root =3D parent;
- parent =3D parent.getParent();
- }
-
- if (null =3D=3D target) {
- target =3D findUIComponentBelow(root, id);
- }
-
- return target;
- }
-
- /**
- * If target component contains generated id and for doesn't, correct =
for id
- * @param forAttr
- * @param component
- *
- */
- public String correctForIdReference(String forAttr, UIComponent compon=
ent) {
- int contains =3D forAttr.indexOf(UIViewRoot.UNIQUE_ID_PREFIX);
-
- if (contains <=3D 0) {
- String id =3D component.getId();
- int pos =3D id.indexOf(UIViewRoot.UNIQUE_ID_PREFIX);
-
- if (pos > 0) {
- return forAttr.concat(id.substring(pos));
- }
- }
-
- return forAttr;
- }
-
- private UIComponent findUIComponentBelow(UIComponent root, String id) {
- UIComponent target =3D null;
-
- for (Iterator iter =3D root.getFacetsAndChildren(); i=
ter.hasNext();) {
- UIComponent child =3D (UIComponent) iter.next();
-
- if (child instanceof NamingContainer) {
- try {
- target =3D child.findComponent(id);
- } catch (IllegalArgumentException iae) {
- continue;
- }
- }
-
- if (target =3D=3D null) {
- if ((child.getChildCount() > 0) || (child.getFacetCount() =
> 0)) {
- target =3D findUIComponentBelow(child, id);
- }
- }
-
- if (target !=3D null) {
- break;
- }
- }
-
- return target;
- }
-
- public static void writeEventHandlerFunction(FacesContext context, UIC=
omponent component, String eventName)
- throws IOException {
-
- ResponseWriter writer =3D context.getResponseWriter();
- Object script =3D component.getAttributes().get(eventName);
-
- if ((script !=3D null) && !script.equals("")) {
- JSFunctionDefinition onEventDefinition =3D new JSFunctionDefin=
ition();
-
- onEventDefinition.addParameter("event");
- onEventDefinition.addToBody(script);
- writer.writeText(eventName + ": " + onEventDefinition.toScript=
(), null);
- } else {
- writer.writeText(eventName + ": ''", null);
- }
- }
-
- public JSFunctionDefinition getAsEventHandler(FacesContext context, UI=
Component component, String attributeName,
- String append) {
- String event =3D (String) component.getAttributes().get(attributeN=
ame);
-
- if (event !=3D null) {
- event =3D event.trim();
-
- if (event.length() !=3D 0) {
- JSFunctionDefinition function =3D new JSFunctionDefinition=
();
-
- function.addParameter("event");
-
- if ((null !=3D append) && (append.length() > 0)) {
- function.addToBody(event + append);
- } else {
- function.addToBody(event);
- }
-
- return function;
- }
- }
-
- return null;
- }
-
- public String escapeJavaScript(Object o) {
- if (o !=3D null) {
- StringBuilder result =3D new StringBuilder();
- JSEncoder encoder =3D new JSEncoder();
- char[] chars =3D o.toString().toCharArray();
- int start =3D 0;
- int end =3D chars.length;
-
- for (int x =3D start; x < end; x++) {
- char c =3D chars[x];
-
- if (encoder.compile(c)) {
- continue;
- }
-
- if (start !=3D x) {
- result.append(chars, start, x - start);
- }
-
- result.append(encoder.encode(c));
- start =3D x + 1;
-
- continue;
- }
-
- if (start !=3D end) {
- result.append(chars, start, end - start);
- }
-
- return result.toString();
- } else {
- return null;
- }
- }
-
- public void encodeChildren(FacesContext context, UIComponent component=
) throws IOException {
- if (component.getChildCount() > 0) {
- for (UIComponent child : component.getChildren()) {
- child.encodeAll(context);
- }
- }
- }
- =
- public boolean hasExplicitId(UIComponent component) {
- return component.getId() !=3D null && !component.getId().startsWit=
h(UIViewRoot.UNIQUE_ID_PREFIX);
- }
- =
- /**
- * Common HTML elements and attributes names.
- *
- * @author asmirnov(a)exadel.com (latest modification by $Author: alex=
smirnov $)
- * @version $Revision: 1.1.2.6 $ $Date: 2007/02/08 19:07:16 $
- *
- */
- public interface HTML {
- // elements
- public static final String A_ELEMENT =3D "a";
- public static final String BODY_ELEMENT =3D "body";
- public static final String IMG_ELEMENT =3D "img";
- public static final String INPUT_ELEM =3D "input";
- public static final String INPUT_TYPE_HIDDEN =3D "hidden";
- public static final String BUTTON =3D "button";
- public static final String CAPTION_ELEMENT =3D "caption";
- public static final String CHARSET_ATTR =3D "charset";
- public static final String COORDS_ATTR =3D "coords";
- public static final String COLGROUP_ELEMENT =3D "colgroup";
- public static final String COL_ELEMENT =3D "col";
- public static final String DISABLED_ATTR =3D "disabled";
- public static final String DIV_ELEM =3D "div";
- public static final String DD_ELEMENT =3D "dd";
- public static final String DL_ELEMENT =3D "dl";
- public static final String DT_ELEMENT =3D "dt";
- public static final String FORM_ELEMENT =3D "form";
- public static final String HEAD_ELEMENT =3D "head";
- public static final String HEIGHT_ATTRIBUTE =3D "height";
- public static final String HREFLANG_ATTR =3D "hreflang";
- public static final String HREF_ATTR =3D "href";
- public static final String HTML_ELEMENT =3D "html";
- public static final String LINK_ELEMENT =3D "link";
- public static final String SCRIPT_ELEM =3D "script";
- public static final String SPAN_ELEM =3D "span";
- public static final String TFOOT_ELEMENT =3D "tfoot";
- public static final String THEAD_ELEMENT =3D "thead";
- public static final String TABLE_ELEMENT =3D "table";
- public static final String TBODY_ELEMENT =3D "tbody";
- public static final String TD_ELEM =3D "td";
- public static final String TR_ELEMENT =3D "tr";
- public static final String TH_ELEM =3D "th";
- public static final String TITLE_ELEM =3D "title";
- public static final String UL_ELEMENT =3D "ul";
- public static final String OL_ELEMENT =3D "ol";
- public static final String LI_ELEMENT =3D "li";
- =
- // attributes
- public static final String FRAME_ATTRIBUTE =3D "frame";
- public static final String BORDER_ATTRIBUTE =3D "border";
- public static final String BGCOLOR_ATTRIBUTE =3D "bgcolor";
- public static final String ACCEPT_ATTRIBUTE =3D "accept";
- public static final String ACCEPT_CHARSET_ATTRIBUTE =3D "accept-ch=
arset";
- public static final String ACCESSKEY_ATTRIBUTE =3D "accesskey";
- public static final String ACTION_ATTRIBUTE =3D "action";
- public static final String ALIGN_ATTRIBUTE =3D "align";
- public static final String ALT_ATTRIBUTE =3D "alt";
- public static final String AUTOCOMPLETE_ATTRIBUTE =3D "autocomplet=
e";
- public static final String CLASS_ATTRIBUTE =3D "class";
- public static final String COLS_ATTRIBUTE =3D "cols";
- public static final String COLSPAN_ATTRIBUTE =3D "colspan";
- public static final String CELLPADDING_ATTRIBUTE =3D "cellpadding";
- public static final String CELLSPACING_ATTRIBUTE =3D "cellspacing";
- public static final String DIR_ATTRIBUTE =3D "dir";
- public static final String ENCTYPE_ATTRIBUTE =3D "enctype";
- =
- public static final String ID_ATTRIBUTE =3D "id";
- public static final String LANG_ATTRIBUTE =3D "lang";
- public static final String LONGDESC_ATTRIBUTE =3D "longdesc";
- public static final String MAXLENGTH_ATTRIBUTE =3D "maxlength";
- public static final String MEDIA_ATTRIBUTE =3D "media";
- public static final String METHOD_ATTRIBUTE =3D "method";
- public static final String NAME_ATTRIBUTE =3D "name";
- public static final String NOWRAP_ATTRIBUTE =3D "nowrap";
- public static final String ROWS_ATTRIBUTE =3D "rows";
- public static final String RULES_ATTRIBUTE =3D "rules";
- public static final String ROWSPAN_ATTRIBUTE =3D "rowspan";
- public static final String READONLY_ATTRIBUTE =3D "readonly";
- public static final String SIZE_ATTRIBUTE =3D "size";
- public static final String SRC_ATTRIBUTE =3D "src";
- public static final String STYLE_ATTRIBUTE =3D "style";
- public static final String SUMMARY_ATTRIBUTE =3D "summary";
- public static final String SCOPE_ATTRIBUTE =3D "scope";
- public static final String TABINDEX_ATTRIBUTE =3D "tabindex";
- public static final String TITLE_ATTRIBUTE =3D "title";
- public static final String TARGET_ATTRIBUTE =3D "target";
- public static final String TYPE_ATTR =3D "type";
- =
- public static final String USEMAP_ATTRIBUTE =3D "usemap";
-
- public static final String VALIGN_ATTRIBUTE =3D "valign";
- public static final String VALUE_ATTRIBUTE =3D "value";
- public static final String WIDTH_ATTRIBUTE =3D "width";
-
-
- public static final String ONBLUR_ATTRIBUTE =3D "onblur";
- public static final String ONCHANGE_ATTRIBUTE =3D "onchange";
- public static final String ONCLICK_ATTRIBUTE =3D "onclick";
- public static final String ONDBLCLICK_ATTRIBUTE =3D "ondblclick";
- public static final String ONFOCUS_ATTRIBUTE =3D "onfocus";
- public static final String ONKEYDOWN_ATTRIBUTE =3D "onkeydown";
- public static final String ONKEYPRESS_ATTRIBUTE =3D "onkeypress";
- public static final String ONKEYUP_ATTRIBUTE =3D "onkeyup";
- public static final String ONLOAD_ATTRIBUTE =3D "onload";
- public static final String ONMOUSEDOWN_ATTRIBUTE =3D "onmousedown";
- public static final String ONMOUSEMOVE_ATTRIBUTE =3D "onmousemove";
- public static final String ONMOUSEOUT_ATTRIBUTE =3D "onmouseout";
- public static final String ONMOUSEOVER_ATTRIBUTE =3D "onmouseover";
- public static final String ONMOUSEUP_ATTRIBUTE =3D "onmouseup";
- public static final String ONRESET_ATTRIBUTE =3D "onreset";
- public static final String ONSELECT_ATTRIBUTE =3D "onselect";
- public static final String ONSUBMIT_ATTRIBUTE =3D "onsubmit";
- public static final String ONUNLOAD_ATTRIBUTE =3D "onunload";
- =
- public static final String REL_ATTR =3D "rel";
- public static final String REV_ATTR =3D "rev";
- public static final String SHAPE_ATTR =3D "shape";
- public static final String STYLE_CLASS_ATTR =3D "styleClass";
-
-
-
- // public static final String ONRESET_ATTRIBUTE =3D "onreset";
- // attributes sets.
- public static final String[] PASS_THRU =3D {
-
- // DIR_ATTRIBUTE,
- // LANG_ATTRIBUTE,
- // STYLE_ATTRIBUTE,
- // TITLE_ATTRIBUTE
- "accesskey", "alt", "cols", "height", "lang", "longdesc", "max=
length", "rows", "size", "tabindex", "title",
- "width", "dir", "rules", "frame", "border", "cellspacing", "ce=
llpadding", "summary", "bgcolor", "usemap",
- "enctype", "accept-charset", "accept", "target", "charset", "c=
oords", "hreflang", "rel", "rev", "shape",
- "disabled", "readonly", "ismap", "align"
- };
-
- /**
- * HTML attributes allowed boolean-values only
- */
- public static final String[] PASS_THRU_BOOLEAN =3D {
- "disabled", "declare", "readonly", "compact", "ismap", "select=
ed", "checked", "nowrap", "noresize",
- "nohref", "noshade", "multiple"
- };
- public static final String[] PASS_THRU_EVENTS =3D {
- "onblur", "onchange", "onclick", "ondblclick", "onfocus", "onk=
eydown", "onkeypress", "onkeyup", "onload",
- "onmousedown", "onmousemove", "onmouseout", "onmouseover", "on=
mouseup", "onreset", "onselect", "onsubmit",
- "onunload"
- };
- public static final String[] PASS_THRU_STYLES =3D {"style", "class=
", };
-
- /**
- * all HTML attributes with URI value.
- */
- public static final String[] PASS_THRU_URI =3D {
- "usemap", "background", "codebase", "cite", "data", "classid",=
"href", "longdesc", "profile", "src"
- };
-
- =
- public static final String TEXT_JAVASCRIPT_TYPE =3D "text/javascri=
pt";
- public static final String REL_STYLESHEET =3D "stylesheet";
- public static final String CSS_TYPE =3D "text/css";
- public static final String JAVASCRIPT_TYPE =3D "text/javascript";
- }
-}
--===============8486541039917368506==--