home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / plugin / composer / io / Comment.java next >
Encoding:
Java Source  |  1998-04-08  |  4.9 KB  |  154 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. package netscape.plugin.composer.io;
  20.  
  21. /** The token type for html comments. HTML comments, for our purposes, have this form:
  22.  * '<!' + text + '>'.
  23.  */
  24.  
  25. public class Comment extends Token {
  26.   String text;
  27.  
  28.   /** Create a comment from a string buffer.
  29.    * @param buf The text of the comment. Doesn't include the '!' at the start of the
  30.    * comment.
  31.    */
  32.   public Comment(StringBuffer buf) {
  33.     text = buf.toString();
  34.   }
  35.  
  36.   /** Create a comment from a string buffer.
  37.    * @param buf The text of the comment. Doesn't include the '!' at the start of the
  38.    * comment.
  39.    */
  40.   Comment(FooStringBuffer buf) {
  41.     text = buf.toString();
  42.   }
  43.  
  44.   /** Create a comment from a string.
  45.    * @param buf The text of the comment. Doesn't include the '!' at the start of the
  46.    * comment.
  47.    */
  48.   public Comment(String text) {
  49.     this.text = text;
  50.   }
  51.  
  52.   /** The text of the comment. Doesn't include the '!' at the start of the
  53.    * comment.
  54.    */
  55.   public String getText() {
  56.     return text;
  57.   }
  58.  
  59.   /** Return the full html representation of the comment, including
  60.    * the "<!" and the ">".
  61.    */
  62.   public String toString() {
  63.     return "<!" + text + ">";
  64.   }
  65.  
  66.   /** Compute the hash code for the comment.
  67.    * @return the hash code of the comment.
  68.    */
  69.  
  70.   public int hashCode() {
  71.     return text.hashCode();
  72.   }
  73.  
  74.   /** Equality test.
  75.    * @param other the objcet to test for equality with this object.
  76.    * @return true if the text of this comment equals the text of
  77.    * the other comment.
  78.    */
  79.   public boolean equals(Object other) {
  80.     if ((other != null) && (other instanceof Comment)) {
  81.       return text.equals(((Comment)other).text);
  82.     }
  83.     return false;
  84.   }
  85.  
  86.   /** Create a selection start comment. Equivalent to
  87.    * createSelectionStart(false);
  88.    * @return a canonical selection start comment.
  89.    */
  90.  
  91.   public static Comment createSelectionStart(){
  92.     return createSelectionStart(false);
  93.   }
  94.  
  95.   /** Create a selection start comment.
  96.    * @param stickyAfter is true if the selection start sticks to the next token.
  97.    * @return a selection start comment.
  98.    */
  99.  
  100.   public static Comment createSelectionStart(boolean stickyAfter){
  101.     return new Comment(stickyAfter ? SELECTION_START_PLUS : SELECTION_START);
  102.   }
  103.   /** Create a selection end comment. Equivalent to
  104.    * createSelectionEnd(false);
  105.    * @return a canonical selection end comment.
  106.    */
  107.  
  108.   public static Comment createSelectionEnd(){
  109.     return createSelectionEnd(false);
  110.   }
  111.  
  112.   /** Create a selection end comment.
  113.    * @param stickyAfter is true if the selection end sticks to the next token.
  114.    * @return a selection end comment.
  115.    */
  116.  
  117.   public static Comment createSelectionEnd(boolean stickyAfter){
  118.     return new Comment(stickyAfter ? SELECTION_END_PLUS : SELECTION_END);
  119.   }
  120.  
  121.   /** Check if this comment is a selection comment.
  122.    * @return true if this comment is a selection comment.
  123.    */
  124.   public boolean isSelection() {
  125.     return isSelectionStart() || isSelectionEnd();
  126.   }
  127.  
  128.   /** Check if this comment is a selection start comment.
  129.    * @return true if this comment is a selection start comment.
  130.    */
  131.   public boolean isSelectionStart() {
  132.     return text.equals(SELECTION_START) || text.equals(SELECTION_START_PLUS);
  133.   }
  134.  
  135.   /** Check if this comment is a selection end comment.
  136.    * @return true if this comment is a selection end comment.
  137.    */
  138.   public boolean isSelectionEnd() {
  139.     return text.equals(SELECTION_END) || text.equals(SELECTION_END_PLUS);
  140.   }
  141.  
  142.   /** Check if this comment is a sticky-after selection comment.
  143.    * @return true if this comment is a selection comment and is a sticky-after comment.
  144.    */
  145.   public boolean isSelectionStickyAfter() {
  146.     return text.equals(SELECTION_START_PLUS) || text.equals(SELECTION_END_PLUS);
  147.   }
  148.  
  149.   private static final String SELECTION_START = new String("-- selection start --");
  150.   private static final String SELECTION_END = new String("-- selection end --");
  151.   private static final String SELECTION_START_PLUS = new String("-- selection start+ --");
  152.   private static final String SELECTION_END_PLUS = new String("-- selection end+ --");
  153. }
  154.