use of com.liferay.blade.api.JSPFile in project liferay-ide by liferay.
the class JSPTagMigrator method correctProblems.
@Override
public int correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
int corrected = 0;
List<Integer> autoCorrectTagOffsets = new ArrayList<>();
Stream<Problem> stream = problems.stream();
Class<? extends JSPTagMigrator> class1 = getClass();
String autoCorrectContext = "jsptag:" + class1.getName();
stream.filter(p -> p.autoCorrectContext.equals(autoCorrectContext)).map(p -> p.getStartOffset()).sorted();
for (Problem problem : problems) {
if ((problem.autoCorrectContext != null) && problem.autoCorrectContext.equals("jsptag:" + class1.getName())) {
autoCorrectTagOffsets.add(problem.getStartOffset());
}
}
Collections.sort(autoCorrectTagOffsets, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return i2.compareTo(i1);
}
});
IFile jspFile = getJSPFile(file);
if (ListUtil.isNotEmpty(autoCorrectTagOffsets)) {
IDOMModel domModel = null;
try {
domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(jspFile);
List<IDOMElement> elementsToCorrect = new ArrayList<>();
for (int startOffset : autoCorrectTagOffsets) {
IndexedRegion region = domModel.getIndexedRegion(startOffset);
if (region instanceof IDOMElement) {
IDOMElement element = (IDOMElement) region;
elementsToCorrect.add(element);
}
}
for (IDOMElement element : elementsToCorrect) {
domModel.aboutToChangeModel();
if (_newAttrValues.length == 1) {
element.setAttribute(_attrNames[0], _newAttrValues[0]);
corrected++;
} else if (_newAttrNames.length == 1) {
String value = element.getAttribute(_attrNames[0]);
element.removeAttribute(_attrNames[0]);
element.setAttribute(_newAttrNames[0], value);
corrected++;
} else if (ListUtil.isNotEmpty(_newTagNames)) {
String tagName = element.getTagName();
NamedNodeMap attributes = element.getAttributes();
NodeList childNodes = element.getChildNodes();
String nodeValue = element.getNodeValue();
String newTagName = "";
for (int i = 0; i < _tagNames.length; i++) {
if (_tagNames[i].equals(tagName)) {
newTagName = _newTagNames[i];
break;
}
}
if (newTagName.equals("")) {
continue;
}
Element newNode = element.getOwnerDocument().createElement(newTagName);
if (nodeValue != null) {
newNode.setNodeValue(nodeValue);
}
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
newNode.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
}
for (int i = 0; i < childNodes.getLength(); i++) {
Node childNode = childNodes.item(i);
newNode.appendChild(childNode.cloneNode(true));
}
element.getParentNode().replaceChild(newNode, element);
corrected++;
}
domModel.changedModel();
domModel.save();
}
} catch (Exception e) {
throw new AutoMigrateException("Unable to auto-correct", e);
} finally {
if (domModel != null) {
domModel.releaseFromEdit();
}
}
IPath location = jspFile.getLocation();
if ((corrected > 0) && !location.toFile().equals(file)) {
try (InputStream jspFileContent = jspFile.getContents()) {
Files.copy(jspFileContent, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
throw new AutoMigrateException("Error writing corrected file.", e);
}
}
}
return corrected;
}
Aggregations