001: /* Copyright 2003 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.i18n;
007:
008: import java.util.Locale;
009:
010: import org.apache.oro.text.perl.Perl5Util;
011: import org.jasig.portal.BrowserInfo;
012: import org.jasig.portal.PortalException;
013: import org.jasig.portal.ResourceMissingException;
014: import org.jasig.portal.StylesheetSet;
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jasig.portal.utils.ResourceLoader;
018: import org.jasig.portal.utils.XSLT;
019:
020: /**
021: * Selects XSLT stylesheets based on locale information.
022: * @author Shoji Kajita <a href="mailto:">kajita@itc.nagoya-u.ac.jp</a>
023: * @version $Revision: 34931 $
024: * @since uPortal 2.2
025: */
026: public class LocaleAwareXSLT extends XSLT {
027:
028: private static final Log log = LogFactory
029: .getLog(LocaleAwareXSLT.class);
030:
031: protected Locale[] locales;
032: private static Perl5Util perl5Util = new Perl5Util();
033:
034: /**
035: * Constructor that configures the calling class.
036: * @param instance class name used to search for resources
037: */
038: public LocaleAwareXSLT(Object instance) {
039: super (instance);
040: }
041:
042: /**
043: * Constructor that configures both the calling class and the locale list.
044: * @param instance class name used to search for resources
045: * @param locales a list of locales
046: */
047: public LocaleAwareXSLT(Object instance, Locale[] locales) {
048: this (instance);
049: this .locales = locales;
050: }
051:
052: /**
053: * Sets the locales.
054: * @param locales a list of locales
055: */
056: public void setLocales(Locale[] locales) {
057: this .locales = locales;
058: }
059:
060: /**
061: * Configures the xsl source by choosing the appropriate stylesheet from
062: * the provided stylesheet list file, taking into account the list of locales.
063: * @param sslUri the URL of the stylesheet list file
064: * @param stylesheetTitle the title of a stylesheet within the stylesheet list file
065: * @param browserInfo the browser info object
066: * @throws org.jasig.portal.PortalException
067: */
068: public void setXSL(String sslUri, String stylesheetTitle,
069: BrowserInfo browserInfo) throws PortalException {
070: StylesheetSet set = getStylesheetSet(ResourceLoader
071: .getResourceAsURLString(caller.getClass(), sslUri));
072: set.setMediaProps(mediaProps);
073: String xslUri = set.getStylesheetURI(stylesheetTitle,
074: browserInfo);
075: xslUri = getLocaleAwareXslUri(xslUri, locales, caller);
076: setXSL(xslUri);
077: }
078:
079: /**
080: * Configures the xsl source by choosing the appropriate stylesheet from
081: * the provided stylesheet list file, taking into account the list of locales.
082: * @param sslUri the URL of the stylesheet list file
083: * @param browserInfo the browser info object
084: * @throws org.jasig.portal.PortalException
085: */
086: public void setXSL(String sslUri, BrowserInfo browserInfo)
087: throws PortalException {
088: setXSL(sslUri, (String) null, browserInfo);
089: }
090:
091: /**
092: * Finds localized version of stylesheet according to the supplied list of locales.
093: * @param xslUri the URL of the stylesheet file
094: * @param locales the list of locales
095: * @param caller the calling class
096: */
097: public static String getLocaleAwareXslUri(String xslUri,
098: Locale[] locales, Object caller) {
099: String localeAwareXslUri = xslUri;
100: int i;
101:
102: if (!LocaleManager.isLocaleAware() || locales == null) {
103: try {
104: xslUri = ResourceLoader.getResourceAsURLString(caller
105: .getClass(), xslUri);
106: if (log.isDebugEnabled())
107: log.debug("LocaleAwareXSLT.getLocaleAwareXslUri: "
108: + "XSL file found as " + xslUri);
109: } catch (ResourceMissingException e) {
110: if (log.isDebugEnabled())
111: log.debug("LocaleAwareXSLT.getLocaleAwareXslUri: "
112: + "XSL file NOT found as " + xslUri);
113: }
114: } else {
115: for (i = 0; i < locales.length; i++) {
116: // localeAwareXslUri = xslUri.replaceAll("\\.xsl", "_" + locales[i] + ".xsl");
117: // replaceAll is introduced from JDK1.4
118: localeAwareXslUri = perl5Util.substitute("s/\\.xsl/_"
119: + locales[i] + ".xsl" + "/g", xslUri);
120: if (log.isDebugEnabled())
121: log
122: .debug("LocaleAwareXSLT.getLocaleAwareXslUri: locale aware xslUri="
123: + localeAwareXslUri);
124: try {
125: xslUri = ResourceLoader.getResourceAsURLString(
126: caller.getClass(), localeAwareXslUri);
127: if (log.isDebugEnabled())
128: log
129: .debug("LocaleAwareXSLT.getLocaleAwareXslUri: "
130: + "XSL file found as " + xslUri);
131: break;
132: } catch (ResourceMissingException e) {
133: if (log.isDebugEnabled()) {
134: log
135: .debug("LocaleAwareXSLT.getLocaleAwareXslUri: XSL file NOT found as "
136: + localeAwareXslUri);
137: log
138: .debug("LocaleAwareXSLT.getLocaleAwareXslUri: Fallbacking...");
139: }
140: }
141: }
142: if (i == locales.length) {
143: try {
144: xslUri = ResourceLoader.getResourceAsURLString(
145: caller.getClass(), xslUri);
146: if (log.isDebugEnabled())
147: log
148: .debug("LocaleAwareXSLT.getLocaleAwareXslUri: "
149: + "XSL file found as " + xslUri);
150: } catch (ResourceMissingException e) {
151: if (log.isDebugEnabled())
152: log
153: .debug("LocaleAwareXSLT.getLocaleAwareXslUri: "
154: + "XSL file NOT found as "
155: + xslUri);
156: }
157: }
158: }
159: return xslUri;
160: }
161: }
|