001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: BulkCopyTask.java 383219 2006-03-04 23:04:55Z michi $ */
020: package org.apache.lenya.cms.ant;
021:
022: import java.io.File;
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.StringTokenizer;
027:
028: import org.apache.lenya.xml.AntDocumentHelper;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031: import org.apache.tools.ant.types.Path;
032: import org.w3c.dom.Document;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Generate an XML containing modules.
037: */
038: public class GenerateModuleList extends Task {
039:
040: private Path moduleDirectories;
041:
042: private String moduleFile;
043:
044: /**
045: * @see org.apache.tools.ant.Task#execute()
046: */
047: public void execute() throws BuildException {
048:
049: List descriptors = new ArrayList();
050:
051: StringTokenizer st = new StringTokenizer(this .moduleDirectories
052: .toString(), File.pathSeparator);
053:
054: while (st.hasMoreTokens()) {
055: String moduleDir = st.nextToken();
056: File path = new File(moduleDir);
057:
058: if (path.isDirectory()) {
059: if (isModuleDirectory(path)) {
060: descriptors.add(path);
061: } else {
062: String[] dirs = path.list();
063: boolean matched = false;
064: for (int i = 0; i < dirs.length; i++) {
065: File moduleSubDir = new File(path, dirs[i]);
066: if (isModuleDirectory(moduleSubDir)) {
067: descriptors.add(moduleSubDir);
068: matched = true;
069: }
070: }
071: if (!matched) {
072: log("No module(s) found in directory [" + path
073: + "]");
074: }
075: }
076: } else {
077: throw new BuildException("No such directory: " + path);
078: }
079: }
080:
081: try {
082: Document doc = AntDocumentHelper.createDocument(NAMESPACE,
083: "modules", null);
084: File[] modules = (File[]) descriptors
085: .toArray(new File[descriptors.size()]);
086: HashMap keys = new HashMap();
087: for (int i = 0; i < modules.length; i++) {
088: Element element = doc.createElementNS(NAMESPACE,
089: "module");
090: String absolutePath = modules[i].getAbsolutePath();
091: String key = absolutePath.substring(absolutePath
092: .lastIndexOf("/") + 1);
093: if (keys.containsKey(key)) {
094: String error = "\nThe module ["
095: + key
096: + "] is already registered with the path ["
097: + keys.get(key)
098: + "]!\n"
099: + "You are trying to reuse the key for the path ["
100: + absolutePath
101: + "]\nPlease make sure you are using unique naming for modules.";
102: throw new BuildException(error);
103: }
104: keys.put(key, absolutePath);
105: element.setAttribute("src", absolutePath);
106: doc.getDocumentElement().appendChild(element);
107: }
108: File file = new File(this .moduleFile.replace('/',
109: File.separatorChar));
110: AntDocumentHelper.writeDocument(doc, file);
111: } catch (Exception e) {
112: throw new BuildException(e);
113: }
114:
115: }
116:
117: protected static final String NAMESPACE = "http://apache.org/lenya/module-list/1.0";
118:
119: protected boolean isModuleDirectory(File path) {
120: File moduleFile = new File(path, "config" + File.separator
121: + "module.xml");
122: return moduleFile.isFile();
123: }
124:
125: /**
126: * Set the module source directories.
127: *
128: * @param dirs
129: * The module directories.
130: */
131: public void setModuleDirs(Path dirs) {
132: this .moduleDirectories = dirs;
133: }
134:
135: /**
136: * Set the file to generate.
137: *
138: * @param file
139: * The file.
140: */
141: public void setModuleFile(String file) {
142: this.moduleFile = file;
143: }
144:
145: }
|