Java XML SAX Parser createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler)

Here you can find the source of createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler)

Description

create Safe Sax Reader

License

Apache License

Declaration

public static XMLReader createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler)
            throws SAXException, ParserConfigurationException 

Method Source Code

//package com.java2s;
/*/*from   w w w . ja v  a2 s. c om*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

public class Main {
    public static XMLReader createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler)
            throws SAXException, ParserConfigurationException {
        if (saxParserFactory == null) {
            throw new IllegalArgumentException("The provided SAX parser factory cannot be null");
        }

        if (contentHandler == null) {
            throw new IllegalArgumentException("The provided SAX content handler cannot be null");
        }

        saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

        SAXParser saxParser = saxParserFactory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(contentHandler);

        return xmlReader;
    }
}

Related

  1. addMessagesToMap(final Map hangmanMap, String messagesURIString)
  2. canDisableExternalDtds(SAXParserFactory parserFactory)
  3. createNamespaceAwareSAXParserFactory()
  4. createSaxParser()
  5. createSAXParser()
  6. createSAXParser(DefaultHandler dh)
  7. createSaxParserFactory()