Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SonarQube XML Plugin
* Copyright (C) 2010-2018 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.plugins.xml.checks;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.apache.commons.lang.StringUtils;
import org.apache.xerces.impl.Constants;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.plugins.xml.Utils;
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;
/**
* RSPEC-2322.
* Perform schema check using xerces parser.
* @author Matthijs Galesloot
*/
@Rule(key = "XmlSchemaCheck")
public class XmlSchemaCheck extends AbstractXmlCheck {
/**
* filePattern indicates which files should be checked.
*/
@RuleProperty(key = "filePattern", description = "Files to be validated using Ant-style matching patterns.")
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",
description = "Whitespace-separated list of schemas to use for validation.",
defaultValue = DEFAULT_SCHEMA,
type = "TEXT")
private String schemas;
/**
* Use Sonar logger instead of SL4FJ logger, in order to be able to unit test the logs.
*/
private static final Logger LOG = Loggers.get(XmlSchemaCheck.class);
private static final Map CACHED_SCHEMAS = new HashMap<>();
public static final String DEFAULT_SCHEMA = "autodetect";
/**
* 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);
}
}
@Override
public void error(SAXParseException e) throws SAXException {
createViolation(e);
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
createViolation(e);
}
@Override
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 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