001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tools.ant;
034:
035: import org.apache.tools.ant.BuildException;
036: import org.apache.tools.ant.Task;
037:
038: import java.io.File;
039: import java.io.FileOutputStream;
040: import java.io.IOException;
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.List;
044:
045: /**
046: * ANT task to build an index file (line seperated) for a script directory to be used by
047: * the ScriptingEngine
048: *
049: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
050: */
051: public class ScriptIndexBuilderTask extends Task {
052:
053: private String scriptDir = null;
054: private String indexFile = null;
055:
056: public void setScriptDir(String scriptDir) {
057: this .scriptDir = scriptDir;
058: }
059:
060: public void setIndexFile(String indexFile) {
061: this .indexFile = indexFile;
062: }
063:
064: public String getTaskName() {
065: return "scriptIndexBuilder";
066: }
067:
068: /**
069: * Rather primitive "write String to file" helper, returns <code>false</code> if failed
070: * Code copied from FxSharedUtils!
071: *
072: * @param contents the String to store
073: * @param file the file, if existing it will be overwritten
074: * @return if successful
075: */
076: public static boolean storeFile(String contents, File file) {
077: if (file.exists()) {
078: System.err.println("Warning: " + file.getName()
079: + " already exists! Overwriting!");
080: }
081: FileOutputStream out = null;
082: try {
083: out = new FileOutputStream(file);
084: out.write(contents.getBytes("UTF-8"));
085: out.flush();
086: out.close();
087: return true;
088: } catch (IOException e) {
089: System.err.println("Failed to store "
090: + file.getAbsolutePath() + ": " + e.getMessage());
091: return false;
092: } finally {
093: if (out != null) {
094: try {
095: out.close();
096: } catch (IOException e) {
097: //ignore
098: }
099: }
100: }
101: }
102:
103: public void execute() throws BuildException {
104: if (scriptDir == null || indexFile == null)
105: usage();
106: File index = new File(indexFile);
107: if (index.exists() && !index.isFile())
108: usage();
109: File idxDir = new File(index.getAbsolutePath()
110: .substring(
111: 0,
112: index.getAbsolutePath().lastIndexOf(
113: File.separatorChar)));
114: if (!idxDir.exists()) {
115: System.out.println("Creating [" + idxDir.getAbsolutePath()
116: + "]");
117: idxDir.mkdirs();
118: }
119: File scripts = new File(scriptDir);
120: if (!scripts.exists() || !scripts.isDirectory())
121: usage();
122: File[] files = scripts.listFiles();
123: List<String> scriptlist = new ArrayList<String>(files.length);
124: for (File f : files) {
125: if (!f.isFile() || ".".equals(f.getName())
126: || "..".equals(f.getName())
127: || index.getName().equals(f.getName()))
128: continue;
129: scriptlist.add(f.getName() + "|" + f.length());
130: }
131: Collections.sort(scriptlist);
132: StringBuilder sb = new StringBuilder(5000);
133: for (String s : scriptlist) {
134: sb.append(s).append("\n");
135: }
136: storeFile(sb.toString(), index);
137: System.out.println("Processed [" + scripts.getAbsolutePath()
138: + "] --> [" + index.getAbsolutePath() + "]");
139: }
140:
141: private void usage() {
142: throw new BuildException(getTaskName()
143: + ": need valid scriptDir and indexFile attributes!");
144: }
145:
146: }
|