/** * 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; import com.liferay.portal.kernel.util.StringUtil; import com.liferay.source.formatter.checks.util.SourceUtil; import com.liferay.source.formatter.checks.util.XMLSourceUtil; import com.liferay.util.xml.Dom4jUtil; import java.util.List; import java.util.Map; import java.util.TreeMap; import org.dom4j.Document; import org.dom4j.Element; /** * @author Hugo Huijser */ public class XMLDDLStructuresFileCheck extends BaseFileCheck { @Override protected String doProcess( String fileName, String absolutePath, String content) throws Exception { if (fileName.endsWith("structures.xml")) { content = _formatDDLStructuresXML(content); } return content; } private String _formatDDLStructuresXML(String content) throws Exception { Document document = SourceUtil.readXML(content); Element rootElement = document.getRootElement(); XMLSourceUtil.sortElementsByChildElement( rootElement, "structure", "name"); List<Element> structureElements = rootElement.elements("structure"); for (Element structureElement : structureElements) { Element structureRootElement = structureElement.element("root"); _sortElementsByAttribute( structureRootElement, "dynamic-element", "name"); List<Element> dynamicElementElements = structureRootElement.elements("dynamic-element"); for (Element dynamicElementElement : dynamicElementElements) { Element metaDataElement = dynamicElementElement.element( "meta-data"); _sortElementsByAttribute(metaDataElement, "entry", "name"); } } return StringUtil.replace( Dom4jUtil.toString(document), "\"/>\n", "\" />\n"); } private void _sortElementsByAttribute( Element element, String elementName, String attributeName) { Map<String, Element> elementsMap = new TreeMap<>(); List<Element> elements = element.elements(); for (Element curElement : elements) { curElement.detach(); if (elementName.equals(curElement.getName())) { String attributeValue = curElement.attributeValue( attributeName); elementsMap.put(attributeValue, 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); } } } }