use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-xtend by eclipse.
the class AbstractRichTextValueConverter method toValue.
@Override
public String toValue(String string, INode node) {
if (string == null)
return null;
try {
String leadingTerminal = getLeadingTerminal();
if (string.length() <= leadingTerminal.length()) {
throw stringLiteralIsNotClosed(node, "");
}
String withoutLeadingTerminal = getWithoutLeadingTerminal(string);
String trailingTerminal = getTrailingTerminal();
if (withoutLeadingTerminal.endsWith(trailingTerminal)) {
String result = withoutLeadingTerminal.substring(0, withoutLeadingTerminal.length() - trailingTerminal.length());
return result;
}
List<String> trailingSubsequences = getTrailingSubsequences();
for (String subsequence : trailingSubsequences) {
if (withoutLeadingTerminal.endsWith(subsequence)) {
throw stringLiteralIsNotClosed(node, withoutLeadingTerminal.substring(0, withoutLeadingTerminal.length() - subsequence.length()));
}
}
throw stringLiteralIsNotClosed(node, withoutLeadingTerminal.substring(0, withoutLeadingTerminal.length()));
} catch (StringIndexOutOfBoundsException e) {
throw new ValueConverterException(e.getMessage(), node, e);
}
}
use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.
the class XtextLinkingService method getUsedGrammar.
private List<EObject> getUsedGrammar(Grammar grammar, INode node) {
try {
String grammarName = (String) valueConverterService.toValue("", "GrammarID", node);
if (grammarName != null) {
final ResourceSet resourceSet = grammar.eResource().getResourceSet();
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null)
rootElement = parseResult.getRootASTElement();
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof Grammar) {
Grammar otherGrammar = (Grammar) rootElement;
if (grammarName.equals(otherGrammar.getName())) {
if (resource instanceof DerivedStateAwareResource)
resource.getContents();
return Collections.<EObject>singletonList(otherGrammar);
}
}
}
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + "." + fileExtension);
URI normalizedURI = null;
if (resourceSet instanceof XtextResourceSet) {
XtextResourceSet set = (XtextResourceSet) resourceSet;
normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
} else {
normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
}
final Resource resource = resourceSet.getResource(normalizedURI, true);
if (!resource.getContents().isEmpty()) {
final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
if (grammarName.equals(usedGrammar.getName()))
return Collections.<EObject>singletonList(usedGrammar);
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
log.debug("Cannot load used grammar.", e);
return Collections.emptyList();
} catch (ValueConverterException e) {
log.debug("Cannot load used grammar.", e);
return Collections.emptyList();
}
}
use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.
the class AbstractInternalAntlrParser method handleValueConverterException.
protected void handleValueConverterException(ValueConverterException vce) {
hadErrors = true;
Exception cause = (Exception) vce.getCause();
if (vce != cause) {
IValueConverterErrorContext errorContext = createValueConverterErrorContext(vce);
currentError = syntaxErrorProvider.getSyntaxErrorMessage(errorContext);
if (vce.getNode() == null) {
appendError(currentNode.getLastChild());
} else {
appendError(vce.getNode());
}
} else {
throw new RuntimeException(vce);
}
}
use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.
the class DefaultEcoreElementFactory method set.
@Override
public void set(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
final EStructuralFeature structuralFeature = object.eClass().getEStructuralFeature(feature);
if (structuralFeature == null)
throw new IllegalArgumentException(object.eClass().getName() + "." + feature + " does not exist");
try {
final Object tokenValue = getTokenValue(value, ruleName, node);
checkNullForPrimitiveFeatures(structuralFeature, tokenValue, node);
object.eSet(structuralFeature, tokenValue);
} catch (ValueConverterWithValueException e) {
final Object tokenValue = e.getValue();
checkNullForPrimitiveFeatures(structuralFeature, tokenValue, node);
object.eSet(structuralFeature, tokenValue);
throw e;
} catch (ValueConverterException e) {
throw e;
} catch (NullPointerException e) {
log.warn(e.getMessage(), e);
throw new ValueConverterException("A NullPointerException occured. This indicates a missing value converter or a bug in its implementation.", node, e);
} catch (Exception e) {
throw new ValueConverterException(null, node, e);
}
}
use of org.eclipse.xtext.conversion.ValueConverterException in project xtext-core by eclipse.
the class DefaultEcoreElementFactory method add.
@Override
@SuppressWarnings("unchecked")
public void add(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
if (value == null)
return;
final EStructuralFeature structuralFeature = object.eClass().getEStructuralFeature(feature);
if (structuralFeature == null)
throw new IllegalArgumentException(object.eClass().getName() + "." + feature + " does not exist");
try {
if (value instanceof EObject) {
// containment lists are unique per-se and the tokenValue was created just a sec ago
((InternalEList<EObject>) object.eGet(structuralFeature)).addUnique((EObject) value);
} else {
final Object tokenValue = getTokenValue(value, ruleName, node);
checkNullForPrimitiveFeatures(structuralFeature, value, node);
((Collection<Object>) object.eGet(structuralFeature)).add(tokenValue);
}
} catch (ValueConverterWithValueException e) {
final Object tokenValue = e.getValue();
checkNullForPrimitiveFeatures(structuralFeature, value, node);
((Collection<Object>) object.eGet(structuralFeature)).add(tokenValue);
throw e;
} catch (ValueConverterException e) {
throw e;
} catch (NullPointerException e) {
log.error(e.getMessage(), e);
throw new ValueConverterException("A NullPointerException occured. This indicates a missing value converter or a bug in its implementation.", node, e);
} catch (Exception e) {
throw new ValueConverterException(null, node, e);
}
}
Aggregations