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:   /**
  99:    * Create a content model initializing all fields to default values.
 100:    */
 101:   public ContentModel()
 102:   {
 103:     // Nothing to do here.
 104:   }
 105: 
 106:   /**
 107:    * Create a content model, consisting of the single element.
 108:    * Examples:
 109:    *<code>
 110:    * a = new ContentModel('+', A, null); // a reprensents A+
 111:    * b = new ContentModel('&amp;', B, a);    // b represents  B &amp; A+
 112:    * c = new ContentModel('*', b, null); // c represents  ( B &amp; A+) *
 113:    * d = new ContentModel('|', A,
 114:    *    new ContentModel('?',b, null);
 115:    *     // d represents
 116:    * </code>
 117:    */
 118:   public ContentModel(Element a_content)
 119:   {
 120:     content = a_content;
 121:   }
 122: 
 123:   /**
 124:    * Create a content model, involving expression of the given type.
 125:    * @param a_type The expression operation type ('*','?' or '+'
 126:    * @param a_content The content for that the expression is applied.
 127:    */
 128:   public ContentModel(int a_type, ContentModel a_content)
 129:   {
 130:     content = a_content;
 131:     type = a_type;
 132:   }
 133: 
 134:   /**
 135:    * Create a content model, involving binary expression of the given type.
 136:    * @param a_type The expression operation type ( ',', '|' or '&amp;').
 137:    * @param a_content The content of the left part of the expression.
 138:    * @param a_next The content model, representing the right part of the
 139:    * expression.
 140:    */
 141:   public ContentModel(int a_type, Object a_content, ContentModel a_next)
 142:   {
 143:     content = a_content;
 144:     type = a_type;
 145:     next = a_next;
 146:   }
 147: 
 148:   /**
 149:    * Adds all list elements to the given vector, ignoring the
 150:    * operations between the elements. The old vector values are not
 151:    * discarded.
 152:    * @param elements - a vector to add the values to.
 153:    */
 154:   public void getElements(Vector<Element> elements)
 155:   {
 156:     ContentModel c = this;
 157: 
 158:     while (c != null)
 159:       {
 160:         // FIXME: correct?
 161:         if (c.content instanceof Element)
 162:           elements.add((Element) c.content);
 163:         c = c.next;
 164:       }
 165:   }
 166: 
 167:   /**
 168:    * Checks if the content model matches an empty input stream.
 169:    * The empty content is created using SGML DTD keyword EMPTY.
 170:    * The empty model is a model with the content field equal to null.
 171:    *
 172:    * @return true if the content field is equal to null.
 173:    */
 174:   public boolean empty()
 175:   {
 176:     return content == null;
 177:   }
 178: 
 179:   /**
 180:    * Get the element, stored in the <code>next.content</code>.
 181:    * The method is programmed as the part of the standard API, but not
 182:    * used in this implementation.
 183:    * @return the value of the field <code>next</code>.
 184:    */
 185:   public Element first()
 186:   {
 187:     return (Element) next.content;
 188:   }
 189: 
 190:   /**
 191:    * Checks if this object can potentially be the first token in the
 192:    * ContenModel list. The method is programmed as the part of the
 193:    *  standard API, but not used in this implementation.
 194:    */
 195:   public boolean first(Object token)
 196:   {
 197:     ContentModel c = this;
 198:     while (c.next != null)
 199:       {
 200:         if (c.content != null && c.content.toString().equals(token.toString()) &&
 201:             c.type != ','
 202:            )
 203: 
 204:           // Agree if the operation with the preceeding element
 205:           // is not the comma operation.
 206:           return true;
 207:         c = c.next;
 208:       }
 209:     return false;
 210:   }
 211: 
 212:   /**
 213:    * Returns a string representation (an expression) of this content model.
 214:    * The expression has BNF-like syntax, except the absence of the
 215:    * unary operator is additionally indicated by " ' ". It is
 216:    * advisable to check the created models for correctness using this
 217:    * method.
 218:    */
 219:   public String toString()
 220:   {
 221:     return transformer.transform(this).toString();
 222:   }
 223: }