Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2006 Henri Sivonen
   * Copyright (c) 2010-2013 Mozilla Foundation
   *
   * 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;
 
 
Checks the textContent of elements whose textContent need special non-schema treatment. To smooth code reuse between a conformance checker and editors that only allow RELAX NG plus custom datatypes, this class uses objects that implement DatatypeStreamingValidator.

Author(s):
hsivonen
Version:
$Id$
 
 public final class TextContentChecker extends Checker {
 
     private final static String XHTML_URL = "http://www.w3.org/1999/xhtml";

    
The stack of DatatypeStreamingValidators corresponding to open elements. Stack entry is null if the corresponding element does not need textContent checking. Grows from the tail.
 
     private boolean inEmptyTitleOrOption = false;

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

    
Returns a DatatypeStreamingValidator for the element if it needs textContent checking or null if it does not.

Parameters:
uri the namespace URI of the element
localName the local name of the element
atts the attributes
Returns:
a DatatypeStreamingValidator or null if checks not necessary
 
             String localNameAttributes atts) {
         if (.equals(uri)) {
             if ("time".equals(localName) && (atts.getIndex("""datetime") < 0)) {
                 return ..createStreamingValidator(null);
             }
             if ("script".equals(localName)) {
                 if (atts.getIndex("""src") < 0) {
                     return ..createStreamingValidator(null);
                 } else {
                     return ..createStreamingValidator(null);
                 }
             } else if ("style".equals(localName)
                     || "textarea".equals(localName)
                     || "title".equals(localName)) {
                 return ..createStreamingValidator(null);
             }
         }
        return null;
    }

    
    public void characters(char[] chint startint length)
            throws SAXException {
        for (int i = starti < start + lengthi++) {
            char c = ch[i];
            switch (c) {
                case ' ':
                case '\t':
                case '\r':
                case '\n':
                    continue;
                default:
                     = false;
            }
        }
        for (DatatypeStreamingValidator dsv : ) {
            if (dsv != null) {
                dsv.addCharacters(chstartlength);
            }
        }
    }

    
    public void endElement(String uriString localNameString qName)
            throws SAXException {
        if ( && .equals(uri)
                && "title".equals(localName)) {
            err("Element \u201Ctitle\u201d must not be empty.");
             = false;
        } else if ( && .equals(uri)
                && "option".equals(localName)) {
            err("Element \u201Coption\u201d without "
                    + "attribute \u201clabel\u201d must not be empty.");
             = false;
        }
        DatatypeStreamingValidator dsv = .removeLast();
        if (dsv != null) {
            try {
                dsv.checkValid();
            } catch (DatatypeException e) {
                String msg = e.getMessage();
                if (msg == null) {
                    err("The text content of element \u201C" + localName
                            + "\u201D from namespace \u201C" + uri
                            + "\u201D was not in the required format.");
                } else {
                    if ("time".equals(localName)) {
                        try {
                            errBadTextContent(e,
                                    TimeDatetime.class,
                                    localNameuri);
                        } catch (ClassNotFoundException ce) {
                        }
                    } else if ("script".equals(localName)) {
                        // need cast to Html5DatatypeException in order to check
                        // what HTML5 datatype class this exception of for
                        assert e instanceof Html5DatatypeException : "Not an Html5DatatypeException";
                        Html5DatatypeException ex5 = (Html5DatatypeExceptione;
                        if (Script.class.equals(ex5.getDatatypeClass())) {
                            try {
                                errBadTextContent(eScript.classlocalName,
                                        uri);
                            } catch (ClassNotFoundException ce) {
                                throw new RuntimeException(e);
                            }
                        } else {
                            try {
                                errBadTextContent(eScriptDocumentation.class,
                                        localNameuri);
                            } catch (ClassNotFoundException ce) {
                            }
                        }
                    } else if ("style".equals(localName)) {
                        try {
                            errBadTextContent(e,
                                    CdoCdcPair.class,
                                    localNameuri);
                        } catch (ClassNotFoundException ce) {
                        }
                    } else if ("textarea".equals(localName)
                            || "title".equals(localName)) {
                        try {
                            warnBadTextContent(e,
                                    CdoCdcPair.class,
                                    localNameuri);
                        } catch (ClassNotFoundException ce) {
                        }
                    } else {
                        err("The text content of element \u201C" + localName
                                // + "\u201D from namespace \u201C" + uri
                                + "\u201D was not in the required format: "
                                + msg.split(": ")[1]);
                    }
                }
            }
        }
    }
    private void errBadTextContent(DatatypeException eClass<?> datatypeClass,
            String localNameString urithrows SAXException,
            ClassNotFoundException {
        if (getErrorHandler() != null) {
            Html5DatatypeException ex5 = (Html5DatatypeExceptione;
            boolean warning = ex5.isWarning() ? true : false;
            DatatypeMismatchException dme = new DatatypeMismatchException(
                    "The text content of element \u201c" + localName
                            // + "\u201D from namespace \u201C" + uri
                            + "\u201d was not in the required format: "
                            + e.getMessage().split(": ")[1],
                    getDocumentLocator(), datatypeClasswarning);
            getErrorHandler().error(dme);
        }
    }
    private void warnBadTextContent(DatatypeException eClass<?> datatypeClass,
            String localNameString urithrows SAXException,
            ClassNotFoundException {
        if (getErrorHandler() != null) {
            DatatypeMismatchException dme = new DatatypeMismatchException(
                    "Possible problem in text content of element \u201c"
                            + localName
                            // + "\u201D from namespace \u201C" + uri
                            + "\u201d: " + e.getMessage().split(": ")[1],
                    getDocumentLocator(), datatypeClasstrue);
            getErrorHandler().error(dme);
        }
    }

    
    public void startElement(String uriString localNameString qName,
            Attributes attsthrows SAXException {
        .addLast(streamingValidatorFor(urilocalNameatts));
        if (.equals(uri)
                && ("title".equals(localName))
                || ("option".equals(localName) && atts.getIndex("""label") < 0)) {
             = true;
        }
    }

    

See also:
Checker.reset()
    public void reset() {
        .clear();
    }
New to GrepCode? Check out our FAQ X