Utility class for loading mapping files and providing them to the
XML marshaller, JDO engine etc. The mapping file can be loaded from
a URL, input stream or SAX
.
If the desired class loader is different than the one used by Castor
(e.g. if Castor is installed as a Java extension), the Mapping
object can be constructed with the proper class loader.
Method from org.exolab.castor.mapping.Mapping Detail: |
public ClassLoader getClassLoader() {
return _classLoader;
}
Returns the class loader used by this mapping object. The returned
class loaded may be the one passed in the constructor, the one used
to load Castor, or in some 1.1 JVMs null. |
public List getMappingSources() throws MappingException {
return Collections.unmodifiableList(_mappings);
}
Get list of mapping sources to resolve. |
public MappingRoot getRoot() {
return _root;
}
|
public void loadMapping(String url) throws IOException, MappingException {
loadMapping(url, DEFAULT_SOURCE_TYPE);
}
Loads the mapping from the specified URL with type defaults to
'CastorXmlMapping'. If an entity resolver was specified, will use
the entity resolver to resolve the URL. This method is also used
to load mappings referenced from another mapping or configuration
file. |
public void loadMapping(URL url) throws IOException, MappingException {
loadMapping(url, DEFAULT_SOURCE_TYPE);
}
Loads the mapping from the specified URL with type defaults to
'CastorXmlMapping'. |
public void loadMapping(InputSource source) {
loadMapping(source, DEFAULT_SOURCE_TYPE);
}
Loads the mapping from the specified input source with type defaults to
'CastorXmlMapping'. |
public void loadMapping(String url,
String type) throws IOException, MappingException {
String location = url;
if (_resolver.getBaseURL() == null) {
setBaseURL(location);
location = URIUtils.getRelativeURI(location);
}
try {
InputSource source = _resolver.resolveEntity(null, location);
if (source == null) { source = new InputSource(location); }
if (source.getSystemId() == null) { source.setSystemId(location); }
LOG.info(Messages.format("mapping.loadingFrom", location));
loadMapping(source, type);
} catch (SAXException ex) {
throw new MappingException(ex);
}
}
Loads the mapping from the specified URL. If an entity resolver
was specified, will use the entity resolver to resolve the URL.
This method is also used to load mappings referenced from another
mapping or configuration file. |
public void loadMapping(URL url,
String type) throws IOException, MappingException {
try {
if (_resolver.getBaseURL() == null) {
_resolver.setBaseURL(url);
}
InputSource source = _resolver.resolveEntity(null, url.toExternalForm());
if (source == null) {
source = new InputSource(url.toExternalForm());
source.setByteStream(url.openStream());
} else
source.setSystemId(url.toExternalForm());
LOG.info(Messages.format("mapping.loadingFrom", url.toExternalForm()));
loadMapping(source, type);
} catch (SAXException ex) {
throw new MappingException(ex);
}
}
Loads the mapping from the specified URL. |
public void loadMapping(InputSource source,
String type) {
_mappings.add(new MappingSource(source, type, _resolver));
}
Loads the mapping from the specified input source. |
public void markAsProcessed(Object id) {
_processed.add(id);
}
Marks the given mapping as having been processed. |
public boolean processed(Object id) {
return _processed.contains(id);
}
Returns true if the given systemID or stream has been marked as processed. |
public void setBaseURL(String url) {
String location = url;
//-- remove filename if necessary:
if (location != null) {
int idx = location.lastIndexOf('/');
if (idx < 0) idx = location.lastIndexOf('\\');
if (idx >= 0) {
int extIdx = location.indexOf('.', idx);
if (extIdx > 0) {
location = location.substring(0, idx);
}
}
}
try {
_resolver.setBaseURL(new URL(location));
} catch (MalformedURLException except) {
// try to parse the url as an absolute path
try {
LOG.info(Messages.format("mapping.wrongURL", location));
_resolver.setBaseURL(new URL("file", null, location));
} catch (MalformedURLException except2) { }
}
}
Sets the base URL for the mapping and related files. If the base
URL is known, files can be included using relative names. Any URL
can be passed, if the URL can serve as a base URL it will be used.
If url is an absolute path, it is converted to a file URL. |
public void setEntityResolver(EntityResolver resolver) {
_resolver = new DTDResolver(resolver);
}
Sets the entity resolver. The entity resolver can be used to
resolve external entities and cached documents that are used
from within mapping files. |