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.kernel.xml.SAXReader;
023    import com.liferay.portal.tools.sourceformatter.XMLSourceProcessor;
024    import com.liferay.portal.util.FileImpl;
025    import com.liferay.portal.xml.SAXReaderImpl;
026    
027    import java.io.File;
028    
029    import java.util.Map;
030    
031    import org.apache.tools.ant.DirectoryScanner;
032    
033    /**
034     * @author Hugo Huijser
035     */
036    public class XMLAttributesSorter {
037    
038            public static void main(String[] args) {
039                    try {
040                            new XMLAttributesSorter(args);
041                    }
042                    catch (Exception e) {
043                            e.printStackTrace();
044                    }
045            }
046    
047            public XMLAttributesSorter(String[] args) throws Exception {
048                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
049    
050                    String fileName = arguments.get("sort.xml.file.name");
051    
052                    if (Validator.isNull(fileName) || fileName.startsWith("$")) {
053                            System.out.println("Specify file name");
054    
055                            return;
056                    }
057    
058                    /*if (!fileName.endsWith(".xml")) {
059                            System.out.println("Specify file with .xml extension");
060    
061                            return;
062                    }*/
063    
064                    String basedir = "./";
065    
066                    DirectoryScanner directoryScanner = new DirectoryScanner();
067    
068                    directoryScanner.setBasedir(basedir);
069                    directoryScanner.setIncludes(new String[] {"**\\" + fileName});
070    
071                    directoryScanner.scan();
072    
073                    String[] fileNames = directoryScanner.getIncludedFiles();
074    
075                    if (fileNames.length == 0) {
076                            System.out.println("No files found with name=" + fileName);
077    
078                            return;
079                    }
080    
081                    for (String fullFileName : fileNames) {
082                            File file = new File(basedir + fullFileName);
083    
084                            fullFileName = StringUtil.replace(
085                                    fullFileName, StringPool.BACK_SLASH, StringPool.SLASH);
086    
087                            String content = _fileUtil.read(file);
088    
089                            String newContent = sortAttributes(content);
090    
091                            newContent = XMLSourceProcessor.formatXML(newContent);
092    
093                            if (!content.equals(newContent)) {
094                                    _fileUtil.write(file, newContent);
095    
096                                    System.out.println(fullFileName);
097                            }
098                    }
099            }
100    
101            protected String sortAttributes(String content) throws Exception {
102                    Document document = _saxReader.read(content);
103    
104                    Element rootElement = document.getRootElement();
105    
106                    rootElement.sortAttributes(true);
107    
108                    return document.formattedString();
109            }
110    
111            private static FileImpl _fileUtil = FileImpl.getInstance();
112            private static SAXReader _saxReader = new SAXReaderImpl();
113    
114    }