GenerateInterfaces.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.internal.message;
00018 
00019 import com.google.common.collect.Lists;
00020 import com.google.common.collect.Sets;
00021 
00022 import org.apache.commons.io.FileUtils;
00023 import org.ros.exception.RosMessageRuntimeException;
00024 import org.ros.internal.message.definition.MessageDefinitionProviderChain;
00025 import org.ros.internal.message.definition.MessageDefinitionTupleParser;
00026 import org.ros.internal.message.service.ServiceDefinitionFileProvider;
00027 import org.ros.internal.message.topic.TopicDefinitionFileProvider;
00028 import org.ros.message.MessageDeclaration;
00029 import org.ros.message.MessageFactory;
00030 import org.ros.message.MessageIdentifier;
00031 
00032 import java.io.File;
00033 import java.io.IOException;
00034 import java.util.Collection;
00035 import java.util.List;
00036 import java.util.ListIterator;
00037 
00041 public class GenerateInterfaces {
00042 
00043   private final TopicDefinitionFileProvider topicDefinitionFileProvider;
00044   private final ServiceDefinitionFileProvider serviceDefinitionFileProvider;
00045   private final MessageDefinitionProviderChain messageDefinitionProviderChain;
00046   private final MessageFactory messageFactory;
00047   static private final String ROS_PACKAGE_PATH = "ROS_PACKAGE_PATH";
00048 
00049   public GenerateInterfaces() {
00050     messageDefinitionProviderChain = new MessageDefinitionProviderChain();
00051     topicDefinitionFileProvider = new TopicDefinitionFileProvider();
00052     messageDefinitionProviderChain.addMessageDefinitionProvider(topicDefinitionFileProvider);
00053     serviceDefinitionFileProvider = new ServiceDefinitionFileProvider();
00054     messageDefinitionProviderChain.addMessageDefinitionProvider(serviceDefinitionFileProvider);
00055     messageFactory = new DefaultMessageFactory(messageDefinitionProviderChain);
00056   }
00057 
00066   private void writeTopicInterfaces(File outputDirectory, Collection<String> packages)
00067       throws IOException {
00068     Collection<MessageIdentifier> topicTypes = Sets.newHashSet();
00069     if (packages.size() == 0) {
00070       packages = topicDefinitionFileProvider.getPackages();
00071     }
00072     for (String pkg : packages) {
00073       Collection<MessageIdentifier> messageIdentifiers =
00074           topicDefinitionFileProvider.getMessageIdentifiersByPackage(pkg);
00075       if (messageIdentifiers != null) {
00076         topicTypes.addAll(messageIdentifiers);
00077       }
00078     }
00079     for (MessageIdentifier topicType : topicTypes) {
00080       String definition = messageDefinitionProviderChain.get(topicType.getType());
00081       MessageDeclaration messageDeclaration = new MessageDeclaration(topicType, definition);
00082       writeInterface(messageDeclaration, outputDirectory, true);
00083     }
00084   }
00085 
00094   private void writeServiceInterfaces(File outputDirectory, Collection<String> packages)
00095       throws IOException {
00096     Collection<MessageIdentifier> serviceTypes = Sets.newHashSet();
00097     if (packages.size() == 0) {
00098       packages = serviceDefinitionFileProvider.getPackages();
00099     }
00100     for (String pkg : packages) {
00101       Collection<MessageIdentifier> messageIdentifiers =
00102           serviceDefinitionFileProvider.getMessageIdentifiersByPackage(pkg);
00103       if (messageIdentifiers != null) {
00104         serviceTypes.addAll(messageIdentifiers);
00105       }
00106     }
00107     for (MessageIdentifier serviceType : serviceTypes) {
00108       String definition = messageDefinitionProviderChain.get(serviceType.getType());
00109       MessageDeclaration serviceDeclaration =
00110           MessageDeclaration.of(serviceType.getType(), definition);
00111       writeInterface(serviceDeclaration, outputDirectory, false);
00112       List<String> requestAndResponse = MessageDefinitionTupleParser.parse(definition, 2);
00113       MessageDeclaration requestDeclaration =
00114           MessageDeclaration.of(serviceType.getType() + "Request", requestAndResponse.get(0));
00115       MessageDeclaration responseDeclaration =
00116           MessageDeclaration.of(serviceType.getType() + "Response", requestAndResponse.get(1));
00117       writeInterface(requestDeclaration, outputDirectory, true);
00118       writeInterface(responseDeclaration, outputDirectory, true);
00119     }
00120   }
00121 
00122   private void writeInterface(MessageDeclaration messageDeclaration, File outputDirectory,
00123       boolean addConstantsAndMethods) {
00124     MessageInterfaceBuilder builder = new MessageInterfaceBuilder();
00125     builder.setPackageName(messageDeclaration.getPackage());
00126     builder.setInterfaceName(messageDeclaration.getName());
00127     builder.setMessageDeclaration(messageDeclaration);
00128     builder.setAddConstantsAndMethods(addConstantsAndMethods);
00129     try {
00130       String content;
00131       content = builder.build(messageFactory);
00132       File file = new File(outputDirectory, messageDeclaration.getType() + ".java");
00133       FileUtils.writeStringToFile(file, content);
00134     } catch (Exception e) {
00135       System.out.printf("Failed to generate interface for %s.\n", messageDeclaration.getType());
00136       e.printStackTrace();
00137     }
00138   }
00139 
00140   public void generate(File outputDirectory, Collection<String> packages,
00141       Collection<File> packagePath) {
00142     for (File directory : packagePath) {
00143       topicDefinitionFileProvider.addDirectory(directory);
00144       serviceDefinitionFileProvider.addDirectory(directory);
00145     }
00146     topicDefinitionFileProvider.update();
00147     serviceDefinitionFileProvider.update();
00148     try {
00149       writeTopicInterfaces(outputDirectory, packages);
00150       writeServiceInterfaces(outputDirectory, packages);
00151     } catch (IOException e) {
00152       throw new RosMessageRuntimeException(e);
00153     }
00154   }
00155 
00156   public static void main(String[] args) {
00157     List<String> arguments = Lists.newArrayList(args);
00158     if (arguments.size() == 0) {
00159       arguments.add(".");
00160     }
00161     String rosPackagePath = System.getenv(ROS_PACKAGE_PATH);
00162     // Overwrite with a supplied package path if specified (--package-path=)
00163     for (ListIterator<String> iter = arguments.listIterator(); iter.hasNext(); ) {
00164         String arg = iter.next();
00165         if (arg.contains("--package-path=")) {
00166                 rosPackagePath = arg.replace("--package-path=", "");
00167                 iter.remove();
00168                 break;
00169         }
00170     }
00171     Collection<File> packagePath = Lists.newArrayList();
00172     for (String path : rosPackagePath.split(File.pathSeparator)) {
00173       File packageDirectory = new File(path);
00174       if (packageDirectory.exists()) {
00175         packagePath.add(packageDirectory);
00176       }
00177     }
00178     GenerateInterfaces generateInterfaces = new GenerateInterfaces();
00179     File outputDirectory = new File(arguments.remove(0));
00180     generateInterfaces.generate(outputDirectory, arguments, packagePath);
00181   }
00182 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44