001: // $Id: EntityResolver.java 162 2005-07-18 22:52:27Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data.processing.parser;
038:
039: import java.io.InputStream;
040: import java.io.InputStreamReader;
041: import java.io.Reader;
042: import java.net.URL;
043:
044: import org.apache.log4j.Logger;
045: import org.xml.sax.InputSource;
046:
047: /**
048: * Example taken from: David Brownell, SAX2, O'Reilly 2002 Paris, p.88ff.
049: *
050: * @author jg
051: */
052: class EntityResolver implements org.xml.sax.EntityResolver {
053: private Logger log = Logger.getLogger(EntityResolver.class);
054:
055: /**
056: * Default contructor no extras
057: */
058: public EntityResolver() {
059: // just instanciate object
060: }
061:
062: /**
063: * @param publicId of xml entity to retrieve
064: * @param systemId of xml entity to retrieve
065: *
066: * @return InputSource of specified xml entity
067: *
068: * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
069: * java.lang.String)
070: */
071: public InputSource resolveEntity(String publicId, String systemId) {
072: StringBuffer logMsg = new StringBuffer(
073: "resolveEntity(publicId=").append(publicId).append(
074: ", systemId=").append(systemId);
075:
076: log.debug(logMsg.toString() + ") - START)");
077:
078: InputSource source = null;
079:
080: try {
081: if (ParserImpl.XSD_URL.equals(systemId)) {
082: URL url = this .getClass().getResource(
083: ParserImpl.XSD_RESOURCE_PATH);
084: Reader xsdReader = new InputStreamReader(this
085: .getClass().getResourceAsStream(
086: ParserImpl.XSD_RESOURCE_PATH));
087:
088: source = new InputSource(url.toExternalForm());
089: source.setCharacterStream(xsdReader);
090: } else if (systemId != null
091: && systemId.startsWith("file:/")) {
092: // try local resource for XML external entity
093: String resourceName = systemId.substring(5, systemId
094: .length());
095:
096: URL url = this .getClass().getResource(resourceName);
097: InputStream resourceStream = this .getClass()
098: .getResourceAsStream(resourceName);
099: if (resourceStream != null) {
100: source = new InputSource(url.toExternalForm());
101: source.setByteStream(resourceStream);
102: }
103: }
104: } finally {
105: log.debug(logMsg + ") - END");
106: }
107:
108: return source;
109: }
110: }
|