Search in sources :

Example 1 with JSPFile

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;
}
Also used : SearchResult(com.liferay.blade.api.SearchResult) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) IPath(org.eclipse.core.runtime.IPath) Node(org.w3c.dom.Node) ListUtil(com.liferay.ide.core.util.ListUtil) IFile(org.eclipse.core.resources.IFile) NamedNodeMap(org.w3c.dom.NamedNodeMap) JSPFile(com.liferay.blade.api.JSPFile) NodeList(org.w3c.dom.NodeList) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Files(java.nio.file.Files) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) File(java.io.File) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem) List(java.util.List) Stream(java.util.stream.Stream) Element(org.w3c.dom.Element) AutoMigrator(com.liferay.blade.api.AutoMigrator) StructuredModelManager(org.eclipse.wst.sse.core.StructuredModelManager) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Comparator(java.util.Comparator) Collections(java.util.Collections) InputStream(java.io.InputStream) IFile(org.eclipse.core.resources.IFile) NamedNodeMap(org.w3c.dom.NamedNodeMap) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) IPath(org.eclipse.core.runtime.IPath) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) Problem(com.liferay.blade.api.Problem)

Aggregations

AutoMigrateException (com.liferay.blade.api.AutoMigrateException)1 AutoMigrator (com.liferay.blade.api.AutoMigrator)1 JSPFile (com.liferay.blade.api.JSPFile)1 Problem (com.liferay.blade.api.Problem)1 SearchResult (com.liferay.blade.api.SearchResult)1 ListUtil (com.liferay.ide.core.util.ListUtil)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Files (java.nio.file.Files)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 StructuredModelManager (org.eclipse.wst.sse.core.StructuredModelManager)1 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)1 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)1