blob: c12d6cfdb44c91880997b7ab91d60141a0ab8eb0 [file] [log] [blame]
/*
* Copyright (c) 2012 Google Inc.
*
* All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
* Public License v1.0 which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.google.eclipse.protobuf.cdt.matching;
import static com.google.common.base.Objects.equal;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.eclipse.protobuf.cdt.util.ExtendedListIterator.newIterator;
import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.MESSAGE;
import static java.util.Collections.unmodifiableList;
import java.util.List;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.*;
import com.google.eclipse.protobuf.cdt.util.ExtendedIterator;
import com.google.eclipse.protobuf.model.util.ModelObjects;
import com.google.eclipse.protobuf.protobuf.Message;
import com.google.inject.Inject;
/**
* @author alruiz@google.com (Alex Ruiz)
*/
class MessageMatcherStrategy extends AbstractProtobufElementMatcherStrategy {
@Inject private ModelObjects modelObjects;
@Override public List<URI> matchingProtobufElementLocations(EObject root, ExtendedIterator<String> qualifiedName) {
List<URI> matches = newArrayList();
ExtendedIterator<EObject> contents = newIterator(root.eContents());
while (qualifiedName.hasNext()) {
String segment = qualifiedName.next();
while (contents.hasNext()) {
EObject o = contents.next();
if (!isSupported(o)) {
continue;
}
Message message = (Message) o;
if (equal(message.getName(), segment)) {
if (qualifiedName.wasLastListElementRetrieved()) {
// this is the last segment. This message is a perfect match.
matches.add(modelObjects.uriOf(message));
} else {
// keep looking for match.
matches.addAll(matchingProtobufElementLocations(message, qualifiedName.notRetrievedYet()));
}
}
if (segment.contains(NESTED_ELEMENT_SEPARATOR)) {
List<Message> matchingNestedElements = matchingNestedElements(message, segment);
if (qualifiedName.wasLastListElementRetrieved()) {
for (Message m : matchingNestedElements) {
matches.add(modelObjects.uriOf(m));
}
} else {
for (Message m : matchingNestedElements) {
matches.addAll(matchingProtobufElementLocations(m, qualifiedName.notRetrievedYet()));
}
}
}
}
}
return unmodifiableList(matches);
}
private List<Message> matchingNestedElements(Message message, String qualifiedName) {
List<Message> matches = newArrayList();
String messageName = message.getName();
if (qualifiedName.startsWith(messageName)) {
String rest = qualifiedName.substring(messageName.length());
if (rest.isEmpty()) {
matches.add(message);
}
else {
if (rest.startsWith(NESTED_ELEMENT_SEPARATOR)) {
rest = rest.substring(1);
}
for (EObject o : message.eContents()) {
if (!isSupported(o)) {
continue;
}
matches.addAll(matchingNestedElements((Message) o, rest));
}
}
}
return matches;
}
@Override public EClass supportedType() {
return MESSAGE;
}
}