boolean isPrimary = workingCopy.isPrimary();
JavaElementDeltaBuilder deltaBuilder = null;
PackageFragmentRoot root = (PackageFragmentRoot)workingCopy.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
boolean isIncluded = !Util.isExcluded(workingCopy);
IFile resource = (IFile)workingCopy.getResource();
IJavaProject project = root.getJavaProject();
if (isPrimary || (root.validateOnClasspath().isOK() && isIncluded && resource.isAccessible() && Util.isValidCompilationUnitName(workingCopy.getElementName(), project.getOption(JavaCore.COMPILER_SOURCE, true), project.getOption(JavaCore.COMPILER_COMPLIANCE, true)))) {
// force opening so that the delta builder can get the old info
if (!isPrimary && !primary.isOpen()) {
primary.open(null);
}
// creates the delta builder (this remembers the content of the cu) if:
// - it is not excluded
// - and it is not a primary or it is a non-consistent primary
if (isIncluded && (!isPrimary || !workingCopy.isConsistent())) {
deltaBuilder = new JavaElementDeltaBuilder(primary);
}
// save the cu
IBuffer primaryBuffer = primary.getBuffer();
if (!isPrimary) {
if (primaryBuffer == null) return;
char[] primaryContents = primaryBuffer.getCharacters();
boolean hasSaved = false;
try {
IBuffer workingCopyBuffer = workingCopy.getBuffer();
if (workingCopyBuffer == null) return;
primaryBuffer.setContents(workingCopyBuffer.getCharacters());
primaryBuffer.save(this.progressMonitor, this.force);
primary.makeConsistent(this);
hasSaved = true;
} finally {
if (!hasSaved){
// restore original buffer contents since something went wrong
primaryBuffer.setContents(primaryContents);
}
}
} else {
// for a primary working copy no need to set the content of the buffer again
primaryBuffer.save(this.progressMonitor, this.force);
primary.makeConsistent(this);
}
} else {
// working copy on cu outside classpath OR resource doesn't exist yet
String encoding = null;
try {
encoding = resource.getCharset();
}
catch (CoreException ce) {
// use no encoding
}
String contents = workingCopy.getSource();
if (contents == null) return;
try {
byte[] bytes = encoding == null
? contents.getBytes()
: contents.getBytes(encoding);
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
if (resource.exists()) {
resource.setContents(
stream,
this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY,
null);
} else {
resource.create(
stream,
this.force,
this.progressMonitor);
}
} catch (CoreException e) {