001: package net.sourceforge.jaxor.parser;
002:
003: import net.sourceforge.jaxor.util.SystemException;
004: import org.apache.tools.ant.BuildException;
005: import org.apache.tools.ant.DirectoryScanner;
006: import org.apache.tools.ant.taskdefs.MatchingTask;
007: import org.apache.tools.ant.types.FileSet;
008: import org.apache.tools.ant.types.XMLCatalog;
009: import org.apache.velocity.runtime.RuntimeServices;
010: import org.apache.velocity.runtime.log.LogSystem;
011:
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.IOException;
015: import java.util.ArrayList;
016: import java.util.Iterator;
017: import java.util.List;
018: import java.util.Properties;
019:
020: public class AntTask extends MatchingTask implements LogSystem {
021: private List _xmlFiles = new ArrayList();
022: private File _destdir;
023: private File _configFile;
024: private File _mapperRegistry;
025: private int _logLevel = 2;
026: private XMLCatalog xmlCatalog = new XMLCatalog();
027: private List _implParsers = new ArrayList();
028: private boolean _validating = false;
029:
030: public void init() throws BuildException {
031: super .init();
032: xmlCatalog.setProject(getProject());
033: }
034:
035: /**
036: * Add the catalog to our internal catalog
037: *
038: * @param xmlCatalog the XMLCatalog instance to use to look up DTDs
039: */
040: public void addConfiguredXMLCatalog(XMLCatalog xmlCatalog) {
041: this .xmlCatalog.addConfiguredXMLCatalog(xmlCatalog);
042: }
043:
044: public void setValidating(boolean validate) {
045: _validating = validate;
046: }
047:
048: public void setLogLevel(int level) {
049: _logLevel = level;
050: }
051:
052: public File getMappers() {
053: return _mapperRegistry;
054: }
055:
056: public void setMappers(File _mapperRegistry) {
057: this ._mapperRegistry = _mapperRegistry;
058: }
059:
060: private Properties getMapperProps() {
061: Properties props = new Properties();
062: if (getMappers() == null)
063: return props;
064: try {
065: props.load(new FileInputStream(getMappers()));
066: return props;
067: } catch (IOException e) {
068: throw new SystemException(e);
069: }
070: }
071:
072: public void addImpl(ImplParser implParser) {
073: _implParsers.add(implParser);
074: }
075:
076: public void addConfiguredFileset(FileSet fs) {
077: _xmlFiles.add(fs);
078: }
079:
080: public File getDestdir() {
081: return _destdir;
082: }
083:
084: public void setDestdir(File destdir) {
085: _destdir = destdir;
086: }
087:
088: public void setConfigFile(File config) {
089: _configFile = config;
090: }
091:
092: public File getConfigFile() {
093: return _configFile;
094: }
095:
096: public void execute() {
097: if (_xmlFiles.size() == 0) {
098: throw new BuildException("Not filesets specified");
099: }
100: SourceMap javaDocs = new SourceMap();
101: for (Iterator iterator = _implParsers.iterator(); iterator
102: .hasNext();) {
103: ImplParser implParser = (ImplParser) iterator.next();
104: javaDocs.put(implParser.findSource(this .getProject()));
105: }
106:
107: for (int i = 0; i < _xmlFiles.size(); i++) {
108: FileSet fs = (FileSet) _xmlFiles.get(i);
109: DirectoryScanner ds = fs.getDirectoryScanner(project);
110: String[] srcFiles = ds.getIncludedFiles();
111: parseFiles(srcFiles, ds.getBasedir(), javaDocs);
112: }
113: }
114:
115: public void init(RuntimeServices runtimeServices) throws Exception {
116: }
117:
118: public void logVelocityMessage(int i, String s) {
119: if (i >= _logLevel)
120: log(s, i);
121: }
122:
123: public void parseFiles(String[] files, File srcDir,
124: SourceMap javaDocBuilders) {
125:
126: EntityCollection collection = new EntityCollection(
127: parseGenerator(), javaDocBuilders, getMapperProps());
128: for (int i = 0; i < files.length; i++) {
129: String src = srcDir.getPath() + File.separator + files[i];
130: try {
131: EntityParser parser = new EntityParser(src, xmlCatalog);
132: parser.run(_validating);
133: Jaxor j = parser.getJaxor();
134: j.setSource(src);
135: collection.add(j);
136: } catch (Exception e) {
137: throw new SystemException("Parsing Failed for file: "
138: + new File(src), e);
139: }
140: }
141: collection.generate(_destdir, this );
142: }
143:
144: private Generator parseGenerator() {
145: File configFile = getConfigFile();
146: if (configFile == null) {
147: return Generator.createDefault();
148: }
149: Generator config = GeneratorConfig.getParser(configFile);
150: return config;
151: }
152:
153: }
|