| Method from com.sun.faces.renderkit.RenderKitUtils Detail: |
public static char[] compressJS(String JSString) {
BufferedReader reader = new BufferedReader(new StringReader(JSString));
StringWriter writer = new StringWriter(1024);
try {
for (String line = reader.readLine();
line != null;
line = reader.readLine()) {
line = line.trim();
writer.write(line);
}
return writer.toString().toCharArray();
} catch (IOException ioe) {
// won't happen
}
return null;
}
This is a utility method for compressing multi-lined javascript.
In the case of #SUN_JSF_JS it offers about a 47% decrease
in length.
For our purposes, compression is just trimming each line and
then writing it out. It's pretty simplistic, but it works.
|
public static String createValidECMAIdentifier(String origIdentifier) {
return origIdentifier.replace("-", "$_");
}
|
public static String determineContentType(String accept,
String serverSupportedTypes,
String preferredType) {
String contentType = null;
if (null == accept || null == serverSupportedTypes) {
return contentType;
}
String[][] clientContentTypes = buildTypeArrayFromString(accept);
String[][] serverContentTypes = buildTypeArrayFromString(serverSupportedTypes);
String[][] preferredContentType = buildTypeArrayFromString(preferredType);
String[][] matchedInfo = findMatch(clientContentTypes, serverContentTypes, preferredContentType);
// if best match exits and best match is not some wildcard,
// return best match
if ((matchedInfo[0][1] != null) && !(matchedInfo[0][2].equals("*"))) {
contentType = matchedInfo[0][1] + CONTENT_TYPE_SUBTYPE_DELIMITER + matchedInfo[0][2];
}
return contentType;
}
Given an accept String from the client, and a String
of server supported content types, determine the best qualified
content type for the client. If no match is found, or either of the
arguments are null, null is returned.
|
public static String getCommandLinkOnClickScript(String formClientId,
String commandClientId,
String target,
Param[] params) {
StringBuilder sb = new StringBuilder(256);
sb.append("if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('");
sb.append(formClientId);
sb.append("'),{'");
sb.append(commandClientId).append("':'").append(commandClientId);
for (Param param : params) {
String pn = param.name;
if (pn != null && pn.length() != 0) {
String pv = param.value;
sb.append("','");
sb.append(pn.replace("'", "\\\'"));
sb.append("':'");
if (pv != null && pv.length() != 0) {
sb.append(pv.replace("'", "\\\'"));
}
}
}
sb.append("'},'");
sb.append(target);
sb.append("');}return false");
return sb.toString();
}
Returns a string that can be inserted into the onclick
handler of a command. This string will add all request parameters
as well as the client ID of the activated command to the form as
hidden input parameters, update the target of the link if necessary,
and handle the form submission. The content of #SUN_JSF_JS
must be rendered prior to using this method.
|
public static RenderKit getCurrentRenderKit(FacesContext context) {
RenderKitFactory renderKitFactory = (RenderKitFactory)
FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
return renderKitFactory.getRenderKit(context,
context
.getViewRoot().getRenderKitId());
}
|
public static ResponseStateManager getResponseStateManager(FacesContext context,
String renderKitId) throws FacesException {
assert (null != renderKitId);
assert (null != context);
RenderKit renderKit = context.getRenderKit();
if (renderKit == null) {
// check request scope for a RenderKitFactory implementation
RenderKitFactory factory = (RenderKitFactory)
RequestStateManager.get(context, RequestStateManager.RENDER_KIT_IMPL_REQ);
if (factory != null) {
renderKit = factory.getRenderKit(context, renderKitId);
} else {
factory = (RenderKitFactory)
FactoryFinder
.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
if (factory == null) {
throw new IllegalStateException();
} else {
RequestStateManager.set(context,
RequestStateManager.RENDER_KIT_IMPL_REQ,
factory);
}
renderKit = factory.getRenderKit(context, renderKitId);
}
}
return renderKit.getResponseStateManager();
}
|
public static List getSelectItems(FacesContext context,
UIComponent component) {
if (context == null) {
throw new IllegalArgumentException(
MessageUtils.getExceptionMessageString(
MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"));
}
ArrayList< SelectItem > list = new ArrayList< SelectItem >();
for (UIComponent kid : component.getChildren()) {
if (kid instanceof UISelectItem) {
UISelectItem item = (UISelectItem) kid;
Object value = item.getValue();
if (value == null) {
list.add(new SelectItem(item.getItemValue(),
item.getItemLabel(),
item.getItemDescription(),
item.isItemDisabled(),
item.isItemEscaped()));
} else if (value instanceof SelectItem) {
list.add((SelectItem) value);
} else {
throw new IllegalArgumentException(
MessageUtils.getExceptionMessageString(
MessageUtils.VALUE_NOT_SELECT_ITEM_ID,
component.getId(),
value.getClass().getName()));
}
} else if (kid instanceof UISelectItems) {
Object value = ((UISelectItems) kid).getValue();
if (value instanceof SelectItem) {
list.add((SelectItem) value);
} else if (value instanceof SelectItem[]) {
SelectItem[] items = (SelectItem[]) value;
// we manually copy the elements so that the list is
// modifiable. Arrays.asList() returns a non-mutable
// list.
//noinspection ManualArrayToCollectionCopy
for (SelectItem item : items) {
list.add(item);
}
} else if (value instanceof Collection) {
for (Object element : ((Collection) value)) {
if (SelectItem.class.isInstance(element)) {
list.add((SelectItem) element);
} else {
throw new IllegalArgumentException(
MessageUtils.getExceptionMessageString(
MessageUtils.VALUE_NOT_SELECT_ITEM_ID,
component.getId(),
value.getClass().getName()));
}
}
} else if (value instanceof Map) {
Map optionMap = (Map) value;
for (Object o : optionMap.entrySet()) {
Entry entry = (Entry) o;
Object key = entry.getKey();
Object val = entry.getValue();
if (key == null || val == null) {
continue;
}
list.add(new SelectItem(val,
key.toString()));
}
} else {
throw new IllegalArgumentException(
MessageUtils.getExceptionMessageString(
MessageUtils.CHILD_NOT_OF_EXPECTED_TYPE_ID,
"UISelectItem/UISelectItems",
component.getFamily(),
component.getId(),
value != null ? value.getClass().getName() : "null"));
}
}
}
return (list);
}
|
public static boolean isXml(String contentType) {
return (RIConstants.XHTML_CONTENT_TYPE.equals(contentType)
|| RIConstants.APPLICATION_XML_CONTENT_TYPE.equals(contentType)
|| RIConstants.TEXT_XML_CONTENT_TYPE.equals(contentType));
}
|
public static synchronized void loadSunJsfJs(ExternalContext extContext) {
Map< String, Object > appMap =
extContext.getApplicationMap();
char[] sunJsfJs;
BufferedReader reader = null;
try {
URL url = Util.getCurrentLoader(appMap)
.getResource("com/sun/faces/sunjsf.js");
if (url == null) {
LOGGER.severe(
"jsf.renderkit.util.cannot_load_js");
return;
}
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
InputStream input = conn.getInputStream();
reader = new BufferedReader(
new InputStreamReader(input));
StringBuilder builder = new StringBuilder(128);
for (String line = reader.readLine();
line != null;
line = reader.readLine()) {
String temp = line.trim();
if (temp.length() == 0
|| temp.startsWith("/*")
|| temp.startsWith("*")
|| temp.startsWith("*/")
|| temp.startsWith("//")) {
continue;
}
builder.append(line).append('\n");
}
builder.deleteCharAt(builder.length() - 1);
if (WebConfiguration
.getInstance(extContext)
.isOptionEnabled(BooleanWebContextInitParameter.CompressJavaScript)) {
sunJsfJs = compressJS(builder.toString());
} else {
sunJsfJs = builder.toString().toCharArray();
}
appMap.put(SUN_JSF_JS, sunJsfJs);
} catch (IOException ioe) {
LOGGER.log(Level.SEVERE,
"jsf.renderkit.util.cannot_load_js",
ioe);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
// ignore
}
}
}
}
Loads the contents of the sunjsf.js file into memory removing any
comments/empty lines it encoutners, and, if enabled, compressing the
result. This method should only be called when the application is
being initialized.
|
public static String prefixAttribute(String attrName,
ResponseWriter writer) {
return (prefixAttribute(attrName,
RIConstants.XHTML_CONTENT_TYPE.equals(writer.getContentType())));
}
|
public static String prefixAttribute(String attrName,
boolean isXhtml) {
if (isXhtml) {
if (Arrays.binarySearch(XHTML_PREFIX_ATTRIBUTES, attrName) > -1) {
return XHTML_ATTR_PREFIX + attrName;
} else {
return attrName;
}
} else {
return attrName;
}
}
|
public static void renderFormInitScript(ResponseWriter writer,
FacesContext context) throws IOException {
WebConfiguration webConfig =
WebConfiguration.getInstance(context.getExternalContext());
if (webConfig.isOptionEnabled(BooleanWebContextInitParameter.ExternalizeJavaScript)) {
// PENDING
// We need to look into how to make this work in a portlet environment.
// For the time being, this feature will need to be disabled when running
// in a portlet.
String mapping = Util.getFacesMapping(context);
String uri;
if ((mapping != null) && (Util.isPrefixMapped(mapping))) {
uri = mapping + '/" + RIConstants.SUN_JSF_JS_URI;
} else {
uri = '/" + RIConstants.SUN_JSF_JS_URI + mapping;
}
writer.write('\n");
writer.startElement("script", null);
writer.writeAttribute("type", "text/javascript", null);
writer.writeAttribute("src",
context.getExternalContext()
.getRequestContextPath() + uri,
null);
writer.endElement("script");
writer.write("\n");
} else {
writer.write('\n");
writer.startElement("script", null);
writer.writeAttribute("type", "text/javascript", null);
writer.writeAttribute("language", "Javascript", null);
writeSunJS(context, writer);
writer.endElement("script");
writer.write("\n");
}
}
|
public static void renderPassThruAttributes(ResponseWriter writer,
UIComponent component,
String[] attributes) throws IOException {
assert (null != writer);
assert (null != component);
Map< String, Object > attrMap = component.getAttributes();
// PENDING - think anyone would run the RI using another implementation
// of the jsf-api? If they did, then this would fall apart. That
// scenario seems extremely unlikely.
if (canBeOptimized(component)) {
//noinspection unchecked
List< String > setAttributes = (List< String >)
component.getAttributes().get(ATTRIBUTES_THAT_ARE_SET_KEY);
if (setAttributes != null) {
renderPassThruAttributesOptimized(writer,
component,
attributes,
setAttributes);
}
} else {
// this block should only be hit by custom components leveraging
// the RI's rendering code. We make no assumptions and loop through
// all known attributes.
boolean isXhtml =
RIConstants.XHTML_CONTENT_TYPE.equals(writer.getContentType());
for (String attrName : attributes) {
Object value =
attrMap.get(attrName);
if (value != null && shouldRenderAttribute(value)) {
writer.writeAttribute(prefixAttribute(attrName, isXhtml),
value,
attrName);
}
}
}
}
Render any "passthru" attributes, where we simply just output the
raw name and value of the attribute. This method is aware of the
set of HTML4 attributes that fall into this bucket. Examples are
all the javascript attributes, alt, rows, cols, etc.
|
public static void renderXHTMLStyleBooleanAttributes(ResponseWriter writer,
UIComponent component) throws IOException {
assert (writer != null);
assert (component != null);
Map attrMap = component.getAttributes();
for (String attrName : BOOLEAN_ATTRIBUTES) {
Object val = attrMap.get(attrName);
if (val == null) {
continue;
}
if (Boolean.valueOf(val.toString())) {
writer.writeAttribute(attrName,
true,
attrName);
}
}
}
Renders the attributes from #BOOLEAN_ATTRIBUTES
using XHMTL semantics (i.e., disabled="disabled").
|
public static void writeSunJS(FacesContext context,
Writer writer) throws IOException {
writer.write((char[]) context.getExternalContext().getApplicationMap()
.get(SUN_JSF_JS));
}
Return the implementation JavaScript. If compression
is enabled, the result will be compressed.
|