Source for gnu.javax.swing.text.html.parser.gnuDTD

   1: /* gnuDTD.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 gnu.javax.swing.text.html.parser;
  40: 
  41: import java.io.PrintStream;
  42: import java.io.Serializable;
  43: 
  44: import java.util.BitSet;
  45: import java.util.Collection;
  46: import java.util.Iterator;
  47: import java.util.Map;
  48: import java.util.Vector;
  49: 
  50: import javax.swing.text.html.parser.AttributeList;
  51: import javax.swing.text.html.parser.ContentModel;
  52: import javax.swing.text.html.parser.Element;
  53: import javax.swing.text.html.parser.Entity;
  54: 
  55: /**
  56:  * <p>
  57:  * The class is derived from {@link gnu.javax.swing.text.html.parser.DTD }
  58:  * making structure creation methods public. This is required when
  59:  * creating the DTD by SGML parser that must have access to the structure.
  60:  *
  61:  * SGML DTD representation. Provides basis for describing a syntax of the
  62:  * HTML documents. The fields of this class are NOT initialized in
  63:  * constructor. You need to do this separately before passing this data
  64:  * structure to the parser constructor.</p>
  65:  *
  66:  * <p>This implementation also provides you the derived class
  67:  * <code>gnu.javax.swing.text.html.parser.DTD.HTML_4_0_1</code>, where
  68:  * all fields are initialized to the values, representing HTML 4.01
  69:  * ("-//W3C//DTD HTML 4.01 Frameset//EN") DTD. You can use it if you do not care
  70:  * about the portability between different implementations of the core
  71:  * class libraries. </p>
  72:  * <p>Use {@link javax.swing.HTML.HTMLEditorKit.Parser#parse }
  73:  * for parsing in accordance with "-//W3C//DTD HTML 4.01 Frameset//EN"
  74:  * without specifying DTD separately.</p>
  75:  *
  76:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  77:  */
  78: public class gnuDTD
  79:   extends javax.swing.text.html.parser.DTD
  80:   implements javax.swing.text.html.parser.DTDConstants, Serializable
  81: {
  82:   /* The undocumented element types, used to specify types, not defined
  83:   in DTDConstants. */
  84: 
  85:   /**
  86:    * The URI element type (not defined in DTDConstants).
  87:    */
  88:   public static final int URI = 512;
  89: 
  90:   /**
  91:    * The Length element type
  92:    */
  93:   public static final int Length = 513;
  94: 
  95:   /**
  96:    * The Char element type
  97:    */
  98:   public static final int Char = 514;
  99: 
 100:   /**
 101:    * The Color element type
 102:    */
 103:   public static final int Color = 515;
 104: 
 105:   /**
 106:    * Creates a new instance of gnuDTD.
 107:    * @param name the name of the DTD.
 108:    */
 109:   public gnuDTD(String name)
 110:   {
 111:     super(name);
 112:   }
 113: 
 114:   /**
 115:    * Creates and returns new attribute (not an attribute list).
 116:    * @param name the name of this attribute
 117:    * @param type the type of this attribute (FIXED, IMPLIED or
 118:    * REQUIRED from <code>DTDConstants</code>).
 119:    * @param modifier the modifier of this attribute
 120:    * @param default_value the default value of this attribute or null if
 121:    * it is not specified.
 122:    * @param allowed_values the allowed values of this attribute. The multiple
 123:    * possible values in this parameter are supposed to be separated by
 124:    * '|', same as in SGML DTD <code>&lt;!ATTLIST </code>tag. This parameter
 125:    * can be null if no list of allowed values is specified.
 126:    * @param atts the previous attribute of this element. This is
 127:    * placed to the field
 128:    * {@link javax.swing.text.html.parser.AttributeList#next },
 129:    * creating a linked list.
 130:    * @return
 131:    */
 132:   public AttributeList defAttributeList(String name, int type, int modifier,
 133:                                         String default_value,
 134:                                         String allowed_values,
 135:                                         AttributeList atts
 136:                                        )
 137:   {
 138:     return super.defAttributeList(name, type, modifier, default_value,
 139:                                   allowed_values, atts
 140:                                  );
 141:   }
 142: 
 143:   /**
 144:    * Define the attributes for the element with the given name.
 145:    * If the element is not exist, it is created. This method is
 146:    * needed if the element attributes are defined befor the
 147:    * element itself.
 148:    * @param forElement
 149:    * @param attributes
 150:    */
 151:   public void defAttrsFor(String forElement, AttributeList attributes)
 152:   {
 153:     super.defineAttributes(forElement, attributes);
 154:   }
 155: 
 156:   /**
 157:    * Creates a new content model.
 158:    * @param type specifies the BNF operation for this content model.
 159:    * The valid operations are documented in the
 160:    * {@link javax.swing.text.html.parser.ContentModel#type }.
 161:    * @param content the content of this content model
 162:    * @param next if the content model is specified by BNF-like
 163:    * expression, contains the rest of this expression.
 164:    * @return The newly created content model.
 165:    */
 166:   public ContentModel defContentModel(int type, Object content,
 167:                                       ContentModel next
 168:                                      )
 169:   {
 170:     return super.defContentModel(type, content, next);
 171:   }
 172: 
 173:   /**
 174:    * Defines a new element and adds it to the element table.
 175:    * If the element alredy exists,
 176:    * overrides it settings with the specified values.
 177:    * @param name the name of the new element
 178:    * @param type the type of the element
 179:    * @param headless true if the element needs no starting tag
 180:    * @param tailless true if the element needs no closing tag
 181:    * @param content the element content.
 182:    * @param exclusions the elements that must be excluded from the
 183:    * content of this element, in all levels of the hierarchy.
 184:    * @param inclusions the elements that can be included as the
 185:    * content of this element.
 186:    * @param attributes the element attributes.
 187:    * @return the created or updated element.
 188:    */
 189:   public Element defElement(String name, int type, boolean headless,
 190:                             boolean tailless, ContentModel content,
 191:                             String[] exclusions, String[] inclusions,
 192:                             AttributeList attributes
 193:                            )
 194:   {
 195:     return super.defElement(name, type, headless, tailless, content,
 196:                             exclusions, inclusions, attributes
 197:                            );
 198:   }
 199: 
 200:   /**
 201:    * Defines a new element and adds it to the element table.
 202:    * If the element alredy exists,
 203:    * overrides it settings with the specified values.
 204:    * @param name the name of the new element
 205:    * @param type the type of the element
 206:    * @param headless true if the element needs no starting tag
 207:    * @param tailless true if the element needs no closing tag
 208:    * @param content the element content.
 209:    * @param exclusions the elements that must be excluded from the
 210:    * content of this element, in all levels of the hierarchy.
 211:    * @param inclusions the elements that can be included as the
 212:    * content of this element.
 213:    * @param attributes the element attributes.
 214:    * @return the created or updated element.
 215:    */
 216:   public Element defElement(String name, int type, boolean headless,
 217:                             boolean tailless, ContentModel content,
 218:                             Collection exclusions, Collection inclusions,
 219:                             AttributeList attributes
 220:                            )
 221:   {
 222:     return super.defElement(name, type, headless, tailless, content,
 223:                             toStringArray(exclusions),
 224:                             toStringArray(inclusions), attributes
 225:                            );
 226:   }
 227: 
 228:   /**
 229:    * Defines a new element and adds it to the element table.
 230:    * If the element alredy exists,
 231:    * overrides it settings with the specified values.
 232:    * @param name the name of the new element
 233:    * @param type the type of the element
 234:    * @param headless true if the element needs no starting tag
 235:    * @param tailless true if the element needs no closing tag
 236:    * @param content the element content.
 237:    * @param exclusions the elements that must be excluded from the
 238:    * content of this element, in all levels of the hierarchy.
 239:    * @param inclusions the elements that can be included as the
 240:    * content of this element.
 241:    * @param attributes the element attributes (an array and not a
 242:    * linked list). The attributes are chained into the linked list
 243:    * inside this method.
 244:    * @return the created or updated element.
 245:    */
 246:   public Element defElement(String name, int type, boolean headless,
 247:                             boolean tailless, ContentModel content,
 248:                             String[] exclusions, String[] inclusions,
 249:                             AttributeList[] attributes
 250:                            )
 251:   {
 252:     AttributeList list;
 253: 
 254:     if (attributes == null || attributes.length == 0)
 255:       list = null;
 256:     else
 257:       {
 258:         if (attributes.length > 1)
 259:           for (int i = 1; i < attributes.length; i++)
 260:             {
 261:               attributes [ i - 1 ].next = attributes [ i ];
 262:             }
 263:         list = attributes [ 0 ];
 264:       }
 265: 
 266:     Element e =
 267:       super.defElement(name, type, headless, tailless, content, exclusions,
 268:                        inclusions, list
 269:                       );
 270:     return e;
 271:   }
 272: 
 273:   /**
 274:    * Creates, adds into the internal table and returns the
 275:    * character entity like <code>&amp;lt;</code>
 276:    *  (means '<code>&lt;</code>' );
 277:    * This method inactivates the recursive refenrences to the same
 278:    * entity.
 279:    * @param name The entity name (without heading &amp; and closing ;)
 280:    * @param type The entity type
 281:    * @param character The entity value (single character)
 282:    * @return The created entity
 283:    */
 284:   public Entity defEntity(String name, int type, String data)
 285:   {
 286:     int r;
 287:     String eref = "%" + name + ";";
 288:     do
 289:       {
 290:         r = data.indexOf(eref);
 291:         if (r > 0)
 292:           {
 293:             data = data.substring(0, r) + data.substring(r + 1);
 294:           }
 295:       }
 296:     while (r > 0);
 297: 
 298:     return super.defEntity(name, type, data);
 299:   }
 300: 
 301:   /**
 302:    * Summarises the document content into the given PrintStream.
 303:    */
 304:   public void dump(PrintStream p)
 305:   {
 306:     Iterator iter = entityHash.entrySet().iterator();
 307:     while (iter.hasNext())
 308:       {
 309:         Map.Entry item = (Map.Entry) iter.next();
 310:         Entity e = (Entity) item.getValue();
 311:         if (e.isGeneral())
 312:           p.println("Entity " + e.getName() + ": " + e.getString());
 313:       }
 314: 
 315:     iter = elementHash.entrySet().iterator();
 316:     while (iter.hasNext())
 317:       {
 318:         Map.Entry item = (Map.Entry) iter.next();
 319:         Element e = (Element) item.getValue();
 320:         p.println("Element " + e.getName());
 321: 
 322:         System.out.println(" includes:");
 323:         dump(e.inclusions);
 324:         System.out.println(" excludes:");
 325:         dump(e.exclusions);
 326:         System.out.println(" attributes:");
 327: 
 328:         AttributeList atts = e.atts;
 329:         while (atts != null)
 330:           {
 331:             p.print("    " + atts.name + " = " + atts.value);
 332:             if (atts.values == null || atts.values.size() == 0)
 333:               p.println();
 334:             else
 335:               {
 336:                 Iterator viter = atts.values.iterator();
 337:                 System.out.print(" ( ");
 338:                 while (viter.hasNext())
 339:                   {
 340:                     System.out.print(viter.next());
 341:                     if (viter.hasNext())
 342:                       System.out.print(" | ");
 343:                   }
 344:                 System.out.println(" ) ");
 345:               }
 346:             atts = atts.next;
 347:           }
 348:       }
 349:   }
 350: 
 351:   /**
 352:    * Prints the content of the given attribute set to the System.out.
 353:    * @param b
 354:    */
 355:   public void dump(BitSet b)
 356:   {
 357:     if (b != null)
 358:       {
 359:         for (int i = 0; i < b.size(); i++)
 360:           {
 361:             if (b.get(i))
 362:               System.out.println(" " + elements.get(i));
 363:           }
 364:       }
 365:     else
 366:       System.out.println(" NULL set");
 367:   }
 368: 
 369:   /**
 370:    * Creates the attribute.
 371:    * @param name The attribute name.
 372:    * @param type The attribute type.
 373:    * @param modifier The attribute modifier.
 374:    * @param defaultValue Default value (or null)
 375:    * @param allowed_values Allowed values (or null)
 376:    * @return The newly created AttributeList. The <code>next</code>
 377:    * field is initialized to null.
 378:    */
 379:   protected AttributeList attr(String name, String default_value,
 380:                                String[] allowed_values, int type, int modifier
 381:                               )
 382:   {
 383:     Vector allowed = null;
 384: 
 385:     if (allowed_values != null)
 386:       {
 387:         allowed = new Vector(allowed_values.length);
 388:         for (int i = 0; i < allowed_values.length; i++)
 389:           {
 390:             allowed.add(allowed_values [ i ]);
 391:           }
 392:       }
 393: 
 394:     AttributeList attr =
 395:       new AttributeList(name, type, modifier, default_value, allowed, null);
 396: 
 397:     return attr;
 398:   }
 399: 
 400:   /**
 401:    * Define the general entity, holding a single character.
 402:    * @param name The entity name (for example, 'amp').
 403:    * The defined entity <b>is</b> stored into the entity table.
 404:    * @param character The entity character (for example, '&').
 405:    */
 406:   protected void defineEntity(String name, int character)
 407:   {
 408:     super.defEntity(name, GENERAL, character);
 409:   }
 410: 
 411:   private String[] toStringArray(Collection c)
 412:   {
 413:     String[] s = new String[ c.size() ];
 414:     Iterator iter = c.iterator();
 415:     for (int i = 0; i < s.length; i++)
 416:       {
 417:         s [ i ] = iter.next().toString();
 418:       }
 419:     return s;
 420:   }
 421: }