View Javadoc

1   package org.apache.torque.generator.configuration.source;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.IOException;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * Resolves system Ids for schema files to the schema file content.
33   */
34  public class EntityReferences implements EntityResolver
35  {
36      /** All known entity references. */
37      private Map<String, byte[]> entityReferences
38              = new HashMap<String, byte[]>();
39  
40      /**
41       * Adds a new entity reference.
42       *
43       * @param systemId the systemId of the entity, not null.
44       * @param content the content of the entity, not null.
45       *
46       * @throws NullPointerException if systemId or content are null.
47       * @throws IllegalArgumentException if the systemId is already defined.
48       */
49      public void addEntityReference(String systemId, byte[] content)
50      {
51          if (systemId == null)
52          {
53              throw new NullPointerException("systemId must not be null");
54          }
55          if (content == null)
56          {
57              throw new NullPointerException("content must not be null");
58          }
59          if (entityReferences.containsKey(systemId))
60          {
61              throw new IllegalArgumentException("systemId is already defined");
62          }
63          entityReferences.put(systemId, content);
64      }
65  
66      /**
67       * Returns whether the given system id is known.
68       *
69       * @param systemId the system id to check.
70       *
71       * @return true if the system id can be resolved, false otherwise.
72       */
73      public boolean containsSystemId(String systemId)
74      {
75          return entityReferences.containsKey(systemId);
76      }
77  
78      /**
79       * Returns a copy of the entity reference map.
80       *
81       * @return a copy of the entity reference map.
82       */
83      public Map<String, byte[]> getEntityReferences()
84      {
85          return new HashMap<String, byte[]>(entityReferences);
86      }
87  
88      /**
89       * An implementation of the SAX <code>EntityResolver</code>
90       * interface to be called by the XML parser.  If the systemId is known,
91       * the corresponding resource from the jar is returned.
92       * In all other cases, null is returned to indicate that the parser
93       * should open a regular connection to the systemId URI.
94       *
95       * @param publicId The public identifier of the external entity
96       * @param systemId The system identifier of the external entity
97       * @return An <code>InputSource</code> for the entity if the
98       *         systemId is known, or null otherwise.
99       */
100     public InputSource resolveEntity(String publicId, String systemId)
101             throws IOException, SAXException
102     {
103 
104         byte[] content = entityReferences.get(systemId);
105         if (content == null)
106         {
107             return null;
108         }
109         return new InputSource(new ByteArrayInputStream(content));
110     }
111 }