org.sonar.plugins.xml.checks.XmlSchemaCheck Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-xml-plugin Show documentation
Show all versions of sonar-xml-plugin Show documentation
Enable analysis and reporting on XML files.
The newest version!
/*
* SonarQube XML Plugin
* Copyright (C) 2010 SonarSource
* [email protected]
*
* 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.checks;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.xerces.impl.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.SonarException;
import org.sonar.check.Cardinality;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.xml.parsers.DetectSchemaParser;
import org.sonar.plugins.xml.parsers.DetectSchemaParser.Doctype;
import org.sonar.plugins.xml.schemas.SchemaResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Perform schema check using xerces parser.
*
* @author Matthijs Galesloot
*/
@Rule(key = "XmlSchemaCheck", priority = Priority.MAJOR,
cardinality = Cardinality.MULTIPLE)
public class XmlSchemaCheck extends AbstractXmlCheck {
/**
* filePattern indicates which files should be checked.
*/
@RuleProperty(key = "filePattern")
private String filePattern;
/**
* schemas may refer to a schema that is provided as a built-in resource, a web resource or a file resource.
*/
@RuleProperty(key = "schemas", defaultValue = "autodetect", type = "TEXT")
private String schemas;
private static final Logger LOG = LoggerFactory.getLogger(XmlSchemaCheck.class);
private static final Map CACHED_SCHEMAS = new HashMap();
/**
* MessageHandler creates violations for errors and warnings. The handler is assigned to {@link Validator} to catch the errors and
* warnings raised by the validator.
*/
private class MessageHandler implements ErrorHandler {
private void createViolation(SAXParseException e) {
XmlSchemaCheck.this.createViolation(e.getLineNumber(), e.getLocalizedMessage());
if (e.getLocalizedMessage().contains(UnrecoverableParseError.FAILUREMESSAGE)) {
throw new UnrecoverableParseError(e);
}
}
public void error(SAXParseException e) throws SAXException {
createViolation(e);
}
public void fatalError(SAXParseException e) throws SAXException {
createViolation(e);
}
public void warning(SAXParseException e) throws SAXException {
createViolation(e);
}
}
/**
* Exception for a parse error from which the parser cannot recover.
*/
private static class UnrecoverableParseError extends RuntimeException {
static final String FAILUREMESSAGE = "The reference to entity \"null\"";
private static final long serialVersionUID = 1L;
public UnrecoverableParseError(SAXParseException e) {
super(e);
}
}
/**
* Create xsd schema for a list of schema's.
*/
private static Schema createSchema(String[] schemaList) {
final String cacheKey = StringUtils.join(schemaList, ",");
// first try to load a cached schema.
Schema schema = CACHED_SCHEMAS.get(cacheKey);
if (schema != null) {
return schema;
}
List