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.schema.compiler;
|
11
|
|
|
12
|
|
import java.net.MalformedURLException;
|
13
|
|
import java.net.URL;
|
14
|
|
import java.util.ArrayList;
|
15
|
|
import java.util.Collections;
|
16
|
|
import java.util.Iterator;
|
17
|
|
import java.util.List;
|
18
|
|
|
19
|
|
import org.jbind.base.Console;
|
20
|
|
import org.jbind.xml.core.cmp.IComponent;
|
21
|
|
import org.jbind.xml.core.cmp.ISchema;
|
22
|
|
import org.jbind.xml.facade.JBindFacade;
|
23
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
24
|
|
import org.jbind.xml.msg.XmlException;
|
25
|
|
import org.jbind.xml.msg.XmlMessages;
|
26
|
|
import org.jbind.xml.schema.instantiation.ISchemaReader;
|
27
|
|
|
28
|
|
public class Compiler implements ICompiler {
|
29
|
|
|
30
|
105
|
public List compileSchema(ISchema aSchema, IConstraintViolations aViolations) {
|
31
|
105
|
List res = new ArrayList();
|
32
|
105
|
if (aViolations.isEmpty()) {
|
33
|
105
|
for (Iterator i = CartridgeMgr.instance.iterCartridges(); i.hasNext(); ) {
|
34
|
420
|
ICartridge cartridge = (ICartridge)i.next();
|
35
|
420
|
for (Iterator j = aSchema.iterComponents(null); j.hasNext(); ) {
|
36
|
2236
|
IComponent component = (IComponent)j.next();
|
37
|
2236
|
try {
|
38
|
2236
|
IFileInfo fileInfo = cartridge.generate(component);
|
39
|
2236
|
if (null != fileInfo) {
|
40
|
743
|
res.add(fileInfo);
|
41
|
|
}
|
42
|
|
} catch (XmlException e) {
|
43
|
0
|
aViolations.add(e.getXmlMessage());
|
44
|
|
}
|
45
|
|
}
|
46
|
|
}
|
47
|
|
}
|
48
|
105
|
return res;
|
49
|
|
}
|
50
|
|
|
51
|
0
|
public List compile(String aSchemaUrl, String aPackage, IConstraintViolations aViolations) {
|
52
|
0
|
ISchema schema = null;
|
53
|
0
|
try {
|
54
|
0
|
URL url = new URL(aSchemaUrl);
|
55
|
0
|
ISchemaReader reader = JBindFacade.createSchemaReader();
|
56
|
0
|
schema = reader.readSchema(url, true, aPackage);// uses built-in classes only
|
57
|
|
} catch (MalformedURLException e) {
|
58
|
0
|
aViolations.add(XmlMessages.invalidSchemaUrl(aSchemaUrl, null));
|
59
|
|
} catch (XmlException e) {
|
60
|
0
|
aViolations.add(e.getXmlMessage());
|
61
|
|
}
|
62
|
|
|
63
|
0
|
List res = null;
|
64
|
0
|
if (aViolations.isEmpty()) {
|
65
|
0
|
res = compileSchema(schema, aViolations);
|
66
|
|
} else {
|
67
|
0
|
res = Collections.EMPTY_LIST;
|
68
|
|
}
|
69
|
0
|
return res;
|
70
|
|
}
|
71
|
|
|
72
|
105
|
public Compiler() {}
|
73
|
|
|
74
|
0
|
private static void usage() {
|
75
|
0
|
Console.out("usage: java " + Compiler.class.getName() + " -schemaUrl <url> [-validateOnly] [-package <package>] [-destionationDir <dir>] [-dataDir <dir>] [-behaviourDir <dir>]");
|
76
|
|
}
|
77
|
|
|
78
|
0
|
public static void main(String[] args) {
|
79
|
0
|
boolean validateOnly = false;
|
80
|
|
|
81
|
0
|
String schemaUrl = null;
|
82
|
0
|
String destinationDir = null;
|
83
|
0
|
String behaviourDir = null;
|
84
|
0
|
String dataDir = null;
|
85
|
|
|
86
|
0
|
String packageName = null;
|
87
|
|
|
88
|
0
|
for (int i = 0; i < args.length; i++) {
|
89
|
0
|
String arg = args[i];
|
90
|
0
|
if ("-schemaUrl".equals(arg)) {
|
91
|
0
|
if (i + 1 < args.length) {
|
92
|
0
|
schemaUrl = args[++i];
|
93
|
|
} else {
|
94
|
0
|
System.out.println("*** missing schema url");
|
95
|
0
|
usage();
|
96
|
0
|
System.exit(1);
|
97
|
|
}
|
98
|
0
|
} else if ("-destinationDir".equals(arg)) {
|
99
|
0
|
if (i + 1 < args.length) {
|
100
|
0
|
destinationDir = args[++i];
|
101
|
|
} else {
|
102
|
0
|
System.out.println("*** missing directory");
|
103
|
0
|
usage();
|
104
|
0
|
System.exit(1);
|
105
|
|
}
|
106
|
0
|
} else if ("-behaviourDir".equals(arg)) {
|
107
|
0
|
if (i + 1 < args.length) {
|
108
|
0
|
behaviourDir = args[++i];
|
109
|
|
} else {
|
110
|
0
|
System.out.println("*** missing directory");
|
111
|
0
|
usage();
|
112
|
0
|
System.exit(1);
|
113
|
|
}
|
114
|
0
|
} else if ("-dataDir".equals(arg)) {
|
115
|
0
|
if (i + 1 < args.length) {
|
116
|
0
|
dataDir = args[++i];
|
117
|
|
} else {
|
118
|
0
|
System.out.println("*** missing directory");
|
119
|
0
|
usage();
|
120
|
0
|
System.exit(1);
|
121
|
|
}
|
122
|
0
|
} else if ("-package".equals(arg)) {
|
123
|
0
|
if (i + 1 < args.length) {
|
124
|
0
|
packageName = args[++i];
|
125
|
|
} else {
|
126
|
0
|
System.out.println("*** missing package");
|
127
|
0
|
usage();
|
128
|
0
|
System.exit(1);
|
129
|
|
}
|
130
|
0
|
} else if ("-validateOnly".equals(arg)) {
|
131
|
0
|
validateOnly = true;
|
132
|
|
} else {
|
133
|
0
|
System.out.println("*** unknown argument: " + arg);
|
134
|
0
|
usage();
|
135
|
0
|
System.exit(1);
|
136
|
|
}
|
137
|
|
}
|
138
|
|
|
139
|
0
|
behaviourDir = (null != behaviourDir) ? behaviourDir : destinationDir;
|
140
|
0
|
dataDir = (null != dataDir) ? dataDir : destinationDir;
|
141
|
|
|
142
|
0
|
if ((null == schemaUrl) && !validateOnly) {
|
143
|
0
|
System.out.println("*** missing schema url");
|
144
|
0
|
usage();
|
145
|
0
|
System.exit(1);
|
146
|
|
}
|
147
|
0
|
if ((null == behaviourDir) && !validateOnly) {
|
148
|
0
|
System.out.println("*** missing behaviour directory");
|
149
|
0
|
usage();
|
150
|
0
|
System.exit(1);
|
151
|
|
}
|
152
|
0
|
if ((null == dataDir) && !validateOnly) {
|
153
|
0
|
System.out.println("*** missing data directory");
|
154
|
0
|
usage();
|
155
|
0
|
System.exit(1);
|
156
|
|
}
|
157
|
|
|
158
|
0
|
IConstraintViolations violations = XmlMessages.constraintViolations();
|
159
|
0
|
List fileInfos = new Compiler().compile(schemaUrl, packageName, violations);
|
160
|
|
|
161
|
0
|
if (violations.isEmpty() && !validateOnly) {
|
162
|
0
|
for (Iterator i = fileInfos.iterator(); i.hasNext(); ) {
|
163
|
0
|
IFileInfo fileInfo = (IFileInfo)i.next();
|
164
|
0
|
String dir = fileInfo.overwrite() ? dataDir : behaviourDir;
|
165
|
0
|
fileInfo.output(dir, violations);
|
166
|
|
}
|
167
|
|
}
|
168
|
|
|
169
|
0
|
if (violations.isEmpty()) {
|
170
|
0
|
System.exit(0);
|
171
|
|
} else {
|
172
|
0
|
Console.err("errors detected:");
|
173
|
0
|
Console.err(violations.output(null, false, false));
|
174
|
0
|
System.exit(-1);
|
175
|
|
}
|
176
|
|
}
|
177
|
|
|
178
|
|
}
|
179
|
|
|