/* * Sonar XML Plugin * Copyright (C) 2010 Matthijs Galesloot * dev@sonar.codehaus.org * * Licensed 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. */ package org.sonar.plugins.xml.parsers; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.sonar.api.utils.SonarException; import org.xml.sax.SAXException; /** * Provides reusable code for Xml parsers. * * @author Matthijs Galesloot * @since 1.0 */ public abstract class AbstractParser { protected static final SAXParserFactory SAX_FACTORY; /** * Build the SAXParserFactory. */ static { SAX_FACTORY = SAXParserFactory.newInstance(); try { SAX_FACTORY.setValidating(false); SAX_FACTORY.setNamespaceAware(false); SAX_FACTORY.setFeature("http://xml.org/sax/features/validation", false); SAX_FACTORY.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); SAX_FACTORY.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); SAX_FACTORY.setFeature("http://xml.org/sax/features/external-general-entities", false); SAX_FACTORY.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (SAXException e) { throw new SonarException(e); } catch (ParserConfigurationException e) { throw new SonarException(e); } } protected SAXParser newSaxParser() { try { return SAX_FACTORY.newSAXParser(); } catch (SAXException e) { throw new SonarException(e); } catch (ParserConfigurationException e) { throw new SonarException(e); } } }