Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2006 Henri Sivonen
   *
   * Permission is hereby granted, free of charge, to any person obtaining a 
   * copy of this software and associated documentation files (the "Software"), 
   * to deal in the Software without restriction, including without limitation 
   * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
   * and/or sell copies of the Software, and to permit persons to whom the 
   * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in 
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
  * DEALINGS IN THE SOFTWARE.
  */
 
 package nu.validator.checker.table;
 
 
Checks XHTML table integrity: overlapping cells, spanning past the end of row group, etc.

Author(s):
hsivonen
Version:
$Id$
 
 public final class TableChecker extends Checker {

    
Constructor.
 
     public TableChecker() {
         super();
     }

    
Holds the current table. (Premature optimization to avoid peeking the top of the stack all the time.)
 
     private Table current;

    
A stack for holding the tables that are open and ancestors of the current table. Grows from the tail.
 
     private final LinkedList<Tablestack = new LinkedList<Table>();

    
Pushes the current table onto the stack and creates a new one.
 
     private void push() {
         if ( != null) {
             .addLast();
         }
          = new Table(this);
     }

    
Ends the current table, discards it and pops the top of the stack to be the new current table.

Throws:
org.xml.sax.SAXException if ending the table throws
 
     private void pop() throws SAXException {
         if ( == null) {
             throw new IllegalStateException("Bug!");
         }
         .end();
         if (.isEmpty()) {
              = null;
         } else {
              = .removeLast();
         }
     }

    
 
     public void startElement(String uriString localNameString qName,
             Attributes attsthrows SAXException {
         if ("http://www.w3.org/1999/xhtml".equals(uri)) {
             if ("table".equals(localName)) {
                 push();
             } else if ( != null) {
                 if ("td".equals(localName)) {
                    .startCell(falseatts);
                } else if ("th".equals(localName)) {
                    .startCell(trueatts);
                } else if ("tr".equals(localName)) {
                    .startRow();
                } else if ("tbody".equals(localName)
                        || "thead".equals(localName)
                        || "tfoot".equals(localName)) {
                    .startRowGroup(localName);
                } else if ("col".equals(localName)) {
                    .startCol(AttributeUtil.parseNonNegativeInteger(atts.getValue(
                            """span")));
                } else if ("colgroup".equals(localName)) {
                    .startColGroup(AttributeUtil.parseNonNegativeInteger(atts.getValue(
                            """span")));
                }
            }
        }
    }

    
    public void endElement(String uriString localNameString qName)
            throws SAXException {
        if ("http://www.w3.org/1999/xhtml".equals(uri)) {
            if ("table".equals(localName)) {
                pop();
            } else if ( != null) {
                if ("td".equals(localName)) {
                    .endCell();
                } else if ("th".equals(localName)) {
                    .endCell();
                } else if ("tr".equals(localName)) {
                    .endRow();
                } else if ("tbody".equals(localName)
                        || "thead".equals(localName)
                        || "tfoot".equals(localName)) {
                    .endRowGroup();
                } else if ("col".equals(localName)) {
                    .endCol();
                } else if ("colgroup".equals(localName)) {
                    .endColGroup();
                }
            }
        }
    }

    
    public void reset() {
        .clear();
         = null;
    }
New to GrepCode? Check out our FAQ X