<commit_before>package org.springframework.roo.project;

import java.util.ArrayList;
import java.util.List;

import org.springframework.roo.model.JavaPackage;
import org.springframework.roo.model.JavaSymbolName;
import org.springframework.roo.support.style.ToStringCreator;
import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.XmlUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Simplified immutable representation of a dependency.
 * 
 * <p>
 * Structured after the model used by Maven and Ivy. This may be replaced in a future
 * release with a more OSGi-centric model.
 * 
 * @author <NAME>
 * @author <NAME>
 * @since 1.0
 *
 */
public class Dependency implements Comparable<Dependency> {
	private JavaPackage groupId;
	private JavaSymbolName artifactId;
	private String versionId;
	private DependencyType type;
	private List<Dependency> exclusions = new ArrayList<Dependency>();
	
	/**
	 * Convenience constructor for producing a JAR dependency.
	 * 
	 * @param groupId the group ID (required)
	 * @param artifactId the artifact ID (required)
	 * @param versionId the version ID (required)
	 */
	public Dependency(String groupId, String artifactId, String versionId) {
		Assert.hasText(groupId, "Group ID required");
		Assert.hasText(artifactId, "Artifact ID required");
		Assert.hasText(versionId, "Version ID required");
		this.groupId = new JavaPackage(groupId);
		this.artifactId = new JavaSymbolName(artifactId);
		this.versionId = versionId;
		this.type = DependencyType.JAR;
	}
	

	/**
	 * Convenience constructor for producing a JAR dependency.
	 * 
	 * @param groupId the group ID (required)
	 * @param artifactId the artifact ID (required)
	 * @param versionId the version ID (required)
	 * @param exclusions the exclusions for this dependency
	 */
	public Dependency(String groupId, String artifactId, String versionId, List<Dependency> exclusions) {
		Assert.hasText(groupId, "Group ID required");
		Assert.hasText(artifactId, "Artifact ID required");
		Assert.hasText(versionId, "Version ID required");
		Assert.notNull(exclusions, "Exclusions required");
		this.groupId = new JavaPackage(groupId);
		this.artifactId = new JavaSymbolName(artifactId);
		this.versionId = versionId;
		this.type = DependencyType.JAR;
		this.exclusions = exclusions;
	}
	
	/**
	 * Convenience constructor when an XML element is available that represents a Maven <dependency>.
	 * 
	 * @param dependency to parse (required)
	 */
	public Dependency(Element dependency) {
		this.type = DependencyType.JAR;
        if (XmlUtils.findFirstElement("//type", dependency) != null || dependency.hasAttribute("type")) {
        	String t;
        	if (dependency.hasAttribute("type")) {
        		t = dependency.getAttribute("type");
        	} else {
        		t = XmlUtils.findFirstElement("//type", dependency).getTextContent().trim().toUpperCase();
        	} 
			if (t.equals("JAR")) {
				// already a JAR, so no need to reassign
			} else if (t.equals("ZIP")) {
			    this.type = DependencyType.ZIP;
			} else {
			    this.type = DependencyType.OTHER;
			}
        }
        //test if it has Maven format
        if (dependency.hasChildNodes() && dependency.getElementsByTagName("groupId").getLength() > 0 && dependency.getElementsByTagName("artifactId").getLength() > 0) {
        	this.groupId = new JavaPackage(dependency.getElementsByTagName("groupId").item(0).getTextContent());
	        this.artifactId = new JavaSymbolName(dependency.getElementsByTagName("artifactId").item(0).getTextContent());

	        NodeList versionElements = dependency.getElementsByTagName("version");
	        if (versionElements.getLength() > 0) {
	        	this.versionId = dependency.getElementsByTagName("version").item(0).getTextContent();
	        }
	        else {
	        	this.versionId = "";
	        }
	        
	        //parsing for exclusions
	        NodeList exclusionList = dependency.getElementsByTagName("exclusions");
	        
	        if(exclusionList.getLength() > 0) {
	        	for (int i = 0; i < exclusionList.getLength(); i++) {
	        		Element exclusionElement = (Element) exclusionList.item(i);
	        		String exclusionId = exclusionElement.getElementsByTagName("groupId").item(0).getTextContent();
	        		String exclusionArtifactId = exclusionElement.getElementsByTagName("artifactId").item(0).getTextContent();
	        		if(!(exclusionArtifactId.length() < 1) && !(exclusionId.length() < 1)) {<commit_msg>ROO-354: Dependencies incorrectly parsed when unexpected <type> element appearing in pom.xml<commit_after>package org.springframework.roo.project;

import java.util.ArrayList;
import java.util.List;

import org.springframework.roo.model.JavaPackage;
import org.springframework.roo.model.JavaSymbolName;
import org.springframework.roo.support.style.ToStringCreator;
import org.springframework.roo.support.util.Assert;
import org.springframework.roo.support.util.XmlUtils;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Simplified immutable representation of a dependency.
 * 
 * <p>
 * Structured after the model used by Maven and Ivy. This may be replaced in a future
 * release with a more OSGi-centric model.
 * 
 * @author <NAME>
 * @author <NAME>
 * @since 1.0
 *
 */
public class Dependency implements Comparable<Dependency> {
	private JavaPackage groupId;
	private JavaSymbolName artifactId;
	private String versionId;
	private DependencyType type;
	private List<Dependency> exclusions = new ArrayList<Dependency>();
	
	/**
	 * Convenience constructor for producing a JAR dependency.
	 * 
	 * @param groupId the group ID (required)
	 * @param artifactId the artifact ID (required)
	 * @param versionId the version ID (required)
	 */
	public Dependency(String groupId, String artifactId, String versionId) {
		Assert.hasText(groupId, "Group ID required");
		Assert.hasText(artifactId, "Artifact ID required");
		Assert.hasText(versionId, "Version ID required");
		this.groupId = new JavaPackage(groupId);
		this.artifactId = new JavaSymbolName(artifactId);
		this.versionId = versionId;
		this.type = DependencyType.JAR;
	}
	

	/**
	 * Convenience constructor for producing a JAR dependency.
	 * 
	 * @param groupId the group ID (required)
	 * @param artifactId the artifact ID (required)
	 * @param versionId the version ID (required)
	 * @param exclusions the exclusions for this dependency
	 */
	public Dependency(String groupId, String artifactId, String versionId, List<Dependency> exclusions) {
		Assert.hasText(groupId, "Group ID required");
		Assert.hasText(artifactId, "Artifact ID required");
		Assert.hasText(versionId, "Version ID required");
		Assert.notNull(exclusions, "Exclusions required");
		this.groupId = new JavaPackage(groupId);
		this.artifactId = new JavaSymbolName(artifactId);
		this.versionId = versionId;
		this.type = DependencyType.JAR;
		this.exclusions = exclusions;
	}
	
	/**
	 * Convenience constructor when an XML element is available that represents a Maven <dependency>.
	 * 
	 * @param dependency to parse (required)
	 */
	public Dependency(Element dependency) {
		this.type = DependencyType.JAR;
        if (XmlUtils.findFirstElement("/type", dependency) != null || dependency.hasAttribute("type")) {
        	String t;
        	if (dependency.hasAttribute("type")) {
        		t = dependency.getAttribute("type");
        	} else {
        		t = XmlUtils.findFirstElement("//type", dependency).getTextContent().trim().toUpperCase();
        	} 
			if (t.equals("JAR")) {
				// already a JAR, so no need to reassign
			} else if (t.equals("ZIP")) {
			    this.type = DependencyType.ZIP;
			} else {
				System.out.println("WARN " + t + " is not recognized");
			    this.type = DependencyType.OTHER;
			}
        }
        //test if it has Maven format
        if (dependency.hasChildNodes() && dependency.getElementsByTagName("groupId").getLength() > 0 && dependency.getElementsByTagName("artifactId").getLength() > 0) {
        	this.groupId = new JavaPackage(dependency.getElementsByTagName("groupId").item(0).getTextContent());
	        this.artifactId = new JavaSymbolName(dependency.getElementsByTagName("artifactId").item(0).getTextContent());

	        NodeList versionElements = dependency.getElementsByTagName("version");
	        if (versionElements.getLength() > 0) {
	        	this.versionId = dependency.getElementsByTagName("version").item(0).getTextContent();
	        }
	        else {
	        	this.versionId = "";
	        }
	        
	        //parsing for exclusions
	        NodeList exclusionList = dependency.getElementsByTagName("exclusions");
	        
	        if(exclusionList.getLength() > 0) {
	        	for (int i = 0; i < exclusionList.getLength(); i++) {
	        		Element exclusionElement = (Element) exclusionList.item(i);
	        		String exclusionId = exclusionElement.getElementsByTagName("groupId").item(0).getTextContent();
	        		String exclusionArtifactId = exclusionElement.getElementsByTagName("artifactId").item(0).getTextContent();
	        		if(!(exclusionArtifactId.length() < 1) && !(exclusionId.length() < 1)) {
{'fraction_non_alphanumeric': 0.11669953977646286, 'fraction_numerical': 0.0038351961428884506, 'mean_word_length': 3.7511712649661635, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793653', 'n_tokens_mistral': 2858, 'n_tokens_neox': 2538, 'n_words': 639}