001    /**
002     * Copyright (c) 2000-2010 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.io.unsync.UnsyncStringReader;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.xml.Document;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.SAXReaderUtil;
024    import com.liferay.portal.util.InitUtil;
025    
026    import java.io.File;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.TreeMap;
032    
033    import org.apache.tools.ant.DirectoryScanner;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class TLDFormatter {
039    
040            public static void main(String[] args) {
041                    try {
042                            InitUtil.initWithSpring();
043    
044                            _formatTLD();
045                    }
046                    catch (Exception e) {
047                            e.printStackTrace();
048                    }
049            }
050    
051            private static void _formatTLD() throws Exception {
052                    String basedir = "./util-taglib/src/META-INF/";
053    
054                    if (!FileUtil.exists(basedir)) {
055                            return;
056                    }
057    
058                    List<String> list = new ArrayList<String>();
059    
060                    DirectoryScanner ds = new DirectoryScanner();
061    
062                    ds.setBasedir(basedir);
063                    ds.setExcludes(new String[] {"**\\liferay-portlet-ext.tld"});
064                    ds.setIncludes(new String[] {"**\\*.tld"});
065    
066                    ds.scan();
067    
068                    list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
069    
070                    String[] files = list.toArray(new String[list.size()]);
071    
072                    for (int i = 0; i < files.length; i++) {
073                            File file = new File(basedir + files[i]);
074    
075                            String content = FileUtil.read(file);
076    
077                            Document document = SAXReaderUtil.read(
078                                    new UnsyncStringReader(
079                                    StringUtil.replace(
080                                            content, "xml/ns/j2ee/web-jsptaglibrary_2_0.xsd",
081                                            "dtd/web-jsptaglibrary_1_2.dtd")));
082    
083                            Element root = document.getRootElement();
084    
085                            _sortElements(root, "tag", "name");
086    
087                            List<Element> tagEls = root.elements("tag");
088    
089                            for (Element tagEl : tagEls) {
090                                    _sortElements(tagEl, "attribute", "name");
091    
092                                    Element dynamicAttributesEl = tagEl.element(
093                                            "dynamic-attributes");
094    
095                                    if (dynamicAttributesEl != null) {
096                                            dynamicAttributesEl.detach();
097    
098                                            tagEl.add(dynamicAttributesEl);
099                                    }
100                            }
101    
102                            String newContent = document.formattedString();
103    
104                            int x = newContent.indexOf("<tlib-version");
105                            int y = newContent.indexOf("</taglib>");
106    
107                            newContent = newContent.substring(x, y);
108    
109                            x = content.indexOf("<tlib-version");
110                            y = content.indexOf("</taglib>");
111    
112                            newContent =
113                                    content.substring(0, x) + newContent + content.substring(y);
114    
115                            if (!content.equals(newContent)) {
116                                    FileUtil.write(file, newContent);
117    
118                                    System.out.println(file);
119                            }
120                    }
121            }
122    
123            private static void _sortElements(
124                    Element parentElement, String name, String sortBy) {
125    
126                    Map<String, Element> map = new TreeMap<String, Element>();
127    
128                    List<Element> elements = parentElement.elements(name);
129    
130                    for (Element element : elements) {
131                            map.put(element.elementText(sortBy), element);
132    
133                            element.detach();
134                    }
135    
136                    for (Map.Entry<String, Element> entry : map.entrySet()) {
137                            Element element = entry.getValue();
138    
139                            parentElement.add(element);
140                    }
141            }
142    
143    }