blob: ecc26015458b306e8a29d48524ba1b48266ec3f0 [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.naming;
import static org.eclipse.xtext.util.Strings.isEmpty;
import java.util.regex.Pattern;
import org.eclipse.xtext.naming.IQualifiedNameConverter.DefaultImpl;
import org.eclipse.xtext.naming.*;
import com.google.eclipse.protobuf.util.Strings;
import com.google.inject.Singleton;
/**
* Provides support for multi-line qualified names.
*
* @author alruiz@google.com (Alex Ruiz)
*/
@Singleton public class ProtobufQualifiedNameConverter extends DefaultImpl {
private final Pattern delimiterPattern = Pattern.compile(delimiterPlusWhitespace());
private String delimiterPlusWhitespace() {
return "\\s*" + Pattern.quote(getDelimiter()) + "\\s*";
}
/**
* Splits the given {@code String} into segments and returns them as a <code>{@link QualifiedName}</code>.
* @param s the given input.
* @throws IllegalArgumentException if the input is empty or {@code null}.
*/
@Override public QualifiedName toQualifiedName(String s) {
if (isEmpty(s)) {
throw new IllegalArgumentException("Qualified name cannot be null or empty");
}
String withoutLineBreaks = Strings.removeLineBreaksFrom(s);
String[] segments = delimiterPattern.split(withoutLineBreaks);
return QualifiedName.create(segments);
}
}