Search in sources :

Example 1 with Charset

use of nu.validator.datatype.Charset in project validator by validator.

the class XmlPiChecker method checkXmlStylesheetPiData.

private void checkXmlStylesheetPiData(String data) throws SAXException {
    boolean hasHref = false;
    boolean hasTitle = false;
    boolean hasMedia = false;
    boolean hasCharset = false;
    boolean hasAlternate = false;
    boolean hasNonEmptyTitle = false;
    boolean alternateIsYes = false;
    boolean badDatatype = false;
    if (inDoctype) {
        warn("An \u201cxml-stylesheet\u201d instruction should not be used" + " within a \u201cDOCTYPE\u201d declaration.");
    }
    if (alreadyHasElement) {
        err("Any \u201cxml-stylesheet\u201d instruction in a document must" + " occur before any elements in the document." + " Suppressing any further errors for this" + " \u201cxml-stylesheet\u201d instruction.");
        return;
    }
    if (!"".equals(data)) {
        Html5DatatypeLibrary dl = new Html5DatatypeLibrary();
        AttributesImpl patts = getPseudoAttributesFromPiData(data);
        String attrName;
        String attrValue;
        for (int i = 0; i < patts.getLength(); i++) {
            attrName = patts.getQName(i);
            attrValue = patts.getValue(i);
            switch(PseudoAttrName.toCaps(attrName)) {
                case HREF:
                    hasHref = true;
                    if (attrValue == null) {
                        break;
                    }
                    try {
                        IriRef ir = (IriRef) dl.createDatatype("iri-ref");
                        ir.checkValid(attrValue);
                    } catch (DatatypeException e) {
                        try {
                            errBadPseudoAttrDatatype(e, IriRef.class, "href", attrValue);
                        } catch (ClassNotFoundException ce) {
                        }
                    }
                    break;
                case TYPE:
                    if (attrValue == null) {
                        break;
                    }
                    try {
                        MimeType mt = (MimeType) dl.createDatatype("mime-type");
                        mt.checkValid(attrValue);
                        attrValue = newAsciiLowerCaseStringFromString(attrValue);
                    } catch (DatatypeException e) {
                        badDatatype = true;
                        try {
                            errBadPseudoAttrDatatype(e, MimeType.class, "type", attrValue);
                        } catch (ClassNotFoundException ce) {
                        }
                    }
                    if (!badDatatype) {
                        if (attrValue.matches("application/xml(;.*)?") || attrValue.matches("text/xml(;.*)?") || attrValue.matches("application/xslt+xml(;.*)?") || attrValue.matches("text/xsl(;.*)?") || attrValue.matches("text/xslt(;.*)?")) {
                            if (!attrValue.matches("text/xsl(;.*)?")) {
                                warn("For indicating XSLT," + " \u201ctext/xsl\u201d is the only" + " MIME type for the" + " \u201cxml-stylesheet\u201d" + " pseudo-attribute \u201ctype\u201d" + " that is supported across browsers.");
                            }
                            if (hasXsltPi) {
                                warn("Browsers do not support multiple" + " \u201cxml-stylesheet\u201d" + " instructions with a" + " \u201ctype\u201d value that" + " indicates XSLT.");
                            }
                            hasXsltPi = true;
                        } else if (!attrValue.matches("^text/css(;.*)?$")) {
                            warn("\u201ctext/css\u201d and" + " \u201ctext/xsl\u201d are the only" + " MIME types for the" + " \u201cxml-stylesheet\u201d" + " pseudo-attribute \u201ctype\u201d" + " that are supported across browsers.");
                        }
                    }
                    break;
                case TITLE:
                    hasTitle = true;
                    if (attrValue == null) {
                        break;
                    }
                    if (!"".equals(attrValue)) {
                        hasNonEmptyTitle = true;
                    }
                    break;
                case MEDIA:
                    hasMedia = true;
                    if (attrValue == null) {
                        break;
                    }
                    try {
                        MediaQuery mq = (MediaQuery) dl.createDatatype("media-query");
                        mq.checkValid(attrValue);
                    } catch (DatatypeException e) {
                        try {
                            errBadPseudoAttrDatatype(e, MediaQuery.class, "media", attrValue);
                        } catch (ClassNotFoundException ce) {
                        }
                    }
                    break;
                case CHARSET:
                    hasCharset = true;
                    if (attrValue == null) {
                        break;
                    }
                    try {
                        Charset c = (Charset) dl.createDatatype("charset");
                        c.checkValid(attrValue);
                    } catch (DatatypeException e) {
                        try {
                            errBadPseudoAttrDatatype(e, Charset.class, "charset", attrValue);
                        } catch (ClassNotFoundException ce) {
                        }
                    }
                    break;
                case ALTERNATE:
                    hasAlternate = true;
                    if (attrValue == null) {
                        break;
                    }
                    if ("yes".equals(attrValue)) {
                        alternateIsYes = true;
                    } else if (!"no".equals(attrValue)) {
                        err("The value of the \u201cxml-stylesheet\u201d" + " pseudo-attribute \u201calternate\u201d" + " must be either \u201cyes\u201d or" + " \u201cno\u201d.");
                    }
                    break;
                default:
                    err("Pseudo-attribute \u201c" + attrName + "\u201d not allowed in" + " \u201cxml-stylesheet\u201d instruction.");
                    break;
            }
        }
        if (alternateIsYes && !hasNonEmptyTitle) {
            err("An \u201cxml-stylesheet\u201d instruction with an" + " \u201calternate\u201d pseudo-attribute whose value is" + " \u201cyes\u201d must also have a \u201ctitle\u201d" + " pseudo-attribute with a non-empty value.");
        }
    }
    if (!hasHref) {
        err("\u201cxml-stylesheet\u201d instruction lacks \u201chref\u201d" + " pseudo-attribute." + " The \u201chref\u201d pseudo-attribute is required in all" + " \u201cxml-stylesheet\u201d instructions.");
    }
    if (hasXsltPi && (hasTitle || hasMedia || hasCharset || hasAlternate)) {
        warn("When processing \u201cxml-stylesheet\u201d instructions," + " browsers ignore the pseudo-attributes \u201ctitle\u201d," + " \u201cmedia\u201d, \u201ccharset\u201d, and" + " \u201calternate\u201d.");
    } else if (hasCharset) {
        warn("Some browsers ignore the value of the" + " \u201cxml-stylesheet\u201d pseudo-attribute" + " \u201ccharset\u201d.");
    }
}
Also used : Html5DatatypeException(nu.validator.datatype.Html5DatatypeException) DatatypeException(org.relaxng.datatype.DatatypeException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) MediaQuery(nu.validator.datatype.MediaQuery) Charset(nu.validator.datatype.Charset) IriRef(nu.validator.datatype.IriRef) MimeType(nu.validator.datatype.MimeType) Html5DatatypeLibrary(nu.validator.datatype.Html5DatatypeLibrary)

Aggregations

Charset (nu.validator.datatype.Charset)1 Html5DatatypeException (nu.validator.datatype.Html5DatatypeException)1 Html5DatatypeLibrary (nu.validator.datatype.Html5DatatypeLibrary)1 IriRef (nu.validator.datatype.IriRef)1 MediaQuery (nu.validator.datatype.MediaQuery)1 MimeType (nu.validator.datatype.MimeType)1 DatatypeException (org.relaxng.datatype.DatatypeException)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1