ContentTemplate.java :  » Web-Server » Brazil » sunlabs » brazil » template » Java Open Source

Java Open Source » Web Server » Brazil 
Brazil » sunlabs » brazil » template » ContentTemplate.java
/*
 * ContentTemplate.java
 *
 * Brazil project web application Framework,
 * export version: 1.1 
 * Copyright (c) 1999-2000 Sun Microsystems, Inc.
 *
 * Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License Version 
 * 1.0 (the "License"). You may not use this file except in compliance with 
 * the License. A copy of the License is included as the file "license.terms",
 * and also available at http://www.sun.com/
 * 
 * The Original Code is from:
 *    Brazil project web application Framework release 1.1.
 * The Initial Developer of the Original Code is: suhler.
 * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
 * All Rights Reserved.
 * 
 * Contributor(s): cstevens, suhler.
 *
 * Version:  1.16
 * Created by suhler on 99/06/28
 * Last modified by suhler on 00/12/27 12:22:42
 */

package sunlabs.brazil.template;

import java.util.Dictionary;

/**
 * Template class for extracting content out of remote html page
 * This class is used by the TemplateHandler, for extracting
 * the "content" out of html documents for later integration with
 * a look-and-feel template using one or more of:
 * {@link PropsTemplate}, 
 * {@link BSLTemplate}, 
 * or
 * {@link sunlabs.brazil.filter.ReplaceFilter},
 *
 * The plan is to snag the title and the content, and put them into
 * request properties.  The resultant processed output will be
 * discarded.  The following properties are gathered:
 * <dl class=other>
 * <dt>title    <dd> The document title
 * <dt>bodyArgs    <dd> The attributes to the body tag, if any
 * <dt>content    <dd> The body, delimited by content.../content>.
 *      The text inside multiple content ... /content pairs
 *      are concatenated together.
 * <dt>script    <dd> All "script"..."/script" tags found in the document head
 * <dt>style    <dd> All "style"..."/style" tags found in the document head
 * <dt>meta-[name]  <dd> Every meta tag "name" and "content"
 * <dt>link-[rel]  <dd> Every link tag "rel" and "href"
 * <dt>user-agent  <dd> The origin user agent
 * <dt>referer    <dd> The user agent referrer (if any)
 * <dt>last-modified  <dd> The document last modified time (if any) in std format
 * <dt>content-length  <dd> The document content length, as fetched from the origin server
 * </dl>
 *
 * @author    Stephen Uhler
 * @version    %V% 1.16
 */

public class ContentTemplate extends Template {
    boolean inHead = true;

    /**
     * Toss everything up to and including this entity.
     */

    public void
    tag_title(RewriteContext hr) {
  hr.reset();
    }

    /**
     * Gather up the title - no tags allowed between title .... /title.
     */

    public void
    tag_slash_title(RewriteContext hr) {
  hr.request.props.put("title", hr.toString().trim());
  hr.reset();
    }

    /**
     * Append all "script" code while in the head section.
     */

    public void
    tag_script(RewriteContext hr) {
  if (inHead) {
      boolean save = hr.accumulate(false);
      hr.nextToken();
      String script = hr.request.props.getProperty("script","") +
        hr.getBody();
      hr.request.props.put("script",script);
      hr.accumulate(save);
      hr.reset();
  }
    }

    /**
     * Append all "style" code while in the head section.
     */

    public void
    tag_style(RewriteContext hr) {
  if (inHead) {
      boolean save = hr.accumulate(false);
      hr.nextToken();
      String style = hr.request.props.getProperty("style","") +
        hr.getBody();
      hr.request.props.put("style", style);
      hr.accumulate(save);
      hr.reset();
  }
    }

    /**
     * Mark end of head section.  All "script" content in the "body"
     * is left alone.
     */

    public void
    tag_slash_head(RewriteContext hr) {
  inHead = false;
    }

    /**
     * toss everything up to and including here, but turn on
     * content accumulation.
     */

    public void
    tag_content(RewriteContext hr) {
  hr.reset();
  hr.accumulate(true);
    }

    /**
     * Grab the "body" attributes, and toss all output to this point.
     */

    public void
    tag_body(RewriteContext hr) {
  inHead = false;
  String bodyArgs = hr.getArgs();
  if (bodyArgs != null) {
      hr.request.props.put("bodyArgs", bodyArgs);
  }
  hr.reset();
    }

    /**
     * Save the content gathered so far, and turn off content accumulation.
     */

    public void
    tag_slash_content(RewriteContext hr) {
  String content = hr.request.props.getProperty("content","") +
    hr.toString();
  hr.request.props.put("content", content);
  hr.accumulate(false);
    }

    /**
     * If no content tags are present, use the entire "body" instead.
     */

    public void
    tag_slash_body(RewriteContext hr) {
  if (!hr.request.props.containsKey("content")) {
      hr.request.props.put("content", hr.toString());
      hr.accumulate(false);
  }
    }

    /**
     * Extract data out of meta tags into the properties.
     * For "http-equiv" tags, set the corrosponding http respones header.
     */

    public void
    tag_meta(RewriteContext hr) {
  String name = hr.get("name");
  String equiv = hr.get("http-equiv");
  String content = hr.get("content");
  if ((name != null) && (content != null)) {
      hr.request.props.put("meta-" + name, content);
  } else if ((equiv != null) && (content != null)) {
      hr.request.addHeader(equiv, content);
  }
    }

    /**
     * Extract data out of link tags into the properties.
     * Prefix the "rel" attribute with "link-" to use as the 
     * property name.
     */

    public void
    tag_link(RewriteContext hr) {
  String type = hr.get("rel");
  String href = hr.get("href");
  if ((type != null) && (href != null)) {
      hr.request.props.put("link-" + type, href);
  }
    }

    /**
     * Extract useful properties out of the http mime headers.
     */

    public boolean
    done(RewriteContext hr) {
  tag_slash_body(hr);

  transfer("user-agent", hr.request.headers, hr.request.props);
  transfer("referer", hr.request.headers, hr.request.props);
  transfer("last-modified", hr.request.responseHeaders, hr.request.props);
  transfer("content-length", hr.request.responseHeaders,hr.request.props);
  return true;
    }

    /**
     * Transfer an item to another hash table, if it exists
     */

    private boolean
    transfer (String key, Dictionary src, Dictionary dst) {
  Object obj = src.get(key);
  if (obj != null) {
      dst.put(key, obj);
      return true;
  } else {
      return false;
  }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.