MessageInterfaceBuilder.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.base.Preconditions;
00020 import com.google.common.collect.Sets;
00021 
00022 import org.apache.commons.lang.StringEscapeUtils;
00023 import org.ros.exception.RosMessageRuntimeException;
00024 import org.ros.internal.message.context.MessageContext;
00025 import org.ros.internal.message.context.MessageContextProvider;
00026 import org.ros.internal.message.field.Field;
00027 import org.ros.internal.message.field.FieldType;
00028 import org.ros.internal.message.field.MessageFields;
00029 import org.ros.internal.message.field.PrimitiveFieldType;
00030 import org.ros.message.MessageDeclaration;
00031 import org.ros.message.MessageFactory;
00032 
00033 import java.util.Set;
00034 
00038 public class MessageInterfaceBuilder {
00039 
00040   private MessageDeclaration messageDeclaration;
00041   private String packageName;
00042   private String interfaceName;
00043   private boolean addConstantsAndMethods;
00044   private String nestedContent;
00045 
00046   // TODO(damonkohler): Upgrade Apache Commons Lang. See
00047   // https://issues.apache.org/jira/browse/LANG-437
00048   private static String escapeJava(String str) {
00049     return StringEscapeUtils.escapeJava(str).replace("\\/", "/").replace("'", "\\'");
00050   }
00051 
00052   public MessageDeclaration getMessageDeclaration() {
00053     return messageDeclaration;
00054   }
00055 
00056   public MessageInterfaceBuilder setMessageDeclaration(MessageDeclaration messageDeclaration) {
00057     Preconditions.checkNotNull(messageDeclaration);
00058     this.messageDeclaration = messageDeclaration;
00059     return this;
00060   }
00061 
00062   public String getPackageName() {
00063     return packageName;
00064   }
00065 
00072   public MessageInterfaceBuilder setPackageName(String packageName) {
00073     this.packageName = packageName;
00074     return this;
00075   }
00076 
00077   public String getInterfaceName() {
00078     return interfaceName;
00079   }
00080 
00081   public MessageInterfaceBuilder setInterfaceName(String interfaceName) {
00082     Preconditions.checkNotNull(interfaceName);
00083     this.interfaceName = interfaceName;
00084     return this;
00085   }
00086 
00087   public boolean getAddConstantsAndMethods() {
00088     return addConstantsAndMethods;
00089   }
00090 
00091   public void setAddConstantsAndMethods(boolean enabled) {
00092     addConstantsAndMethods = enabled;
00093   }
00094 
00095   public String getNestedContent() {
00096     return nestedContent;
00097   }
00098 
00099   public void setNestedContent(String nestedContent) {
00100     this.nestedContent = nestedContent;
00101   }
00102 
00103   public String build(MessageFactory messageFactory) {
00104     Preconditions.checkNotNull(messageDeclaration);
00105     Preconditions.checkNotNull(interfaceName);
00106     StringBuilder builder = new StringBuilder();
00107     if (packageName != null) {
00108       builder.append(String.format("package %s;\n\n", packageName));
00109     }
00110     builder.append(String.format(
00111         "public interface %s extends org.ros.internal.message.Message {\n", interfaceName));
00112     builder.append(String.format("  static final java.lang.String _TYPE = \"%s\";\n",
00113         messageDeclaration.getType()));
00114     builder.append(String.format("  static final java.lang.String _DEFINITION = \"%s\";\n",
00115         escapeJava(messageDeclaration.getDefinition())));
00116     if (addConstantsAndMethods) {
00117       MessageContextProvider messageContextProvider = new MessageContextProvider(messageFactory);
00118       MessageContext messageContext = messageContextProvider.get(messageDeclaration);
00119       appendConstants(messageContext, builder);
00120       appendSettersAndGetters(messageContext, builder);
00121     }
00122     if (nestedContent != null) {
00123       builder.append("\n");
00124       builder.append(nestedContent);
00125     }
00126     builder.append("}\n");
00127     return builder.toString();
00128   }
00129 
00130   @SuppressWarnings("deprecation")
00131   private String getJavaValue(PrimitiveFieldType primitiveFieldType, String value) {
00132     switch (primitiveFieldType) {
00133       case BOOL:
00134         return Boolean.valueOf(!value.equals("0") && !value.equals("false")).toString();
00135       case FLOAT32:
00136         return value + "f";
00137       case STRING:
00138         return "\"" + escapeJava(value) + "\"";
00139       case BYTE:
00140       case CHAR:
00141       case INT8:
00142       case UINT8:
00143       case INT16:
00144       case UINT16:
00145       case INT32:
00146       case UINT32:
00147       case INT64:
00148       case UINT64:
00149       case FLOAT64:
00150         return value;
00151       default:
00152         throw new RosMessageRuntimeException("Unsupported PrimitiveFieldType: " + primitiveFieldType);
00153     }
00154   }
00155 
00156   private void appendConstants(MessageContext messageContext, StringBuilder builder) {
00157     MessageFields messageFields = new MessageFields(messageContext);
00158     for (Field field : messageFields.getFields()) {
00159       if (field.isConstant()) {
00160         Preconditions.checkState(field.getType() instanceof PrimitiveFieldType);
00161         // We use FieldType and cast back to PrimitiveFieldType below to avoid a
00162         // bug in the Sun JDK: http://gs.sun.com/view_bug.do?bug_id=6522780
00163         FieldType fieldType = (FieldType) field.getType();
00164         String value = getJavaValue((PrimitiveFieldType) fieldType, field.getValue().toString());
00165         builder.append(String.format("  static final %s %s = %s;\n", fieldType.getJavaTypeName(),
00166             field.getName(), value));
00167       }
00168     }
00169   }
00170 
00171   private void appendSettersAndGetters(MessageContext messageContext, StringBuilder builder) {
00172     MessageFields messageFields = new MessageFields(messageContext);
00173     Set<String> getters = Sets.newHashSet();
00174     for (Field field : messageFields.getFields()) {
00175       if (field.isConstant()) {
00176         continue;
00177       }
00178       String type = field.getJavaTypeName();
00179       String getter = messageContext.getFieldGetterName(field.getName());
00180       String setter = messageContext.getFieldSetterName(field.getName());
00181       if (getters.contains(getter)) {
00182         // In the case that two or more message fields have the same name except
00183         // for capitalization, we only generate a getter and setter pair for the
00184         // first one. The following fields will only be accessible via the
00185         // RawMessage interface.
00186         continue;
00187       }
00188       getters.add(getter);
00189       builder.append(String.format("  %s %s();\n", type, getter));
00190       builder.append(String.format("  void %s(%s value);\n", setter, type));
00191     }
00192   }
00193 }


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