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

com.liferay.source.formatter.checks.XMLBuildFileCheck Maven / Gradle / Ivy

There is a newer version: 1.0.1437
Show newest version
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library 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 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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.
 */

package com.liferay.source.formatter.checks;

import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringPool;
import com.liferay.source.formatter.checks.comparator.ElementComparator;
import com.liferay.source.formatter.checks.util.SourceUtil;
import com.liferay.source.formatter.util.FileUtil;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

/**
 * @author Hugo Huijser
 */
public class XMLBuildFileCheck extends BaseFileCheck {

	@Override
	protected String doProcess(
			String fileName, String absolutePath, String content)
		throws DocumentException, IOException {

		if (fileName.startsWith(getBaseDirName() + "build") ||
			(fileName.contains("/build") && !fileName.contains("/tools/"))) {

			_checkBuildXML(fileName, absolutePath, content);
		}

		return content;
	}

	private void _checkBuildProjectName(String fileName, Document document) {
		Matcher matcher = _projectNamePattern.matcher(fileName);

		if (!matcher.find()) {
			return;
		}

		String expectedProjectName = matcher.group(1);

		Element rootElement = document.getRootElement();

		String projectName = rootElement.attributeValue("name");

		if (!projectName.equals(expectedProjectName)) {
			addMessage(
				fileName, "Incorrect project name '" + projectName + "'");
		}
	}

	private void _checkBuildXML(
			String fileName, String absolutePath, String content)
		throws DocumentException, IOException {

		Document document = SourceUtil.readXML(content);

		_checkBuildProjectName(fileName, document);

		checkElementOrder(
			fileName, document.getRootElement(), "macrodef", null,
			new ElementComparator());
		checkElementOrder(
			fileName, document.getRootElement(), "target", null,
			new ElementComparator());

		int x = content.lastIndexOf("\n\t");
		int y = content.indexOf("\n\t y)) {
			addMessage(fileName, "Macrodefs go before process-ivy");
		}

		int z = content.indexOf("\n\t");

		if ((z != -1) && (x > z)) {
			addMessage(fileName, "Macrodefs go before targets");
		}

		if (!isModulesApp(absolutePath, true)) {
			_checkImportFiles(fileName, content);
		}

		_checkTargetNames(fileName, absolutePath, content);
	}

	private void _checkImportFiles(String fileName, String content) {
		int pos = fileName.lastIndexOf(StringPool.SLASH);

		if (pos == -1) {
			return;
		}

		String dirPath = fileName.substring(0, pos + 1);

		Matcher matcher = _importFilePattern.matcher(content);

		while (matcher.find()) {
			String importFileName = matcher.group(1);

			if (importFileName.contains("${")) {
				continue;
			}

			File file = new File(dirPath + importFileName);

			if (!file.exists()) {
				addMessage(
					fileName, "Incorrect import file '" + importFileName + "'");
			}
		}
	}

	private void _checkTargetName(
			String targetName, String buildFileName, String fileName)
		throws DocumentException, IOException {

		List targetNames = _getTargetNames(
			buildFileName, fileName, null, false);

		if ((targetNames == null) || targetNames.contains(targetName)) {
			return;
		}

		int x = targetName.lastIndexOf(CharPool.PERIOD);

		if (x != -1) {
			targetName = targetName.substring(x + 1);
		}

		if (!targetNames.contains(targetName)) {
			addMessage(fileName, "Target '" + targetName + "' does not exist");
		}
	}

	private void _checkTargetNames(
			String fileName, String absolutePath, String content)
		throws DocumentException, IOException {

		Document document = SourceUtil.readXML(content);

		Element rootElement = document.getRootElement();

		List antCallElements = _getElementsByName(
			"antcall", rootElement, null);

		for (Element antCallElement : antCallElements) {
			String targetName = antCallElement.attributeValue("target");

			if ((targetName == null) ||
				targetName.contains(StringPool.OPEN_CURLY_BRACE)) {

				continue;
			}

			_checkTargetName(targetName, absolutePath, fileName);
		}

		String fileDirName = fileName.substring(
			0, fileName.lastIndexOf(CharPool.SLASH) + 1);

		List antElements = _getElementsByName(
			"ant", rootElement, null);

		for (Element antElement : antElements) {
			String targetName = antElement.attributeValue("target");

			if ((targetName == null) ||
				targetName.contains(StringPool.OPEN_CURLY_BRACE)) {

				continue;
			}

			String fullDirName = fileDirName;

			String dirName = antElement.attributeValue("dir");

			if (dirName != null) {
				if (dirName.contains(StringPool.OPEN_CURLY_BRACE)) {
					continue;
				}

				fullDirName = fullDirName + dirName + StringPool.SLASH;
			}

			String antFileName = antElement.attributeValue("antfile");

			if (antFileName == null) {
				antFileName = "build.xml";
			}

			_checkTargetName(targetName, fullDirName + antFileName, fileName);
		}
	}

	private List _getElementsByName(
		String name, Element element, List elements) {

		if (elements == null) {
			elements = new ArrayList<>();
		}

		List childElements = element.elements();

		for (Element childElement : childElements) {
			String elementName = childElement.getName();

			if (elementName.equals(name)) {
				elements.add(childElement);
			}

			elements = _getElementsByName(name, childElement, elements);
		}

		return elements;
	}

	private List _getTargetNames(
			String buildFileName, String fileName, List targetNames,
			boolean importFile)
		throws DocumentException, IOException {

		if (buildFileName.contains(StringPool.OPEN_CURLY_BRACE)) {
			return null;
		}

		File file = new File(buildFileName);

		if (!file.exists()) {
			if (!importFile) {
				addMessage(
					fileName,
					"Ant element points to nonexistent build file '" +
						buildFileName + "'");
			}

			return null;
		}

		Document document = SourceUtil.readXML(FileUtil.read(file));

		Element rootElement = document.getRootElement();

		if (targetNames == null) {
			targetNames = new ArrayList<>();
		}

		List targetElements = rootElement.elements("target");

		for (Element targetElement : targetElements) {
			targetNames.add(targetElement.attributeValue("name"));
		}

		List importElements = rootElement.elements("import");

		for (Element importElement : importElements) {
			String buildDirName = buildFileName.substring(
				0, buildFileName.lastIndexOf(CharPool.SLASH) + 1);

			String importFileName =
				buildDirName + importElement.attributeValue("file");

			targetNames = _getTargetNames(
				importFileName, fileName, targetNames, true);
		}

		return targetNames;
	}

	private static final Pattern _importFilePattern = Pattern.compile(
		"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy