File: ValidatingDocumentBuilderFactory.java

package info (click to toggle)
isorelax 20041111-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 412 kB
  • sloc: java: 3,061; makefile: 22; xml: 11
file content (108 lines) | stat: -rw-r--r-- 4,084 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.iso_relax.jaxp;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.iso_relax.verifier.Schema;
import org.iso_relax.verifier.VerifierConfigurationException;

/**
 * Wraps another {@link DocumentBuilderFactory} and adds validation capability.
 * 
 * @author Daisuke OKAJIMA
 */
public class ValidatingDocumentBuilderFactory extends DocumentBuilderFactory
{
    protected Schema _Schema;
    protected DocumentBuilderFactory _WrappedFactory;
    
    private boolean validation = true;

    
    /**
     * creates a new instance that wraps the default DocumentBuilderFactory
     * @param schema the compiled Schema object. It can not be null.
     */
    public ValidatingDocumentBuilderFactory(Schema schema)
    {
        this(DocumentBuilderFactory.newInstance(), schema);
    }    
    
    /**
     * creates a new instance with an internal DocumentBuilderFactory and Schema.
     * @param wrapped internal DocumentBuilderFactory
     * @param schema  compiled schema. 
     */
    public ValidatingDocumentBuilderFactory(DocumentBuilderFactory wrapped, Schema schema)
    {
        _WrappedFactory = wrapped;
        _Schema = schema;
    }

    /**
     * returns a new DOM parser.
     * If setValidating(false) is called previously, this method
     * simply returns the implementation of wrapped DocumentBuilder.
     */
    public DocumentBuilder newDocumentBuilder() throws ParserConfigurationException
    {
        if(isValidating()) {
            try {
                return new ValidatingDocumentBuilder(
                    _WrappedFactory.newDocumentBuilder(),
                    _Schema.newVerifier());
            } catch(VerifierConfigurationException ex) {
                throw new ParserConfigurationException(ex.getMessage());
            }
        } else
            //if validation is disabled, we simply return the implementation of wrapped DocumentBuilder
            return _WrappedFactory.newDocumentBuilder();
    }

    /**
     * @see DocumentBuilderFactory#setAttribute(String, Object)
     */
    public void setAttribute(String name, Object value)
    {
        _WrappedFactory.setAttribute(name, value);
    }

    /**
     * @see DocumentBuilderFactory#getAttribute(String)
     */
    public Object getAttribute(String name)
    {
        return _WrappedFactory.getAttribute(name);
    }

    public boolean isValidating()
    { return validation; }
    public void setValidating(boolean _validating)
    { this.validation = _validating; }
    
    public boolean isCoalescing()
    { return _WrappedFactory.isCoalescing(); }
    public boolean isExpandEntityReference()
    { return _WrappedFactory.isExpandEntityReferences(); }
    public boolean isIgnoringComments()
    { return _WrappedFactory.isIgnoringComments(); }
    public boolean isIgnoringElementContentWhitespace()
    { return _WrappedFactory.isIgnoringElementContentWhitespace(); }
    public boolean isNamespaceAware()
    { return _WrappedFactory.isNamespaceAware(); }
    public void setCoalescing(boolean coalescing)
    { _WrappedFactory.setCoalescing(coalescing); }
    public void setExpandEntityReference(boolean expandEntityRef)
    { _WrappedFactory.setExpandEntityReferences(expandEntityRef); }
    public void setIgnoringComments(boolean ignoreComments)
    { _WrappedFactory.setIgnoringComments(ignoreComments); }
    public void setIgnoringElementContentWhitespace(boolean whitespace)
    { _WrappedFactory.setIgnoringElementContentWhitespace(whitespace); }
    public void setNamespaceAware(boolean awareness)
    { _WrappedFactory.setNamespaceAware(awareness); }
    public boolean getFeature(String name) throws ParserConfigurationException
    { return _WrappedFactory.getFeature(name); }
    public void setFeature(String name, boolean value) throws ParserConfigurationException
    { _WrappedFactory.setFeature(name, value); }
}