On Thu, 30 Aug 2001, Andreas [iso-8859-1] Th�nnessen wrote:

> My idea was, that the validotor should allready have this
> information. The XMLContentModel provides a method whatCanGoHere()
> which looks quite like what I want. But I don't have any clue on how
> to access this...

I got this working awhile back. I don't remember much about it, but I do
still have the code lying around. The "fix" method is what you want to
look at, and I've included a couple helper methods which might be useful.

Questions welcome, answers not guaranteed.

j

    /**
     * Perform the acutal fixing operation. Given a tag name,
     * find all elements with that name and determine what the
     * DTD thinks they should have been named, then rename them that
     * name.
     * <br />
     * Note: we could instead walk through all elements in the document
     * and ask if each node is invalid, then attempt to fix it. This is
     * faster, and I am assured that all invalid elements will be named
     * "MsoNormal." If that situation changes, the algorithm we use should
     * be made more general.
     * @param nodeName The name of the elements which we believe
     * are invalid.
     */
    public void fix (String nodeName) throws Exception {
        DocumentImpl doc = (DocumentImpl) this.parser.getDocument();
        XMLValidator val = this.parser.getValidator();
        Node node, parent;
        NodeList nl = doc.getElementsByTagName(nodeName);

        for (int i = 0; i < nl.getLength(); i++) {
            node = nl.item(i);
            parent = node.getParentNode();
            InsertableElementsInfo info =
                this.makeInfoObject (node);
            int failedIndex =
                val.whatCanGoHere
                (this.getElementDeclIndex
                 (this.getElementIndex(parent.getNodeName())),
                 true, info);

            if (failedIndex == -1)
                fixNode(node, info, doc);
        }
    }

    /**
     * Given an element name, return the integer which the parser
     * associates with that name.
     */
    protected int getElementIndex (String elementName) {
        if (elementName.equals("#text")) {
            return (-1);
        } else {
            return(this.pool.addSymbol(elementName));
        }
    }

    /**
     * Given an element declaration integer, return the element name
     * integer which the parser associates with it.
     */
    protected int getElementDeclIndex (int elementIndex)
        throws Exception {
        if (this.parser == null) {
            throw new Exception
                ("Null parser in FixInvalidXML: should have been set in
constructor");
        }

        XMLValidator val = this.parser.getValidator();
        Grammar g = val.getGrammar();

        if (g == null) {
            throw new Exception
                ("FixInvalidXML failed to locate grammar object");
        }

        return(g.getElementDeclIndex(elementIndex, -1));
    }

---
  "Users complain that they receive too much spam, while spammers protest
messages are legal." -InfoWorld
  "You do not have to do everything disagreeable that you have a right to
do." -Judith Martin (Miss Manners)



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to