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;
|
11
|
|
|
12
|
|
import java.io.InputStream;
|
13
|
|
import java.net.MalformedURLException;
|
14
|
|
import java.net.URL;
|
15
|
|
|
16
|
|
import com.sun.resolver.Catalog;
|
17
|
|
import com.sun.resolver.tools.CatalogResolver;
|
18
|
|
|
19
|
|
import org.jbind.base.Console;
|
20
|
|
import org.jbind.xml.core.bridge.IDataImplFactory;
|
21
|
|
import org.jbind.xml.core.bridge.IRegEx;
|
22
|
|
import org.jbind.xml.core.bridge.IRegExFactory;
|
23
|
|
import org.jbind.xml.core.bridge.IXPathFactory;
|
24
|
|
import org.jbind.xml.facade.XercesRegExFactory;
|
25
|
|
import org.jbind.xml.instance.impl.DataImplFactory;
|
26
|
|
import org.jbind.xml.instance.impl.JaxenXPathFactory;
|
27
|
|
import org.jbind.xml.msg.XmlException;
|
28
|
|
import org.jbind.xml.msg.XmlMessages;
|
29
|
|
import org.xml.sax.EntityResolver;
|
30
|
|
|
31
|
|
/**
|
32
|
|
* Fixed configuration class. This class can be used in when JBind is to be
|
33
|
|
* used under JDK 1.3.
|
34
|
|
*/
|
35
|
|
public class FixedConfig implements IConfig {
|
36
|
|
|
37
|
|
/**
|
38
|
|
* JBind XML catalog. ({@value}).
|
39
|
|
*/
|
40
|
|
public static final String JBIND_XML_CATALOG = "/org/jbind/catalog.xml";
|
41
|
|
|
42
|
|
/**
|
43
|
|
* Application XML catalog. ({@value}).
|
44
|
|
*/
|
45
|
|
public static final String APPLICATION_XML_CATALOG = "/catalog.xml";
|
46
|
|
|
47
|
|
/**
|
48
|
|
* Constant that identifies the load method that uses the
|
49
|
|
* <code>Config.class.getResource(String aResourceName)</code> to access a
|
50
|
|
* resource.
|
51
|
|
*/
|
52
|
|
public static final String LOAD_BY_CLASS_LOADER = "loadByClassLoader";
|
53
|
|
|
54
|
|
/**
|
55
|
|
* Constant that identifies a load method that uses a configured URL.
|
56
|
|
* Relative URLs are resolved relative to the current user home directory.
|
57
|
|
*/
|
58
|
|
public static final String LOAD_BY_URL = "loadByUrl";
|
59
|
|
|
60
|
|
public static final FixedConfig instance = new FixedConfig();
|
61
|
|
|
62
|
|
private URL myUserHome;
|
63
|
|
private IXPathFactory myXPathFactory;
|
64
|
|
private IRegExFactory myRegExFactory;
|
65
|
|
private IDataImplFactory myDataImplFactory;
|
66
|
|
private EntityResolver myEntityResolver;
|
67
|
|
private CatalogResolver myCatalogResolver;
|
68
|
|
|
69
|
0
|
private FixedConfig() {
|
70
|
0
|
String userHome = System.getProperty("user.home");
|
71
|
0
|
if (null == userHome) {
|
72
|
0
|
userHome = System.getProperty("user.dir");
|
73
|
|
}
|
74
|
0
|
if (null != userHome) {
|
75
|
0
|
try {
|
76
|
0
|
myUserHome = new URL("file:" + userHome + "/");
|
77
|
|
} catch (MalformedURLException e) {
|
78
|
0
|
e.printStackTrace();
|
79
|
|
}
|
80
|
|
}
|
81
|
0
|
if (null == myUserHome) {
|
82
|
0
|
Console.out("user home directory not set - configuration resources with relative URLs may not be found");
|
83
|
|
}
|
84
|
0
|
System.getProperties().setProperty("xml.catalog.ignoreMissing", "true");
|
85
|
|
|
86
|
0
|
myCatalogResolver = new CatalogResolver();
|
87
|
0
|
String[] catalogs = null;
|
88
|
0
|
catalogs = new String[]{ JBIND_XML_CATALOG, APPLICATION_XML_CATALOG };
|
89
|
|
|
90
|
0
|
for (int i = 0; i < catalogs.length; i++) {
|
91
|
0
|
try {
|
92
|
0
|
String resourceName = catalogs[i];
|
93
|
0
|
String loadMode = LOAD_BY_CLASS_LOADER;
|
94
|
0
|
URL url = getConfigurationResourceUrl(resourceName, loadMode);
|
95
|
0
|
if (null == url) {
|
96
|
0
|
continue;
|
97
|
|
}
|
98
|
0
|
try {
|
99
|
0
|
InputStream is = url.openStream();
|
100
|
0
|
is.close();
|
101
|
|
} catch (Exception e) {
|
102
|
0
|
continue;
|
103
|
|
}
|
104
|
0
|
addCatalog(url);
|
105
|
|
} catch (Exception e) {
|
106
|
0
|
e.printStackTrace();
|
107
|
0
|
Console.err("could not parse catalog: " + catalogs[i]);
|
108
|
|
}
|
109
|
|
}
|
110
|
|
|
111
|
0
|
myXPathFactory = new JaxenXPathFactory();
|
112
|
0
|
myDataImplFactory = new DataImplFactory();
|
113
|
0
|
myRegExFactory = new XercesRegExFactory();
|
114
|
0
|
myEntityResolver = myCatalogResolver;
|
115
|
|
}
|
116
|
|
|
117
|
0
|
public boolean checkLanguageCode() {
|
118
|
0
|
return false;
|
119
|
|
}
|
120
|
|
|
121
|
0
|
public boolean checkCountryCode() {
|
122
|
0
|
return false;
|
123
|
|
}
|
124
|
|
|
125
|
0
|
public String[] getCartridgeClasses() throws Exception {
|
126
|
0
|
return new String[]{};
|
127
|
|
}
|
128
|
|
|
129
|
0
|
public String resolveUri(String aUri) throws XmlException {
|
130
|
0
|
try {
|
131
|
0
|
return myCatalogResolver.getCatalog().resolveURI(aUri);
|
132
|
|
} catch (Exception e) {
|
133
|
0
|
throw new XmlException(e, XmlMessages.wrappedException(e, null));
|
134
|
|
}
|
135
|
|
}
|
136
|
0
|
public String getPackageForNamespace(String aNamespace) {
|
137
|
0
|
return null;
|
138
|
|
}
|
139
|
|
|
140
|
0
|
public boolean strictSchemaValidation() {
|
141
|
0
|
return false;
|
142
|
|
}
|
143
|
|
|
144
|
0
|
public String getCountry(String aCode) {
|
145
|
0
|
return null;
|
146
|
|
}
|
147
|
|
|
148
|
0
|
public String getLanguage(String aCode) {
|
149
|
0
|
return null;
|
150
|
|
}
|
151
|
|
|
152
|
0
|
public IXPathFactory getXPathFactory() {
|
153
|
0
|
return myXPathFactory;
|
154
|
|
}
|
155
|
|
|
156
|
0
|
public IDataImplFactory getDataImplFactory() {
|
157
|
0
|
return myDataImplFactory;
|
158
|
|
}
|
159
|
|
|
160
|
0
|
public EntityResolver getEntityResolver() {
|
161
|
0
|
return myEntityResolver;
|
162
|
|
}
|
163
|
|
|
164
|
0
|
public IRegExFactory getRegExFactory() {
|
165
|
0
|
return myRegExFactory;
|
166
|
|
}
|
167
|
|
|
168
|
0
|
public IRegEx createRegEx(String aRegEx) throws Exception {
|
169
|
0
|
return myRegExFactory.createRegEx(aRegEx);
|
170
|
|
}
|
171
|
|
|
172
|
0
|
private URL getConfigurationResourceUrl(String aResourceName, String aLoadMode) throws Exception {
|
173
|
0
|
URL res = null;
|
174
|
0
|
if (LOAD_BY_CLASS_LOADER.equals(aLoadMode)) {
|
175
|
0
|
res = Config.class.getResource(aResourceName);
|
176
|
0
|
} else if (LOAD_BY_URL.equals(aLoadMode)) {
|
177
|
0
|
if (null != myUserHome) {
|
178
|
0
|
res = new URL(myUserHome, aResourceName);
|
179
|
|
} else {
|
180
|
0
|
res = new URL(aResourceName);
|
181
|
|
}
|
182
|
|
} else {
|
183
|
0
|
throw new Exception("unknown load mode: " + aLoadMode);
|
184
|
|
}
|
185
|
0
|
return res;
|
186
|
|
}
|
187
|
|
|
188
|
0
|
public void addCatalog(URL aUrl) {
|
189
|
0
|
try {
|
190
|
0
|
Catalog catalog = myCatalogResolver.getCatalog();
|
191
|
0
|
catalog.parseCatalog(aUrl);
|
192
|
|
} catch (Exception e) {
|
193
|
0
|
e.printStackTrace();
|
194
|
0
|
Console.err("could not add catalog: " + aUrl);
|
195
|
|
}
|
196
|
|
}
|
197
|
|
|
198
|
0
|
public boolean useContextClassLoader() {
|
199
|
0
|
return true;
|
200
|
|
}
|
201
|
|
|
202
|
|
}
|
203
|
|
|