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

The following examples show how to use org.eclipse.xtext.util.Strings#emptyIfNull() . 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: AbstractQuickfixTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected XtextResource getXtextResource(String model) {
	StringInputStream in = new StringInputStream(Strings.emptyIfNull(model));
	URI uri = URI.createURI(""); // creating an in-memory EMF Resource

	ResourceSet resourceSet = resourceSetProvider.get(project);
	Resource resource = injector.getInstance(IResourceFactory.class).createResource(uri);
	resourceSet.getResources().add(resource);

	try {
		resource.load(in, null);
		if (resource instanceof LazyLinkingResource) {
			((LazyLinkingResource) resource).resolveLazyCrossReferences(CancelIndicator.NullImpl);
		} else {
			EcoreUtil.resolveAll(resource);
		}
		return (XtextResource) resource;
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 2
Source File: AbstractConversionTable.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void updateControls() {
	final IExtraControlController ctrl = getController();
	final String preferenceName = getPreferenceKey();
	final String rawValue = Strings.emptyIfNull(ctrl.getValue(preferenceName));
	final List<Pair<String, String>> conversions = new ArrayList<>();
	ExtraLanguagePreferenceAccess.parseConverterPreferenceValue(rawValue,
		(source, target) -> conversions.add(new Pair<>(source, target)));
	setTypeConversions(conversions, false);
}
 
Example 3
Source File: XClosureImplCustom.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String toString() {
	String expressionAsString = Strings.emptyIfNull(expression == null ? null : expression.toString());
	if (isExplicitSyntax()) {
		return String.format("[%s | %s ]", Joiner.on(", ").join(getFormalParameters()), expressionAsString);
	} else {
		return String.format("[ %s ]", expressionAsString);
	}
}
 
Example 4
Source File: BuildData.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.18
 */
public BuildData(String projectName, ResourceSet resourceSet, ToBeBuilt toBeBuilt, QueuedBuildData queuedBuildData, boolean indexingOnly, Runnable buildRequestor, Set<String> removedProjects) {
	this.projectName = Strings.emptyIfNull(projectName);
	this.removedProjects = ImmutableSet.copyOf(removedProjects);
	this.resourceSet = resourceSet;
	this.toBeBuilt = toBeBuilt;
	this.queuedBuildData = queuedBuildData;
	this.indexingOnly = indexingOnly;
	this.sourceLevelURICache = new SourceLevelURICache();
	this.buildRequestor = buildRequestor;
}
 
Example 5
Source File: XtextValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkJavaPackageNamingConventions(GeneratedMetamodel metamodel){
	Severity severity = getIssueSeverities(getContext(), getCurrentObject()).getSeverity(INVALID_JAVAPACKAGE_NAME);
	if (severity == null || severity == Severity.IGNORE) {
		// Don't perform any check if the result is ignored
		return;
	}
	final String metamodelName = Strings.emptyIfNull(metamodel.getName());
	if (!Strings.equal(metamodelName, metamodelName.toLowerCase())) {
		addIssue("The generated metamodel name must not contain uppercase characters", metamodel, XtextPackage.eINSTANCE.getGeneratedMetamodel_Name(),
			INVALID_JAVAPACKAGE_NAME, metamodel.getName());
	}
}
 
Example 6
Source File: Xtext2EcoreTransformer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private void addGeneratedEPackage(GeneratedMetamodel generatedMetamodel) throws TransformationException {
	// we do not allow the same alias twice for generated metamodels
	String alias = Strings.emptyIfNull(generatedMetamodel.getAlias());
	if (generatedEPackages.containsKey(alias))
		throw new TransformationException(TransformationErrorCode.AliasForMetamodelAlreadyExists, "alias '" + alias
				+ "' already exists", generatedMetamodel);

	if (generatedMetamodel.getEPackage() == null)
		throw new TransformationException(TransformationErrorCode.UnknownMetaModelAlias, "Cannot create EPackage without NsURI.", generatedMetamodel);

	// instantiate EPackages for generated metamodel
	EPackage generatedEPackage = generatedMetamodel.getEPackage();
	generatedEPackages.put(alias, generatedEPackage);
	collectClassInfosOf(generatedEPackage, generatedMetamodel);
}
 
Example 7
Source File: StringWithOffset.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public StringWithOffset(String value) {
	this.value = Strings.emptyIfNull(value);
	this.offset = 0;
}
 
Example 8
Source File: SarlScriptExecutor.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public void setBootClassPath(String classpath) {
	this.bootClasspath = Strings.emptyIfNull(classpath);
}
 
Example 9
Source File: SarlScriptExecutor.java    From sarl with Apache License 2.0 4 votes vote down vote up
@Override
public void setClassPath(String classpath) {
	this.classpath = Strings.emptyIfNull(classpath);
}
 
Example 10
Source File: DocumentationFormatter.java    From sarl with Apache License 2.0 4 votes vote down vote up
private <T> void formatSinglelineComment(String indentationString, FormattedTextAccessor<T> backend) {
	final String indent = Strings.emptyIfNull(indentationString);
	final String comment = backend.getCommentText();
	// Compute the starting offset of the text inside the comment
	int offset = comment.indexOf(getSinglelineCommentPrefix());
	if (offset < 0) {
		backend.replace(0, 0, getSinglelineCommentPrefix());
		offset = 0;
	} else {
		offset += getSinglelineCommentPrefix().length();
	}
	final int endOffset = comment.length();
	T currentLine = backend.getFirstLine(backend.getCommentOffset());
	boolean firstLine = true;
	while (currentLine != null) {
		String lineText = backend.getLineText(currentLine);
		int lineOffset = backend.getLineOffset(currentLine);
		int lineLength = backend.getLineLength(currentLine);
		// Clamp the line text to the comment area.
		if (firstLine) {
			if (lineOffset < offset) {
				final int len = offset - lineOffset;
				lineText = lineText.substring(len);
				lineOffset += len;
				lineLength -= len;
			}
		} else if (lineOffset >= endOffset) {
			// After the end of comment
			backend.applyReplacements();
			return;
		} else {
			final String prefix;
			if (!startsWith(lineText, 0, getSinglelineCommentPrefix())) {
				prefix = indent + getSinglelineCommentPrefix();
			} else {
				prefix = indent;
			}
			backend.replace(lineOffset, 0, prefix);
		}
		// Skip the comment characters that corresponds to the Javadoc format: //[*-+=].
		int realCommentStart = 0;
		final Set<Character> specialChars = getSinglelineCommentSpecialChars();
		while (realCommentStart < lineLength && specialChars.contains(lineText.charAt(realCommentStart))) {
			++realCommentStart;
		}
		// Search for the first non whitespace
		int firstNonWhiteSpacePos = realCommentStart;
		while (firstNonWhiteSpacePos < lineLength && Character.isWhitespace(lineText.charAt(firstNonWhiteSpacePos))) {
			++firstNonWhiteSpacePos;
		}
		// Add whitespace at the beginning.
		if (firstNonWhiteSpacePos == lineLength) {
			// Empty comment
			if (realCommentStart < firstNonWhiteSpacePos) {
				backend.replace(realCommentStart + lineOffset, lineLength - realCommentStart, EMPTY_STR);
			}
		} else {
			final int expectedNbWhiteSpaces = getWhiteSpacesOnFirstLine();
			final int nbWhiteSpaces = firstNonWhiteSpacePos - realCommentStart;
			if (nbWhiteSpaces != expectedNbWhiteSpaces) {
				backend.replace(realCommentStart + lineOffset, nbWhiteSpaces, makeWhiteSpaces(expectedNbWhiteSpaces));
			}
			// Format the comment text
			formatLineText(
				lineText.substring(firstNonWhiteSpacePos, lineLength), true,
				new SubAccessor<>(backend, lineOffset + firstNonWhiteSpacePos));
			// Remove trailing whitespaces
			int endOfText = lineLength;
			while ((endOfText - 1) > firstNonWhiteSpacePos && Character.isWhitespace(lineText.charAt(endOfText - 1))) {
				--endOfText;
			}
			if (endOfText < lineLength) {
				backend.replace(endOfText + lineOffset, lineLength - endOfText, EMPTY_STR);
			}
		}
		firstLine = false;
		currentLine = backend.getNextLine(currentLine);
	}
	backend.applyReplacements();
}
 
Example 11
Source File: EClassifierInfo.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean addFeature(String featureName, EClassifierInfo featureType, boolean isMultivalue,
		boolean isContainment, AbstractElement parserElement) throws TransformationException {
	throw new UnsupportedOperationException("Cannot add feature " + featureName + " to simple datatype "
			+ Strings.emptyIfNull(this.getEClassifier().getName()));
}
 
Example 12
Source File: EClassifierInfo.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean addSupertype(EClassifierInfo superTypeInfo) {
	throw new UnsupportedOperationException("Cannot add supertype "
			+ Strings.emptyIfNull(superTypeInfo.getEClassifier().getName()) + " to simple datatype "
			+ Strings.emptyIfNull(this.getEClassifier().getName()));
}
 
Example 13
Source File: ConcurrentIssueRegistry.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private static String getMessage(LSPIssue issue) {
	String result = issue.getMessage();
	return Strings.emptyIfNull(result);
}
 
Example 14
Source File: RichStringProcessor.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Boolean caseLiteral(Literal object) {
	if (announced == null || announced != object.getLiteral()) {
		acceptor.announceNextLiteral(object.getLiteral());
		announced = object.getLiteral();
	}
	Line line = object.getLine();
	TextLine textLine = new TextLine(Strings.emptyIfNull(object.getLiteral().getValue()), object.getOffset(), object.getLength(), 0);
	CharSequence ws = textLine.getLeadingWhiteSpace();
	ProcessedRichString string = line.getRichString();
	boolean firstOrLast = string.getLines().get(0) == line || string.getLines().get(string.getLines().size()-1) == line;
	if (isTemplateLine(line)) {
		if (line.getParts().get(0) == object) {
			if (!firstOrLast) {
				boolean followedByOpening = false;
				if (line.getParts().size() >= 2) {
					LinePart next = line.getParts().get(1);
					if (next instanceof ForLoopStart || next instanceof IfConditionStart) {
						followedByOpening = true;
					}
				}
				if (!followedByOpening) {
					pushSemanticIndentation(indentationHandler.getTotalIndentation());
				} else {
					pushSemanticIndentation(ws);
				}
			}
		}
		announceTemplateText(textLine, object.getLiteral());
	} else {
		if (skipCount <= 1) {
			firstOrLast = false;
			if (skipCount == 0 && line.getParts().get(0) == object) {
				if (textLine.length() == ws.length()) {
					for(int i = 1; i < line.getParts().size(); i++) {
						if (line.getParts().get(i) instanceof Literal && !(line.getParts().get(i) instanceof LineBreak)) {
							Literal nextLiteralInSameLine = (Literal) line.getParts().get(i);
							TextLine nextLiteralLine = new TextLine(nextLiteralInSameLine.getLiteral().getValue(), nextLiteralInSameLine.getOffset(), nextLiteralInSameLine.getLength(), 0);
							CharSequence nextLeading = nextLiteralLine.getLeadingWhiteSpace();
							if (nextLeading.length() > 0) {
								ws = ws.toString() + nextLeading;
							}
							skipCount++;
							if (nextLeading.length() != nextLiteralLine.length()) {
								break;
							}
						} else {
							break;
						}
					}
					if (skipCount != 0) {
						pushSemanticIndentation(ws);
					} else {
						pushSemanticIndentation(ws);
						announceIndentation();
						announceSemanticText(textLine.subSequence(ws.length(), textLine.length()), object.getLiteral());
					}
				} else {
					pushSemanticIndentation(ws);
					announceIndentation();
					announceSemanticText(textLine.subSequence(ws.length(), textLine.length()), object.getLiteral());
				}
			} else {
				if (skipCount == 1) {
					skipCount--;
					announceIndentation();
					announceSemanticText(textLine.subSequence(ws.length(), textLine.length()), object.getLiteral());
				} else {
					announceSemanticText(textLine, object.getLiteral());
				}
			}
		} else {
			skipCount--;
		}
	}
	if (!firstOrLast && line.getParts().get(line.getParts().size() - 1) == object) {
		popIndentation();
	}
	computeNextPart(object);
	return Boolean.TRUE;
}
 
Example 15
Source File: CodeBuilderConfig.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Set the comment for the auto generated blocks.
 *
 * @param comment the text of the comment.
 */
public void setAutoGeneratedComment(String comment) {
	this.autoGeneratedComment = Strings.emptyIfNull(comment);
}
 
Example 16
Source File: AbstractConversionTable.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Change the source of the mapping.
 *
 * @param from the source.
 */
public void setSource(String from) {
	this.from = Strings.emptyIfNull(from);
}
 
Example 17
Source File: SarlBatchCompiler.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Change the extra languages' generators that should be enabled.
 *
 * @param identifiers the identifier, the identifiers (separated by {@link File#pathSeparator} of the
 *     extra languages' generator(s) to be enabled. If this parameter is {@code null}, all the extra
 *     languages' generator are disabled.
 * @since 0.8
 */
public void setExtraLanguageGenerators(String identifiers) {
	this.enabledExtraLanguageContributions = Strings.emptyIfNull(identifiers);
}
 
Example 18
Source File: AbstractConversionTable.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Constructor.
 *
 * @param from the source of the mapping.
 * @param to the target of the mapping.
 */
public ConversionMapping(String from, String to) {
	this.from = Strings.emptyIfNull(from);
	this.to = Strings.emptyIfNull(to);
}
 
Example 19
Source File: AbstractExternalHighlightingFragment2.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Replies the version of the language specification.
 *
 * @return the version.
 */
protected String getLanguageVersion() {
	return Strings.emptyIfNull(this.languageVersion);
}
 
Example 20
Source File: AbstractConversionTable.java    From sarl with Apache License 2.0 2 votes vote down vote up
/** Change the target of the mapping.
 *
 * @param to the target.
 */
public void setTarget(String to) {
	this.to = Strings.emptyIfNull(to);
}