View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.translators;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  
25  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
26  import nl.tudelft.goal.EIS2Java.translation.Parameter2Java;
27  import nl.tudelft.goal.EIS2Java.translation.Translator;
28  import eis.iilang.Parameter;
29  
30  
31  /**
32   * A simple translator that will only translate addresses of the form
33   * ut://[host]:[port].
34   * 
35   * 
36   * @author mpkorstanje
37   * 
38   */
39  public class URITranslator implements Parameter2Java<URI> {
40  
41  	@Override
42  	public URI translate(Parameter parameter) throws TranslationException {
43  		String uriString = Translator.getInstance().translate2Java(parameter, String.class);
44  
45  		try {
46  			return new URI(uriString);
47  		} catch (URISyntaxException e) {
48  			String message = String.format("%s was not a URI. Expected a URI of the form ut://[host]:[port].", uriString);
49  			throw new TranslationException(message, e);
50  		}
51  
52  	}
53  
54  	@Override
55  	public Class<URI> translatesTo() {
56  		return URI.class;
57  	}
58  
59  
60  }