001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.metadata.ant;
018:
019: import java.io.*;
020: import java.util.Date;
021: import java.util.Iterator;
022:
023: import org.apache.tools.ant.BuildException;
024: import org.apache.tools.ant.DirectoryScanner;
025: import org.apache.tools.ant.Task;
026: import org.apache.tools.ant.types.FileSet;
027: import org.compass.core.config.binding.XmlMetaDataBinding;
028: import org.compass.core.config.CompassSettings;
029: import org.compass.core.metadata.Alias;
030: import org.compass.core.metadata.MetaData;
031: import org.compass.core.metadata.impl.DefaultCompassMetaData;
032: import org.compass.core.metadata.impl.DefaultMetaDataGroup;
033: import org.compass.core.util.DTDEntityResolver;
034: import org.compass.core.util.config.ConfigurationHelper;
035: import org.compass.core.util.config.XmlConfigurationHelperBuilder;
036: import org.xml.sax.EntityResolver;
037:
038: /**
039: * @author kimchy
040: */
041: public class MetaDataTask extends Task {
042:
043: private File destDir;
044:
045: private FileSet fileset;
046:
047: public void setDestDir(File destDir) {
048: this .destDir = destDir;
049: }
050:
051: public void addFileset(FileSet fileset) {
052: this .fileset = fileset;
053: }
054:
055: public void execute() throws BuildException {
056: EntityResolver entityResolver = new DTDEntityResolver();
057:
058: DirectoryScanner ds = fileset.getDirectoryScanner(getProject());
059: String[] files = ds.getIncludedFiles();
060: File dir = fileset.getDir(getProject());
061: for (int i = 0; i < files.length; i++) {
062: if (!files[i].endsWith("cmd.xml")) {
063: continue;
064: }
065: File srcFile = new File(dir, files[i]);
066: ConfigurationHelper conf;
067: try {
068: XmlConfigurationHelperBuilder builder = new XmlConfigurationHelperBuilder();
069: builder.setEntityResolver(entityResolver);
070: conf = builder.buildFromFile(srcFile);
071: } catch (Exception e) {
072: throw new BuildException(e.getMessage(), e);
073: }
074:
075: DefaultCompassMetaData compassMetaData = new DefaultCompassMetaData();
076: XmlMetaDataBinding metaDataBinding = new XmlMetaDataBinding();
077: metaDataBinding.setUpBinding(null, compassMetaData,
078: new CompassSettings());
079: metaDataBinding.doAddConfiguration(conf);
080: for (Iterator it = compassMetaData.groupsIterator(); it
081: .hasNext();) {
082: DefaultMetaDataGroup group = (DefaultMetaDataGroup) it
083: .next();
084: String className = group.getId();
085: className = className.substring(0, 1).toUpperCase()
086: + className.substring(1, className.length());
087: File javaFile = new File(destDir, files[i]);
088: // create the parent dir if not exists
089: if (!javaFile.getParentFile().exists()) {
090: javaFile.mkdirs();
091: }
092: javaFile = new File(javaFile.getParentFile(), className
093: + ".java");
094: String packageName = "";
095: File tmpFile = javaFile.getParentFile();
096: while (!destDir.getAbsolutePath().equals(
097: tmpFile.getAbsolutePath())) {
098: packageName = tmpFile.getName() + "." + packageName;
099: tmpFile = tmpFile.getParentFile();
100: }
101: packageName = packageName.substring(0, packageName
102: .length() - 1);
103: try {
104: System.out
105: .println("Generating common meta data for ["
106: + srcFile.getAbsolutePath() + "]");
107: PrintWriter writer = new PrintWriter(
108: new OutputStreamWriter(
109: new FileOutputStream(javaFile)));
110: PrintHelper printHelper = new PrintHelper(writer);
111: printHelper
112: .print("// Auto generated by compass meta-data ant task at: "
113: + new Date());
114: printHelper.print("package " + packageName + ";");
115: printHelper.emptyLine();
116: // writer.println("import org.compass.core.Property;");
117: printHelper.printJavaDoc(group.getDescription());
118: printHelper.startClass(false, className);
119: printHelper.printJavaDoc(group.getDescription());
120: printHelper.startClass(true, "group");
121: printHelper.printConstant("Id", group.getId());
122: printHelper.printConstant("DispayName", group
123: .getDisplayName());
124: printHelper.printConstant("Uri", group.getUri());
125: printHelper.endClass();
126: printHelper.startClass(true, "alias");
127: for (Iterator aliasIt = group.aliasIterator(); aliasIt
128: .hasNext();) {
129: Alias alias = (Alias) aliasIt.next();
130: printHelper
131: .printJavaDoc(alias.getDescription());
132: printHelper.startClass(true, alias.getId());
133: printHelper.printConstant("Id", alias.getId());
134: printHelper.printConstant("Name", alias
135: .getName());
136: printHelper.printConstant("DisplayName", alias
137: .getDisplayName());
138: printHelper
139: .printConstant("Uri", alias.getUri());
140: printHelper.printConstant("GroupId", alias
141: .getGroup().getId());
142: printHelper.endClass();
143: }
144: printHelper.endClass();
145: printHelper.startClass(true, "metaData");
146: for (Iterator metaDataIt = group.metaDataIterator(); metaDataIt
147: .hasNext();) {
148: MetaData metaData = (MetaData) metaDataIt
149: .next();
150: printHelper.printJavaDoc(metaData
151: .getDescription());
152: printHelper.startClass(true, metaData.getId());
153: printHelper.printConstant("Id", metaData
154: .getId());
155: printHelper.printConstant("Name", metaData
156: .getName());
157: printHelper.printConstant("DisplayName",
158: metaData.getDisplayName());
159: printHelper.printConstant("Uri", metaData
160: .getUri());
161: printHelper.printConstant("GroupId", metaData
162: .getGroup().getId());
163: if (metaData.getFormat() != null) {
164: printHelper.printConstant("Format",
165: metaData.getFormat());
166: }
167: printHelper.endClass();
168: }
169: printHelper.endClass();
170:
171: printHelper.endClass();
172: printHelper.close();
173: } catch (FileNotFoundException e) {
174: throw new BuildException(
175: "Failed to write to file: "
176: + javaFile.getAbsolutePath(), e);
177: }
178: }
179: }
180: }
181:
182: private static final class PrintHelper {
183: private PrintWriter writer;
184:
185: private int indent = 0;
186:
187: public PrintHelper(PrintWriter writer) {
188: this .writer = writer;
189: }
190:
191: public void close() {
192: writer.flush();
193: writer.close();
194: }
195:
196: public void emptyLine() {
197: writer.println();
198: }
199:
200: public void startClass(boolean isStatic, String className) {
201: writeIndent();
202: writer.print("public ");
203: if (isStatic) {
204: writer.print("static ");
205: }
206: className = className.substring(0, 1).toUpperCase()
207: + className.substring(1, className.length());
208: writer.print("final class " + className + " {");
209: emptyLine();
210: indent++;
211: }
212:
213: public void endClass() {
214: indent--;
215: writeIndent();
216: writer.println("}");
217: emptyLine();
218: }
219:
220: public void print(String line) {
221: writer.println(line);
222: }
223:
224: public void printConstant(String name, String value) {
225: printConstant("String", name, value);
226: }
227:
228: public void printConstant(String type, String name, String value) {
229: writeIndent();
230: writer.print("public static final ");
231: writer.print(type);
232: writer.print(" ");
233: writer.print(name);
234: writer.print(" = ");
235: writer.print("\"");
236: writer.print(value);
237: writer.print("\";");
238: emptyLine();
239: }
240:
241: public void printJavaDoc(String description) {
242: writeIndent();
243: writer.println("/**");
244: writeIndent();
245: writer.print(" * ");
246: writer.println(description);
247: writeIndent();
248: writer.println(" */");
249: }
250:
251: private void writeIndent() {
252: for (int i = 0; i < indent; i++) {
253: writer.print("\t");
254: }
255: }
256:
257: }
258:
259: }
|