Skip to content

Commit

Permalink
Fix problems during saving of ST resources
Browse files Browse the repository at this point in the history
Use a copy of the internal library element for reconciling to avoid
affecting the current resource during save. Also perform a simple load
instead of a full reparse, which would unnecessarily unload and clear
the internal library element. Further, catch and wrap exceptions that
may occur during reconciling.
  • Loading branch information
mx990 authored and azoitl committed Jan 30, 2025
1 parent bd66d94 commit df0a7cc
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,20 @@ public void doSave(final OutputStream outputStream, final Map<?, ?> options) thr
if (isSavePlainST(options)) {
super.doSave(outputStream, options);
} else { // outputStream shall contain full XML for library element
final LibraryElement libraryElement = getInternalLibraryElement();
final LibraryElement internalLibraryElement = getInternalLibraryElement();
// perform checks
if (getContents().isEmpty() || libraryElement == null) {
throw new IllegalStateException(
if (getContents().isEmpty() || !isIncludeInternalLibraryElement() || internalLibraryElement == null) {
throw new IOException(
"The ST core resource must contain at least one element and have an associated library element"); //$NON-NLS-1$
}
// copy the internal library element
final LibraryElement libraryElement = EcoreUtil.copy(internalLibraryElement);
// reconcile
reconcile(libraryElement, options);
// chain save library element to outputStream
try {
final FordiacTypeResource typeResource = new FordiacTypeResource(uri);
typeResource.getContents().add(EcoreUtil.copy(libraryElement));
typeResource.getContents().add(libraryElement);
typeResource.save(outputStream, options);
} catch (final Exception e) {
throw new IOException("Error saving to library element: " + e.getMessage(), e); //$NON-NLS-1$
Expand All @@ -203,7 +205,7 @@ protected void reconcile(final LibraryElement libraryElement, final Map<?, ?> op
}
} else {
// reparse
reparse(text);
super.doLoad(new LazyStringInputStream(text, getEncoding()), null);
// partition
final var partition = partitioner.partition(this);
if (partition.isEmpty()) {
Expand All @@ -212,6 +214,10 @@ protected void reconcile(final LibraryElement libraryElement, final Map<?, ?> op
// reconcile
reconciler.reconcile(libraryElement, partition);
}
} catch (final IOException e) {
throw e;
} catch (final Exception e) {
throw new IOException("Cannot reconcile source", e); //$NON-NLS-1$
}
}

Expand Down

0 comments on commit df0a7cc

Please sign in to comment.