All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.xml.checks.XmlFile Maven / Gradle / Ivy

/*
 * 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 java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;

import com.google.common.io.Files;
import org.sonar.api.batch.fs.InputFile;

/**
 * Checks and analyzes report measurements, issues and other findings in WebSourceCode.
 *
 * @author Matthijs Galesloot
 */
public class XmlFile {

  private static final Logger LOG = LoggerFactory.getLogger(XmlFile.class);
  private static final String XML_PROLOG_START_TAG = " 0 || lineNb > 1)) {
            hasCharsBeforeProlog = true;
          }
          break;
        }
        lineNb++;
      }

      if (hasCharsBeforeProlog) {
        processCharBeforePrologInFile(fileSystem, lineNb);
      }
    } catch (IOException e) {
      LOG.warn("Unable to analyse file {}", inputFile.absolutePath(), e);
    }
  }

  /**
   * Create a temporary file without any character before the prolog and update the following
   * attributes in order to correctly report issues:
   * 
    *
  • lineDeltaForIssue *
  • file */ private void processCharBeforePrologInFile(FileSystem fileSystem, int lineDelta) { try { String content = Files.toString(inputFile.file(), fileSystem.encoding()); File tempFile = new File(fileSystem.workDir(), inputFile.file().getName()); int index = content.indexOf(XML_PROLOG_START_TAG); Files.write(content.substring(index), tempFile, fileSystem.encoding()); noCharBeforePrologFile = tempFile; if (lineDelta > 1) { lineDeltaForIssue = lineDelta - 1; } } catch (IOException e) { LOG.warn("Unable to analyse file {}", inputFile.absolutePath(), e); } } public InputFile getInputFile() { return inputFile; } public int getLineDelta() { return lineDeltaForIssue; } public File getIOFile() { return noCharBeforePrologFile != null ? noCharBeforePrologFile : inputFile.file(); } public int getPrologLine() { return lineDeltaForIssue + 1; } public boolean hasCharsBeforeProlog() { return hasCharsBeforeProlog; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy