[jdom-interest] Validating against a schema using elementscanner from the contrib.

Laurent Bihanic laurent.bihanic at atosorigin.com
Tue Apr 22 03:28:05 PDT 2003


Robert (Jamie) Munro wrote:
> Is it possible to validate against a schema, like in this FAQ entry:
> http://www.jdom.org/docs/faq.html#schemas
> but when using elementScanner from the contrib to build the document?
> 
> I would guess that I need to do:
>   ElementScanner scanner = new ElementScanner();
>   scanner.setFeature("http://apache.org/xml/features/validation/schema",
> true);
> 
> but scanner.setFeature has not been written yet...

ElementScanner inherits from SAX's XMLFilterImpl which provides both 
setFeature and setProperty methods. Yet, the code you propose won't work as 
you have to attach ElementScanner to a parent (e.g. a SAX parser) before 
calling XMLFilterImpl's setFeature or setProperty.

Try:
   ElementScanner scanner = new ElementScanner(parser);
   scanner.setFeature("http://apache.org/xml/features/validation/schema",
true);

Opposite to SAXBuilder, ElementScanner does not store features and properties 
until a parser is available.
To add such support, you could add the following import statements and methods:

import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;

    public void setFeature(String name, boolean value)
                 throws SAXNotRecognizedException, SAXNotSupportedException {
       if (this.getParent() != null) {
          this.getParent().setFeature(name, value);
       }
       this.parserBuilder.setFeature(name, value);
    }

    public void setProperty(String name, Object value)
                 throws SAXNotRecognizedException, SAXNotSupportedException {
       if (this.getParent() != null) {
          this.getParent().setProperty(name, value);
       }
       this.parserBuilder.setProperty(name, value);
    }

Jason, I've attached a diff for you to merge the patch.

Laurent
-------------- next part --------------
Index: ElementScanner.java
===================================================================
RCS file: /home/cvspublic/jdom-contrib/src/java/org/jdom/contrib/input/scanner/ElementScanner.java,v
retrieving revision 1.4
diff -r1.4 ElementScanner.java
67a68,69
> import org.xml.sax.SAXNotRecognizedException;
> import org.xml.sax.SAXNotSupportedException;
202,205d203
<     * This filter will have no parent: you must assign a parent
<     * before you do any configuration with {@link #setFeature} or
<     * {@link #setProperty}.</p>
<     * <p>
366a365,406
> 
>    /**
>     * Sets the state of a feature.
>     *
>     * @param  name     the feature name, which is a fully-qualified
>     *                  URI.
>     * @param  state    the requested state of the feature.
>     *
>     * @throws SAXNotRecognizedException   when the XMLReader does not
>     *         recognize the feature name.
>     * @throws SAXNotSupportedException    when the XMLReader
>     *         recognizes the feature name but cannot set the
>     *         requested value.
>     */
>    public void setFeature(String name, boolean value)
>                 throws SAXNotRecognizedException, SAXNotSupportedException {
>       if (this.getParent() != null) {
>          this.getParent().setFeature(name, value);
>       }
>       this.parserBuilder.setFeature(name, value);
>    }
> 
>    /**
>     * Set the value of a property.
>     *
>     * @param  name   the property name, which is a fully-qualified
>     *                URI.
>     * @param state   the requested value for the property.
>     *
>     * @throws SAXNotRecognizedException   when the XMLReader does not
>     *         recognize the property name.
>     * @throws SAXNotSupportedException    when the XMLReader
>     *         recognizes the property name but cannot set the
>     *         requested value.
>     */
>    public void setProperty(String name, Object value)
>                 throws SAXNotRecognizedException, SAXNotSupportedException {
>       if (this.getParent() != null) {
>          this.getParent().setProperty(name, value);
>       }
>       this.parserBuilder.setProperty(name, value);
>    }


More information about the jdom-interest mailing list