Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: [HELP needed] first attempt based on sonar-puppet module to parse generic-test-data #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* SonarQube Puppet Plugin
* Copyright (C) 2011-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 com.iadams.sonarqube.puppet;

import java.io.File;
import java.util.List;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.config.Configuration;
import org.sonar.api.utils.WildcardPattern;

public abstract class PuppetReportSensor implements Sensor {

private static final Logger LOG = LoggerFactory.getLogger(PuppetReportSensor.class);

protected Configuration conf;

public PuppetReportSensor(Configuration conf) {
this.conf = conf;
}

@Override
public void describe(SensorDescriptor descriptor) {
descriptor
.name(getClass().getSimpleName())
.onlyOnLanguage(Puppet.KEY)
.onlyOnFileType(InputFile.Type.MAIN);
}

@Override
public void execute(SensorContext context) {
try {
List<File> reports = getReports(conf, context.fileSystem().baseDir().getPath(), reportPathKey(), defaultReportPath());
processReports(context, reports);
} catch (javax.xml.stream.XMLStreamException e) {
String msg = new StringBuilder()
.append("Cannot feed the data into sonar, details: '")
.append(e)
.append("'")
.toString();
throw new IllegalStateException(msg, e);
}
}

public static List<File> getReports(Configuration conf, String baseDirPath, String reportPathPropertyKey, String defaultReportPath) {
String reportPath = conf.get(reportPathPropertyKey).orElse(defaultReportPath);
boolean propertyIsProvided = !Objects.equals(reportPath, defaultReportPath);

LOG.debug("Using pattern '{}' to find reports", reportPath);

DirectoryScanner scanner = new DirectoryScanner(new File(baseDirPath), WildcardPattern.create(reportPath));
List<File> includedFiles = scanner.getIncludedFiles();

if (includedFiles.isEmpty()) {
if (propertyIsProvided) {
// try absolute path
File file = new File(reportPath);
if (!file.exists()) {
LOG.warn("No report was found for {} using pattern {}", reportPathPropertyKey, reportPath);
} else {
includedFiles.add(file);
}
} else {
LOG.debug("No report was found for {} using default pattern {}", reportPathPropertyKey, reportPath);
}
}
return includedFiles;
}

protected void processReports(SensorContext context, List<File> reports) throws javax.xml.stream.XMLStreamException {
}

protected abstract String reportPathKey();

protected abstract String defaultReportPath();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* SonarQube Puppet Plugin
* Copyright (C) 2011-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 com.iadams.sonarqube.puppet.parser;

import com.ctc.wstx.stax.WstxInputFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.lang.StringUtils;
import org.codehaus.staxmate.SMInputFactory;
import org.codehaus.staxmate.in.SMHierarchicCursor;

/**
* Class copied from deprecated class StaxParser of sonar-plugin-api.
*/
public class StaxParser {

private SMInputFactory inf;

private XmlStreamHandler streamHandler;

public StaxParser(XmlStreamHandler streamHandler) {
this.streamHandler = streamHandler;
WstxInputFactory xmlFactory = (WstxInputFactory) XMLInputFactory.newInstance();
xmlFactory.configureForLowMemUsage();
xmlFactory.getConfig().setUndeclaredEntityResolver(new UndeclaredEntitiesXMLResolver());
xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
inf = new SMInputFactory(xmlFactory);
}

public void parse(File xmlFile) throws XMLStreamException {
try (FileInputStream input = new FileInputStream(xmlFile)) {
parse(input);
} catch (IOException e) {
throw new XMLStreamException(e);
}
}

public void parse(InputStream xmlInput) throws XMLStreamException {
SMHierarchicCursor rootCursor = inf.rootElementCursor(xmlInput);
try {
streamHandler.stream(rootCursor);
} finally {
rootCursor.getStreamReader().closeCompletely();
}
}

private static class UndeclaredEntitiesXMLResolver implements XMLResolver {

@Override
public Object resolveEntity(String arg0, String arg1, String fileName, String undeclaredEntity) throws XMLStreamException {
String undeclared = undeclaredEntity;
// avoid problems with XML docs containing undeclared entities.. return the entity under its raw form if not a Unicode expression
if (StringUtils.startsWithIgnoreCase(undeclaredEntity, "u") && undeclaredEntity.length() == 5) {
int unicodeCharHexValue = Integer.parseInt(undeclaredEntity.substring(1), 16);
if (Character.isDefined(unicodeCharHexValue)) {
undeclared = new String(new char[] {(char) unicodeCharHexValue});
}
}
return undeclared;
}
}

/**
* Simple interface for handling XML stream to parse.
*/
@FunctionalInterface
public interface XmlStreamHandler {
void stream(SMHierarchicCursor rootCursor) throws XMLStreamException;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SonarQube Puppet Plugin
* Copyright (C) 2011-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.
*/
@ParametersAreNonnullByDefault
package com.iadams.sonarqube.puppet.parser;

import javax.annotation.ParametersAreNonnullByDefault;
Loading