1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.ant;
|
11
|
|
|
12
|
|
import java.io.File;
|
13
|
|
import java.net.URL;
|
14
|
|
import java.util.ArrayList;
|
15
|
|
import java.util.Enumeration;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.List;
|
18
|
|
import java.util.StringTokenizer;
|
19
|
|
|
20
|
|
import org.apache.tools.ant.BuildException;
|
21
|
|
import org.apache.tools.ant.DirectoryScanner;
|
22
|
|
import org.apache.tools.ant.Task;
|
23
|
|
import org.apache.tools.ant.types.FileSet;
|
24
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
25
|
|
import org.jbind.xml.msg.XmlMessages;
|
26
|
|
import org.jbind.xml.schema.cmp.Schemas;
|
27
|
|
import org.jbind.xml.schema.compiler.Compiler;
|
28
|
|
import org.jbind.xml.schema.compiler.IFileInfo;
|
29
|
|
|
30
|
|
public class CompileTask extends Task {
|
31
|
|
|
32
|
|
private String myPackage = null;
|
33
|
|
private File myDestinationDir = null;
|
34
|
|
private File myDataDir = null;
|
35
|
|
private File myBehaviourDir = null;
|
36
|
|
private File mySchemaFile = null;
|
37
|
|
private String mySchemaUrl = null;
|
38
|
|
private boolean myDerivePackages = false;
|
39
|
|
private ArrayList myFileSets = new ArrayList();
|
40
|
|
|
41
|
0
|
public void setPackage(String aPackage) {
|
42
|
0
|
myPackage = aPackage;
|
43
|
|
}
|
44
|
0
|
public void setDestinationDir(File aFile) {
|
45
|
0
|
myDestinationDir = aFile;
|
46
|
|
}
|
47
|
0
|
public void setDataDir(File aFile) {
|
48
|
0
|
myDataDir = aFile;
|
49
|
|
}
|
50
|
0
|
public void setBehaviourDir(File aFile) {
|
51
|
0
|
myBehaviourDir = aFile;
|
52
|
|
}
|
53
|
0
|
public void setFile(File aFile) {
|
54
|
0
|
mySchemaFile = aFile;
|
55
|
|
}
|
56
|
0
|
public void setUrl(String aUrl) {
|
57
|
0
|
mySchemaUrl = aUrl;
|
58
|
|
}
|
59
|
0
|
public void setDerivePackages(boolean aBoolean) {
|
60
|
0
|
myDerivePackages = aBoolean;
|
61
|
|
}
|
62
|
0
|
public void addFileset(FileSet aFileSet) {
|
63
|
0
|
myFileSets.add(aFileSet);
|
64
|
|
}
|
65
|
|
|
66
|
0
|
public void execute() throws BuildException {
|
67
|
0
|
if ((myDestinationDir == null) && (myDataDir == null)) {
|
68
|
0
|
throw new BuildException("missing data directory", location);
|
69
|
|
}
|
70
|
0
|
if ((myDestinationDir == null) && (myBehaviourDir == null)) {
|
71
|
0
|
throw new BuildException("missing behaviour directory", location);
|
72
|
|
}
|
73
|
|
|
74
|
0
|
IConstraintViolations violations = XmlMessages.constraintViolations();
|
75
|
|
|
76
|
0
|
try {
|
77
|
|
|
78
|
|
// collect the schema URLs
|
79
|
|
|
80
|
0
|
ArrayList schemaUrls = new ArrayList();
|
81
|
0
|
ArrayList packages = new ArrayList();
|
82
|
0
|
if (null != mySchemaFile) {
|
83
|
0
|
schemaUrls.add(mySchemaFile.toURL());
|
84
|
0
|
packages.add(myPackage);
|
85
|
|
}
|
86
|
0
|
if (null != mySchemaUrl) {
|
87
|
0
|
schemaUrls.add(new URL(mySchemaUrl));
|
88
|
0
|
packages.add(myPackage);
|
89
|
|
}
|
90
|
|
|
91
|
0
|
for (Iterator i = myFileSets.iterator(); i.hasNext(); ) {
|
92
|
0
|
FileSet fs = (FileSet)i.next();
|
93
|
0
|
DirectoryScanner ds = fs.getDirectoryScanner(project);
|
94
|
0
|
File baseDir = ds.getBasedir();
|
95
|
0
|
String[] files = ds.getIncludedFiles();
|
96
|
0
|
for (int idx = 0; idx < files.length; idx++) {
|
97
|
0
|
File f = new File(baseDir, files[idx]);
|
98
|
0
|
schemaUrls.add(f.toURL());
|
99
|
0
|
if (myDerivePackages) {
|
100
|
0
|
String p = null;
|
101
|
0
|
for (Enumeration e = new StringTokenizer(files[idx], File.separator);
|
102
|
0
|
e.hasMoreElements(); ) {
|
103
|
0
|
String s = (String)e.nextElement();
|
104
|
0
|
if (e.hasMoreElements()) {
|
105
|
0
|
p = (null == p) ? s : p + "." + s;
|
106
|
|
}
|
107
|
|
}
|
108
|
0
|
packages.add(p);
|
109
|
|
} else {
|
110
|
0
|
packages.add(myPackage);
|
111
|
|
}
|
112
|
|
}
|
113
|
|
}
|
114
|
|
|
115
|
0
|
String dataDir = (null != myDataDir) ? myDataDir.getAbsolutePath() : myDestinationDir.getAbsolutePath();
|
116
|
0
|
String behaviourDir = (null != myBehaviourDir) ? myBehaviourDir.getAbsolutePath() : myDestinationDir.getAbsolutePath();
|
117
|
|
|
118
|
|
// complile the schemas and output the files
|
119
|
0
|
Compiler compiler = new Compiler();
|
120
|
0
|
for (int i = 0; i < schemaUrls.size(); i++) {
|
121
|
0
|
IConstraintViolations v = XmlMessages.constraintViolations();
|
122
|
0
|
URL url = (URL)schemaUrls.get(i);
|
123
|
0
|
String pckg = (String)packages.get(i);
|
124
|
0
|
Schemas.getInstance().reset();
|
125
|
0
|
List fileInfos = compiler.compile(url.toString(), pckg, v);
|
126
|
0
|
if (v.isEmpty()) {
|
127
|
0
|
for (Iterator j = fileInfos.iterator(); j.hasNext(); ) {
|
128
|
0
|
IFileInfo fileInfo = (IFileInfo)j.next();
|
129
|
0
|
String dir = fileInfo.overwrite() ? dataDir : behaviourDir;
|
130
|
0
|
fileInfo.output(dir, v);
|
131
|
|
}
|
132
|
|
}
|
133
|
0
|
if (!v.isEmpty()) {
|
134
|
0
|
violations.add(v);
|
135
|
|
}
|
136
|
|
}
|
137
|
|
|
138
|
|
} catch (Exception e) {
|
139
|
0
|
e.printStackTrace();
|
140
|
0
|
throw new BuildException(e, location);
|
141
|
|
}
|
142
|
|
|
143
|
0
|
if (!violations.isEmpty()) {
|
144
|
0
|
throw new BuildException(violations.output(null, false, false), location);
|
145
|
|
}
|
146
|
|
|
147
|
|
}
|
148
|
|
|
149
|
|
}
|
150
|
|
|