001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.xml;
024:
025: import java.net.URL;
026: import java.util.Hashtable;
027:
028: import org.xml.sax.EntityResolver;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * @author mkobold
034: */
035: public class SaxResolver implements EntityResolver {
036:
037: /**
038: * The URLs of dtds and schemas that have been registered, keyed by the public identifier that corresponds.
039: */
040: protected Hashtable<String, String> entityValidator = new Hashtable<String, String>();
041:
042: /**
043: * Extension to make the difference between DTD and Schema.
044: */
045: protected String schemaExtension = "xsd";
046:
047: /**
048: * Create a new <code>EntityResolver</code> that will redirect all remote dtds and schema to a locat destination.
049: *
050: * @param digester The digester instance.
051: */
052: public SaxResolver() {
053:
054: }
055:
056: /**
057: * Register the specified DTD/Schema URL for the specified public identifier. This must be called before the first
058: * call to <code>parse()</code>. When adding a schema file (*.xsd), only the name of the file will get added. If
059: * two schemas with the same name are added, only the last one will be stored.
060: *
061: * @param publicId Public identifier of the DTD to be resolved
062: * @param entityURL The URL to use for reading this DTD
063: */
064: public void register(String publicId, String entityURL) {
065:
066: String key = publicId;
067: if (publicId.indexOf(schemaExtension) != -1)
068: key = publicId.substring(publicId.lastIndexOf('/') + 1);
069: entityValidator.put(key, entityURL);
070: }
071:
072: public void register(String publicId, String entityURL,
073: Class<?> clazz) {
074:
075: URL url = clazz.getResource(entityURL);
076: register(publicId, url.toString());
077: }
078:
079: /**
080: * Resolve the requested external entity.
081: *
082: * @param publicId The public identifier of the entity being referenced
083: * @param systemId The system identifier of the entity being referenced
084: * @exception SAXException if a parsing exception occurs
085: */
086: public InputSource resolveEntity(String publicId, String systemId)
087: throws SAXException {
088:
089: // Has this system identifier been registered?
090: String entityURL = null;
091: if (publicId != null) {
092: entityURL = entityValidator.get(publicId);
093: }
094:
095: // Redirect the schema location to a local destination
096: String key = null;
097: if (entityURL == null && systemId != null) {
098: key = systemId.substring(systemId.lastIndexOf('/') + 1);
099: entityURL = entityValidator.get(key);
100: }
101:
102: if (entityURL == null) {
103: return (null);
104: }
105:
106: try {
107: return (new InputSource(entityURL));
108: } catch (Exception e) {
109: throw new SAXException(e);
110: }
111: }
112: }
|