-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunSaxon.java
More file actions
207 lines (197 loc) · 5.81 KB
/
RunSaxon.java
File metadata and controls
207 lines (197 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import java.io.*;
import java.net.*;
import javax.net.ssl.*;
/**
* If an argument starts with a --, it is taken as a stylesheet for the
* If an argument starts with a -, it is taken as the new extension (beware)
* following files.
* Caveat emptor
*/
/*
class MyTrustManager extends X509TrustManager {
public static void main(String args[]) {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
*/
class RunSaxon {
protected static class ExitException extends SecurityException
{
public final int status;
public ExitException(int status)
{
super("There is no escape! "+status);
this.status = status;
}
}
/*
private static class NoExitSecurityManager extends SecurityManager
{
@Override
public void checkPermission(java.security.Permission perm)
{
// allow anything.
}
@Override
public void checkPermission(java.security.Permission perm, Object context)
{
// allow anything.
}
@Override
public void checkExit(int status)
{
super.checkExit(status);
throw new ExitException(status);
}
}
*/
public static void main(String args[]) {
try {
String stylesheet = "src/main/lib/stylesheets/X3dToJson.xslt";
String extension = "json";
boolean overwrite = false;
boolean silent = false;
String outdir = "./";
// System.setSecurityManager(new NoExitSecurityManager());
for (int a = 0; a < args.length; a++) {
String source = args[a];
if (source.startsWith("---overwrite")) {
overwrite = true;
System.err.println("overwrite on");
continue;
}
if (source.startsWith("---silent")) {
silent = true;
System.err.println("silent on");
continue;
}
if (source.startsWith("---")) {
outdir = source.substring(3);
System.err.println("outdir "+outdir);
continue;
}
if (source.startsWith("--")) {
stylesheet = source.substring(2);
// System.err.println("stylesheet "+stylesheet);
continue;
}
if (source.startsWith("-")) {
extension = source.substring(1);
// System.err.println("extension "+extension);
continue;
}
String out = "";
try {
if (source.startsWith("http")) {
/*
// Create a new trust manager that trust all certificates
TrustManager[] trustAllCerts = new TrustManager[]{
new MyTrustManager()
};
// Activate the new trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
*/
System.err.println("URL is "+source);
URL u = new URL(source);
if (source.indexOf("https://") == 0) {
source = "C:/x3d-code/"+source.substring(8);
} else if (source.indexOf("http://") == 0) {
source = "C:/x3d-code/"+source.substring(7);
}
BufferedReader br = null;
PrintWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(u.openStream()));
System.err.println("Downloading URL to "+source);
if (source.lastIndexOf("/") > 0) {
File dir = new File(source.substring(0, source.lastIndexOf("/")));
dir.mkdirs();
}
bw = new PrintWriter(new FileWriter(source));
String line = null;
while ((line = br.readLine()) != null) {
bw.println(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (br != null) {
br.close();
}
if (bw != null) {
bw.close();
}
}
}
out = source;
out = outdir+out.substring(0, out.lastIndexOf("."))+"."+extension;
if (overwrite) {
System.err.print("BEGIN "+source+" > "+extension+", ");
System.err.println("out "+out);
if (out.lastIndexOf("/") > 0) {
File dir = new File(out.substring(0, out.lastIndexOf("/")));
dir.mkdirs();
}
net.sf.saxon.Transform.main(new String[] {
"-warnings:recover",
"-o:"+out,
"-s:"+source,
"-xsl:"+stylesheet,
"packageName=net.x3djsonld.data",
"className="+out.substring(out.lastIndexOf("/")+1, out.lastIndexOf(".")).replace(".", "_")
});
// -t #timing -c # compiled
System.err.println("END "+source);
if (!silent) {
System.out.println(out);
}
} else if (!new File(out).exists()) {
System.err.println("BEGIN "+source);
System.err.println("out "+out);
if (out.lastIndexOf("/") > 0) {
File dir = new File(out.substring(0, out.lastIndexOf("/")));
dir.mkdirs();
}
net.sf.saxon.Transform.main(new String[] {
"-warnings:recover",
"-o:"+out,
"-s:"+source,
"-xsl:"+stylesheet,
"packageName=net.x3djsonld.data",
"className="+out.substring(out.lastIndexOf("/")+1, out.lastIndexOf(".")).replace(".", "_")
});
// -t #timing -c # compiled
System.err.println("END "+source);
if (!silent) {
System.out.println(out);
}
} else {
if (!silent) {
System.err.println("EXISTS. DELETE? "+out);
System.out.println(out);
}
}
} catch (Throwable e) {
System.err.println("FATAL "+source+" > "+out);
e.printStackTrace();
}
}
//System.setSecurityManager(null); // or save and restore original
} catch (ExitException ee) {
ee.printStackTrace();
}
}
}