+

Search Tips   |   Advanced Search

styles_rules.jspf

 

<%@ page import="javax.ccpp.Profile" %>
<%@ page import="javax.ccpp.Attribute" %>
<%

// This is the common location to specify browser and locale specific css rules.
// All CSS jsps should statically include this file.

%>

<%--  ******************  Initialize Locales  ******************  --%>

<%!
/* maps a locale to a hashmap of css rules for that locale */
private static java.util.HashMap localeRulesMap;

/* the default css rules to apply to any locale not found in the map */
private static java.util.HashMap defaultLocaleRules;

/* the policy manager service used to access policy information */
private static com.ibm.policy.manager.PolicyManagerService policyManagerService = null;

/* the poc content servlet used to access theme graphics in places such as theme policies */
private static com.ibm.portal.resolver.service.PortalPocServiceHome pocHome;

// -------------------------------
// cache for color palette properties
// cache key is concatenation of color palette name and policy path
// cache value is color palette properties
// color palette properties need to be cached as Properties.load() is expensive.
// -------------------------------

private static java.util.Map colorPalettePropertiesCacheMap = new java.util.HashMap();

// --------------------------------
// cache for policy version 
// cache key is concatenation of color palette name and policy path, the same as colorPalettePropertiesCacheMap
// cache value is policy version
// As there is only one policy version can exist in a system, we use it as indication for invalidation/reload.
// Invalidate/reload color palette properties cache when cached policy version does not match request policy version
// --------------------------------

private static java.util.Map policyVersionCacheMap = new java.util.HashMap();


/* initializes servlet variables with maps definine the css rules for each locale. */
/* this initialization will occur only once per server restart for performance.    */

public void jspInit() {

    //-----------------------------------
    // Create maps for the font sizes
    //
    // Since we are using ems and percents for font sizes, these maps are all the same now.
    // (unlike previous portal releases)
    // However, we keep them just in case we or a customer ever decide to go back to size keywords.
    //
    // DO NOT CHANGE THE NAME OF THE "portal_______" KEYS OR YOU WILL
    // BREAK THE THEME CUSTOMIZER PORTLET.   THESE NAMES ARE CHOSEN ON PURPOSE SO THAT THEY CAN BE
    // DISTINGUISHED FROM THE STANDARD CSS SIZE KEYWORDS THAT ARE ALSO DISPLAYED TO THE USER IN THE SAME DROPDOWN LIST.
    //-----------------------------------
        
    java.util.HashMap xsmallFontSizes = new java.util.HashMap();
    xsmallFontSizes.put("portalSmall","0.9166em");
    xsmallFontSizes.put("portalNormal","1em");
    xsmallFontSizes.put("portalLarge","1.1667em");
    xsmallFontSizes.put("portalXLarge","1.333em");
    xsmallFontSizes.put("portalXXLarge","1.6667em");
    xsmallFontSizes.put("portalXXXLarge","2.73em");
    xsmallFontSizes.put("portalMonospace","1.1667em");

    java.util.HashMap smallFontSizes = new java.util.HashMap();
    smallFontSizes.put("portalSmall","0.9166em");
    smallFontSizes.put("portalNormal","1em");
    smallFontSizes.put("portalLarge","1.1667em");
    smallFontSizes.put("portalXLarge","1.333em");
    smallFontSizes.put("portalXXLarge","1.6667em");
    smallFontSizes.put("portalXXXLarge","2.5em");
    smallFontSizes.put("portalMonospace","1.1667em");

    java.util.HashMap normalFontSizes = new java.util.HashMap();
    normalFontSizes.put("portalSmall","0.9166em");
    normalFontSizes.put("portalNormal","1em");
    normalFontSizes.put("portalLarge","1.1667em");
    normalFontSizes.put("portalXLarge","1.333em");
    normalFontSizes.put("portalXXLarge","1.6667em");
    normalFontSizes.put("portalXXXLarge","2.5em");
    normalFontSizes.put("portalMonospace","1.1667em");

    java.util.HashMap largerFontSizes = new java.util.HashMap();
    largerFontSizes.put("portalSmall","0.9166em");
    largerFontSizes.put("portalNormal","1em");
    largerFontSizes.put("portalLarge","1.1667em");
    largerFontSizes.put("portalXLarge","1.333em");
    largerFontSizes.put("portalXXLarge","1.6667em");
    largerFontSizes.put("portalXXXLarge","2.5em");
    largerFontSizes.put("portalMonospace","1.1667em");

    //in asian fonts, small is smallest legible size, so small and normal are the same.
    java.util.HashMap asianFontSizes = new java.util.HashMap();
    asianFontSizes.put("portalSmall","1em");
    asianFontSizes.put("portalNormal","1em");
    asianFontSizes.put("portalLarge","1.1667em");
    asianFontSizes.put("portalXLarge","1.333em");
    asianFontSizes.put("portalXXLarge","1.6667em");
    asianFontSizes.put("portalXXXLarge","2.5em");
    asianFontSizes.put("portalMonospace","1.1667em");

    //-----------------------------------
    // Create CSS rules maps. These are the variables that will be mapped to specific locales,
    // then looked for each locale and then referenced throughout the CSS jsps via JSTL EL.
    //-----------------------------------

    //create the default rules map to base all the variants on, and to use as a fallback if an unknown locale is requested
    defaultLocaleRules = new java.util.HashMap();
    defaultLocaleRules.put("portalSansSerif","Helvetica, Arial, sans-serif");
    defaultLocaleRules.put("portalMonospace","monospace");
    defaultLocaleRules.put("fontSize", normalFontSizes);
    defaultLocaleRules.put("isRTL", Boolean.FALSE);
    defaultLocaleRules.put("bidiLeft","left");
    defaultLocaleRules.put("bidiRight","right");
    defaultLocaleRules.put("bodyFontSize","75%");
    defaultLocaleRules.put("emphasisFontWeight","bold");

    java.util.HashMap latin1Rules = new java.util.HashMap(defaultLocaleRules);
    latin1Rules.put("portalSansSerif","Helvetica, Arial, sans-serif");
    latin1Rules.put("fontSize", smallFontSizes);

    java.util.HashMap bidiRules = new java.util.HashMap(defaultLocaleRules);
    bidiRules.put("isRTL", Boolean.TRUE);
    bidiRules.put("bidiLeft","right");
    bidiRules.put("bidiRight","left");
    bidiRules.put("bidiImageRTL","_rtl");

    java.util.HashMap arRules = new java.util.HashMap(bidiRules);
    arRules.put("fontSize", largerFontSizes);
    arRules.put("bodyFontSize","81.25%");

    // iw and he are the same so have only one map -
    // this is so bold fonts will display propertly 
    java.util.HashMap iwRules = new java.util.HashMap(bidiRules);
    iwRules.put("fontSize", largerFontSizes);
    iwRules.put("bodyFontSize","81.25%");

    java.util.HashMap asianRules = new java.util.HashMap(defaultLocaleRules);
    asianRules.put("fontSize", asianFontSizes);
    asianRules.put("portalSansSerif", null); // no font family is to be specified
    asianRules.put("emphasisFontWeight","normal");
    asianRules.put("bodyFontSize","83.25%");

    java.util.HashMap jaRules = new java.util.HashMap(asianRules);
    jaRules.put("portalSansSerif","MS PGothic");

    java.util.HashMap koRules = new java.util.HashMap(asianRules);
    koRules.put("portalSansSerif","Gulim");


    // zh needs to specify a special font for the Select form element or there are problems in some browsers
    java.util.HashMap zhRules = new java.util.HashMap(asianRules);
    zhRules.put("portalSansSerifSelect","SimSun-18030");

    java.util.HashMap thRules = new java.util.HashMap(defaultLocaleRules);
    thRules.put("fontSize", largerFontSizes);
    thRules.put("bodyFontSize","81.25%");

    // create map between each locale and its rules map

    localeRulesMap = new java.util.HashMap();


    // bidi locales

    localeRulesMap.put("ar",arRules);
    localeRulesMap.put("iw", iwRules);
    localeRulesMap.put("he", iwRules);


    // latin1 locales

    localeRulesMap.put("en", latin1Rules);
    localeRulesMap.put("da", latin1Rules);
    localeRulesMap.put("de", latin1Rules);
    localeRulesMap.put("es", latin1Rules);
    localeRulesMap.put("fi", latin1Rules);
    localeRulesMap.put("fr", latin1Rules);
    localeRulesMap.put("it", latin1Rules);
    localeRulesMap.put("nl", latin1Rules);
    localeRulesMap.put("no", latin1Rules);
    localeRulesMap.put("pt", latin1Rules);
    localeRulesMap.put("pt_BR", latin1Rules);
    localeRulesMap.put("sv", latin1Rules);


    // other latin and slavic
    // These languages use the same general settings and font sizes as Latin-I
    // but must have generic font families (i.e. NOT Verdana, Arial, etc.)

    localeRulesMap.put("ca", defaultLocaleRules);
    localeRulesMap.put("cs", defaultLocaleRules);
    localeRulesMap.put("el", defaultLocaleRules);
    localeRulesMap.put("hr", defaultLocaleRules);
    localeRulesMap.put("hu", defaultLocaleRules);
    localeRulesMap.put("pl", defaultLocaleRules);
    localeRulesMap.put("ro", defaultLocaleRules);
    localeRulesMap.put("ru", defaultLocaleRules);
    localeRulesMap.put("sk", defaultLocaleRules);
    localeRulesMap.put("sl", defaultLocaleRules);
    localeRulesMap.put("tr", defaultLocaleRules);
    localeRulesMap.put("uk", defaultLocaleRules);

    // asian locales

    localeRulesMap.put("ja", jaRules);
    localeRulesMap.put("ko", koRules);
    localeRulesMap.put("zh", zhRules);
    localeRulesMap.put("zh_TW", asianRules);

    // other locales

    localeRulesMap.put("th", thRules);


    // access the policy manager and poc URI services
    try
    {
        /* only perform this JNDI lookup once as this is an expensive call performance wise */

        javax.naming.Context ctx = new javax.naming.InitialContext();

        policyManagerService = (com.ibm.policy.manager.PolicyManagerService) ctx.lookup("portal:service/policy/PolicyManager");
        pocHome = (com.ibm.portal.resolver.service.PortalPocServiceHome) ctx.lookup( "portal:service/resolver/PocService" );

    }
    catch ( javax.naming.NamingException ne )
    {
        // we do not log anything here because if portal ever enters
        // this catch statement then there are severe problems that have 
        // occurred while fetching the model home and will already have been
        // logged by errors reported by the lookup.
    }
   


} // end jspInit()
%>

<%

    //store the rules map and default rules in the request for use by JSTL
    request.setAttribute("localeRulesMap",localeRulesMap);
    request.setAttribute("defaultRules",defaultLocaleRules);

   // get our context info
   String themeContextInfo = (String)request.getParameter("themeContextInfo");
   StringBuffer encodedThemeContextInfo = new StringBuffer();


   // Parse the theme context info into request attributes for use by JSTL.
   // These are of the form:
   //      token:value:token:value....

   java.util.StringTokenizer strTok = new java.util.StringTokenizer(themeContextInfo, ":");
   while(strTok.hasMoreTokens()) 
   {
      String tok = strTok.nextToken();
      String value = null;
      if (strTok.hasMoreTokens()) 
      {
         value = strTok.nextToken();
      }
      if (null != tok && null != value) 
      {
         request.setAttribute(tok, value);
         if (encodedThemeContextInfo.length() > 0) 
         {
            encodedThemeContextInfo.append(":");
         }
         encodedThemeContextInfo.append(java.net.URLEncoder.encode(tok, "UTF-8"));
         encodedThemeContextInfo.append(":");
         encodedThemeContextInfo.append(java.net.URLEncoder.encode(value, "UTF-8"));
      }      
   }
%>


<%-- Set Browser variables --%>
<c:set var="isIE" value="${requestScope.browserVendor eq 'Microsoft'}" scope="request"/>
<c:set var="isMoz" value="${requestScope.browserVendor ne 'Microsoft'}" scope="request"/>

<%-- Set locale based CSS rules variables --%>
<c:set var="cssRules" value="${requestScope.localeRulesMap[requestScope.locale]}" scope="request"/>


<%-- if locale isnt found need to fallback to a default--%>
<c:if test="${empty requestScope.cssRules}">
    <c:set var="cssRules" value="${requestScope.defaultRules}" scope="request"/>
</c:if>


<%--  ******************  Load ColorPalette  ******************  --%>

<%-- compute the cache key for colorPalettePropertiesCacheMap --%>

<%-- cache key is concatenation of policy path and color palette name 
        as cached value color palette properties is jointed properties from 
        color palette properties file and policy path  --%>

<c:set var="colorCacheKey" value="${requestScope.policyPath}_${requestScope.colorPalette}" scope="request"/>

<% 
   java.util.Properties cachedColorProperties;
   String cacheKey = (String) request.getAttribute("colorCacheKey");
   String policyVersion = (String) request.getAttribute("policyVersion");
   boolean bneedLoad = false;
   if (colorPalettePropertiesCacheMap.get(cacheKey) == null) 
   {
       // never being loaded
       bneedLoad = true;
   } 
   else 
   {
       // loaded, check version
       if (policyVersion != null  && policyVersion.trim().length() > 0) 
       {
           // policy version exists in request, need to compare current cache version to see whether reload needs
           String cacheVersion = (String) policyVersionCacheMap.get(cacheKey);

           if (cacheVersion == null || cacheVersion.trim().length() == 0) 
           {
               // reload as no cache version exists
               bneedLoad = true;
           } 
           else if (!policyVersion.equals(cacheVersion)) 
           {
               // request version is not the same as cached version
               bneedLoad = true;
           }
       }
   }
   if (!bneedLoad) 
   {
       cachedColorProperties = (java.util.Properties) colorPalettePropertiesCacheMap.get(cacheKey);
   } 
   else 
   {
       cachedColorProperties = new java.util.Properties();
       colorPalettePropertiesCacheMap.put(cacheKey, cachedColorProperties);

       if (policyVersion != null  && policyVersion.trim().length() > 0) 
       {
           policyVersionCacheMap.put(cacheKey, policyVersion);
       }
%>

<%-- load the color palette property file into a string--%>
<c-rt:import url="colors/${requestScope.colorPalette}.properties" var="colorPropertiesStr" scope="page" />
<%

   // parse the string into a Properties object (which is a map and can be referenced with EL easily)
   java.io.StringBufferInputStream sbis = new java.io.StringBufferInputStream((String)pageContext.findAttribute("colorPropertiesStr"));

   cachedColorProperties.load(sbis);
   

   // Now load any theme customizer overrides specified in the policy.
   String policyPath = (String)request.getAttribute("policyPath");   

   try 
   {
       if (null != policyPath && null != policyManagerService) 
       {
          com.ibm.policy.manager.PolicyManager policyManager = policyManagerService.getPolicyManager();
          com.ibm.policy.manager.PolicyValueSet themePolicyMap = policyManager.getPVSByPath(policyPath);
          
          if (themePolicyMap != null) 
          {
             request.setAttribute("themePolicyMap", themePolicyMap);

             java.lang.String colorPalettePropertiesStr = (java.lang.String)themePolicyMap.get("colorPaletteProperties");

             if (null != colorPalettePropertiesStr) 
             {      
                sbis = new java.io.StringBufferInputStream(colorPalettePropertiesStr);
                java.util.Properties themePolicyColorPaletteProperties = new java.util.Properties();
                themePolicyColorPaletteProperties.load(sbis);
                
                cachedColorProperties.putAll(themePolicyColorPaletteProperties);
             }
          }
       }
    } 
    catch (Exception e) 
    {
       // This exception should not happen. But if it does, log it and keep going.
       // The theme can continue to function... however, the theme customizer portlet
       // settings will not be loaded.

       System.out.println("styles_rules.jspf: " + e + " " + policyPath);
    }

   }  // done loading
   

   // make a clone of what we have in the cache
   // clone is needed as we can't cache resolved POC urls 

   java.util.Properties colorProperties = new java.util.Properties();
   colorProperties.putAll(cachedColorProperties);


   // save cloned into request scope

   request.setAttribute("colors", colorProperties);

    
    // Now resolve any POC URI strings in the color palette properties to URLs.
    // POC URI strings begin with "themeGraphic:".  They need to be converted to URLs using
    // the resolver classes in the PortalPocSerivce.  Before these URIs can be converted to URLs,
    // we have to append some context information in the form of parameters identifying things
    // such as the current theme policy, color palette, theme object id, etc... so the handler 
    // on the other end of the URL can do its work.

    java.util.Enumeration keys = colorProperties.propertyNames();
    while (keys.hasMoreElements()) 
    {
        String key = (String)keys.nextElement();
        String value = colorProperties.getProperty(key);
      
        if (null != value && value.startsWith("themeGraphic:") ) 
        {

           String uriString = value + ":" + encodedThemeContextInfo.toString();

           com.ibm.portal.resolver.service.PortalPocService pocSvc = null;

           try 
           {
               // compute the graphic URL from the POC URI               
               pocSvc = pocHome.getPortalPocService(request, response);

               com.ibm.portal.resolver.accessors.url.PocURLFactory urlFct = pocSvc.getURLFactory();
               com.ibm.portal.resolver.PocURL url = urlFct.newURL(Boolean.FALSE);

               java.net.URI contentHandlerURI = new java.net.URI(uriString);

               url.setURI( contentHandlerURI );
               url.setMode("download");
               url.setProtected( Boolean.valueOf((String)request.getAttribute("protectedUrl")) );

               java.io.StringWriter sw = new java.io.StringWriter();

               url.writeDispose(sw);

               String pocUrl = "url(" + sw.toString() + ")";
              
                colorProperties.setProperty(key, pocUrl);
           } 
           catch (Exception e) 
           {
               // This exception should not happen. But if it does, log it and keep going.
               // The theme can continue to function... however, the theme graphic specified
               // by this color palette property will not be loaded.
               System.out.println("styles_rules.jspf: " + e + " " + uriString);
               e.printStackTrace();
           }
           finally 
           {
               if ( pocSvc != null ) { pocSvc.dispose(); }
           }
        }
      
    }
    
%>


/* colorPalette=<c:out value="${requestScope.colorPalette}" /> 
                          themePolicy=<c:out 
                          value="${requestScope.themePolicyMap.Title}" /> */