Source for javax.swing.text.html.parser.ContentModel

   1: /* ContentModel.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing.text.html.parser;
  40: 
  41: import gnu.javax.swing.text.html.parser.models.transformer;
  42: 
  43: import java.io.Serializable;
  44: 
  45: import java.util.Vector;
  46: 
  47: /**
  48:  * A representation of the element content. The instances of this class
  49:  * can be arranged into the linked list, representing a BNF expression.
  50:  * The content model is constructed as a branched tree structure in the
  51:  * following way:
  52:  * <pre>
  53:  * a = new ContentModel('+', A, null); // a reprensents A+
  54:  * b = new ContentModel('&amp;', B, a); // b represents B &amp; A+
  55:  * c = new ContentModel('*', b, null); // c represents ( B &amp; A+) *
  56:  * d = new ContentModel('|', new ContentModel('*', A, null),
  57:  *          new ContentModel('?', B, null)); // d represents ( A* | B? )
  58:  * </pre>
  59:  * where the valid operations are:
  60:  * <ul>
  61:  * <li><code>E* </code> E occurs zero or more times</li>
  62:  * <li><code>E+ </code> E occurs one or more times</li>
  63:  * <li><code>E? </code> E occurs once or not atl all</li>
  64:  * <li><code>A,B</code> A occurs before B</li>
  65:  * <li><code>A|B</code> both A and B are permitted in any order.
  66:  * The '|' alone does not permit the repetetive occurence of A or B
  67:  * (use <code>(A|B)*</code>.</li>
  68:  * <li><code>A&amp;B</code> both A and B must occur once (in any order)</li>
  69:  * </ul>
  70:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  71:  */
  72: public final class ContentModel
  73:   implements Serializable
  74: {
  75:   /** Use serialVersionUID for interoperability. */
  76:   private static final long serialVersionUID = -1130825523866321257L;
  77: 
  78:   /**
  79:    * The next content model model ( = pointer to the next element of
  80:    * the linked list) for the binary expression (',','&amp;' or '|'). Null
  81:    * for the last element in the list.
  82:    */
  83:   public ContentModel next;
  84: 
  85:   /**
  86:    * The document content, containing either Element or the enclosed
  87:    * content model (that would be in the parentheses in BNF expression).
  88:    */
  89:   public Object content;
  90: 
  91:   /**
  92:    * Specifies the BNF operation between this node and the node,
  93:    * stored in the field <code>next</code> (or for this node, if it is
  94:    * an unary operation.
  95:    */
  96:   public int type;
  97: 
  98:   /** Create a content model initializing all fields to default values. */
  99:   public ContentModel()
 100:   {
 101:   }
 102: 
 103:   /**
 104:    * Create a content model, consisting of the single element.
 105:    * Examples:
 106:    *<code>
 107:    * a = new ContentModel('+', A, null); // a reprensents A+
 108:    * b = new ContentModel('&amp;', B, a);    // b represents  B &amp; A+
 109:    * c = new ContentModel('*', b, null); // c represents  ( B &amp; A+) *
 110:    * d = new ContentModel('|', A,
 111:    *    new ContentModel('?',b, null);
 112:    *     // d represents
 113:    * </code>
 114:    */
 115:   public ContentModel(Element a_content)
 116:   {
 117:     content = a_content;
 118:   }
 119: 
 120:   /**
 121:    * Create a content model, involving expression of the given type.
 122:    * @param a_type The expression operation type ('*','?' or '+'
 123:    * @param a_content The content for that the expression is applied.
 124:    */
 125:   public ContentModel(int a_type, ContentModel a_content)
 126:   {
 127:     content = a_content;
 128:     type = a_type;
 129:   }
 130: 
 131:   /**
 132:    * Create a content model, involving binary expression of the given type.
 133:    * @param a_type The expression operation type ( ',', '|' or '&amp;').
 134:    * @param a_content The content of the left part of the expression.
 135:    * @param a_next The content model, representing the right part of the
 136:    * expression.
 137:    */
 138:   public ContentModel(int a_type, Object a_content, ContentModel a_next)
 139:   {
 140:     content = a_content;
 141:     type = a_type;
 142:     next = a_next;
 143:   }
 144: 
 145:   /**
 146:    * Adds all list elements to the given vector, ignoring the
 147:    * operations between the elements. The old vector values are not
 148:    * discarded.
 149:    * @param elements - a vector to add the values to.
 150:    */
 151:   public void getElements(Vector elements)
 152:   {
 153:     ContentModel c = this;
 154: 
 155:     while (c != null)
 156:       {
 157:         elements.add(c.content);
 158:         c = c.next;
 159:       }
 160:   }
 161: 
 162:   /**
 163:    * Checks if the content model matches an empty input stream.
 164:    * The empty content is created using SGML DTD keyword EMPTY.
 165:    * The empty model is a model with the content field equal to null.
 166:    *
 167:    * @return true if the content field is equal to null.
 168:    */
 169:   public boolean empty()
 170:   {
 171:     return content == null;
 172:   }
 173: 
 174:   /**
 175:    * Get the element, stored in the <code>next.content</code>.
 176:    * The method is programmed as the part of the standard API, but not
 177:    * used in this implementation.
 178:    * @return the value of the field <code>next</code>.
 179:    */
 180:   public Element first()
 181:   {
 182:     return (Element) next.content;
 183:   }
 184: 
 185:   /**
 186:    * Checks if this object can potentially be the first token in the
 187:    * ContenModel list. The method is programmed as the part of the
 188:    *  standard API, but not used in this implementation.
 189:    */
 190:   public boolean first(Object token)
 191:   {
 192:     ContentModel c = this;
 193:     while (c.next != null)
 194:       {
 195:         if (c.content != null && c.content.toString().equals(token.toString()) &&
 196:             c.type != ','
 197:            )
 198: 
 199:           // Agree if the operation with the preceeding element
 200:           // is not the comma operation.
 201:           return true;
 202:         c = c.next;
 203:       }
 204:     return false;
 205:   }
 206: 
 207:   /**
 208:    * Returns a string representation (an expression) of this content model.
 209:    * The expression has BNF-like syntax, except the absence of the
 210:    * unary operator is additionally indicated by " ' ". It is
 211:    * advisable to check the created models for correctness using this
 212:    * method.
 213:    */
 214:   public String toString()
 215:   {
 216:     return transformer.transform(this).toString();
 217:   }
 218: }