001    package com.thaiopensource.relaxng.util;
002    
003    import com.thaiopensource.util.PropertyMap;
004    import com.thaiopensource.util.PropertyMapBuilder;
005    import com.thaiopensource.validate.ValidateProperty;
006    import com.thaiopensource.validate.ValidationDriver;
007    import com.thaiopensource.validate.rng.CompactSchemaReader;
008    import com.thaiopensource.validate.rng.RngProperty;
009    import com.thaiopensource.xml.sax.XMLReaderCreator;
010    import com.thaiopensource.xml.sax.Sax2XMLReaderCreator;
011    import org.xml.sax.ErrorHandler;
012    
013    /**
014     * Provides a compatibility wrapper around ValidationDriver.  New applications
015     * should use ValidationDriver directly.
016     *
017     * @author <a href="mailto:jjc@jclark.com">James Clark</a>
018     * @see ValidationDriver
019     * @deprecated
020     */
021    public class ValidationEngine extends ValidationDriver {
022    
023      /**
024       * Flag indicating that ID/IDREF/IDREFS should be checked.
025       * @see RngProperty#CHECK_ID_IDREF
026       */
027      public static final int CHECK_ID_IDREF = 01;
028      /**
029       * Flag indicating that the schema is in the RELAX NG compact syntax rather than the XML syntax.
030       * @see CompactSchemaReader
031       */
032      public static final int COMPACT_SYNTAX = 02;
033      /**
034       * @see RngProperty#FEASIBLE
035       */
036      public static final int FEASIBLE = 04;
037    
038      /**
039       * Default constructor.  Equivalent to <code>ValidationEngine(null, null, CHECK_ID_IDREF)</code>.
040       */
041      public ValidationEngine() {
042        this(null, null, CHECK_ID_IDREF);
043      }
044      /**
045       * Constructs a <code>ValidationEngine</code>.
046       *
047       * @param xrc the <code>XMLReaderCreator</code> to be used for constructing <code>XMLReader</code>s;
048       * if <code>null</code> uses <code>Sax2XMLReaderCreator</code>
049       * @param eh the <code>ErrorHandler</code> to be used for reporting errors; if <code>null</code>
050       * uses <code>DraconianErrorHandler</code>
051       * @param flags bitwise OR of flags selected from <code>CHECK_ID_IDREF</code>, <code>COMPACT_SYNTAX</code>,
052       * <code>FEASIBLE</code>, <code>MNS</code>
053       * @see com.thaiopensource.xml.sax.DraconianErrorHandler
054       * @see com.thaiopensource.xml.sax.Sax2XMLReaderCreator
055       * @see #CHECK_ID_IDREF
056       * @see #COMPACT_SYNTAX
057       * @see #FEASIBLE
058       */
059      public ValidationEngine(XMLReaderCreator xrc,
060                              ErrorHandler eh,
061                              int flags) {
062        super(makePropertyMap(xrc, eh, flags),
063              (flags & COMPACT_SYNTAX) == 0 ? null : CompactSchemaReader.getInstance());
064      }
065    
066      private static PropertyMap makePropertyMap(XMLReaderCreator xrc, ErrorHandler eh, int flags) {
067        PropertyMapBuilder builder = new PropertyMapBuilder();
068        if (xrc == null)
069          xrc = new Sax2XMLReaderCreator();
070        ValidateProperty.XML_READER_CREATOR.put(builder, xrc);
071        if (eh != null)
072          ValidateProperty.ERROR_HANDLER.put(builder, eh);
073        if ((flags & CHECK_ID_IDREF) != 0)
074          RngProperty.CHECK_ID_IDREF.add(builder);
075        if ((flags & FEASIBLE) != 0)
076          RngProperty.FEASIBLE.add(builder);
077        return builder.toPropertyMap();
078      }
079    
080      /**
081       * Constructs a <code>ValidationEngine</code>.
082       *
083       * @param xrc the <code>XMLReaderCreator</code> to be used for constructing <code>XMLReader</code>s;
084       * if <code>null</code> uses <code>Sax2XMLReaderCreator</code>
085       * @param eh the <code>ErrorHandler</code> to be used for reporting errors; if <code>null</code>
086       * uses <code>DraconianErrorHandler</code>
087       * @param checkIdIdref <code>true</code> if ID/IDREF/IDREFS should be checked; <code>false</code> otherwise
088       * @see com.thaiopensource.xml.sax.DraconianErrorHandler
089       * @see com.thaiopensource.xml.sax.Sax2XMLReaderCreator
090       */
091      public ValidationEngine(XMLReaderCreator xrc,
092                              ErrorHandler eh,
093                              boolean checkIdIdref) {
094        this(xrc, eh, checkIdIdref ? CHECK_ID_IDREF : 0);
095      }
096    
097      /**
098       * Constructs a <code>ValidationEngine</code>.
099       *
100       * @param xrc the <code>XMLReaderCreator</code> to be used for constructing <code>XMLReader</code>s;
101       * if <code>null</code> uses <code>Sax2XMLReaderCreator</code>
102       * @param eh the <code>ErrorHandler</code> to be used for reporting errors; if <code>null</code>
103       * uses <code>DraconianErrorHandler</code>
104       * @param checkIdIdref <code>true</code> if ID/IDREF/IDREFS should be checked; <code>false</code> otherwise
105       * @param compactSyntax <code>true</code> if the RELAX NG compact syntax should be used to parse the schema;
106       * <code>false</code> if the XML syntax should be used
107       * @see com.thaiopensource.xml.sax.DraconianErrorHandler
108       * @see com.thaiopensource.xml.sax.Sax2XMLReaderCreator
109       */
110      public ValidationEngine(XMLReaderCreator xrc, ErrorHandler eh, boolean checkIdIdref, boolean compactSyntax) {
111        this(xrc,
112             eh,
113             (checkIdIdref ? CHECK_ID_IDREF : 0)
114             | (compactSyntax ? COMPACT_SYNTAX : 0));
115      }
116    
117    
118      public ValidationEngine(XMLReaderCreator xrc, ErrorHandler eh, boolean checkIdIdref, boolean compactSyntax,
119                              boolean feasible) {
120        this(xrc,
121             eh,
122             (checkIdIdref ? CHECK_ID_IDREF : 0)
123             | (compactSyntax ? COMPACT_SYNTAX : 0)
124             | (feasible ? FEASIBLE : 0));
125      }
126    
127    }