001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.util.FileUtil;
025:
026: import java.io.File;
027:
028: /**
029: * <a href="CSSBuilder.java.html"><b><i>View Source</i></b></a>
030: *
031: * @author Brian Wing Shun Chan
032: *
033: */
034: public class CSSBuilder {
035:
036: public static void main(String[] args) {
037: if (args.length == 2) {
038: new CSSBuilder(args[0], args[1]);
039: } else {
040: throw new IllegalArgumentException();
041: }
042: }
043:
044: public CSSBuilder(String cssDir, String mergedFile) {
045: try {
046: File mainCssFile = new File(cssDir + "/main.css");
047:
048: if (!mainCssFile.exists()) {
049: System.out.println("Do not pack " + cssDir
050: + "/main.css because it does not exist.");
051:
052: return;
053: }
054:
055: String content = FileUtil.read(mainCssFile);
056:
057: content = replaceImports(cssDir, content);
058:
059: FileUtil.write(mergedFile, content);
060: } catch (Exception e) {
061: e.printStackTrace();
062: }
063: }
064:
065: public String replaceImports(String cssDir, String s)
066: throws Exception {
067: StringMaker sm = new StringMaker(s.length());
068:
069: int pos = 0;
070:
071: while (true) {
072: int x = s.indexOf(_BEGIN, pos);
073: int y = s.indexOf(_END, x + _BEGIN.length());
074:
075: if ((x == -1) || (y == -1)) {
076: sm.append(s.substring(pos, s.length()));
077:
078: break;
079: } else {
080: sm.append(s.substring(pos, x));
081:
082: String importFile = s.substring(x + _BEGIN.length(), y);
083:
084: String importContent = FileUtil.read(cssDir + "/"
085: + importFile);
086:
087: importContent = replaceImports(cssDir, importContent);
088:
089: sm.append(importContent);
090:
091: pos = y + _END.length();
092: }
093: }
094:
095: return sm.toString();
096: }
097:
098: private static final String _BEGIN = "@import url(";
099:
100: private static final String _END = ");";
101:
102: }
|