Search in sources :

Example 51 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project liferay-ide by liferay.

the class LayoutTplUtil method findChildElementsByClassName.

public static IDOMElement[] findChildElementsByClassName(IDOMElement parentElement, String childElementTag, String className) {
    if ((parentElement == null) || !(parentElement.hasChildNodes())) {
        return null;
    }
    List<IDOMElement> childElements = new ArrayList<>();
    List<Element> divChildren = getChildElementsByTagName(parentElement, childElementTag);
    for (int i = 0; i < divChildren.size(); i++) {
        IDOMElement childDivElement = (IDOMElement) divChildren.get(i);
        if (hasClassName(childDivElement, className)) {
            childElements.add(childDivElement);
        }
    }
    return childElements.toArray(new IDOMElement[0]);
}
Also used : LayoutTplElement(com.liferay.ide.layouttpl.core.model.LayoutTplElement) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) ArrayList(java.util.ArrayList) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 52 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project liferay-ide by liferay.

the class LayoutTplElementsFactory method initPortletLayoutFromElement.

public void initPortletLayoutFromElement(PortletLayoutElement portletLayout, IDOMElement domElement) {
    if (domElement == null) {
        return;
    }
    String existingClassName = domElement.getAttribute("class");
    if (!CoreUtil.isNullOrEmpty(existingClassName) && existingClassName.contains(portletLayout.getClassName().content())) {
        portletLayout.setClassName(existingClassName);
    }
    IDOMElement[] portletColumnDOMElements = LayoutTplUtil.findChildElementsByClassName(domElement, "div", "portlet-column");
    for (IDOMElement portletColumnElement : portletColumnDOMElements) {
        PortletColumnElement portletColumn = portletLayout.getPortletColumns().insert();
        initPortletColumnFromElement(portletColumn, portletColumnElement);
    }
}
Also used : IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 53 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project liferay-ide by liferay.

the class LayoutTplElementsFactory method initPortletColumnFromElement.

public void initPortletColumnFromElement(PortletColumnElement portletColumn, IDOMElement domElement) {
    if (domElement == null) {
        return;
    }
    String existingClassName = domElement.getAttribute("class");
    if (!CoreUtil.isNullOrEmpty(existingClassName) && !existingClassName.equals(portletColumn.getClassName().content()) && existingClassName.contains("portlet-column")) {
        portletColumn.setClassName(existingClassName);
    }
    portletColumn.setWeight(LayoutTplUtil.getWeightValue(domElement, -1));
    IDOMElement[] portletLayoutDOMElements = LayoutTplUtil.findChildElementsByClassName(domElement, "div", "portlet-layout");
    if (ListUtil.isNotEmpty(portletLayoutDOMElements)) {
        for (IDOMElement portletLayoutDOMElement : portletLayoutDOMElements) {
            PortletLayoutElement portletLayout = portletColumn.getPortletLayouts().insert();
            initPortletLayoutFromElement(portletLayout, portletLayoutDOMElement);
        }
    }
}
Also used : IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 54 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement 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)

Example 55 with IDOMElement

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project liferay-ide by liferay.

the class PortletContentAssistInfoProvider method _getTextContent.

private String _getTextContent(IDOMElement element, String elementName) {
    NodeList nodes = element.getElementsByTagName(elementName);
    if (nodes.getLength() < 1) {
        return "";
    }
    Element childElement = (Element) nodes.item(0);
    Text text = (Text) childElement.getFirstChild();
    if (text == null) {
        return "";
    }
    return text.getData();
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) Text(org.w3c.dom.Text)

Aggregations

IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)59 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)20 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)19 Node (org.w3c.dom.Node)18 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)11 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)9 IDOMAttr (org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr)9 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)8 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)8 NamedNodeMap (org.w3c.dom.NamedNodeMap)8 ArrayList (java.util.ArrayList)7 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)6 List (java.util.List)5 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)5 Text (org.w3c.dom.Text)5 ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)4