Search in sources :

Example 6 with DocumentFragment

use of nu.validator.saxtree.DocumentFragment in project validator by validator.

the class MessageEmitterAdapter method elaborateDatatypes.

private void elaborateDatatypes(Map<String, DatatypeException> map) throws SAXException {
    Set<DocumentFragment> fragments = new HashSet<>();
    for (Map.Entry<String, DatatypeException> entry : map.entrySet()) {
        DatatypeException ex = entry.getValue();
        if (ex instanceof Html5DatatypeException) {
            Html5DatatypeException ex5 = (Html5DatatypeException) ex;
            DocumentFragment fragment = HTML5_DATATYPE_ADVICE.get(ex5.getDatatypeClass());
            if (fragment != null) {
                fragments.add(fragment);
            }
        }
    }
    if (!fragments.isEmpty()) {
        ContentHandler ch = emitter.startElaboration();
        if (ch != null) {
            TreeParser treeParser = new TreeParser(ch, null);
            XhtmlSaxEmitter xhtmlSaxEmitter = new XhtmlSaxEmitter(ch);
            xhtmlSaxEmitter.startElement("dl");
            for (DocumentFragment fragment : fragments) {
                treeParser.parse(fragment);
            }
            xhtmlSaxEmitter.endElement("dl");
        }
        emitter.endElaboration();
    }
}
Also used : Html5DatatypeException(nu.validator.datatype.Html5DatatypeException) DatatypeException(org.relaxng.datatype.DatatypeException) Html5DatatypeException(nu.validator.datatype.Html5DatatypeException) TreeParser(nu.validator.saxtree.TreeParser) XhtmlSaxEmitter(nu.validator.xml.XhtmlSaxEmitter) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) DocumentFragment(nu.validator.saxtree.DocumentFragment) ContentHandler(org.xml.sax.ContentHandler) HashSet(java.util.HashSet)

Example 7 with DocumentFragment

use of nu.validator.saxtree.DocumentFragment in project validator by validator.

the class Html5SpecBuilder method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    if ("p" == localName && NS == uri) {
        return;
    }
    switch(state) {
        case AWAITING_HEADING:
            if ("h4" == localName && NS == uri) {
                referenceText.setLength(0);
                currentId = atts.getValue("", "id");
                currentName = null;
                state = State.IN_H4;
            }
            break;
        case IN_H4:
            if ("code" == localName && NS == uri) {
                nameText.setLength(0);
                state = State.IN_CODE_IN_H4;
            }
            break;
        case IN_CODE_IN_H4:
            break;
        case AWAITING_ELEMENT_DL:
            if ("dl" == localName && NS == uri && "element".equals(atts.getValue("", "class"))) {
                state = State.IN_ELEMENT_DL_START;
            }
            break;
        case IN_ELEMENT_DL_START:
            if ("dt" == localName && NS == uri) {
                referenceText.setLength(0);
                state = State.IN_CATEGORIES_DT;
            } else {
                throw new SAXParseException("Malformed spec: Expected dt in dl.", locator);
            }
            break;
        case IN_CATEGORIES_DT:
            if ("a" == localName && NS == uri) {
                state = State.IN_CATEGORIES_DT;
                break;
            }
        case IN_CONTEXT_DT:
            if ("a" == localName && NS == uri) {
                state = State.IN_CONTEXT_DT;
                break;
            }
        case IN_CONTENT_MODEL_DT:
            if ("a" == localName && NS == uri) {
                state = State.IN_CONTENT_MODEL_DT;
                break;
            }
        case IN_TAG_OMISSION_DT:
            if ("a" == localName && NS == uri) {
                state = State.IN_TAG_OMISSION_DT;
                break;
            }
        case IN_ATTRIBUTES_DT:
            if ("a" == localName && NS == uri) {
                state = State.IN_ATTRIBUTES_DT;
                break;
            }
            throw new SAXParseException("Malformed spec: Not expecting children in dts.", locator);
        case CAPTURING_CATEGORIES_DDS:
        case CAPTURING_CONTEXT_DDS:
        case CAPTURING_CONTENT_MODEL_DDS:
        case CAPTURING_TAG_OMISSION_DDS:
        case CAPTURING_ATTRIBUTES_DDS:
            if ("dt" == localName && NS == uri && captureDepth == 0) {
                ignoreTextNodes = true;
                DocumentFragment fragment = (DocumentFragment) fragmentBuilder.getRoot();
                fragmentBuilder = null;
                referenceText.setLength(0);
                if (state == State.CAPTURING_CATEGORIES_DDS) {
                    categoriesByElement.put(currentName, fragment);
                    state = State.IN_CONTEXT_DT;
                } else if (state == State.CAPTURING_CONTEXT_DDS) {
                    contextsByElement.put(currentName, fragment);
                    state = State.IN_CONTENT_MODEL_DT;
                } else if (state == State.CAPTURING_CONTENT_MODEL_DDS) {
                    contentModelsByElement.put(currentName, fragment);
                    state = State.IN_TAG_OMISSION_DT;
                } else if (state == State.CAPTURING_TAG_OMISSION_DDS) {
                    state = State.IN_ATTRIBUTES_DT;
                } else {
                    attributesByElement.put(currentName, fragment);
                    state = State.AWAITING_HEADING;
                }
            } else {
                captureDepth++;
                String href = null;
                if ("a" == localName && NS == uri && (href = atts.getValue("", "href")) != null) {
                    if (href.startsWith("#")) {
                        href = SPEC_LINK_URI + href;
                    }
                    AttributesImpl attributesImpl = new AttributesImpl();
                    attributesImpl.addAttribute("href", href);
                    fragmentBuilder.startElement(uri, localName, qName, attributesImpl);
                } else {
                    fragmentBuilder.startElement(uri, localName, qName, EmptyAttributes.EMPTY_ATTRIBUTES);
                }
            }
            break;
    }
}
Also used : AttributesImpl(nu.validator.xml.AttributesImpl) SAXParseException(org.xml.sax.SAXParseException) DocumentFragment(nu.validator.saxtree.DocumentFragment)

Aggregations

DocumentFragment (nu.validator.saxtree.DocumentFragment)7 TreeParser (nu.validator.saxtree.TreeParser)4 XhtmlSaxEmitter (nu.validator.xml.XhtmlSaxEmitter)4 ContentHandler (org.xml.sax.ContentHandler)4 AttributesImpl (nu.validator.xml.AttributesImpl)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Html5DatatypeException (nu.validator.datatype.Html5DatatypeException)1 SourceHandler (nu.validator.source.SourceHandler)1 DatatypeException (org.relaxng.datatype.DatatypeException)1 SAXParseException (org.xml.sax.SAXParseException)1