Guest User

Example TextProvider?

a guest
Jun 8th, 2011
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. package com.example.struts2.DBTextProvider;
  2.  
  3. import java.util.*;
  4.  
  5. import com.opensymphony.xwork2.*;
  6. import com.opensymphony.xwork2.util.*;
  7.  
  8.  
  9. import com.example.text.util.RssUtil;
  10.  
  11.  
  12. /**
  13.  * TextProvider implementation that calls static methods on an external helper.
  14.  * This external helper RssUtil gets some resources from the database and uses a legacy API.
  15.  */
  16. public class DBTextProvider
  17.     implements TextProvider,
  18.                    LocaleProvider
  19. {
  20.     //~ Instance fields --------------------------------------------------------
  21.  
  22.     private transient TextProvider textProvider;
  23.  
  24.     //~ Methods ----------------------------------------------------------------
  25.  
  26.     public Locale getLocale(  )
  27.     {
  28.         ActionContext ctx = ActionContext.getContext(  );
  29.         if ( ctx != null )
  30.         {
  31.             return ctx.getLocale(  );
  32.         }
  33.         else
  34.         {
  35.             return null;
  36.         }
  37.     }
  38.  
  39.     public boolean hasKey( String key )
  40.     {
  41.         //        if ( RssUtil.lookupText( getLocale(  ), key ) != null )
  42.         //        {
  43.         //            return true;
  44.         //        }
  45.         return getTextProvider(  ).hasKey( key );
  46.     }
  47.  
  48.     public String getText( String aTextName )
  49.     {
  50.         //        String rssText = RssUtil.convertText( aTextName );
  51.         //        if ( ( rssText != null ) && !rssText.equals( aTextName ) )
  52.         //        {
  53.         //            return rssText;
  54.         //        }
  55.         return getTextProvider(  ).getText( aTextName );
  56.     }
  57.  
  58.     public String getText( String aTextName,
  59.                            String defaultValue )
  60.     {
  61.         //        String rssText = RssUtil.convertText( aTextName );
  62.         //        if ( ( rssText != null ) && !rssText.equals( aTextName ) )
  63.         //        {
  64.         //            return rssText;
  65.         //        }
  66.         return getTextProvider(  ).getText( aTextName, defaultValue );
  67.     }
  68.  
  69.     public String getText( String aTextName,
  70.                            String defaultValue,
  71.                            String obj )
  72.     {
  73.         //        Object msgArgs[] = new Object[] { obj };
  74.         //        String rssText = RssUtil.convertText( aTextName, msgArgs );
  75.         //        if ( ( rssText != null ) && !rssText.equals( aTextName ) )
  76.         //        {
  77.         //            return rssText;
  78.         //        }
  79.         return getTextProvider(  ).getText( aTextName, defaultValue, obj );
  80.     }
  81.  
  82.     public String getText( String       aTextName,
  83.                            List<Object> args )
  84.     {
  85.         //        String rssText = RssUtil.convertText( aTextName, args.toArray(  ) );
  86.         //        if ( ( rssText != null ) && !rssText.equals( aTextName ) )
  87.         //        {
  88.         //            return rssText;
  89.         //        }
  90.         return getTextProvider(  ).getText( aTextName, args );
  91.     }
  92.  
  93.     public String getText( String key,
  94.                            String args[] )
  95.     {
  96.         //        String rssText = RssUtil.convertText( key, args );
  97.         //        if ( ( rssText != null ) && !rssText.equals( key ) )
  98.         //        {
  99.         //            return rssText;
  100.         //        }
  101.         return getTextProvider(  ).getText( key, args );
  102.     }
  103.  
  104.     public String getText( String       aTextName,
  105.                            String       defaultValue,
  106.                            List<Object> args )
  107.     {
  108.         //        String rssText = RssUtil.convertText( aTextName, args.toArray(  ) );
  109.         //        if ( ( rssText != null ) && !rssText.equals( aTextName ) )
  110.         //        {
  111.         //            return rssText;
  112.         //        }
  113.         return getTextProvider(  ).getText( aTextName, defaultValue, args );
  114.     }
  115.  
  116.     public String getText( String key,
  117.                            String defaultValue,
  118.                            String args[] )
  119.     {
  120.         //        String rssText = RssUtil.convertText( key, args );
  121.         //        if ( ( rssText != null ) && !rssText.equals( key ) )
  122.         //        {
  123.         //            return rssText;
  124.         //        }
  125.         return getTextProvider(  ).getText( key, defaultValue, args );
  126.     }
  127.  
  128.     public String getText( String       key,
  129.                            String       defaultValue,
  130.                            List<Object> args,
  131.                            ValueStack   stack )
  132.     {
  133.         //        String rssText = RssUtil.convertText( key, args.toArray(  ) );
  134.         //        if ( ( rssText != null ) && !rssText.equals( key ) )
  135.         //        {
  136.         //            return rssText;
  137.         //        }
  138.         return getTextProvider(  ).getText( key, defaultValue, args, stack );
  139.     }
  140.  
  141.     public String getText( String     key,
  142.                            String     defaultValue,
  143.                            String     args[],
  144.                            ValueStack stack )
  145.     {
  146.         //        String rssText = RssUtil.convertText( key, args );
  147.         //        if ( ( rssText != null ) && !rssText.equals( key ) )
  148.         //        {
  149.         //            return rssText;
  150.         //        }
  151.         return getTextProvider(  ).getText( key, defaultValue, args, stack );
  152.     }
  153.  
  154.     public ResourceBundle getTexts(  )
  155.     {
  156.         // should return RssUtil.resourceProperties as a Bundle?
  157.         return getTextProvider(  ).getTexts(  );
  158.     }
  159.  
  160.     public ResourceBundle getTexts( String aBundleName )
  161.     {
  162.         return getTextProvider(  ).getTexts( aBundleName );
  163.     }
  164.  
  165.     private TextProvider getTextProvider(  )
  166.     {
  167.         if ( textProvider == null )
  168.         {
  169.             TextProviderFactory tpf = new TextProviderFactory(  );
  170.  
  171.             textProvider = tpf.createInstance( getClass(  ), this );
  172.         }
  173.  
  174.         return textProvider;
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment