View Javadoc

1   /*
2    * Created on 19 févr. 2005
3    *
4    * TODO To change the template for this generated file go to
5    * Window - Preferences - Java - Code Style - Code Templates
6    */
7   package genie.core;
8   
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  import java.util.HashMap;
13  
14  import org.xml.sax.EntityResolver;
15  import org.xml.sax.InputSource;
16  import org.xml.sax.SAXException;
17  
18  /***
19   * 
20   *
21   * @author T. Kia Ntoni
22   * 
23   * 19 févr. 2005 
24   * EntityValidator @version 
25   */
26  public class EntityValidator implements IEntityResolver {
27      /***
28       * Logger for this class
29       */
30      private static final Log log = LogFactory.getLog(EntityValidator.class);
31  
32      /***
33       * The URLs of entityValidator that have been registered, keyed by the public
34       * identifier that corresponds.
35       */
36      protected HashMap entityValidator = new HashMap();
37      /***
38       * 
39       */
40      public EntityValidator() {
41          super();
42          // TODO Auto-generated constructor stub
43      }
44      /***
45       * Resolve the requested external entity.
46       *
47       * @param publicId The public identifier of the entity being referenced
48       * @param systemId The system identifier of the entity being referenced
49       *
50       * @exception SAXException if a parsing exception occurs
51       * 
52       */
53      public InputSource resolveEntity(String publicId, String systemId)
54              throws SAXException {
55          if (log.isDebugEnabled()) {
56              log.debug("resolveEntity(String, String) - start");
57          }     
58                  
59          if (log.isDebugEnabled()) {
60              log.debug("resolveEntity('" + publicId + "', '" + systemId + "')");
61          }
62                                         
63          // Has this system identifier been registered?
64          String entityURL = null;
65          if (publicId != null) {
66              entityURL = (String) entityValidator.get(publicId);
67              log.debug(" Entity url based on public ID[" + publicId + "]:'" + entityURL + "'");
68          }
69           
70          // Redirect the schema location to a local destination
71          if (entityURL == null && systemId != null){
72              entityURL = (String)entityValidator.get(systemId);
73              log.debug(" Entity url based on sytem ID[" + systemId + "]:'" + entityURL + "'");
74          } 
75      
76          if (entityURL == null) { 
77              if (systemId == null) {
78                  // cannot resolve
79                  if (log.isDebugEnabled()) {
80                      log.debug(" Cannot resolve entity: '" + entityURL + "'");
81                  }
82                  return (null);
83                  
84              } else {
85                  // try to resolve using system ID
86                  if (log.isDebugEnabled()) {
87                      log.debug(" Trying to resolve using system ID '" + systemId + "'");
88                  } 
89                  entityURL = systemId;
90              }
91          }
92          
93          // Return an input source to our alternative URL
94          if (log.isDebugEnabled()) {
95              log.debug(" Resolving to alternate DTD '" + entityURL + "'");
96          }  
97          
98          try {
99              InputSource returnInputSource = (new InputSource(entityURL));
100             if (log.isDebugEnabled()) {
101                 log.debug("resolveEntity(String, String) - end");
102             }
103             return returnInputSource;
104         } catch (Exception e) {
105             log.error("resolveEntity(String, String)", e);
106 
107             throw new SAXException(e);
108         }
109     }
110     
111     /***
112      * <p>Register the specified DTD URL for the specified public identifier.
113      * This must be called before the first call to <code>parse()</code>.
114      * </p><p>
115      * <code>Digester</code> contains an internal <code>EntityResolver</code>
116      * implementation. This maps <code>PUBLICID</code>'s to URLs 
117      * (from which the resource will be loaded). A common use case for this
118      * method is to register local URLs (possibly computed at runtime by a 
119      * classloader) for DTDs. This allows the performance advantage of using
120      * a local version without having to ensure every <code>SYSTEM</code>
121      * URI on every processed xml document is local. This implementation provides
122      * only basic functionality. If more sophisticated features are required,
123      * using {@link #setEntityResolver} to set a custom resolver is recommended.
124      * </p><p>
125      * <strong>Note:</strong> This method will have no effect when a custom 
126      * <code>EntityResolver</code> has been set. (Setting a custom 
127      * <code>EntityResolver</code> overrides the internal implementation.) 
128      * </p>
129      * @param publicOrSystemId Public identifier of the DTD to be resolved
130      * @param entityURL The URL to use for reading this DTD
131      */
132     public void register(String publicOrSystemId, String entityURL) {
133 
134         if (log.isDebugEnabled()) {
135             log.debug("register('" + publicOrSystemId + "', '" + entityURL + "'");
136         }
137         entityValidator.put(publicOrSystemId, entityURL);
138 
139     }
140 
141 }