MessageDefinitionFileProvider.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.definition;
00018 
00019 import com.google.common.collect.Maps;
00020 
00021 import org.ros.internal.message.StringFileProvider;
00022 
00023 import org.apache.commons.io.FilenameUtils;
00024 import org.ros.message.MessageDefinitionProvider;
00025 import org.ros.message.MessageIdentifier;
00026 
00027 import java.io.File;
00028 import java.util.Collection;
00029 import java.util.HashSet;
00030 import java.util.Map;
00031 import java.util.Map.Entry;
00032 
00036 public class MessageDefinitionFileProvider implements MessageDefinitionProvider {
00037 
00038   private final StringFileProvider stringFileProvider;
00039   private final Map<String, Collection<MessageIdentifier>> messageIdentifiers;
00040   private final Map<String, String> definitions;
00041 
00042   public MessageDefinitionFileProvider(StringFileProvider stringFileProvider) {
00043     this.stringFileProvider = stringFileProvider;
00044     messageIdentifiers = Maps.newConcurrentMap();
00045     definitions = Maps.newConcurrentMap();
00046   }
00047 
00048   private static String getParent(String filename) {
00049     return FilenameUtils.getFullPathNoEndSeparator(filename);
00050   }
00051 
00052   protected static String getParentBaseName(String filename) {
00053     return FilenameUtils.getBaseName(getParent(filename));
00054   }
00055 
00056   private static MessageIdentifier fileToMessageIdentifier(File file) {
00057     String filename = file.getAbsolutePath();
00058     String name = FilenameUtils.getBaseName(filename);
00059     String pkg = getParentBaseName(getParent(filename));
00060     return MessageIdentifier.of(pkg, name);
00061   }
00062 
00063   private void addDefinition(File file, String definition) {
00064     MessageIdentifier topicType = fileToMessageIdentifier(file);
00065     if (definitions.containsKey(topicType.getType())) {
00066       // First definition wins.
00067       return;
00068     }
00069     definitions.put(topicType.getType(), definition);
00070     if (!messageIdentifiers.containsKey(topicType.getPackage())) {
00071       messageIdentifiers.put(topicType.getPackage(), new HashSet<MessageIdentifier>());
00072     }
00073     messageIdentifiers.get(topicType.getPackage()).add(topicType);
00074   }
00075 
00081   public void update() {
00082     stringFileProvider.update();
00083     for (Entry<File, String> entry : stringFileProvider.getStrings().entrySet()) {
00084       addDefinition(entry.getKey(), entry.getValue());
00085     }
00086   }
00087 
00091   public void addDirectory(File directory) {
00092     stringFileProvider.addDirectory(directory);
00093   }
00094 
00095   @Override
00096   public Collection<String> getPackages() {
00097     return messageIdentifiers.keySet();
00098   }
00099 
00100   @Override
00101   public Collection<MessageIdentifier> getMessageIdentifiersByPackage(String pkg) {
00102     return messageIdentifiers.get(pkg);
00103   }
00104 
00105   @Override
00106   public String get(String type) {
00107     return definitions.get(type);
00108   }
00109 
00110   @Override
00111   public boolean has(String type) {
00112     return definitions.containsKey(type);
00113   }
00114 }


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