Project

Profile

Help

Revision 8fea89bc

Added by Michael Kay over 13 years ago

Fix bug 1967462 - locally requested DTD validation doesn't work

View differences:

latest9.0/bj/net/sf/saxon/StandardErrorHandler.java
19 19

  
20 20
    private ErrorListener errorListener;
21 21
    private Writer errorOutput;
22
    private int errorCount = 0;
22 23

  
23 24
    public StandardErrorHandler(ErrorListener listener) {
24 25
        errorListener = listener;
......
48 49
            return;     // ignore this warning from AElfred2
49 50
        }
50 51
        //System.err.println("ErrorHandler.warning " + e.getMessage());
52
        errorCount++;
51 53
        if (errorListener != null) {
52 54
            try {
53 55
                errorListener.warning(new TransformerException(e));
......
79 81
    */
80 82

  
81 83
    protected void reportError (SAXParseException e, boolean isFatal) {
82

  
84
        errorCount++;
83 85
        if (errorListener != null) {
84 86
            try {
85 87
                ExpressionLocation loc =
......
111 113
            }
112 114
        }
113 115
    }
116

  
117
    public int getErrorCount() {
118
        return errorCount;
119
    }
114 120
}
115 121

  
116 122
//
latest9.0/bj/net/sf/saxon/event/Sender.java
161 161
            return;
162 162

  
163 163
        } else if (source instanceof SAXSource) {
164
            sendSAXSource((SAXSource)source, next, schemaValidation, topLevelNameCode, stripSpace, xInclude, topLevelType);
164
            sendSAXSource((SAXSource)source, next, schemaValidation, topLevelNameCode, stripSpace, xInclude,
165
                    dtdValidation == Validation.STRICT, topLevelType);
165 166
            return;
166 167

  
167 168
        } else if (source instanceof StreamSource) {
......
181 182
                }
182 183
                SAXSource sax = new SAXSource(parser, is);
183 184
                sax.setSystemId(source.getSystemId());
184
                sendSAXSource(sax, next, schemaValidation, topLevelNameCode, stripSpace, xInclude, topLevelType);
185
                sendSAXSource(sax, next, schemaValidation, topLevelNameCode, stripSpace, xInclude,
186
                        dtdValidation == Validation.STRICT, topLevelType);
185 187
                if (reuseParser) {
186 188
                    config.reuseSourceParser(parser);
187 189
                }
......
252 254
    }
253 255

  
254 256
    private void sendSAXSource(SAXSource source, Receiver receiver, int validation,
255
                               int topLevelElement, int stripSpace, boolean xInclude, SchemaType topLevelType)
257
                               int topLevelElement, int stripSpace, boolean xInclude,
258
                               boolean dtdValidation, SchemaType topLevelType)
256 259
    throws XPathException {
257 260
        XMLReader parser = source.getXMLReader();
258 261
        boolean reuseParser = false;
......
281 284
            }
282 285
        }
283 286

  
287
        try {
288
            parser.setFeature("http://xml.org/sax/features/validation", dtdValidation);
289
        } catch (SAXNotRecognizedException err) {
290
            if (dtdValidation) {
291
                throw new XPathException("XML Parser does not recognize request for DTD validation");
292
            }
293
        } catch (SAXNotSupportedException err) {
294
            if (dtdValidation) {
295
                throw new XPathException("XML Parser does not support DTD validation");
296
            }
297
        }
298

  
299

  
284 300
        if (xInclude) {
285 301
            boolean tryAgain = false;
286 302
            try {
......
307 323
//        if (config.isTiming()) {
308 324
//            System.err.println("Using SAX parser " + parser);
309 325
//        }
310
        parser.setErrorHandler(new StandardErrorHandler(pipe.getErrorListener()));
326
        StandardErrorHandler errorHandler = new StandardErrorHandler(pipe.getErrorListener());
327
        parser.setErrorHandler(errorHandler);
311 328

  
312 329

  
313 330
        if ((validation & Validation.VALIDATION_MODE_MASK) != Validation.PRESERVE) {
......
366 383
        } catch (java.io.IOException err) {
367 384
            throw new XPathException(err);
368 385
        }
386
        if (errorHandler.getErrorCount() > 0) {
387
            throw new XPathException("XML Parser reported one or more errors");
388
        }
369 389
        if (reuseParser) {
370 390
            config.reuseSourceParser(parser);
371 391
        }
latest9.0/bj/net/sf/saxon/s9api/DocumentBuilder.java
2 2

  
3 3
import net.sf.saxon.AugmentedSource;
4 4
import net.sf.saxon.Configuration;
5
import net.sf.saxon.om.DocumentInfo;
6
import net.sf.saxon.om.ExternalObjectModel;
5 7
import net.sf.saxon.om.NodeInfo;
6 8
import net.sf.saxon.om.Validation;
7
import net.sf.saxon.om.ExternalObjectModel;
8
import net.sf.saxon.om.DocumentInfo;
9 9
import net.sf.saxon.trans.XPathException;
10 10

  
11 11
import javax.xml.transform.Source;
12 12
import javax.xml.transform.stream.StreamSource;
13
import java.net.URI;
14 13
import java.io.File;
14
import java.net.URI;
15 15

  
16 16
/**
17 17
 * A document builder holds properties controlling how a Saxon document tree should be built, and
......
324 324
        return (XdmNode)XdmItem.wrapItem(wrappedNode);
325 325
    }
326 326

  
327
//    public static void main(String[] args) {
328
//        String validXML = "<!DOCTYPE a [<!ELEMENT a EMPTY>]><a/>";
329
//        String invalidXML = "<!DOCTYPE a [<!ELEMENT a (b+)>]><a/>";
330
//        DocumentBuilder builder = new Processor(false).newDocumentBuilder();
331
//        builder.setDTDValidation(true);
332
//
333
//        StringReader sr = new StringReader(validXML);
334
//        SAXSource source = new SAXSource(new InputSource(sr));
335
//        try {
336
//            XdmNode d = builder.build(source);
337
//            //assertNotNull(d);
338
//        } catch (SaxonApiException e) {
339
//            System.err.println(e.getMessage());
340
//        }
341
//
342
//        sr = new StringReader(invalidXML);
343
//        source = new SAXSource(new InputSource(sr));
344
//        try {
345
//            XdmNode d = builder.build(source);
346
//            System.err.println("invalid document parsed without error");
347
//        } catch (SaxonApiException e) {
348
//            // expected failure;
349
//        }
350
//
351
//        builder.setDTDValidation(false);
352
//        sr = new StringReader(invalidXML);
353
//        source = new SAXSource(new InputSource(sr));
354
//        try {
355
//            XdmNode d = builder.build(source);
356
//            System.err.println(d);
357
//        } catch (SaxonApiException e) {
358
//            System.err.println("Parsing failed but validation was not requested: " + e.getMessage());
359
//        }
360
//
361
//    }
362

  
327 363
}
328 364

  
329 365
//

Also available in: Unified diff