1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.castor.anttask;
17
18 import java.io.File;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.util.Vector;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.DirectoryScanner;
25 import org.apache.tools.ant.taskdefs.MatchingTask;
26 import org.apache.tools.ant.types.FileSet;
27 import org.exolab.castor.xml.schema.Schema;
28 import org.exolab.castor.xml.schema.util.XMLInstance2Schema;
29 import org.xml.sax.SAXException;
30
31
32
33
34
35
36
37
38 public final class XMLInstance2SchemaTask extends MatchingTask {
39
40
41
42
43
44 private static final String NO_XML_DOCUMENT_MSG =
45 "At least one XML document instance must be provided.";
46
47
48
49
50
51
52 private File _xmlInstanceFile;
53
54
55
56
57 private File _xmlInstanceDir;
58
59
60
61
62 private Vector _xmlInstanceFileSets = new Vector();
63
64
65
66
67
68
69 private String _xmlSchemaFileName;
70
71
72
73
74 private boolean _defaultGroupingAsAll;
75
76
77
78
79
80
81
82
83 public void setFile(final File file) {
84 _xmlInstanceFile = file;
85 }
86
87
88
89
90
91
92
93 public void setDir(final File dir) {
94 _xmlInstanceDir = dir;
95 }
96
97
98
99
100
101
102 public void addFileset(final FileSet set) {
103 _xmlInstanceFileSets.addElement(set);
104 }
105
106
107
108
109
110 public void setXmlSchemaFileName(final String ddlFileName) {
111 _xmlSchemaFileName = ddlFileName;
112 }
113
114
115
116
117
118 public void setDefaultGrouping(final String defaultGroupingAsAll) {
119 _defaultGroupingAsAll = true;
120 }
121
122
123
124
125
126
127
128 private void config() {
129 }
130
131
132
133
134
135
136
137
138 private void processFile(final String filePath, final String outputFilePath) {
139 log("Processing " + filePath);
140 try {
141 XMLInstance2Schema schemaGenerator = new XMLInstance2Schema();
142 if (_defaultGroupingAsAll) {
143 schemaGenerator.setDefaultGroupingAsAll();
144 }
145 Schema schema = schemaGenerator.createSchema(filePath);
146 String outputFileName = outputFilePath;
147 if (outputFileName == null) {
148 outputFileName = deriveOutputFilePath(filePath);
149 }
150 FileWriter dstWriter = new FileWriter(outputFileName);
151 schemaGenerator.serializeSchema(dstWriter, schema);
152 } catch (IOException e) {
153 throw new BuildException ("Problem writing to the given putput sink "
154 + _xmlInstanceFile.getAbsolutePath(), e);
155 } catch (SAXException e) {
156 throw new BuildException ("Problem streaming the generated XML schema instance "
157 + "to the given file.", e);
158 }
159 }
160
161
162
163
164
165
166 private String deriveOutputFilePath(final String outputFileName) {
167 return outputFileName + ".xsd";
168 }
169
170
171
172
173
174
175
176
177
178 public void execute() {
179
180 if (_xmlInstanceFile == null && _xmlInstanceDir == null
181 && _xmlInstanceFileSets.size() == 0) {
182 throw new BuildException(NO_XML_DOCUMENT_MSG);
183 }
184
185 config();
186
187
188 if (_xmlInstanceFile != null) {
189 processFile(_xmlInstanceFile.getAbsolutePath(), _xmlSchemaFileName);
190 }
191
192
193 if (_xmlInstanceDir != null && _xmlInstanceDir.exists() && _xmlInstanceDir.isDirectory()) {
194 log("Given XML schema file name will be ignored.");
195 DirectoryScanner ds = this.getDirectoryScanner(_xmlInstanceDir);
196
197 String[] files = ds.getIncludedFiles();
198 for (int i = 0; i < files.length; i++) {
199 String filePath = _xmlInstanceDir.getAbsolutePath() + File.separator + files[i];
200 processFile(filePath, null);
201 }
202 }
203
204
205 for (int i = 0; i < _xmlInstanceFileSets.size(); i++) {
206 log("Given XML schema file name will be ignored.");
207 FileSet fs = (FileSet) _xmlInstanceFileSets.elementAt(i);
208 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
209 File subdir = fs.getDir(getProject());
210
211 String[] files = ds.getIncludedFiles();
212 for (int j = 0; j < files.length; j++) {
213 String filePath = subdir.getAbsolutePath() + File.separator + files[j];
214 processFile(filePath, null);
215 }
216 }
217 }
218 }