Java Code Examples for org.eclipse.xtext.util.Strings#split()

The following examples show how to use org.eclipse.xtext.util.Strings#split() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SerializerScopeProvider.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected IScope doGetTypeScope(XMemberFeatureCall call, JvmType type) {
	if (call.isPackageFragment()) {
		if (type instanceof JvmDeclaredType) {
			int segmentIndex = countSegments(call);
			String packageName = ((JvmDeclaredType) type).getPackageName();
			List<String> splitted = Strings.split(packageName, '.');
			String segment = splitted.get(segmentIndex);
			return new SingletonScope(EObjectDescription.create(segment, type), IScope.NULLSCOPE);
		}
		return IScope.NULLSCOPE;
	} else {
		if (type instanceof JvmDeclaredType && ((JvmDeclaredType) type).getDeclaringType() == null) {
			return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
		} else {
			XAbstractFeatureCall target = (XAbstractFeatureCall) call.getMemberCallTarget();
			if (target.isPackageFragment()) {
				String qualifiedName = type.getQualifiedName();
				int dot = qualifiedName.lastIndexOf('.');
				String simpleName = qualifiedName.substring(dot + 1);
				return new SingletonScope(EObjectDescription.create(simpleName, type), IScope.NULLSCOPE);
			} else {
				return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);	
			}
		}
	}
}
 
Example 2
Source File: NestedTypeLiteralScope.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected List<IEObjectDescription> getLocalElementsByName(QualifiedName name) {
	XAbstractFeatureCall featureCall = getFeatureCall();
	if (featureCall.isExplicitOperationCallOrBuilderSyntax())
		return Collections.emptyList();
	if (rawEnclosingType instanceof JvmDeclaredType && name.getSegmentCount() == 1) {
		String singleSegment = name.getFirstSegment();
		List<String> lookup = Collections.singletonList(singleSegment);
		if (singleSegment.indexOf('$') != -1) {
			lookup = Strings.split(singleSegment, '$');
		}
		JvmType result = findNestedType((JvmDeclaredType)rawEnclosingType, lookup.iterator());
		if (result != null) {
			IEObjectDescription description = EObjectDescription.create(name, result);
			return Collections.<IEObjectDescription>singletonList(new TypeLiteralDescription(description, enclosingType, isVisible(result)));
		}
	}
	return Collections.emptyList();
}
 
Example 3
Source File: NestedTypeAwareImportNormalizerWithDotSeparator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected QualifiedName resolveNonWildcard(QualifiedName relativeName) {
	if (relativeName.getSegmentCount()==1) {
		List<String> split = Strings.split(relativeName.getFirstSegment(), '$');
		if (split.size() == 0) {
			// relativeName may be just something like '$'
			return internalResolve(relativeName);
		}
		return internalResolve(QualifiedName.create(split));
	} else {
		StringBuilder concatenated = new StringBuilder();
		for(int i = 0; i < relativeName.getSegmentCount(); i++) {
			String segment = relativeName.getSegment(i);
			if (segment.indexOf('$') == -1) {
				if (concatenated.length() != 0) {
					concatenated.append('$');
				}
				concatenated.append(segment);
			} else {
				return null;
			}
		}
		return internalResolve(relativeName);
	}
}
 
Example 4
Source File: QualifiedNameValueConverter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String toString(String value) {
	if (value == null) {
		throw new ValueConverterException("'null' is not a valid qualified name value", null, null);
	}
	String valueDelimiter = getValueNamespaceDelimiter();
	List<String> segments = valueDelimiter.length() == 1 ? Strings.split(value, valueDelimiter.charAt(0)) : Strings.split(value, valueDelimiter);
	int size = segments.size();
	if (size == 1) {
		return delegateToString(segments.get(0));
	}
	StringBuilder result = new StringBuilder(value.length());
	String delimiterToUse = getStringNamespaceDelimiter();
	for (int i = 0; i < size; i++) {
		if (i != 0) {
			result.append(delimiterToUse);
		}
		if (i == size - 1 && getWildcardLiteral().equals(segments.get(i))) {
			result.append(getWildcardLiteral());
		} else {
			result.append(delegateToString(segments.get(i)));
		}
	}
	return result.toString();
}
 
Example 5
Source File: AbstractAntlrGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.7
 */
protected void normalizeTokens(String grammarFileName, Charset encoding) {
	String tokenFile = toTokenFileName(grammarFileName);
	String content = readFileIntoString(tokenFile, encoding);
	content = new NewlineNormalizer(getLineDelimiter()).normalizeLineDelimiters(content);
	List<String> splitted = Strings.split(content, getLineDelimiter());
	Collections.sort(splitted);
	content = Strings.concat(getLineDelimiter(), splitted) + getLineDelimiter();
	writeStringIntoFile(tokenFile, content, encoding);
}
 
Example 6
Source File: XtextFragmentProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public EObject getEObject(Resource resource, String fragment, IFragmentProvider.Fallback fallback) {
	if (!fragment.startsWith(PREFIX))
		return fallback.getEObject(fragment);
	String fragmentWithoutPrefix = fragment.substring(PREFIX.length());
	List<String> splitted = Strings.split(fragmentWithoutPrefix, '/');
	if (splitted.isEmpty()) {
		return fallback.getEObject(fragment);
	}
	String firstPart = splitted.get(0);
	Grammar grammar = null;
	for(EObject content: resource.getContents()) {
		if (content instanceof Grammar) {
			Grammar g = (Grammar) content;
			if (firstPart.equals(g.getName())) {
				grammar = g;
				if (splitted.size() == 1)
					return grammar;
				break;
			}
		}
	}
	if (splitted.size() == 2) {
		return GrammarUtil.findRuleForName(grammar, splitted.get(1));
	} else {
		return fallback.getEObject(fragment);
	}
}
 
Example 7
Source File: NestedTypesScope.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void doGetDescriptions(JvmType type, JvmType declarator, int index, List<IEObjectDescription> result) {
	String typeName = type.getQualifiedName('.');
	String declaratorName = declarator.getQualifiedName('.');
	int declaratorLength = declaratorName.length();
	String subName = typeName.substring(declaratorLength + 1);
	List<String> segments = Strings.split(subName, '.');
	result.add(EObjectDescription.create(QualifiedName.create(segments), type));
	result.add(EObjectDescription.create(subName.replace('.', '$'), type));
}
 
Example 8
Source File: StaticQualifierPrefixMatcher.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isCandidateMatchingPrefix(String name, String prefix) {
	if (delegate.isCandidateMatchingPrefix(name, prefix))
		return true;
	if (name.indexOf(delimiter) >= 0) { // assume a fqn if delimiter is present
		if (prefix.indexOf(delimiter) < 0) { 
			// prefix is without a dot - either namespace or last segment
			// namespace was checked prior by delegate
			String lastSegment = getLastSegment(name);
			if (lastSegment != null && delegate.isCandidateMatchingPrefix(lastSegment, prefix))
				return true;
		} else {
			List<String> splitPrefix = Strings.split(prefix, delimiter);
			if (splitPrefix.isEmpty())
				return false;
			List<String> splitName = Strings.split(name, delimiter);
			if (splitName.size() < splitPrefix.size()) {
				return false;
			}
			for(int i = 0; i < splitPrefix.size(); i++) {
				if (!delegate.isCandidateMatchingPrefix(splitName.get(i), splitPrefix.get(i)))
					return false;
			}
			return true;
		}
	}
	return false;
}
 
Example 9
Source File: ProcQualifiedNameConverter.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Splits the given string into segments and returns them as a {@link QualifiedName}.
 * 
 * @exception IllegalArgumentException
 *                if the input is empty or null.
 */
public QualifiedName toQualifiedName(String qualifiedNameAsString) {
	if (qualifiedNameAsString == null)
		throw new IllegalArgumentException("Qualified name cannot be null");
	if (qualifiedNameAsString.equals(""))
		throw new IllegalArgumentException("Qualified name cannot be empty");
	if (Strings.isEmpty(getDelimiter()))
		return QualifiedName.create(qualifiedNameAsString);
	List<String> segs = getDelimiter().length() == 1 ? Strings.split(qualifiedNameAsString, getDelimiter()
			.charAt(0)) : Strings.split(qualifiedNameAsString, getDelimiter());
	return QualifiedName.create(segs);
}
 
Example 10
Source File: AbstractConstructorScopeTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetElementsByName_04() {
	List<String> segments = Strings.split("org.eclipse.xtext.common.types.testSetups.NestedParameterizedTypes$WrappedCollection$WrappedIterator", '.');
	QualifiedName qualifiedName = QualifiedName.create(segments);
	Iterable<IEObjectDescription> descriptions = getConstructorScope().getElements(qualifiedName);
	for(IEObjectDescription description: descriptions) {
		assertEquals(qualifiedName, description.getName());
	}
	assertEquals(3, Iterables.size(descriptions));
}
 
Example 11
Source File: SarlBatchCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Change the classpath.
 *
 * <p>The classpath is a list the names of folders or jar files that are separated by {@link File#pathSeparator}.
 *
 * @param classpath the new classpath.
 */
public void setClassPath(String classpath) {
	this.classpath = new ArrayList<>();
	for (final String path : Strings.split(classpath, File.pathSeparator)) {
		this.classpath.add(normalizeFile(path));
	}
}
 
Example 12
Source File: LazyURIEncoder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * decodes the uriFragment
 * 
 * @param res the resource that contains the feature holder
 * @param uriFragment the fragment that should be decoded
 * @return the decoded information
 * @see LazyURIEncoder#encode(EObject, EReference, INode)
 */
public Triple<EObject, EReference, INode> decode(Resource res, String uriFragment) {
	if (isUseIndexFragment(res)) {
		return getLazyProxyInformation(res, uriFragment);
	}
	List<String> split = Strings.split(uriFragment, SEP);
	EObject source = resolveShortFragment(res, split.get(1));
	EReference ref = fromShortExternalForm(source.eClass(), split.get(2));
	INode compositeNode = NodeModelUtils.getNode(source);
	if (compositeNode==null)
		throw new IllegalStateException("Couldn't resolve lazy link, because no node model is attached.");
	INode textNode = getNode(compositeNode, split.get(3));
	return Tuples.create(source, ref, textNode);
}
 
Example 13
Source File: SARLQuickfixProvider.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Replies the qualified name for the given name.
 *
 * @param name the name.
 * @return the qualified name.
 */
public QualifiedName qualifiedName(String name) {
	if (!com.google.common.base.Strings.isNullOrEmpty(name)) {
		final List<String> segments = Strings.split(name, "."); //$NON-NLS-1$
		return QualifiedName.create(segments);
	}
	return QualifiedName.create();
}
 
Example 14
Source File: ExternalAntlrLexerFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private void normalizeTokens(String grammarFileName, Charset encoding) {
	String tokenFile = toTokenFileName(grammarFileName);
	String content = readFileIntoString(tokenFile, encoding);
	content = new NewlineNormalizer(getLineDelimiter()).normalizeLineDelimiters(content);
	List<String> splitted = Strings.split(content, getLineDelimiter());
	Collections.sort(splitted);
	content = Strings.concat(getLineDelimiter(), splitted) + getLineDelimiter();
	writeStringIntoFile(tokenFile, content, encoding);
}
 
Example 15
Source File: LazyURIEncoder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.4
 */
public INode getNode(EObject object, String fragment) {
	if (isUseIndexFragment(object.eResource())) {
		return decode(object.eResource(), fragment).getThird();
	}
	INode compositeNode = NodeModelUtils.getNode(object);
	if (compositeNode == null)
		throw new IllegalStateException("Couldn't resolve lazy link, because no node model is attached.");
	List<String> split = Strings.split(fragment, LazyURIEncoder.SEP);
	INode node = getNode(compositeNode, split.get(3));
	return node;
}
 
Example 16
Source File: AbstractConstructorScopeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testGetElementsByName_03() {
	List<String> segments = Strings.split("org.eclipse.xtext.common.types.testSetups.NestedParameterizedTypes.WrappedCollection.WrappedIterator", '.');
	QualifiedName qualifiedName = QualifiedName.create(segments);
	Iterable<IEObjectDescription> descriptions = getConstructorScope().getElements(qualifiedName);
	for(IEObjectDescription description: descriptions) {
		assertEquals(qualifiedName, description.getName());
	}
	assertEquals(3, Iterables.size(descriptions));
}
 
Example 17
Source File: NestedTypesScope.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void addDescriptions(JvmDeclaredType type, JvmType declarator, List<IEObjectDescription> result) {
	String typeName = type.getQualifiedName('.');
	String declaratorName = declarator.getQualifiedName('.');
	int declaratorLength = declaratorName.length();
	String subName = typeName.substring(declaratorLength + 1);
	List<String> segments = Strings.split(subName, '.');
	result.add(EObjectDescription.create(QualifiedName.create(segments), type));
	result.add(EObjectDescription.create(subName.replace('.', '$'), type));
	for(JvmDeclaredType nestedType: type.getAllNestedTypes()) {
		addDescriptions(nestedType, declarator, result);
	}
}
 
Example 18
Source File: FQNPrefixMatcher.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isCandidateMatchingPrefix(String name, String prefix) {
	if (delegate.isCandidateMatchingPrefix(name, prefix)) {
		return true;
	}
	String delimiter = getDelimiter();
	if (!Strings.isEmpty(delimiter) && name.indexOf(delimiter) >= 0) {
		if (prefix.indexOf(delimiter) < 0) {
			String lastSegment = getLastSegment(name, delimiter);
			if (lastSegment != null && delegate.isCandidateMatchingPrefix(lastSegment, prefix)) {
				return true;
			}
		} else {
			List<String> splitPrefix = Strings.split(prefix, delimiter);
			if (splitPrefix.isEmpty()) {
				return false;
			}
			List<String> splitName = Strings.split(name, delimiter);
			if (splitName.size() < splitPrefix.size()) {
				return false;
			}
			for (int i = 0; i < splitPrefix.size(); i++) {
				if (!delegate.isCandidateMatchingPrefix(splitName.get(i), splitPrefix.get(i))) {
					return false;
				}
			}
			return true;
		}
	}
	return false;
}
 
Example 19
Source File: XbaseSeverityConverter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns decoded delegation key or <code>null</code> if encodedValue can not be parsed.
 * @return {@link Pair} where getFirst() is delegationKey and getSecond() is the defaultSeverity.
 * @see XbaseSeverityConverter#encodeDefaultSeverity(String, String)
 */
public static Pair<String, String> decodeDelegationKey(String encodedValue) {
	List<String> split = Strings.split(encodedValue, DEFAULT_SEVERITY_SEPARATOR);
	if (split.size() == 2) {
		return Tuples.create(split.get(0), split.get(1));
	} else if (split.size() == 1) {
		return Tuples.create(split.get(0), SeverityConverter.SEVERITY_WARNING);
	} else {
		return null;
	}
}
 
Example 20
Source File: ProjectDescriptionLoader.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adjust the path value of the "main" property of the given package.json document as follows (in-place change of
 * the given JSON document):
 * <ol>
 * <li>if the path points to a folder, then "/index.js" will be appended,
 * <li>if neither a folder nor a file exist at the location the path points to and the path does not end in ".js",
 * then ".js" will be appended.
 * </ol>
 */
private void adjustMainPath(URI location, JSONDocument packageJSON) {
	JSONValue content = packageJSON.getContent();
	if (!(content instanceof JSONObject))
		return;
	JSONObject contentCasted = (JSONObject) content;
	NameValuePair mainProperty = JSONModelUtils.getNameValuePair(contentCasted, MAIN.name).orElse(null);
	if (mainProperty == null) {
		return;
	}
	String main = asNonEmptyStringOrNull(mainProperty.getValue());
	if (main == null) {
		return;
	}
	String[] mainSegments;
	if (File.separatorChar == '/') {
		List<String> splitted = Strings.split(main, '/');
		mainSegments = splitted.toArray(String[]::new);
	} else {
		mainSegments = windowsPattern.split(main);
	}

	URI locationWithMain = location.appendSegments(mainSegments);

	if (!main.endsWith(".js") && isFile(URIConverter.INSTANCE, locationWithMain.appendFileExtension("js"))) {
		main += ".js";
		mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
	} else if (isDirectory(URIConverter.INSTANCE, locationWithMain)) {
		if (!(main.endsWith("/") || main.endsWith(File.separator))) {
			main += "/";
		}
		main += "index.js";
		mainProperty.setValue(JSONModelUtils.createStringLiteral(main));
	}
}