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]);
}
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);
}
}
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);
}
}
}
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;
}
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();
}
Aggregations