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 net.sourceforge.javadpkg.Context;
22  import net.sourceforge.javadpkg.ParseException;
23  import net.sourceforge.javadpkg.control.PackageVersion;
24  import net.sourceforge.javadpkg.control.PackageVersionParser;
25  
26  
27  /**
28   * <p>
29   * A {@link PackageVersionParser} implementation.
30   * </p>
31   *
32   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
33   * @version <b>1.0</b>, 01.01.2016 by Gerrit Hohl
34   */
35  public class PackageVersionParserImpl implements PackageVersionParser {
36  	
37  	
38  	/** The regular expression for a valid upstream version. */
39  	private static final String	REGEXP_UPSTREAM_VERSION	= "([0-9]){1}([A-Za-z0-9.+:~])*";
40  	/** The regular expression for a valid Debian revision. */
41  	private static final String	REGEXP_DEBIAN_REVISION	= "([A-Za-z0-9.+~-])+";
42  	// TODO Handle Debian revisions containing a hyphen (-).
43  	
44  	
45  	/**
46  	 * <p>
47  	 * Creates a parser.
48  	 * </p>
49  	 */
50  	public PackageVersionParserImpl() {
51  		super();
52  	}
53  
54  
55  	@Override
56  	public PackageVersion parsePackageVersion(String value, Context context) throws ParseException {
57  		PackageVersion packageVersion;
58  		String rest, epoch = null, upstreamVersion, debianRevision = null;
59  		int index, intValue;
60  		
61  		if (value == null)
62  			throw new IllegalArgumentException("Argument value is null.");
63  		if (context == null)
64  			throw new IllegalArgumentException("Argument context is null.");
65  		
66  		rest = value;
67  		
68  		// --- Epoch ---
69  		index = rest.indexOf(':');
70  		if (index != -1) {
71  			epoch = rest.substring(0, index);
72  			rest = rest.substring(index + 1);
73  			if (epoch.startsWith("0") && !epoch.equals("0")) {
74  				throw new ParseException("Version |" + value + "| contains an invalid epoch |" + epoch
75  						+ "|: An epoch can't start with 0 except it is 0.");
76  			}
77  			try {
78  				intValue = Integer.parseInt(epoch);
79  			} catch (NumberFormatException e) {
80  				throw new ParseException(
81  						"Version |" + value + "| contains an invalid epoch |" + epoch + "|: The epoch is not a valid integer.");
82  			}
83  			if (intValue < 0)
84  				throw new ParseException(
85  						"Version |" + value + "| contains an invalid epoch |" + epoch + "|: The epoch is negative.");
86  		}
87  
88  		// --- Upstream version ---
89  		index = rest.indexOf('-');
90  		if (index == -1) {
91  			upstreamVersion = rest;
92  			rest = "";
93  		} else {
94  			upstreamVersion = rest.substring(0, index);
95  			rest = rest.substring(index + 1);
96  		}
97  		if (!upstreamVersion.matches(REGEXP_UPSTREAM_VERSION))
98  			throw new ParseException("Version |" + value + "| contains an invalid upstream version |" + upstreamVersion + "|");
99  		
100 		// --- Debian revision ---
101 		if (!rest.isEmpty()) {
102 			debianRevision = rest;
103 			if (!debianRevision.matches(REGEXP_DEBIAN_REVISION))
104 				throw new ParseException(
105 						"Version |" + value + "| contains an invalid Debian revision |" + debianRevision + "|");
106 		}
107 		
108 		packageVersion = new PackageVersionImpl(epoch, upstreamVersion, debianRevision);
109 		return packageVersion;
110 	}
111 
112 
113 	/* **********************************************************************
114 	 * **********************************************************************
115 	 * **********************************************************************
116 	 * **********************************************************************
117 	 * **********************************************************************
118 	 */
119 
120 
121 	/**
122 	 * <p>
123 	 * The {@link PackageVersion} implementation of this class.
124 	 * </p>
125 	 *
126 	 * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
127 	 * @version <b>1.0</b>, 01.01.2016 by Gerrit Hohl
128 	 */
129 	private class PackageVersionImpl implements PackageVersion {
130 		
131 		
132 		/** The epoch (optional). */
133 		private String	epoch;
134 		/** The upstream version. */
135 		private String	upstreamVersion;
136 		/** The Debian revision (optional). */
137 		private String	debianRevision;
138 
139 
140 		/**
141 		 * <p>
142 		 * Creates a package version.
143 		 * </p>
144 		 *
145 		 * @param epoch
146 		 *            The epoch (optional).
147 		 * @param upstreamVersion
148 		 *            The upstream version.
149 		 * @param debianRevision
150 		 *            The Debian revision (optional).
151 		 */
152 		public PackageVersionImpl(String epoch, String upstreamVersion, String debianRevision) {
153 			super();
154 			
155 			this.epoch = epoch;
156 			this.upstreamVersion = upstreamVersion;
157 			this.debianRevision = debianRevision;
158 		}
159 
160 
161 		@Override
162 		public String getEpoch() {
163 			return this.epoch;
164 		}
165 		
166 		
167 		@Override
168 		public String getUpstreamVersion() {
169 			return this.upstreamVersion;
170 		}
171 		
172 		
173 		@Override
174 		public String getDebianRevision() {
175 			return this.debianRevision;
176 		}
177 		
178 		
179 		@Override
180 		public String getText() {
181 			StringBuilder sb;
182 			
183 			
184 			sb = new StringBuilder();
185 			if (this.epoch != null) {
186 				sb.append(this.epoch);
187 				sb.append(':');
188 			}
189 			sb.append(this.upstreamVersion);
190 			if (this.debianRevision != null) {
191 				sb.append('-');
192 				sb.append(this.debianRevision);
193 			}
194 			return sb.toString();
195 		}
196 
197 
198 	}
199 
200 
201 }