Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.

Commit eee4905

Browse files
committed
single interface generator for genjava.
1 parent 2a4c144 commit eee4905

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (C) 2014 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
17+
package org.ros.internal.message;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.util.List;
22+
23+
import org.apache.commons.io.FileUtils;
24+
import org.apache.commons.io.FilenameUtils;
25+
26+
import org.ros.exception.RosMessageRuntimeException;
27+
import org.ros.internal.message.definition.MessageDefinitionReflectionProvider;
28+
import org.ros.internal.message.definition.MessageDefinitionTupleParser;
29+
import org.ros.message.MessageDeclaration;
30+
import org.ros.message.MessageFactory;
31+
import org.ros.message.MessageIdentifier;
32+
33+
import com.google.common.collect.Lists;
34+
35+
/**
36+
* @author [email protected] (Daniel Stonier)
37+
*/
38+
public class GenerateInterface {
39+
40+
private static void writeInterface(MessageDeclaration messageDeclaration, File outputDirectory,
41+
boolean addConstantsAndMethods, MessageFactory messageFactory) {
42+
MessageInterfaceBuilder builder = new MessageInterfaceBuilder();
43+
builder.setPackageName(messageDeclaration.getPackage());
44+
builder.setInterfaceName(messageDeclaration.getName());
45+
builder.setMessageDeclaration(messageDeclaration);
46+
builder.setAddConstantsAndMethods(addConstantsAndMethods);
47+
try {
48+
String content;
49+
content = builder.build(messageFactory);
50+
File file = new File(outputDirectory, messageDeclaration.getType() + ".java");
51+
System.out.println("Output File: " + file.getAbsolutePath());
52+
FileUtils.writeStringToFile(file, content);
53+
} catch (Exception e) {
54+
System.out.printf("Failed to generate interface for %s.\n", messageDeclaration.getType());
55+
e.printStackTrace();
56+
}
57+
}
58+
59+
public static void main(String[] args) {
60+
List<String> arguments = Lists.newArrayList(args);
61+
if (arguments.size() != 3) {
62+
System.out
63+
.println("Incorrect usage, please provide two args: _output_directory_, _pkg_ and _path_to_msg/srv_file_");
64+
System.exit(1);
65+
}
66+
File outputDirectory = new File(arguments.remove(0));
67+
String pkg = arguments.remove(0);
68+
File file = new File(arguments.remove(0));
69+
70+
System.out.println("Output Directory: " + outputDirectory.getAbsolutePath());
71+
System.out.println("Package: " + pkg);
72+
System.out.println("Message: " + file.getAbsolutePath());
73+
74+
String name = FilenameUtils.getBaseName(file.getName());
75+
String extension = FilenameUtils.getExtension(file.getName());
76+
77+
System.out.println(" Name: " + name);
78+
System.out.println(" Extension: " + extension);
79+
String definition;
80+
try {
81+
definition = FileUtils.readFileToString(file, "US-ASCII");
82+
} catch (IOException e) {
83+
throw new RosMessageRuntimeException(e);
84+
}
85+
MessageIdentifier messageIdentifier = MessageIdentifier.of(pkg, name);
86+
MessageDeclaration messageDeclaration = new MessageDeclaration(messageIdentifier, definition);
87+
MessageDefinitionReflectionProvider messageDefinitionProvider =
88+
new MessageDefinitionReflectionProvider();
89+
messageDefinitionProvider.add(messageIdentifier.getType(), definition);
90+
MessageFactory messageFactory = new DefaultMessageFactory(messageDefinitionProvider);
91+
if (extension.equals("msg")) {
92+
writeInterface(messageDeclaration, outputDirectory, true, messageFactory);
93+
} else if (extension.equals("srv")) {
94+
writeInterface(messageDeclaration, outputDirectory, false, messageFactory);
95+
List<String> requestAndResponse = MessageDefinitionTupleParser.parse(definition, 2);
96+
MessageDeclaration requestDeclaration =
97+
MessageDeclaration.of(messageIdentifier.getType() + "Request", requestAndResponse.get(0));
98+
MessageDeclaration responseDeclaration =
99+
MessageDeclaration
100+
.of(messageIdentifier.getType() + "Response", requestAndResponse.get(1));
101+
writeInterface(requestDeclaration, outputDirectory, true, messageFactory);
102+
writeInterface(responseDeclaration, outputDirectory, true, messageFactory);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)