Search in sources :

Example 56 with IDOMElement

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

the class PortletContentAssistInfoProvider method doGetTextInfo.

@Override
protected String doGetTextInfo(IDOMElement portletNameElt) {
    IDOMElement portletElt = (IDOMElement) portletNameElt.getParentNode();
    StringBuilder buf = new StringBuilder();
    buf.append("<b>------------------------ Portlet ------------------------</b> ");
    // description
    buf.append("<br><b>Portlet name:</b> ");
    String portletName = _getTextContent(portletElt, "portlet-name");
    if (portletName != null) {
        buf.append(portletName);
    }
    // display-name
    buf.append("<br><b>Display name:</b> ");
    String displayName = _getTextContent(portletElt, "display-name");
    if (displayName != null) {
        buf.append(displayName);
    }
    // portlet-class
    buf.append("<br><b>Portlet class:</b> ");
    String portletClass = _getTextContent(portletElt, "portlet-class");
    if (portletClass != null) {
        buf.append(portletClass);
    }
    buf.append("<br><b>File:</b> ");
    buf.append(portletElt.getModel().getBaseLocation());
    return buf.toString();
}
Also used : IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 57 with IDOMElement

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

the class MVCPortletClassInPortletXML method correctProblems.

@Override
public int correctProblems(File file, List<Problem> problems) throws AutoMigrateException {
    int corrected = 0;
    IFile xmlFile = getXmlFile(file);
    IDOMModel xmlModel = null;
    if (xmlFile != null) {
        try {
            xmlModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForEdit(xmlFile);
            List<IDOMElement> elementsToCorrect = new ArrayList<>();
            for (Problem problem : problems) {
                if (_KEY.equals(problem.autoCorrectContext)) {
                    IndexedRegion region = xmlModel.getIndexedRegion(problem.startOffset);
                    if (region instanceof IDOMElement) {
                        IDOMElement element = (IDOMElement) region;
                        elementsToCorrect.add(element);
                    }
                }
            }
            for (IDOMElement element : elementsToCorrect) {
                xmlModel.aboutToChangeModel();
                _removeChildren(element);
                Text textContent = element.getOwnerDocument().createTextNode("com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet");
                element.appendChild(textContent);
                xmlModel.changedModel();
                corrected++;
            }
            xmlModel.save();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlModel != null) {
                xmlModel.releaseFromEdit();
            }
        }
    }
    IPath location = xmlFile.getLocation();
    if ((corrected > 0) && !location.toFile().equals(file)) {
        try (InputStream xmlFileContent = xmlFile.getContents()) {
            Files.copy(xmlFileContent, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            throw new AutoMigrateException("Error writing corrected file.", e);
        }
    }
    return corrected;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) AutoMigrateException(com.liferay.blade.api.AutoMigrateException) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Text(org.w3c.dom.Text) 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 58 with IDOMElement

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

the class LiferayMavenLegacyProjectUpdater method _getPluginsNode.

private Node _getPluginsNode(IDOMDocument document) {
    NodeList buildList = document.getElementsByTagName("build");
    if ((buildList != null) && (buildList.getLength() == 1)) {
        IDOMElement buildNode = (IDOMElement) buildList.item(0);
        NodeList pluginsList = buildNode.getElementsByTagName("plugins");
        if ((pluginsList == null) || (pluginsList.getLength() != 1)) {
            Element pluginsNode = document.createElement("plugins");
            buildNode.appendChild(pluginsNode);
            return pluginsNode;
        } else {
            return pluginsList.item(0);
        }
    } else {
        Element buildNode = document.createElement("build");
        Element pluginsNode = document.createElement("plugins");
        buildNode.appendChild(pluginsNode);
        Node node = document.getElementsByTagName("project").item(0);
        node.appendChild(buildNode);
        return pluginsNode;
    }
}
Also used : 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) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Example 59 with IDOMElement

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

the class LiferayMavenLegacyProjectUpdater method upgradePomFile.

@Override
public void upgradePomFile(IProject project, File outputFile) {
    IFile pomFile = project.getFile("pom.xml");
    IFile tempPomFile = project.getFile(".pom-tmp.xml");
    boolean needUpgrade = isNeedUpgrade(pomFile);
    if ((outputFile == null) && !needUpgrade) {
        return;
    }
    IDOMModel domModel = null;
    try {
        if (outputFile != null) {
            pomFile.copy(tempPomFile.getFullPath(), true, null);
            pomFile = tempPomFile;
        }
        domModel = (IDOMModel) StructuredModelManager.getModelManager().getModelForRead(pomFile);
        if (needUpgrade) {
            IDOMDocument document = domModel.getDocument();
            domModel.aboutToChangeModel();
            NodeList elements = document.getElementsByTagName("artifactId");
            if (elements != null) {
                for (int i = 0; i < elements.getLength(); i++) {
                    IDOMElement element = (IDOMElement) elements.item(i);
                    String textContent = element.getTextContent();
                    if (!CoreUtil.empty(textContent)) {
                        textContent = textContent.trim();
                        if (textContent.equals("liferay-maven-plugin")) {
                            IDOMElement liferayMavenPluginNode = (IDOMElement) element.getParentNode();
                            _removeChildren(liferayMavenPluginNode);
                            IDOMElement pluginsNode = (IDOMElement) liferayMavenPluginNode.getParentNode();
                            pluginsNode.removeChild(liferayMavenPluginNode);
                        } else if (textContent.equals("portal-service") || textContent.equals("util-java") || textContent.equals("util-bridges") || textContent.equals("util-taglib") || textContent.equals("util-slf4j")) {
                            IDOMElement dependencyElement = (IDOMElement) element.getParentNode();
                            String[] fixArtifactIdandVersion = _getFixedArtifactIdAndVersion(textContent);
                            _removeChildren(element);
                            Text artifactIdTextContent = element.getOwnerDocument().createTextNode(fixArtifactIdandVersion[0]);
                            element.appendChild(artifactIdTextContent);
                            NodeList versionList = dependencyElement.getElementsByTagName("version");
                            if ((versionList != null) && (versionList.getLength() == 1)) {
                                IDOMElement versionElement = (IDOMElement) versionList.item(0);
                                _removeChildren(versionElement);
                                Text versionTextContent = element.getOwnerDocument().createTextNode(fixArtifactIdandVersion[1]);
                                versionElement.appendChild(versionTextContent);
                            }
                        }
                    }
                }
            }
            if (_isProtletProject(project)) {
                _addCssBuilderPlugin(_getPluginsNode(document));
            }
            if (_isServiceBuilderProject(project)) {
                _addServiceBuilderPlugin(_getPluginsNode(document), project.getName());
                _addDependency(_getDependenciesNode(document), "biz.aQute.bnd", "biz.aQute.bnd.annotation", "3.2.0", "provided");
            }
            if (_isServiceBuildersubProject(project)) {
                _addDependency(_getDependenciesNode(document), "biz.aQute.bnd", "biz.aQute.bnd.annotation", "3.2.0", "provided");
            }
            if (_isThemeProject(project)) {
                _addProperties(_getPropertiesNode(document));
                _addMavenThemePlugins(_getPluginsNode(document));
            }
            _cleanBuildNode(document);
            domModel.changedModel();
        }
        if (FileUtil.exists(tempPomFile)) {
            tempPomFile.delete(true, null);
        }
        if (outputFile != null) {
            try (OutputStream fos = Files.newOutputStream(outputFile.toPath())) {
                domModel.save(fos);
            } catch (Exception e) {
            }
        } else {
            domModel.save();
        }
    } catch (Exception e) {
        LiferayMavenCore.logError("update pom file error", e);
    } finally {
        if (domModel != null) {
            domModel.releaseFromRead();
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) OutputStream(java.io.OutputStream) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Text(org.w3c.dom.Text) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

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