Is Scala/Java not respecting w3 “excess dtd traffic” specs?

后端 未结 9 1176
青春惊慌失措
青春惊慌失措 2021-02-01 07:40

I\'m new to Scala, so I may be off base on this, I want to know if the problem is my code. Given the Scala file httpparse, simplified to:

object Http {
   import         


        
9条回答
  •  孤街浪徒
    2021-02-01 08:27

    I've bumped into the SAME issue, and I haven't found an elegant solution (I'm thinking into posting the question to the Scala mailing list) Meanwhile, I found a workaround: implement your own SAXParserFactoryImpl so you can set the f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); property. The good thing is it doesn't require any code change to the Scala code base (I agree that it should be fixed, though). First I'm extending the default parser factory:

    package mypackage;
    
    public class MyXMLParserFactory extends SAXParserFactoryImpl {
          public MyXMLParserFactory() throws SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException {
            super();
            super.setFeature("http://xml.org/sax/features/validation", false);
            super.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false); 
            super.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); 
            super.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 
          } 
        }
    

    Nothing special, I just want the chance to set the property.

    (Note: that this is plain Java code, most probably you can write the same in Scala too)

    And in your Scala code, you need to configure the JVM to use your new factory:

    System.setProperty("javax.xml.parsers.SAXParserFactory", "mypackage.MyXMLParserFactory");
    

    Then you can call XML.load without validation

提交回复
热议问题