Customizing the XML data handler

You can customize the XML data handler by:

Building a custom XML name handler

The XML data handler calls the name handler to extract the name of the business object from an XML message. The default name handler included with the XML data handler looks for the tag:


   <!DOCTYPE Name>
   

From this tag and the BOPrefix meta-object attribute, the data handler generates the name of the business object. The XML data handler determines which name handler to invoke by using the value of the NameHandlerClass attribute stored in the data-handler meta-object. If you need the name handler to function in a different way, you must:

  1. Create a custom name handler by extending the NameHandler class.
  2. Configure the XML data handler to use the custom class by updating the default value of the NameHandlerClass attribute in the meta-object for the XML data handler.

The following sample code extends the DataHandler class to create a custom data handler, CustomDataHandler, for the XML data handler:

package com.crossworlds.DataHandlers.xml;
    
   // DataHandler Dependencies
   import com.crossworlds.DataHandlers.
        Exceptions.MalformedDataException;
   import com.crossworlds.DataHandlers.NameHandler;
   import com.crossworlds.DataHandlers.DataHandler;
    
   // Java classes
   import java.io.*;
   import java.lang.Exception;
    
   /***********************************************************
    * CustomNameHandler class. This class extends the Namehandler 
    * class and implements method:
    *      getBOName( Reader serializedData, String subType )
    * The method getBOName contains the logic to extract the BOName
   *************************************************************/
   public class CustomNameHandler extends NameHandler
   {
    
            /**
             * This method generates the business object name from 
             * the data extracted from the 'serializedData' arg.
             * In this case, it is up to the caller to create 
             * the BOName.
             */
    
            public String  getBOName( Reader serializedData, 
                                      String subType )
              throws MalformedDataException
            {
              // The NameHandler uses DataHandler tracing. If the 
              // DataHandler is not set, the NameHandler won't run.
              if (dh == null) 
               return null;
              // Log a message
              dh.traceWrite(
              "Entering CustomNameHandler.getBOName for subtype '"
                + subType + "'.", 4);
    
              // This method parses the XML document and extracts the
              // business object name from the following tag in 
              // the XML doc:
              //        <cml title= 
              // For example, in:
              //        <cml title="cholestrol" id="cml_cholesterol">
              // the business object name is 'cholestrol'.
    
              // Log a message
              dh.traceWrite( 
                "Name resolution will be done using <cml title= ",4);
              String name = null;
    
              try
              {
                // Read line of data from the Reader object
                LineNumberReader lineReader = 
                  new LineNumberReader( serializedData );
                serializedData.mark( 1000 );
                String line = lineReader.readLine();
                while ( line != null )
                {
                  // search for <cml title= in the line
                  int start = line.indexOf("<cml title=");
                  if ( start != -1 )
                  {
                    start += 12;
                    // search for the ending quotes for the tile tag
                    int end = line.indexOf('\"', start);
    
                    // extract name from line
                    name = line.substring(start, end);
                    break;
                  }
                  line = lineReader.readLine();
                }
    
                if ( name == null || name.length() == 0 )
                  throw new MalformedDataException(
                   "Error: can't determine the BusinessObject Name.");
    
              }
    
              catch(Exception e)
              {
                throw new MalformedDataException( e.getMessage() );
              }
              serializedData.reset();
              return name;
            }
   }
   

Building a custom entity resolver

The SAX parser used by the XML data handler calls the entity resolver to find external entities (referenced DTDs) within an XML document. The entity resolver included with the XML data handler can ignore external references or search for them on a local file system. If you need to specify another way for external entities to be found, you must create a custom entity resolver class.

The XML data handler determines which entity resolver to invoke by using the value of the EntityResolver attribute stored in the XML data-handler meta-object.

Copyright IBM Corp. 1997, 2003