View Javadoc

1   // $Id: EntityResolver.java 351 2008-08-14 20:20:56Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the name of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   ********************************************************************************/
38  package junitx.ddtunit.data.processing.parser;
39  
40  import java.io.InputStream;
41  import java.io.InputStreamReader;
42  import java.io.Reader;
43  import java.net.URL;
44  
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  import org.xml.sax.InputSource;
48  
49  /**
50   * Example taken from: David Brownell, SAX2, O'Reilly 2002 Paris, p.88ff.
51   * 
52   * @author jg
53   */
54  class EntityResolver implements org.xml.sax.EntityResolver {
55      private Logger log = LoggerFactory.getLogger(EntityResolver.class);
56  
57      /**
58       * Default contructor no extras
59       */
60      public EntityResolver() {
61          // just instanciate object
62      }
63  
64      /**
65       * @param publicId of xml entity to retrieve
66       * @param systemId of xml entity to retrieve
67       * 
68       * @return InputSource of specified xml entity
69       * 
70       * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
71       *      java.lang.String)
72       */
73      public InputSource resolveEntity(String publicId, String systemId) {
74          StringBuffer logMsg = new StringBuffer("resolveEntity(publicId=")
75              .append(publicId).append(", systemId=").append(systemId);
76  
77          log.debug(logMsg.toString() + ") - START)");
78  
79          InputSource source = null;
80  
81          try {
82              if (ParserImpl.XSD_URL.equals(systemId)) {
83                  URL url = this.getClass().getResource(ParserImpl.XSD_RESOURCE_PATH);
84                  Reader xsdReader = new InputStreamReader(this.getClass()
85                      .getResourceAsStream(ParserImpl.XSD_RESOURCE_PATH));
86  
87                  source = new InputSource(url.toExternalForm());
88                  source.setCharacterStream(xsdReader);
89              } else if (systemId != null && systemId.startsWith("file:/")) {
90                  // try local resource for XML external entity
91                  String resourceName = systemId.substring(5, systemId.length());
92  
93                  URL url = this.getClass().getResource(resourceName);
94                  InputStream resourceStream = this.getClass()
95                      .getResourceAsStream(resourceName);
96                  if (resourceStream != null) {
97                      source = new InputSource(url.toExternalForm());
98                      source.setByteStream(resourceStream);
99                  }
100             }
101         } finally {
102             log.debug(logMsg + ") - END");
103         }
104 
105         return source;
106     }
107 }