View Javadoc
1   /*
2    * dpkg - Debian Package library and the Debian Package Maven plugin
3    * (c) Copyright 2016 Gerrit Hohl
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package net.sourceforge.javadpkg.control.impl;
20  
21  import java.util.List;
22  
23  import net.sourceforge.javadpkg.BuildException;
24  import net.sourceforge.javadpkg.Context;
25  import net.sourceforge.javadpkg.control.PackageDependency;
26  import net.sourceforge.javadpkg.control.PackageDependencyBuilder;
27  import net.sourceforge.javadpkg.control.PackageNameBuilder;
28  import net.sourceforge.javadpkg.control.PackageVersionBuilder;
29  import net.sourceforge.javadpkg.control.PackageVersionRelationOperatorBuilder;
30  
31  
32  /**
33   * <p>
34   * A {@link PackageDependencyBuilder} implementation.
35   * </p>
36   *
37   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
38   * @version <b>1.0</b>, 28.04.2016 by Gerrit Hohl
39   */
40  public class PackageDependencyBuilderImpl implements PackageDependencyBuilder {
41  
42  
43  	/** The builder for the package names. */
44  	private PackageNameBuilder						packageNameBuilder;
45  	/** The builder for the relation operator. */
46  	private PackageVersionRelationOperatorBuilder	packageVersionRelationOperatorBuilder;
47  	/** The builder for the package version. */
48  	private PackageVersionBuilder					packageVersionBuilder;
49  
50  
51  	/**
52  	 * <p>
53  	 * Creates a builder.
54  	 * </p>
55  	 *
56  	 * @param packageNameBuilder
57  	 *            The builder for the package names.
58  	 * @param packageVersionRelationOperatorBuilder
59  	 *            The builder for the relation operator.
60  	 * @param packageVersionBuilder
61  	 *            The builder for the package version.
62  	 * @throws IllegalArgumentException
63  	 *             If any of the parameters are <code>null</code>.
64  	 */
65  	public PackageDependencyBuilderImpl(PackageNameBuilder packageNameBuilder,
66  			PackageVersionRelationOperatorBuilder packageVersionRelationOperatorBuilder,
67  			PackageVersionBuilder packageVersionBuilder) {
68  			
69  		super();
70  
71  		if (packageNameBuilder == null)
72  			throw new IllegalArgumentException("Argument packageNameBuilder is null.");
73  		if (packageVersionRelationOperatorBuilder == null)
74  			throw new IllegalArgumentException("Argument packageVersionRelationOperatorBuilder is null.");
75  		if (packageVersionBuilder == null)
76  			throw new IllegalArgumentException("Argument packageVersionBuilder is null.");
77  			
78  		this.packageNameBuilder = packageNameBuilder;
79  		this.packageVersionRelationOperatorBuilder = packageVersionRelationOperatorBuilder;
80  		this.packageVersionBuilder = packageVersionBuilder;
81  	}
82  
83  
84  	@Override
85  	public String buildPackageDependency(PackageDependency dependency, Context context) throws BuildException {
86  		String value;
87  		StringBuilder sb;
88  		
89  		
90  		if (dependency == null)
91  			throw new IllegalArgumentException("Argument dependency is null.");
92  		if (context == null)
93  			throw new IllegalArgumentException("Argument context is null.");
94  			
95  		// --- Do we have a condition? ---
96  		if (dependency.isCondition()) {
97  			sb = new StringBuilder();
98  			// --- Add all dependencies including the condition ---
99  			for (PackageDependency dep : dependency.getConditionPackageDependencies()) {
100 				if (sb.length() > 0) {
101 					switch (dependency.getCondition()) {
102 						case OR:
103 							sb.append(" | ");
104 							break;
105 							
106 						default:
107 							throw new BuildException("Unsupported dependency condition |" + dependency.getCondition() + "|.");
108 					}
109 					
110 				}
111 				value = this.buildPackageDependency(dep, context);
112 				sb.append(value);
113 			}
114 			value = sb.toString();
115 		}
116 		// --- Otherwise we have a simple dependency ---
117 		else {
118 			sb = new StringBuilder();
119 
120 			// --- Package name ---
121 			try {
122 				value = this.packageNameBuilder.buildPackageName(dependency.getPackage(), context);
123 			} catch (BuildException e) {
124 				throw new BuildException("Couldn't build dependency: " + e.getMessage(), e);
125 			}
126 			sb.append(value);
127 			
128 			if (dependency.getVersion() != null) {
129 				sb.append(" (");
130 				// --- Relational operator ---
131 				if (dependency.getRelationOperator() != null) {
132 					try {
133 						value = this.packageVersionRelationOperatorBuilder
134 								.buildPackageVersionRelationOperator(dependency.getRelationOperator(), context);
135 					} catch (BuildException e) {
136 						throw new BuildException("Couldn't build dependency: " + e.getMessage(), e);
137 					}
138 					sb.append(value);
139 					sb.append(' ');
140 				}
141 
142 				// --- Version ---
143 				try {
144 					value = this.packageVersionBuilder.buildPackageVersion(dependency.getVersion(), context);
145 				} catch (BuildException e) {
146 					throw new BuildException("Couldn't build dependency: " + e.getMessage(), e);
147 				}
148 				sb.append(value);
149 				sb.append(')');
150 			}
151 			value = sb.toString();
152 		}
153 		return value;
154 	}
155 
156 
157 	@Override
158 	public String buildPackageDependencies(List<PackageDependency> dependencies, Context context) throws BuildException {
159 		StringBuilder result;
160 		String value;
161 		
162 		
163 		if (dependencies == null)
164 			throw new IllegalArgumentException("Argument dependencies is null.");
165 		if (context == null)
166 			throw new IllegalArgumentException("Argument context is null.");
167 
168 		result = new StringBuilder();
169 		for (PackageDependency dependency : dependencies) {
170 			if (result.length() > 0) {
171 				result.append(", ");
172 			}
173 			
174 			try {
175 				value = this.buildPackageDependency(dependency, context);
176 			} catch (BuildException e) {
177 				throw new BuildException("Couldn't build dependencies: " + e.getMessage(), e);
178 			}
179 			result.append(value);
180 		}
181 		return result.toString();
182 	}
183 
184 
185 }