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