001    /**
002     * Copyright (c) 2000-2012 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.seleniumbuilder;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.StringPool;
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.tools.servicebuilder.ServiceBuilder;
025    
026    import java.io.File;
027    
028    /**
029     * @author Michael Hashimoto
030     */
031    public class SeleniumBuilderFileUtil {
032    
033            public SeleniumBuilderFileUtil(String baseDir) {
034                    _baseDir = baseDir;
035            }
036    
037            public String getBaseDir() {
038                    return _baseDir;
039            }
040    
041            public String getClassName(String fileName) {
042                    int x = fileName.indexOf(CharPool.PERIOD);
043    
044                    String fileSuffix = fileName.substring(x + 1);
045    
046                    String classSuffix = StringUtil.upperCaseFirstLetter(fileSuffix);
047    
048                    return getClassName(fileName, classSuffix);
049            }
050    
051            public String getClassName(String fileName, String classSuffix) {
052                    int x = fileName.lastIndexOf(StringPool.SLASH);
053    
054                    String packagePath = StringUtil.replace(
055                            fileName.substring(0, x + 1), StringPool.SLASH, StringPool.PERIOD);
056    
057                    String simpleClassName = getName(fileName) + classSuffix;
058    
059                    return packagePath + simpleClassName;
060            }
061    
062            public String getName(String fileName) {
063                    int x = fileName.lastIndexOf(StringPool.SLASH);
064                    int y = fileName.indexOf(CharPool.PERIOD);
065    
066                    return fileName.substring(x + 1, y);
067            }
068    
069            public String getNormalizedContent(String fileName) throws Exception {
070                    String content = readFile(fileName);
071    
072                    if (content != null) {
073                            content = content.trim();
074                            content = StringUtil.replace(content, "\n", "");
075                            content = StringUtil.replace(content, "\r\n", "");
076                            content = StringUtil.replace(content, "\t", " ");
077                            content = content.replaceAll(" +", " ");
078                    }
079    
080                    return content;
081            }
082    
083            public Element getRootElement(String fileName) throws Exception {
084                    String content = getNormalizedContent(fileName);
085    
086                    Document document = SAXReaderUtil.read(content, true);
087    
088                    Element rootElement = document.getRootElement();
089    
090                    validateDocument(fileName, rootElement);
091    
092                    return rootElement;
093            }
094    
095            public String normalizeFileName(String fileName) {
096                    return StringUtil.replace(
097                            fileName, StringPool.BACK_SLASH, StringPool.SLASH);
098            }
099    
100            public String readFile(String fileName) throws Exception {
101                    return FileUtil.read(getBaseDir() + "/" + fileName);
102            }
103    
104            public void writeFile(String fileName, String content, boolean format)
105                    throws Exception {
106    
107                    File file = new File(getBaseDir() + "-generated/" + fileName);
108    
109                    if (format) {
110                            ServiceBuilder.writeFile(file, content);
111                    }
112                    else {
113                            System.out.println("Writing " + file);
114    
115                            FileUtil.write(file, content);
116                    }
117            }
118    
119            protected void validateDocument(String fileName, Element rootElement)
120                    throws Exception {
121    
122                    int x = fileName.lastIndexOf(StringPool.SLASH);
123                    int y = fileName.indexOf(CharPool.PERIOD);
124    
125                    String shortFileName = fileName.substring(x + 1, y);
126    
127                    if (fileName.endsWith(".path")) {
128                            Element headElement = rootElement.element("head");
129    
130                            Element titleElement = headElement.element("title");
131    
132                            String title = titleElement.getText();
133    
134                            if ((title == null) || !shortFileName.equals(title)) {
135                                    System.out.println(fileName + " has an <title>");
136                            }
137    
138                            Element bodyElement = rootElement.element("body");
139    
140                            Element tableElement = bodyElement.element("table");
141    
142                            Element theadElement = tableElement.element("thead");
143    
144                            Element trElement = theadElement.element("tr");
145    
146                            Element tdElement = trElement.element("td");
147    
148                            String tdText = tdElement.getText();
149    
150                            if ((tdText == null) || !shortFileName.equals(tdText)) {
151                                    System.out.println(fileName + " has an invalid <td>");
152                            }
153                    }
154                    else {
155                            String name = rootElement.attributeValue("name");
156    
157                            if ((name == null) || !name.equals(shortFileName)) {
158                                    System.out.println(fileName + " has an invalid name=\"\"");
159                            }
160                    }
161            }
162    
163            private String _baseDir;
164    
165    }