Oracle9iAS Portal Developer Kit
How to add Custom Tags to the Provider Definition File

In the previous articles you learned how to use the PDK-Java Framework to develop a portlet and render portlet content in various render modes i.e. Show, Help, About, Show Details, Edit & Edit Defaults. This article explains how to add custom tags to the provider.xml file and use the data provided by those additional tags.

The example explained in this article introduces a new tag <myCustomTag> as a child tag to <provider> tag in provider.xml. This tag holds a string as value and this string is rendered inside the portlet by the renderer. This tag is not a part of the PDK-Java framework and is added only to explain how to add custom tags to the provider.xml file.

DefaultNodeHandler Class

PDK Java framework translates the XML DOM object tree of the provider.xml file into an analogous hierarchy of application objects, whose classes, attributes and associations all reflect the configuration of the node in the XML tree. The nodes in the XML document are recursively processed and the corresponding tree of application objects are built. Every 'complex' node is considered to correspond to a new application object and every simple node is considered to correspond to a particular property of the context application object.

This processing is done by a class 'DefaultNodeHandler' in PDK Java framework. Whenever a complex node is encountered, an instance of the application object defined in class attribute corresponding to the complex tag is created. The same process is continued if its children are also complex nodes. Whenever  a simple node is encountered, the corresponding property on the context application object is set to the value of the simple element.

The main restriction placed on a complex element is that, each complex element must have a class attribute containing the fully qualified name of the corresponding application object class. For each possible child of a complex element that is itself a complex element, the application object class should have a public method that should take the child application object as a single Object parameter and should have a name prefixed by "set" or "add" and suffixed by the name of the child element. For each possible child of a complex element that is a simple element, the application object class should have a method to set a corresponding property. The method should take the String value of the first text node child of the simple element as a parameter.

For a detailed understanding of DefaultNodeHandler class refer to the API documentation of PDK-Java framework.

Custom Tag <myCustomTag>

As mentioned earlier, the tag <myCustomTag> will be introduced under the <provider> tag. The provider.xml snippet is shown with the custom tag <myCustomTag>.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?providerDefinition version="2.0"?>
<provider class="oracle.portal.provider.v1.http.MyCustomTagProvider">
   <session>true</session>
   <useOldStyleHeaders>false</useOldStyleHeaders>
   <myCustomTag>My custom tag information</myCustomTag>
   <portlet class="oracle.portal.provider.v1.http.DefaultPortlet">
      <id>1</id>
      ...
      ...
      ... 
   </portlet>
</provider>
provider.xml

Extending DefaultProvider

This is a simple tag and hence the application object that corresponds to the parent node should contain a 'set' or 'add' method that should take the String value of the first text node child of the simple element as a parameter. Since the tag <myCustomTag> does not belong to the PDK-Java framework, the application object oracle.portal.provider.v1.http.DefaultProvider does not have a 'set' or 'add' method to set the value of the <myCustomTag> element. To use this tag, we need an application object that should have 'set' or 'add' methods for both the custom tag <myCustomTag> and the tags that are part of PDK-Java framework. This can be achieved by means of writing a new class 'oracle.portal.provider.v1.http.MyCustomTagProvider' and make it extend 'oracle.portal.provider.v1.http.DefaultProvider'. Inside 'oracle.portal.provider.v1.http.MyCustomTagProvider' we shall introduce a 'set' method to set the value of the tag and a 'get' method that will return the value. The 'oracle.portal.provider.v1.http.MyCustomTagProvider' class is shown below.
 

package oracle.portal.provider.v1.http;

import java.io.*;
import oracle.portal.provider.v1.*;
import oracle.portal.provider.v1.http.*;

public class MyCustomTagProvider extends DefaultProvider
{
    private String mMyCustomTag=null;

    public void setMyCustomTag(String myCustomTag)
    {
        mMyCustomTag=myCustomTag;
    }
    public String getMyCustomTag()
    {
        return mMyCustomTag;
    }
}

MyCustomTagProvider.java

Rendering Custom Tag Value

The value of tag <myCustomTag> is set using the method setMyCustomTag(String) and the value can be obtained using the  getMyCustomTag() method. The renderer 'oracle.portal.provider.v1.http.MyCustomTagRenderer' shown below invokes the getMyCustomTag()method to display the value of <myCustomTag>.
 

package oracle.portal.provider.v1.http;

import java.io.*;
import oracle.portal.provider.v1.*;
import oracle.portal.provider.v1.http.BaseManagedRenderer;
import oracle.portal.provider.v1.http.*;

public class MyCustomTagRenderer extends BaseManagedRenderer
{
    public void renderBody(PortletRenderRequest pr) throws PortletException
    {
        String myCustomTagValue=null;
        try
        {
            PrintWriter out   = pr.getWriter();
            Provider provider = pr.getProvider();
            myCustomTagValue = ((MyCustomTagProvider)provider).getMyCustomTag();
            out.println(myCustomTagValue);
        }
        catch (IOException ioe)
        {
            throw new PortletException(ioe);
        }
    }
}

MyCustomTagRenderer.java

In this article you learned how to add your own custom tags to the provider definition file (provider.xml) and use the values of those tags.

Sample


Revision History