+

Search Tips   |   Advanced Search

Create a Text Provider class

A text provider is used to provide localized text that can be used within web content item forms. For example, a text provider can be used to localize the field labels or help text within an authoring template so that each user sees the labels or help text in their own language. To use a text provider, create a text provider class and then register the text provider by deploying it on the server.

  1. Create a java class that implements the interface com.ibm.workplace.wcm.api.plugin.textprovider.TextProvider . This class implement the following methods:

      public String getProviderName() {}
      This method returns the unique name of the text provider.

      public String getString(String key, Locale displayLocale) {}
      This method returns some translated text, given a key identifying the message and a locale.

      public Collection<String> getProviderKeys() {}
      This method returns a list of keys used when accessing the text provider. These keys are displayed in the authoring UI when a user is configuring the text provider.

      public boolean isShownInAuthoringUI() {}
      This method allows us to prevent the text provider from appearing in the authoring UI.

    See the Javadoc documentation for further information. The Javadoc files for Web Content Manager are located in the PORTAL_HOME/doc/Javadoc/spi_docs/com/ibm/workplace/wcm directory.

  2. Methods inherited from com.ibm.portal.Localized must also be implemented.

      public String getTitle(Locale displayLocale) {}
      This method returns the title for the text provider that will be used to allow selection of the text provider.

      public ListModel<Locale> getLocales()
      This method returns a list of locales supported by this text provider.

      public String getDescription(Locale p_arg0)
      This method returns a description of the text provider.

    For example:

    package test;
     import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Locale;
    import java.util.MissingResourceException;
    import java.util.Properties;
    import java.util.ResourceBundle;
    import java.util.Set;
     import com.ibm.portal.ListModel;
    import com.ibm.portal.ModelException;
    import com.ibm.workplace.wcm.api.plugin.textprovider.TextProvider;
     /**
     * A simple text provider implementation  */
    public class SimpleTextProvider implements TextProvider {
       /** Path to Bundle properties file (English used for loading keyset) */
       private static final String BUNDLE_PROPERTIES = "/test/SimpleBundle_en.properties";
        /** The text provider title */
       public static final String PROVIDER_TITLE = "Simple Text Provider";
           /** The unique text provider name */
       public static final String PROVIDER_NAME = "test.SimpleTextProvider";
           /** A brief description of this text provider */
       private static final String PROVIDER_DESCRIPTION = "Simple Text provider";
           /** The resource bundle used to lookup strings */
       private static final String RESOURCE_BUNDLE_NAME = "test.SimpleBundle";
           /**
        * A simple list model holding locales.
        */
       protected static class SimpleLocaleListModel<K> implements ListModel<Locale>
       {
          /** the list of locales of this list model */
          final List<Locale> m_localeList = new ArrayList<Locale>();
           /**
           * Constructs this simple list model holding the given locales.
           *         
           * @param p_locales        
           *           the locales of this list model. May be <code>null</code>.
           */
          public SimpleLocaleListModel(final Locale[] p_locales)
          {
             if (p_locales != null)
             {
                for (int i = 0; i < p_locales.length; ++i)
                {
     m_localeList.add(p_locales[i]);
                }
             }
          }
           @Override
          public Iterator<Locale> iterator() throws ModelException
          {
             return m_localeList.iterator();
          }
       }
        /** a list model that only contains the English language locale */
       private static final ListModel<Locale> ENGLISH_ONLY = new SimpleLocaleListModel<Locale>(new Locale[]{Locale.ENGLISH});
           @Override
       public String getProviderName()
       {
          return PROVIDER_NAME;
       }
        @Override
       public String getTitle(Locale p_displayLocale)
       {
          // Perform any localization for the plugin title here       
          return PROVIDER_TITLE;
       }
           @Override
       public String getDescription(Locale p_displayLocale)
       {
          return PROVIDER_DESCRIPTION;
       }
        @Override
       public String getString(String p_key, Locale p_displayLocale)
       {
          String value;
              try       {
             ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME, p_displayLocale);
             value = bundle.getString(p_key);
          }
          catch (MissingResourceException e)
          {
             // The bundle or key was not found. Return null.
             value = null;
          }
          return value;
       }
        @Override
       public Collection<String> getProviderKeys()
       {
          Collection<String> keys = null;
          try       {
             LinkedProperties props = new LinkedProperties();
             props.load(getClass().getClassLoader().getResourceAsStream(BUNDLE_PROPERTIES));
         keys = props.getKeySet();
          }
          catch (IOException e)
          {
             // The bundle was not found. Return null.
             e.printStackTrace();
          }
                 return keys;
       }
         @Override
       public ListModel<Locale> getLocales()
       {
          return ENGLISH_ONLY;
       }
        @Override
       public boolean isShownInAuthoringUI()
       {
          return true;
       }
           /** Used to provide the properties in order */
       private class LinkedProperties extends Properties {
           /** Keys */
          private final LinkedHashSet<String> keys = new LinkedHashSet<String>();
               /**
           * @return An ordered set of keys        */
          public Set<String> getKeySet()
          {
             return keys;
          }
           @Override
          public Object put(Object key, Object value) {
              keys.add((String) key);
              return super.put(key, value);
          }
      }
    }

    See the Javadoc documentation for further information.

  3. A plugin.xml file is needed whether the deployment is done using a WAR or EAR, or using a loose jar. If deploying via an application in a WAR or EAR, include the plugin.xml file in the application's "WEB-INF" folder. When using a jar, include the plugin.xml in the root of the jar.
    <?xml version="1.0" encoding="UTF-8"?>
    <plugin 
           name="Sample Text Provider"
           version="1.0.0"
           provider-name="IBM">
             <extension
        point="com.ibm.workplace.wcm.api.TextProvider"
        >
        <provider class="com.acme.SampleTextprovider"/>
     </extension>
     </plugin>


What to do next

  • Each plug-in is represented by a single <extension></extension> tag.

  • The value of the point attribute must be "com.ibm.workplace.wcm.api.TextProvider".

  • Provide an id value of the choice.

  • Specify the provider class for the plug-in.

Naming conventions:

If we create a new plugin application with the same names and IDs as an existing plugin, the new plugin may override the first. When creating plugin applications ensure the following are unique across the system:

  • The plugin ID, plugin name and extension ID of the plugin.xml file.

  • The fully qualified class name plus path of all classes within the application.

  • The file path of any files within the application.


Parent Create custom plug-ins