1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.ListUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.util.InitUtil;
32  
33  import java.io.File;
34  import java.io.StringReader;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.TreeMap;
40  
41  import org.apache.tools.ant.DirectoryScanner;
42  
43  /**
44   * <a href="TLDFormatter.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class TLDFormatter {
50  
51      public static void main(String[] args) {
52          try {
53              InitUtil.initWithSpring();
54  
55              _formatTLD();
56          }
57          catch (Exception e) {
58              e.printStackTrace();
59          }
60      }
61  
62      private static void _formatTLD() throws Exception {
63          String basedir = "./util-taglib/src/META-INF/";
64  
65          if (!FileUtil.exists(basedir)) {
66              return;
67          }
68  
69          List<File> list = new ArrayList<File>();
70  
71          DirectoryScanner ds = new DirectoryScanner();
72  
73          ds.setBasedir(basedir);
74          ds.setExcludes(new String[] {"**\\liferay-portlet-ext.tld"});
75          ds.setIncludes(new String[] {"**\\*.tld"});
76  
77          ds.scan();
78  
79          list.addAll(ListUtil.fromArray(ds.getIncludedFiles()));
80  
81          String[] files = list.toArray(new String[list.size()]);
82  
83          for (int i = 0; i < files.length; i++) {
84              File file = new File(basedir + files[i]);
85  
86              String content = FileUtil.read(file);
87  
88              Document document = SAXReaderUtil.read(
89                  new StringReader(
90                  StringUtil.replace(
91                      content, "xml/ns/j2ee/web-jsptaglibrary_2_0.xsd",
92                      "dtd/web-jsptaglibrary_1_2.dtd")));
93  
94              Element root = document.getRootElement();
95  
96              _sortElements(root, "tag", "name");
97  
98              List<Element> tagEls = root.elements("tag");
99  
100             for (Element tagEl : tagEls) {
101                 _sortElements(tagEl, "attribute", "name");
102 
103                 Element dynamicAttributesEl = tagEl.element(
104                     "dynamic-attributes");
105 
106                 if (dynamicAttributesEl != null) {
107                     dynamicAttributesEl.detach();
108 
109                     tagEl.add(dynamicAttributesEl);
110                 }
111             }
112 
113             String newContent = document.formattedString();
114 
115             int x = newContent.indexOf("<tlib-version");
116             int y = newContent.indexOf("</taglib>");
117 
118             newContent = newContent.substring(x, y);
119 
120             x = content.indexOf("<tlib-version");
121             y = content.indexOf("</taglib>");
122 
123             newContent =
124                 content.substring(0, x) + newContent + content.substring(y);
125 
126             if (!content.equals(newContent)) {
127                 FileUtil.write(file, newContent);
128 
129                 System.out.println(file);
130             }
131         }
132     }
133 
134     private static void _sortElements(
135         Element parentElement, String name, String sortBy) {
136 
137         Map<String, Element> map = new TreeMap<String, Element>();
138 
139         List<Element> elements = parentElement.elements(name);
140 
141         for (Element element : elements) {
142             map.put(element.elementText(sortBy), element);
143 
144             element.detach();
145         }
146 
147         for (Map.Entry<String, Element> entry : map.entrySet()) {
148             Element element = entry.getValue();
149 
150             parentElement.add(element);
151         }
152     }
153 
154 }