001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.tools.sourceformatter.XMLSourceProcessor;
023    import com.liferay.portal.util.FileImpl;
024    import com.liferay.portal.xml.SAXReaderImpl;
025    
026    import java.io.File;
027    
028    import java.util.Map;
029    
030    import org.apache.tools.ant.DirectoryScanner;
031    
032    /**
033     * @author Hugo Huijser
034     */
035    public class XMLAttributesSorter {
036    
037            public static void main(String[] args) {
038                    try {
039                            new XMLAttributesSorter(args);
040                    }
041                    catch (Exception e) {
042                            e.printStackTrace();
043                    }
044            }
045    
046            public XMLAttributesSorter(String[] args) throws Exception {
047                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
048    
049                    String fileName = arguments.get("sort.xml.file.name");
050    
051                    if (Validator.isNull(fileName) || fileName.startsWith("$")) {
052                            System.out.println("Specify file name");
053    
054                            return;
055                    }
056    
057                    if (!fileName.endsWith(".xml")) {
058                            System.out.println("Specify file with .xml extension");
059    
060                            return;
061                    }
062    
063                    String basedir = "./";
064    
065                    DirectoryScanner directoryScanner = new DirectoryScanner();
066    
067                    directoryScanner.setBasedir(basedir);
068                    directoryScanner.setIncludes(new String[] {"**\\" + fileName});
069    
070                    directoryScanner.scan();
071    
072                    String[] fileNames = directoryScanner.getIncludedFiles();
073    
074                    if (fileNames.length == 0) {
075                            System.out.println("No files found with name=" + fileName);
076    
077                            return;
078                    }
079    
080                    for (String fullFileName : fileNames) {
081                            File file = new File(basedir + fullFileName);
082    
083                            fullFileName = StringUtil.replace(
084                                    fullFileName, StringPool.BACK_SLASH, StringPool.SLASH);
085    
086                            String content = _fileUtil.read(file);
087    
088                            String newContent = sortAttributes(content);
089    
090                            newContent = XMLSourceProcessor.formatXML(newContent);
091    
092                            if (!content.equals(newContent)) {
093                                    _fileUtil.write(file, newContent);
094    
095                                    System.out.println(fullFileName);
096                            }
097                    }
098            }
099    
100            protected String sortAttributes(String content) throws Exception {
101                    Document document = _saxReaderUtil.read(content);
102    
103                    Element rootElement = document.getRootElement();
104    
105                    rootElement.sortAttributes(true);
106    
107                    return document.formattedString();
108            }
109    
110            private static FileImpl _fileUtil = FileImpl.getInstance();
111            private static SAXReaderImpl _saxReaderUtil = SAXReaderImpl.getInstance();
112    
113    }