Ejemplo n.º 1
0
 public static Locale getLocale(FacesContext context) {
   Locale locale = null;
   UIViewRoot viewRoot = context.getViewRoot();
   if (viewRoot != null) locale = viewRoot.getLocale();
   if (locale == null) locale = Locale.getDefault();
   return locale;
 }
Ejemplo n.º 2
0
  /*
   * This method provides custom logic of wrapping viewRoot instances.
   * This is done, because different environments casts current viewRoot
   * instance to their own implementation of UIViewRoot class.
   * So, we need to comply with inheritance hierarchy.
   */
  private UIViewRoot getAjaxViewRoot(UIViewRoot root, FacesContext context) {
    UIViewRoot riRoot;
    Class ajax4jsfViewRootClass = null;
    try {
      ajax4jsfViewRootClass = Class.forName("org.ajax4jsf.framework.ajax.AjaxViewRoot");
    } catch (ClassNotFoundException e) {
      // absence of the Ajax4jsf library is a valid case
    }
    boolean isAjax4jsf = (ajax4jsfViewRootClass != null);
    if (isAjax4jsf)
      throw new IllegalArgumentException(
          "OpenFaces warning: The old Ajax4jsf framework is not supported. Use RichFaces that now incorporates this framework instead.");

    Class richFacesAjaxViewRootClass = null;
    try {
      richFacesAjaxViewRootClass = Class.forName("org.ajax4jsf.component.AjaxViewRoot");
    } catch (ClassNotFoundException e) {
      // absence of the RichFaces library is a valid case
    }

    if (Environment.isExoPortal()) riRoot = createExoAjaxViewRoot(root);
    else if (richFacesAjaxViewRootClass == null) riRoot = new AjaxViewRoot();
    else if (Environment.isGateInPortal(context)) riRoot = createGateInAjaxViewRoot(root);
    else riRoot = createA4jAjaxViewRoot(root);
    // fill properties from default.
    riRoot.setViewId(root.getViewId());
    riRoot.setLocale(root.getLocale());
    String renderKitId = root.getRenderKitId();
    // Fix facelets bug - for debug requests renderKitId is null !
    if (null == renderKitId) {
      renderKitId = calculateRenderKitId(context);
    }
    riRoot.setRenderKitId(renderKitId);
    return riRoot;
  }
Ejemplo n.º 3
0
 /**
  * @param context the <code>FacesContext</code> for the current request
  * @return the Locale from the UIViewRoot, the the value of Locale.getDefault()
  */
 public static Locale getLocaleFromContextOrSystem(FacesContext context) {
   Locale result, temp = Locale.getDefault();
   UIViewRoot root;
   result = temp;
   if (null != context) {
     if (null != (root = context.getViewRoot())) {
       if (null == (result = root.getLocale())) {
         result = temp;
       }
     }
   }
   return result;
 }
Ejemplo n.º 4
0
  public static String lerBundle(String messageId, String resource) {
    ResourceBundle bundle = null;
    String msg = "";
    if (FacesContext.getCurrentInstance() != null) {
      UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
      bundle = ResourceBundle.getBundle(resource, viewRoot.getLocale());
    } else {

      bundle = ResourceBundle.getBundle(resource);
    }
    try {
      msg = bundle.getString(messageId);
    } catch (Exception e) {
    }
    return msg;
  }
Ejemplo n.º 5
0
 /** returns the active locale for a given FacesContext */
 public Locale getContextLocale(final FacesContext ctx) {
   UIViewRoot root;
   // Start out with the default locale
   Locale locale;
   Locale defaultLocale = Locale.getDefault();
   locale = defaultLocale;
   // See if this FacesContext has a ViewRoot
   if (null != (root = ctx.getViewRoot())) {
     // If so, ask it for its Locale
     if (null == (locale = root.getLocale())) {
       // If the ViewRoot has no Locale, fall back to the default.
       locale = defaultLocale;
     }
   }
   return locale;
 }
Ejemplo n.º 6
0
 /** @see javax.faces.context.FacesContext#getELContext() */
 @Override
 public ELContext getELContext() {
   assertNotReleased();
   if (elContext == null) {
     Application app = getApplication();
     elContext = new ELContextImpl(app.getELResolver());
     elContext.putContext(FacesContext.class, this);
     UIViewRoot root = this.getViewRoot();
     if (null != root) {
       elContext.setLocale(root.getLocale());
     }
     ELContextListener[] listeners = app.getELContextListeners();
     if (listeners.length > 0) {
       ELContextEvent event = new ELContextEvent(elContext);
       for (ELContextListener listener : listeners) {
         listener.contextCreated(event);
       }
     }
   }
   return elContext;
 }
Ejemplo n.º 7
0
 public ResourceBundle getResourceBundle(FacesContext context, String var) {
   ApplicationResourceBundle bundle = resourceBundles.get(var);
   if (bundle == null) {
     return null;
   }
   UIViewRoot root;
   // Start out with the default locale
   Locale locale;
   Locale defaultLocale = Locale.getDefault();
   locale = defaultLocale;
   // See if this FacesContext has a ViewRoot
   if (null != (root = context.getViewRoot())) {
     // If so, ask it for its Locale
     if (null == (locale = root.getLocale())) {
       // If the ViewRoot has no Locale, fall back to the default.
       locale = defaultLocale;
     }
   }
   assert (null != locale);
   // ResourceBundleBean bean = resourceBundles.get(var);
   return bundle.getResourceBundle(locale);
 }
  @Override
  public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent)
      throws IOException {

    ResponseWriter responseWriter = facesContext.getResponseWriter();
    InputFile inputFile = (InputFile) uiComponent;
    JavaScriptFragment alloyNamespace = new JavaScriptFragment("A");

    // Determine the valid content-types and maximum file size from the validator (if specified).
    JavaScriptFragment contentTypes = new JavaScriptFragment("[]");
    String validContentTypes = inputFile.getContentTypes();

    if (validContentTypes != null) {
      contentTypes = toJavaScriptArray(validContentTypes.split(","));
    }

    String clientId = inputFile.getClientId(facesContext);
    Long maxFileSize = inputFile.getMaxFileSize();

    if (maxFileSize == null) {
      maxFileSize = Long.MAX_VALUE;
    }

    // If the component should render the upload progress table, then initialize the YUI progress
    // uploader widget.
    if (inputFile.isShowProgress()) {

      String clientVarName = getClientVarName(facesContext, inputFile);
      String clientKey = inputFile.getClientKey();

      if (clientKey == null) {
        clientKey = clientVarName;
      }

      UIViewRoot viewRoot = facesContext.getViewRoot();
      Locale locale = viewRoot.getLocale();
      String formClientId = getParentFormClientId(inputFile);
      Application application = facesContext.getApplication();
      ViewHandler viewHandler = application.getViewHandler();
      String actionURL = viewHandler.getActionURL(facesContext, viewRoot.getViewId());
      String partialActionURL = facesContext.getExternalContext().encodePartialActionURL(actionURL);
      String namingContainerId = "";

      if (viewRoot instanceof NamingContainer) {
        namingContainerId = viewRoot.getContainerClientId(facesContext);
      }

      AjaxParameters ajaxParameters = new AjaxParameters(inputFile, clientId, formClientId);
      String execute = ajaxParameters.getExecute();
      String render = ajaxParameters.getRender();

      String notStartedMessage = getMessageContext().getMessage(locale, "not-started");
      JavaScriptFragment clientComponent =
          new JavaScriptFragment("Liferay.component('" + clientKey + "')");
      encodeFunctionCall(
          responseWriter,
          "LFAI.initProgressUploader",
          alloyNamespace,
          clientComponent,
          contentTypes,
          clientId,
          formClientId,
          namingContainerId,
          inputFile.isAuto(),
          execute,
          render,
          partialActionURL,
          maxFileSize,
          notStartedMessage);
    }

    // Otherwise, if the component should render the upload preview table, then format the
    // preview-uploader.js
    // template and write it to the response.
    else if (inputFile.isShowPreview()) {

      encodeFunctionCall(
          responseWriter,
          "LFAI.initPreviewUploader",
          alloyNamespace,
          contentTypes,
          clientId,
          maxFileSize);
    }
  }