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

   1: /* tagStack.java -- The HTML tag stack.
   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 gnu.javax.swing.text.html.parser.models.node;
  42: import gnu.javax.swing.text.html.parser.models.transformer;
  43: 
  44: import java.util.BitSet;
  45: import java.util.Enumeration;
  46: import java.util.LinkedList;
  47: import java.util.ListIterator;
  48: 
  49: import javax.swing.text.SimpleAttributeSet;
  50: import javax.swing.text.html.HTML;
  51: import javax.swing.text.html.parser.*;
  52: 
  53: /**
  54:  * <p>The HTML content validator, is responsible for opening and
  55:  * closing elements with optional start/end tags, detecting
  56:  * the wrongly placed html tags and reporting errors. The working instance
  57:  * is the inner class inside the {@link javax.swing.text.html.parser.Parser }
  58:  * </p>
  59:  * <p>This class could potentially
  60:  * provide basis for automated closing and insertion of the html tags,
  61:  * correcting the found html errors.
  62:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  63:  */
  64: public abstract class htmlValidator
  65: {
  66:   /**
  67:    * The tag reference, holding additional information that the tag
  68:    * has been forcibly closed.
  69:    */
  70:   protected class hTag
  71:   {
  72:     protected final Element element;
  73:     protected final HTML.Tag tag;
  74:     protected final TagElement tgElement;
  75:     protected boolean forcibly_closed;
  76:     protected node validationTrace;
  77: 
  78:     protected hTag(TagElement an_element)
  79:     {
  80:       element = an_element.getElement();
  81:       tag = an_element.getHTMLTag();
  82:       tgElement = an_element;
  83: 
  84:       if (element.content != null)
  85:         validationTrace = transformer.transform(element.content, dtd);
  86:     }
  87: 
  88:     /**
  89:      * This is called when the tag must be forcibly closed because
  90:      * it would make the newly appearing tag invalid.
  91:      * The parser is not notified about such event (just the error
  92:      * is reported). For such tags, the closing message does not
  93:      * appear when later reaching the end of stream. The exception is
  94:      * the &lt;head&gt; tag: the parser is notified about its silent closing
  95:      * when &lt;body&gt; or other html content appears.
  96:      */
  97:     protected void forciblyCloseDueContext()
  98:     {
  99:       forcibly_closed = true;
 100:     }
 101: 
 102:     /**
 103:      * This is called when the tag must be forcibly closed after
 104:      * reaching the end of stream. The parser is notified as if
 105:      * closing the tag explicitly.
 106:      */
 107:     protected void forciblyCloseDueEndOfStream()
 108:     {
 109:       forcibly_closed = true;
 110:       handleSupposedEndTag(element);
 111:     }
 112:   }
 113: 
 114:   /**
 115:    * The DTD, providing information about the valid document structure.
 116:    */
 117:   protected final DTD dtd;
 118: 
 119:   /**
 120:   * The stack, holding the current tag context.
 121:   */
 122:   protected final LinkedList stack = new LinkedList();
 123: 
 124:   /**
 125:    * Creates a new tag stack, using the given DTD.
 126:    * @param a_dtd A DTD, providing the information about the valid
 127:    * tag content.
 128:    */
 129:   public htmlValidator(DTD a_dtd)
 130:   {
 131:     dtd = a_dtd;
 132:   }
 133: 
 134:   /**
 135:    * Close all opened tags (called at the end of parsing).
 136:    */
 137:   public void closeAll()
 138:   {
 139:     hTag h;
 140:     while (!stack.isEmpty())
 141:       {
 142:         h = (hTag) stack.getLast();
 143:         if (!h.forcibly_closed && !h.element.omitEnd())
 144:           s_error("Unclosed <" + h.tag + ">, closing at the end of stream");
 145: 
 146:         handleSupposedEndTag(h.element);
 147: 
 148:         closeTag(h.tgElement);
 149:       }
 150:   }
 151: 
 152:   /**
 153:    * Remove the given tag from the stack or (if found) from the list
 154:    * of the forcibly closed tags.
 155:    */
 156:   public void closeTag(TagElement tElement)
 157:   {
 158:     HTML.Tag tag = tElement.getHTMLTag();
 159:     hTag x;
 160:     hTag close;
 161: 
 162:     if (!stack.isEmpty())
 163:       {
 164:         ListIterator iter = stack.listIterator(stack.size());
 165: 
 166:         while (iter.hasPrevious())
 167:           {
 168:             x = (hTag) iter.previous();
 169:             if (tag.equals(x.tag))
 170:               {
 171:                 if (x.forcibly_closed && !x.element.omitEnd())
 172:                   s_error("The tag <" + x.tag +
 173:                           "> has already been forcibly closed"
 174:                          );
 175: 
 176: 
 177:                 // If the tag has a content model defined, forcibly close all
 178:                 // tags that were opened after the tag being currently closed.
 179:                 closing: 
 180:                 if (x.element.content != null)
 181:                   {
 182:                     iter = stack.listIterator(stack.size());
 183:                     while (iter.hasPrevious())
 184:                       {
 185:                         close = (hTag) iter.previous();
 186:                         if (close == x)
 187:                           break closing;
 188:                         handleSupposedEndTag(close.element);
 189:                         iter.remove();
 190:                       }
 191:                   }
 192: 
 193:                 stack.remove(x);
 194:                 return;
 195:               }
 196:           }
 197:       }
 198:     s_error("Closing unopened <" + tag + ">");
 199:   }
 200: 
 201:   /**
 202:    * Add the given HTML tag to the stack of the opened tags. Forcibly closes
 203:    * all tags in the stack that does not allow this tag in they content (error
 204:    * is reported).
 205:    * @param element
 206:    */
 207:   public void openTag(TagElement tElement, htmlAttributeSet parameters)
 208:   {
 209:     // If this is a fictional call, the message from the parser
 210:     // has recursively returned - ignore.
 211:     if (tElement.fictional())
 212:       return;
 213: 
 214:     validateParameters(tElement, parameters);
 215: 
 216:     // If the stack is empty, start from HTML
 217:     if (stack.isEmpty() && tElement.getHTMLTag() != HTML.Tag.HTML)
 218:       {
 219:         Element html = dtd.getElement(HTML.Tag.HTML.toString());
 220:         openFictionalTag(html);
 221:       }
 222: 
 223:     Object v = tagIsValidForContext(tElement);
 224:     if (v != Boolean.TRUE)
 225:       {
 226:         // The tag is not valid for context, the content
 227:         // model suggest to open another tag.
 228:         if (v instanceof Element)
 229:           {
 230:             int n = 0;
 231:             while (v instanceof Element && (n++ < 100))
 232:               {
 233:                 Element fe = (Element) v;
 234: 
 235:                 // notify the content model that we add the proposed tag
 236:                 node ccm = getCurrentContentModel();
 237:                 if (ccm != null)
 238:                   ccm.show(fe);
 239:                 openFictionalTag(fe);
 240: 
 241:                 Object vv = tagIsValidForContext(tElement);
 242:                 if (vv instanceof Element) // One level of nesting is supported.
 243:                   {
 244:                     openFictionalTag((Element) vv);
 245: 
 246:                     Object vx = tagIsValidForContext(tElement);
 247:                     if (vx instanceof Element)
 248:                       openFictionalTag((Element) vx);
 249:                   }
 250:                 else if (vv == Boolean.FALSE)
 251:                   {
 252:                     // The tag is still not valid for the current
 253:                     // content after opening a fictional element.
 254:                     if (fe.omitEnd())
 255:                       {
 256:                         // close the previously opened fictional tag.
 257:                         closeLast();
 258:                         vv = tagIsValidForContext(tElement);
 259:                         if (vv instanceof Element)
 260: 
 261:                           // another tag was suggested by the content model
 262:                           openFictionalTag((Element) vv);
 263:                       }
 264:                   }
 265:                 v = tagIsValidForContext(tElement);
 266:               }
 267:           }
 268:         else // If the current element has the optional end tag, close it.
 269:           {
 270:             if (!stack.isEmpty())
 271:               {
 272:                 closing: 
 273:                 do
 274:                   {
 275:                     hTag last = (hTag) stack.getLast();
 276:                     if (last.element.omitEnd())
 277:                       {
 278:                         closeLast();
 279:                         v = tagIsValidForContext(tElement);
 280:                         if (v instanceof Element) // another tag was suggested by the content model
 281:                           {
 282:                             openFictionalTag((Element) v);
 283:                             break closing;
 284:                           }
 285:                       }
 286:                     else
 287:                       break closing;
 288:                   }
 289:                 while (v == Boolean.FALSE && !stack.isEmpty());
 290:               }
 291:           }
 292:       }
 293: 
 294:     stack.add(new hTag(tElement));
 295:   }
 296: 
 297:   /**
 298:    * Clear the stack.
 299:    */
 300:   public void restart()
 301:   {
 302:     stack.clear();
 303:   }
 304: 
 305:   /**
 306:    * Check if this tag is valid for the current context. Return Boolean.True if
 307:    * it is OK, Boolean.False if it is surely not OK or the Element that the
 308:    * content model recommends to insert making the situation ok. If Boolean.True
 309:    * is returned, the content model current position is moved forward. Otherwise
 310:    * this position remains the same.
 311:    * 
 312:    * @param tElement
 313:    * @return
 314:    */
 315:   public Object tagIsValidForContext(TagElement tElement)
 316:   {
 317:     // Check the current content model, if one is available.
 318:     node cv = getCurrentContentModel();
 319: 
 320:     if (cv != null)
 321:       return cv.show(tElement.getElement());
 322: 
 323:     // Check exclusions and inclusions.
 324:     ListIterator iter = stack.listIterator(stack.size());
 325:     hTag t = null;
 326:     final int idx = tElement.getElement().index;
 327: 
 328:     // Check only known tags.
 329:     if (idx >= 0)
 330:       {
 331:         BitSet inclusions = new BitSet();
 332:         while (iter.hasPrevious())
 333:           {
 334:             t = (hTag) iter.previous();
 335:             if (! t.forcibly_closed)
 336:               {
 337:                 if (t.element.exclusions != null
 338:                     && t.element.exclusions.get(idx))
 339:                   return Boolean.FALSE;
 340: 
 341:                 if (t.element.inclusions != null)
 342:                   inclusions.or(t.element.inclusions);
 343:               }
 344:           }
 345:         if (! inclusions.get(idx))
 346:           {
 347:             // If we need to insert something, and cannot do this, but
 348:             // it is allowed to insert the paragraph here, insert the
 349:             // paragraph.
 350:             Element P = dtd.getElement(HTML_401F.P);
 351:             if (inclusions.get(P.index))
 352:               return P;
 353:             else
 354:               return Boolean.FALSE;
 355:           }
 356:       }
 357:     return Boolean.TRUE;
 358:   }
 359: 
 360:   /**
 361:    * Validate tag without storing in into the tag stack. This is called
 362:    * for the empty tags and results the subsequent calls to the openTag
 363:    * and closeTag.
 364:    */
 365:   public void validateTag(TagElement tElement, htmlAttributeSet parameters)
 366:   {
 367:     openTag(tElement, parameters);
 368:     closeTag(tElement);
 369:   }
 370: 
 371:   /**
 372:    * Check for mandatory elements, subsequent to the last tag:
 373:    * @param tElement The element that will be inserted next.
 374:    */
 375:   protected void checkContentModel(TagElement tElement, boolean first)
 376:   {
 377:     if (stack.isEmpty())
 378:       return;
 379: 
 380:     hTag last = (hTag) stack.getLast();
 381:     if (last.validationTrace == null)
 382:       return;
 383: 
 384:     Object r = last.validationTrace.show(tElement.getElement());
 385:     if (r == Boolean.FALSE)
 386:       s_error("The <" + last.element + "> does not match the content model " +
 387:               last.validationTrace
 388:              );
 389:     else if (r instanceof Element) // The content model recommends insertion of this element
 390:       {
 391:         if (!first)
 392:           closeTag(last.tgElement);
 393:         handleSupposedStartTag((Element) r);
 394:         openTag(new TagElement((Element) r), null);
 395:       }
 396:   }
 397: 
 398:   /**
 399:    * The method is called when the tag must be closed because
 400:    * it does not allow the subsequent elements inside its context
 401:    * or the end of stream has been reached. The parser is only
 402:    * informed if the element being closed does not require the
 403:    * end tag (the "omitEnd" flag is set).
 404:    * The closing message must be passed to the parser mechanism
 405:    * before passing message about the opening the next tag.
 406:    *
 407:    * @param element The tag being fictionally (forcibly) closed.
 408:    */
 409:   protected abstract void handleSupposedEndTag(Element element);
 410: 
 411:   /**
 412:    * The method is called when the validator decides to open the
 413:    * tag on its own initiative. This may happen if the content model
 414:    * includes the element with the optional (supposed) start tag.
 415:    *
 416:    * @param element The tag being opened.
 417:    */
 418:   protected abstract void handleSupposedStartTag(Element element);
 419: 
 420:   /**
 421:    * Handles the error message. This method must be overridden to pass
 422:    * the message where required.
 423:    * @param msg The message text.
 424:    */
 425:   protected abstract void s_error(String msg);
 426: 
 427:   /**
 428:    * Validate the parameters, report the error if the given parameter is
 429:    * not in the parameter set, valid for the given attribute. The information
 430:    * about the valid parameter set is taken from the Element, enclosed
 431:    * inside the tag. The method does not validate the default parameters.
 432:    * @param tag The tag
 433:    * @param parameters The parameters of this tag.
 434:    */
 435:   protected void validateParameters(TagElement tag, htmlAttributeSet parameters)
 436:   {
 437:     if (parameters == null ||
 438:         parameters == htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET ||
 439:         parameters == SimpleAttributeSet.EMPTY
 440:        )
 441:       return;
 442: 
 443:     Enumeration enumeration = parameters.getAttributeNames();
 444: 
 445:     while (enumeration.hasMoreElements())
 446:       {
 447:         validateAttribute(tag, parameters, enumeration);
 448:       }
 449: 
 450:     // Check for missing required values.
 451:     AttributeList a = tag.getElement().getAttributes();
 452: 
 453:     while (a != null)
 454:       {
 455:         if (a.getModifier() == DTDConstants.REQUIRED)
 456:           if (parameters.getAttribute(a.getName()) == null)
 457:             {
 458:               s_error("Missing required attribute '" + a.getName() + "' for <" +
 459:                       tag.getHTMLTag() + ">"
 460:                      );
 461:             }
 462:         a = a.next;
 463:       }
 464:   }
 465: 
 466:   private node getCurrentContentModel()
 467:   {
 468:     if (!stack.isEmpty())
 469:       {
 470:         hTag last = (hTag) stack.getLast();
 471:         return last.validationTrace;
 472:       }
 473:     else
 474:       return null;
 475:   }
 476: 
 477:   private void closeLast()
 478:   {
 479:     handleSupposedEndTag(((hTag) stack.getLast()).element);
 480:     stack.removeLast();
 481:   }
 482: 
 483:   private void openFictionalTag(Element e)
 484:   {
 485:     handleSupposedStartTag(e);
 486:     stack.add(new hTag(new TagElement(e, true)));
 487:     if (!e.omitStart())
 488:       s_error("<" + e + "> is expected (supposing it)");
 489:   }
 490: 
 491:   private void validateAttribute(TagElement tag, htmlAttributeSet parameters,
 492:                                  Enumeration enumeration
 493:                                 )
 494:   {
 495:     Object foundAttribute;
 496:     AttributeList dtdAttribute;
 497:     foundAttribute = enumeration.nextElement();
 498:     dtdAttribute = tag.getElement().getAttribute(foundAttribute.toString());
 499:     if (dtdAttribute == null)
 500:       {
 501:         StringBuffer valid =
 502:           new StringBuffer("The tag <" + tag.getHTMLTag() +
 503:                            "> cannot contain the attribute '" + foundAttribute +
 504:                            "'. The valid attributes for this tag are: "
 505:                           );
 506: 
 507:         AttributeList a = tag.getElement().getAttributes();
 508: 
 509:         while (a != null)
 510:           {
 511:             valid.append(a.name.toUpperCase());
 512:             valid.append(' ');
 513:             a = a.next;
 514:           }
 515:         s_error(valid.toString());
 516:       }
 517: 
 518:     else
 519:       {
 520:         String value = parameters.getAttribute(foundAttribute).toString();
 521: 
 522:         if (dtdAttribute.type == DTDConstants.NUMBER)
 523:           validateNumberAttribute(tag, foundAttribute, value);
 524: 
 525:         if (dtdAttribute.type == DTDConstants.NAME ||
 526:             dtdAttribute.type == DTDConstants.ID
 527:            )
 528:           validateNameOrIdAttribute(tag, foundAttribute, value);
 529: 
 530:         if (dtdAttribute.values != null)
 531:           validateAttributeWithValueList(tag, foundAttribute, dtdAttribute,
 532:                                          value
 533:                                         );
 534:       }
 535:   }
 536: 
 537:   private void validateAttributeWithValueList(TagElement tag,
 538:                                               Object foundAttribute,
 539:                                               AttributeList dtdAttribute,
 540:                                               String value
 541:                                              )
 542:   {
 543:     if (!dtdAttribute.values.contains(value.toLowerCase()) &&
 544:         !dtdAttribute.values.contains(value.toUpperCase())
 545:        )
 546:       {
 547:         StringBuffer valid;
 548:         if (dtdAttribute.values.size() == 1)
 549:           valid =
 550:             new StringBuffer("The attribute '" + foundAttribute +
 551:                              "' of the tag <" + tag.getHTMLTag() +
 552:                              "> cannot have the value '" + value +
 553:                              "'. The only valid value is "
 554:                             );
 555:         else
 556:           valid =
 557:             new StringBuffer("The attribute '" + foundAttribute +
 558:                              "' of the tag <" + tag.getHTMLTag() +
 559:                              "> cannot have the value '" + value + "'. The " +
 560:                              dtdAttribute.values.size() +
 561:                              " valid values are: "
 562:                             );
 563: 
 564:         Enumeration vv = dtdAttribute.values.elements();
 565:         while (vv.hasMoreElements())
 566:           {
 567:             valid.append('"');
 568:             valid.append(vv.nextElement());
 569:             valid.append("\"  ");
 570:           }
 571:         s_error(valid.toString());
 572:       }
 573:   }
 574: 
 575:   private void validateNameOrIdAttribute(TagElement tag, Object foundAttribute,
 576:                                          String value
 577:                                         )
 578:   {
 579:     boolean ok = true;
 580: 
 581:     if (!Character.isLetter(value.charAt(0)))
 582:       ok = false;
 583: 
 584:     char c;
 585:     for (int i = 0; i < value.length(); i++)
 586:       {
 587:         c = value.charAt(i);
 588:         if (!(
 589:               Character.isLetter(c) || Character.isDigit(c) ||
 590:               "".indexOf(c) >= 0
 591:             )
 592:            )
 593:           ok = false;
 594:       }
 595:     if (!ok)
 596:       s_error("The '" + foundAttribute + "' attribute of the tag <" +
 597:               tag.getHTMLTag() + "> must start from letter and consist of " +
 598:               "letters, digits, hypens, colons, underscores and periods. " +
 599:               "It cannot be '" + value + "'"
 600:              );
 601:   }
 602: 
 603:   private void validateNumberAttribute(TagElement tag, Object foundAttribute,
 604:                                        String value
 605:                                       )
 606:   {
 607:     try
 608:       {
 609:         Integer.parseInt(value);
 610:       }
 611:     catch (NumberFormatException ex)
 612:       {
 613:         s_error("The '" + foundAttribute + "' attribute of the tag <" +
 614:                 tag.getHTMLTag() + "> must be a valid number and not '" +
 615:                 value + "'"
 616:                );
 617:       }
 618:   }
 619: }