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