Yaml->Props file rename issue on Win fixed
This commit is contained in:
@@ -11,8 +11,7 @@
|
||||
package org.springframework.ide.eclipse.boot.refactoring;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
@@ -30,6 +29,7 @@ import org.eclipse.ltk.core.refactoring.RefactoringStatus;
|
||||
import org.eclipse.ltk.core.refactoring.TextFileChange;
|
||||
import org.eclipse.ltk.core.refactoring.resource.RenameResourceChange;
|
||||
import org.eclipse.text.edits.ReplaceEdit;
|
||||
import org.springsource.ide.eclipse.commons.frameworks.core.util.IOUtil;
|
||||
import org.springsource.ide.eclipse.commons.livexp.util.ExceptionUtil;
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
@@ -86,80 +86,70 @@ public class ConvertYamlToPropertiesRefactoring extends Refactoring {
|
||||
public RefactoringStatus checkFinalConditions(IProgressMonitor pm)
|
||||
throws CoreException, OperationCanceledException {
|
||||
RefactoringStatus status = new RefactoringStatus();
|
||||
String yamlContent = null;
|
||||
try {
|
||||
if (hasComments(yamlFile)) {
|
||||
status.merge(RefactoringStatus.createWarningStatus("The yaml file has comments, which will be lost in the refactoring!"));
|
||||
}
|
||||
yamlContent = IOUtil.toString(yamlFile.getContents());
|
||||
} catch (Exception e) {
|
||||
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems reading file: "+yamlFile.getFullPath())));
|
||||
return status;
|
||||
}
|
||||
|
||||
try (InputStream content = yamlFile.getContents()) {
|
||||
for (Object d : new Yaml().loadAll(yamlFile.getContents())) {
|
||||
if (d instanceof Map) {
|
||||
// Add doc divider if not empty
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, ?> o = (Map<String, ?>) d;
|
||||
try {
|
||||
YamlToPropertiesConverter converter = new YamlToPropertiesConverter(o);
|
||||
Properties props = converter.getProperties();
|
||||
StringWriter write = new StringWriter();
|
||||
props.store(write, null);
|
||||
write.flush();
|
||||
write.close();
|
||||
if (!propsContent.isEmpty()) {
|
||||
propsContent.append("#---\n");
|
||||
}
|
||||
// Skip over the date header. Comments are not present but date header is.
|
||||
if (write.getBuffer().charAt(0) == '#') {
|
||||
int idx = write.getBuffer().indexOf("\n");
|
||||
this.propsContent.append(idx >= 0 && idx < write.getBuffer().length() ? write.getBuffer().substring(idx + 1) : write.getBuffer().toString());
|
||||
} else {
|
||||
this.propsContent.append(write.getBuffer().toString());
|
||||
}
|
||||
status.merge(converter.getStatus());
|
||||
} catch (IOException e) {
|
||||
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems writing to .properties file: "+propsFile.getFullPath())));
|
||||
}
|
||||
} else if (d == null) {
|
||||
if (hasComments(yamlContent)) {
|
||||
status.merge(RefactoringStatus.createWarningStatus("The yaml file has comments, which will be lost in the refactoring!"));
|
||||
}
|
||||
|
||||
for (Object d : new Yaml().loadAll(new StringReader(yamlContent))) {
|
||||
if (d instanceof Map) {
|
||||
// Add doc divider if not empty
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, ?> o = (Map<String, ?>) d;
|
||||
try {
|
||||
YamlToPropertiesConverter converter = new YamlToPropertiesConverter(o);
|
||||
Properties props = converter.getProperties();
|
||||
StringWriter write = new StringWriter();
|
||||
props.store(write, null);
|
||||
write.flush();
|
||||
write.close();
|
||||
if (!propsContent.isEmpty()) {
|
||||
propsContent.append("#---\n");
|
||||
}
|
||||
// Skip over the date header. Comments are not present but date header is.
|
||||
if (write.getBuffer().charAt(0) == '#') {
|
||||
int idx = write.getBuffer().indexOf("\n");
|
||||
this.propsContent.append(idx >= 0 && idx < write.getBuffer().length() ? write.getBuffer().substring(idx + 1) : write.getBuffer().toString());
|
||||
} else {
|
||||
this.propsContent.append(write.getBuffer().toString());
|
||||
}
|
||||
status.merge(converter.getStatus());
|
||||
} catch (IOException e) {
|
||||
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems writing to .properties file: "+propsFile.getFullPath())));
|
||||
}
|
||||
} else if (d == null) {
|
||||
if (!propsContent.isEmpty()) {
|
||||
propsContent.append("#---\n");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
status.merge(RefactoringStatus.create(ExceptionUtil.status(e, "Problems parsing as a .yaml file: "+yamlFile.getFullPath())));
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
private boolean hasComments(IFile yamlFile) throws Exception {
|
||||
InputStream is = null;
|
||||
try {
|
||||
is = yamlFile.getContents();
|
||||
LoaderOptions loaderOptions = new LoaderOptions();
|
||||
loaderOptions.setProcessComments(true);
|
||||
boolean hasComments = false;
|
||||
for (Event e : new Yaml(loaderOptions).parse(new InputStreamReader(is))) {
|
||||
if (e instanceof StreamEndEvent) {
|
||||
inputTextLen = e.getEndMark().getIndex();
|
||||
}
|
||||
if (!hasComments && e instanceof CommentEvent ce) {
|
||||
if (ce.getCommentType() == CommentType.BLANK_LINE) {
|
||||
// document separator
|
||||
} else {
|
||||
hasComments = true;
|
||||
}
|
||||
private boolean hasComments(String yamlContent) {
|
||||
LoaderOptions loaderOptions = new LoaderOptions();
|
||||
loaderOptions.setProcessComments(true);
|
||||
boolean hasComments = false;
|
||||
for (Event e : new Yaml(loaderOptions).parse(new StringReader(yamlContent))) {
|
||||
if (e instanceof StreamEndEvent) {
|
||||
inputTextLen = e.getEndMark().getIndex();
|
||||
}
|
||||
if (!hasComments && e instanceof CommentEvent ce) {
|
||||
if (ce.getCommentType() == CommentType.BLANK_LINE) {
|
||||
// document separator
|
||||
} else {
|
||||
hasComments = true;
|
||||
}
|
||||
}
|
||||
return hasComments;
|
||||
} catch (Throwable t) {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return hasComments;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user