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.reader;
|
11
|
|
|
12
|
|
import java.io.InputStream;
|
13
|
|
import java.net.URL;
|
14
|
|
|
15
|
|
import org.jbind.xml.Config;
|
16
|
|
import org.jbind.xml.base.IBindingAttributes;
|
17
|
|
import org.jbind.xml.base.INamespaces;
|
18
|
|
import org.jbind.xml.core.cmp.ISchema;
|
19
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
20
|
|
import org.jbind.xml.msg.XmlException;
|
21
|
|
import org.jbind.xml.msg.XmlMessages;
|
22
|
|
import org.jbind.xml.schema.cmp.Schemas;
|
23
|
|
import org.jbind.xml.schema.instantiation.IImportJobHelper;
|
24
|
|
import org.jbind.xml.schema.instantiation.ISchemaElement;
|
25
|
|
|
26
|
|
public class ImportJob extends SourceJob {
|
27
|
|
|
28
|
26
|
public ImportJob(IImportJobHelper aHelper, ISchemaElement aSchemaElement) {
|
29
|
26
|
super(aHelper, aSchemaElement);
|
30
|
|
}
|
31
|
|
|
32
|
26
|
protected void doExecuteCreation(IConstraintViolations aViolations) {
|
33
|
26
|
IImportJobHelper helper = (IImportJobHelper)getHelper();
|
34
|
|
|
35
|
26
|
String importedNamespace = helper.getNamespace();
|
36
|
|
|
37
|
26
|
if (INamespaces.NO.equals(importedNamespace) && getSchemaElement().isChameleon()) {
|
38
|
0
|
aViolations.add(XmlMessages.importOfNoNamespaceNotAllowedInChameleonSchema(getHelper()));
|
39
|
0
|
return;
|
40
|
|
}
|
41
|
|
|
42
|
26
|
if (getSchemaElement().getTargetNamespace().equals(importedNamespace)) {
|
43
|
0
|
aViolations.add(XmlMessages.importedNamespaceMustNotBeTheSame(getHelper()));
|
44
|
0
|
return;
|
45
|
|
}
|
46
|
|
|
47
|
|
// Check if a schema for the namespace is already known.
|
48
|
26
|
ISchema importedSchema = Schemas.getInstance().getSchema(importedNamespace);
|
49
|
26
|
if (null == importedSchema) {
|
50
|
24
|
try {
|
51
|
24
|
URL url = null;
|
52
|
24
|
String resolvedNamespace = Config.instance.resolveUri(importedNamespace);
|
53
|
24
|
if (null != resolvedNamespace) {
|
54
|
4
|
url = new URL(resolvedNamespace);
|
55
|
|
} else {
|
56
|
20
|
url = getUrl();
|
57
|
20
|
if (null == url) {
|
58
|
0
|
try {
|
59
|
0
|
url = new URL(importedNamespace);
|
60
|
0
|
InputStream is = url.openStream();
|
61
|
0
|
is.close();
|
62
|
|
} catch (Exception e) {
|
63
|
0
|
aViolations.add(XmlMessages.missingSchemaLocationForImport(importedNamespace, getHelper()));
|
64
|
0
|
return;
|
65
|
|
}
|
66
|
|
}
|
67
|
|
}
|
68
|
24
|
IInstantiator instantiator = getInstantiator().newInstantiator();
|
69
|
24
|
String packageName = helper.getLocalStringBindingAttribute(IBindingAttributes.PACKAGE);
|
70
|
24
|
importedSchema = instantiator.readSchema(url, getInstantiator().getUseBuiltInClassesOnly(), packageName);
|
71
|
|
|
72
|
24
|
if (!importedNamespace.equals(importedSchema.getNamespace())) {
|
73
|
0
|
aViolations.add(XmlMessages.invalidImportedNamespace(importedNamespace, importedSchema.getNamespace(), url, getLocation()));
|
74
|
|
} else {
|
75
|
24
|
Schemas.getInstance().setSchema(importedSchema);
|
76
|
|
}
|
77
|
|
} catch (XmlException e) {
|
78
|
0
|
aViolations.add(e.getXmlMessage());
|
79
|
|
} catch (Exception e) {
|
80
|
0
|
aViolations.add(XmlMessages.wrappedException(e, getLocation()));
|
81
|
|
}
|
82
|
|
|
83
|
|
}
|
84
|
|
|
85
|
26
|
if (null != importedSchema) {
|
86
|
26
|
getInstantiator().getSchema().addImportedSchema(importedSchema);
|
87
|
|
}
|
88
|
|
|
89
|
|
}
|
90
|
|
|
91
|
0
|
public String getDescription() {
|
92
|
0
|
return "importJob: " + getLocation();
|
93
|
|
}
|
94
|
|
|
95
|
|
}
|
96
|
|
|