Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
Library | Service | Contents | Index

Customizing Content with XML: Dynamic News Application, 15 of 16

## 4 Customizing Presentation

After fetching news items from the database, Dynamic News converts them to XML documents. XML separates content from presentation, making it easy to build custom HTML pages.

Dynamic News uses different XSL stylesheets to convert XML documents into HTML pages customized for various browsers:

- One for Netscape Navigator

- One for Microsoft Internet Explorer

- A generic stylesheet for other browsers.

It's a four-step process:

1. Get the user's browser type.

2. Get news items.

3. Build an XML document.

4. Convert XML to HTML.

Each time it receives an HTTP request, the application inspects the user-agent header to find out what kind of browser made the request. The following lines from xmlnews.dynamic.DynamicServlet.service show how the servlet creates a RequestHandler object (implemented in xmlnews/common/RequestHandler.java) and parses the request to get the browser type. Then the servlet uses this information to return an HTML page based on the end-user's preferences and browser type.

    public void service(HttpServletRequest p_request, HttpServletResponse p_
    response)
    throws ServletException {
             ...
            // Instantiate a Request Handler (declared elsewhere)
             m_reqHandler = new RequestHandler(m_userPreference, m_general,m_
    status);
             RequestParams l_reqParams = m_reqHandler.parseRequest(p_request, m_
    connection);
             String l_browserType = l_reqParams.m_browserType;
             ...
             // Display the Dynamic Page
             this.sendDynamicPage(l_browserType,p_response,l_userName,m_
    userPreference,
                                  m_servletPath+"?REQUEST_TYPE=SET_ADVANCED_USER_
    PREFS",
                    m_servletPath+"?REQUEST_TYPE=LOGIN_REQUEST",
                    m_servletPath+"?REQUEST_TYPE=LOG_OUT_REQUEST",
                    m_servletPath);
             ...
         }

The code that actually extracts the browser type from the user-agent header resides in `xmlnews.common.GenUtility.getBrowserType`, which follows:

            public String getBrowserType(HttpServletRequest p_request) throws
    Exception {

              // Get all the Header Names associated with the Request
              Enumeration l_enum = p_request.getHeaderNames();

              String l_Version     = null;
              String l_browValue   = null;
              String l_browserType = null;

              while (l_enum.hasMoreElements()) {
                 String l_name = (String)l_enum.nextElement();
                 if (l_name.equalsIgnoreCase("user-agent"))
                     l_browValue = p_request.getHeader(l_name);
              }

              // If the value contains a String "MSIE" then it is Internet Explorer
              if (l_browValue.indexOf("MSIE") > 0 ) {
                  StringTokenizer l_st = new StringTokenizer(l_browValue, ";");
                  // Parse the Header to get the browser version.
                  l_browserType = "IE";
                  while (l_st.hasMoreTokens()) {
                     String l_tempStr = l_st.nextToken();
                     if (l_tempStr.indexOf("MSIE") > 0 ) {
                        StringTokenizer l_st1 = new StringTokenizer(l_tempStr, " ");
                        l_st1.nextToken();
                        l_Version = l_st1.nextToken();
                     }
                  }
              // If the value contains a String "en" then it is Netscape
              } else if (l_browValue.indexOf("en") > 0) {
                  l_browserType = "NET";
                  String l_tVersion = l_browValue.substring(8);
                  int l_tempInd  = l_tVersion.indexOf("[");
                  l_Version = l_tVersion.substring(0, l_tempInd);
              }

              // Return the Browser Type and Version after concatenating
              return l_browserType + l_Version;
            }

After getting the end-user's browser type, the DynamicServlet's service method passes it to `xmlnews.dynamic.DynamicServlet.sendDynamicPage`.

This method generates HTML by fetching XML documents from the database and converting them to HTML by applying an XSL stylesheet appropriate for the end-user's browser type.

    public void sendDynamicPage(String p_browserType,HttpServletResponse p_response,
      String p_userName,UserPreference p_pref,String p_userPrefURL,
      String p_signOnURL,String p_logout,
      String p_servletPath) throws Exception {
      String l_finalHTML = ""; // Holds the html
         if (p_browserType.startsWith("IE4") || (p_browserType.startsWith("IE5"))) {
             // Send the XML and XSL as parameters to get the HTML string.
             l_finalHTML = m_handler.applyXSLtoXML(
                                 this.dynamicProcessingXML(m_connection, p_pref),
                                 m_dyEnv.m_dynNewsHome + "/DynamicIE.xsl"
                                 );
      String l_thisbit = m_general.postProcessing(l_finalHTML,p_userName,
             p_userPrefURL,p_signOnURL,p_logout,p_servletPath);
             PrintWriter l_output = p_response.getWriter();
             l_output.print(l_thisbit);
             l_output.close();
         }
          else if (p_browserType.startsWith("NET4") ||
             (p_browserType.startsWith("NET5"))) {
             // Do the same thing, but apply the stylesheet "/DynamicNS.xsl"
             ...
          // When the Browser is other than IE or Netscape.
          } else {
          // Do the same thing, but apply the stylesheet "/Dynamic.xsl"
          ...
          }
       }

The key methods are:

- `xmlnews.dynamic.DynamicServlet.dynamicProcessingXML`

  This queries the database for news items that match the end-user's preferences. It converts the result set into an XML document by calling `xmlnews.common.XMLDocHandler.constructXMLDoc`.

- `xmlnews.common.XMLDocHandler.applyXSLtoXML`

  This converts an XML document into HTML using a specified XSL stylesheet. It uses XSL Transformation capabilities of Oracle XML Parser Version 2.0. More specifically, it uses the Document Object Model (DOM) parser to create a tree that represents the structure of the XML document. To build the final HTML string, it creates an element to serve as the root of the tree, then appends the parsed DOM document.

| Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
Library | Service | Contents | Index