Md5Generator.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.Lists;
00021 
00022 import org.ros.internal.message.definition.MessageDefinitionParser;
00023 import org.ros.internal.message.definition.MessageDefinitionTupleParser;
00024 import org.ros.internal.message.definition.MessageDefinitionParser.MessageDefinitionVisitor;
00025 
00026 import org.apache.commons.codec.digest.DigestUtils;
00027 import org.ros.internal.message.field.PrimitiveFieldType;
00028 import org.ros.message.MessageDefinitionProvider;
00029 
00030 import java.util.List;
00031 
00035 public class Md5Generator {
00036 
00037   private final MessageDefinitionProvider messageDefinitionProvider;
00038 
00039   public Md5Generator(MessageDefinitionProvider messageDefinitionProvider) {
00040     this.messageDefinitionProvider = messageDefinitionProvider;
00041   }
00042 
00043   public String generate(String messageType) {
00044     String messageDefinition = messageDefinitionProvider.get(messageType);
00045     Preconditions.checkNotNull(messageDefinition, "No definition for message type: " + messageType);
00046     List<String> parts = MessageDefinitionTupleParser.parse(messageDefinition, -1);
00047     StringBuilder text = new StringBuilder();
00048     for (String part : parts) {
00049       text.append(generateText(messageType, part));
00050     }
00051     return DigestUtils.md5Hex(text.toString());
00052   }
00053 
00054   private String generateText(String messageType, String messageDefinition) {
00055     final List<String> constants = Lists.newArrayList();
00056     final List<String> variables = Lists.newArrayList();
00057     MessageDefinitionVisitor visitor = new MessageDefinitionVisitor() {
00058       @Override
00059       public void variableValue(String type, String name) {
00060         if (!PrimitiveFieldType.existsFor(type)) {
00061           type = generate(type);
00062         }
00063         variables.add(String.format("%s %s\n", type, name));
00064       }
00065 
00066       @Override
00067       public void variableList(String type, int size, String name) {
00068         if (!PrimitiveFieldType.existsFor(type)) {
00069           String md5Checksum = generate(type);
00070           variables.add(String.format("%s %s\n", md5Checksum, name));
00071         } else {
00072           if (size != -1) {
00073             variables.add(String.format("%s[%d] %s\n", type, size, name));
00074           } else {
00075             variables.add(String.format("%s[] %s\n", type, name));
00076           }
00077         }
00078       }
00079 
00080       @Override
00081       public void constantValue(String type, String name, String value) {
00082         constants.add(String.format("%s %s=%s\n", type, name, value));
00083       }
00084     };
00085     MessageDefinitionParser messageDefinitionParser = new MessageDefinitionParser(visitor);
00086     messageDefinitionParser.parse(messageType, messageDefinition);
00087     String text = "";
00088     for (String constant : constants) {
00089       text += constant;
00090     }
00091     for (String variable : variables) {
00092       text += variable;
00093     }
00094     return text.trim();
00095   }
00096 }


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