/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.liferay.source.formatter.checks.util; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.dom4j.Element; /** * @author Hugo Huijser */ public class XMLSourceUtil { public static void sortElementsByChildElement( Element element, String elementName, String childElementName) { Map<String, Element> elementsMap = new TreeMap<>(); List<Element> elements = element.elements(); for (Element curElement : elements) { curElement.detach(); if (elementName.equals(curElement.getName())) { String childElementValue = curElement.elementText( childElementName); elementsMap.put(childElementValue, curElement); } } for (Element curElement : elements) { if (elementName.equals(curElement.getName())) { break; } element.add(curElement); } for (Map.Entry<String, Element> entry : elementsMap.entrySet()) { Element curElement = entry.getValue(); element.add(curElement); } boolean foundLastElementWithElementName = false; for (int i = 0; i < elements.size(); i++) { Element curElement = elements.get(i); if (!foundLastElementWithElementName) { if (elementName.equals(curElement.getName()) && ((i + 1) < elements.size())) { Element nextElement = elements.get(i + 1); if (!elementName.equals(nextElement.getName())) { foundLastElementWithElementName = true; } } } else { element.add(curElement); } } } }