View Javadoc

1   /*
2    * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: ResourceEntityResolver.java,v 2bab6eb875e8 2010/08/11 16:45:50 max $ */
18  
19  package net.sourceforge.jeuclid;
20  
21  import java.io.InputStream;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.annotation.concurrent.ThreadSafe;
26  
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.InputSource;
29  
30  /**
31   * Entity Resolver for standard MathML entities.
32   * <p>
33   * This class contains support for resolving all entities which are in the
34   * default MathML namespaces. It currently has support for
35   * <ul>
36   * <li>MathML 1.0.1</li>
37   * <li>OpenOffice MathML 1.0.1</li>
38   * <li>MathML 2.0</li>
39   * </ul>
40   * 
41   * @version $Revision: 2bab6eb875e8 $
42   */
43  @ThreadSafe
44  public class ResourceEntityResolver implements EntityResolver {
45  
46      /**
47       * The system ID for mathML.
48       */
49      public static final String MML2_SYSTEMID = "http://www.w3.org/TR/MathML2/dtd/mathml2.dtd";
50  
51      /**
52       * The public ID for mathML.
53       */
54      public static final String MML2_PUBLICID = "-//W3C//DTD MathML 2.0//EN";
55  
56      private static final String MML1_SYSTEMID_PATH = "http://www.w3.org/Math/DTD/mathml1";
57  
58      private static final Map<String, String> PUBLIC_ID_TO_INTERNAL = new HashMap<String, String>();
59  
60      private static final Map<String, String> PUBLIC_ID_TO_SYSYEM = new HashMap<String, String>();
61  
62      /**
63       * Default constructor.
64       */
65      public ResourceEntityResolver() {
66          // Empty on purpose.
67      }
68  
69      /** {@inheritDoc} */
70      public InputSource resolveEntity(final String publicId,
71              final String systemId) {
72          InputSource retval = null;
73  
74          String mappedPath = ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
75                  .get(publicId);
76  
77          if ((mappedPath == null)
78                  && (systemId
79                          .startsWith(ResourceEntityResolver.MML1_SYSTEMID_PATH))) {
80              mappedPath = "/net/sourceforge/jeuclid/mathml.1.0.1"
81                      + systemId
82                              .substring(ResourceEntityResolver.MML1_SYSTEMID_PATH
83                                      .length());
84          }
85          if (mappedPath != null) {
86              retval = this.loadMappedResource(publicId, systemId, mappedPath);
87          }
88          return retval;
89      }
90  
91      private InputSource loadMappedResource(final String publicId,
92              final String systemId, final String mappedPath) {
93          InputSource retval = null;
94          final InputStream resourceStream = ResourceEntityResolver.class
95                  .getResourceAsStream(mappedPath);
96          if (resourceStream != null) {
97              retval = new InputSource(resourceStream);
98              retval.setPublicId(publicId);
99              String mappedSystemId = ResourceEntityResolver.PUBLIC_ID_TO_SYSYEM
100                     .get(publicId);
101             if (mappedSystemId == null) {
102                 mappedSystemId = systemId;
103             }
104             retval.setSystemId(mappedSystemId);
105         }
106         return retval;
107     }
108 
109     static {
110         // CHECKSTYLE:OFF
111         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
112                 "-//OpenOffice.org//DTD Modified W3C MathML 1.01//EN",
113                 "/net/sourceforge/jeuclid/openoffice.mathml.1.0.1/math.dtd");
114 
115         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
116                 ResourceEntityResolver.MML2_PUBLICID,
117                 "/net/sourceforge/jeuclid/mathml.2.0/mathml2.dtd");
118 
119         ResourceEntityResolver.PUBLIC_ID_TO_SYSYEM.put(
120                 ResourceEntityResolver.MML2_PUBLICID,
121                 ResourceEntityResolver.MML2_SYSTEMID);
122 
123         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
124                 "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN",
125                 "/net/sourceforge/jeuclid/mathml.2.0/xhtml-math11-f.dtd");
126         ResourceEntityResolver.PUBLIC_ID_TO_SYSYEM.put(
127                 "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN",
128                 "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd");
129 
130         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
131                 "-//W3C//ENTITIES MathML 2.0 Qualified Names 1.0//EN",
132                 "/net/sourceforge/jeuclid/mathml.2.0/mathml2-qname-1.mod");
133         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
134                 .put(
135                         "-//W3C//ENTITIES Added Math Symbols: Arrow Relations for MathML 2.0//EN",
136                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isoamsa.ent");
137         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
138                 .put(
139                         "-//W3C//ENTITIES Added Math Symbols: Binary Operators for MathML 2.0//EN",
140                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isoamsb.ent");
141         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
142                 .put(
143                         "-//W3C//ENTITIES Added Math Symbols: Delimiters for MathML 2.0//EN",
144                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isoamsc.ent");
145         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
146                 .put(
147                         "-//W3C//ENTITIES Added Math Symbols: Negated Relations for MathML 2.0//EN",
148                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isoamsn.ent");
149         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
150                 .put(
151                         "-//W3C//ENTITIES Added Math Symbols: Ordinary for MathML 2.0//EN",
152                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isoamso.ent");
153         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
154                 .put(
155                         "-//W3C//ENTITIES Added Math Symbols: Relations for MathML 2.0//EN",
156                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isoamsr.ent");
157         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
158                 "-//W3C//ENTITIES Greek Symbols for MathML 2.0//EN",
159                 "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isogrk3.ent");
160         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
161                 "-//W3C//ENTITIES Math Alphabets: Fraktur for MathML 2.0//EN",
162                 "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isomfrk.ent");
163         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
164                 .put(
165                         "-//W3C//ENTITIES Math Alphabets: Open Face for MathML 2.0//EN",
166                         "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isomopf.ent");
167         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
168                 "-//W3C//ENTITIES Math Alphabets: Script for MathML 2.0//EN",
169                 "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isomscr.ent");
170         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
171                 "-//W3C//ENTITIES General Technical for MathML 2.0//EN",
172                 "/net/sourceforge/jeuclid/mathml.2.0/iso9573-13/isotech.ent");
173         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
174                 "-//W3C//ENTITIES Box and Line Drawing for MathML 2.0//EN",
175                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isobox.ent");
176         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
177                 "-//W3C//ENTITIES Russian Cyrillic for MathML 2.0//EN",
178                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isocyr1.ent");
179         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
180                 "-//W3C//ENTITIES Non-Russian Cyrillic for MathML 2.0//EN",
181                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isocyr2.ent");
182         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
183                 "-//W3C//ENTITIES Diacritical Marks for MathML 2.0//EN",
184                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isodia.ent");
185         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
186                 "-//W3C//ENTITIES Added Latin 1 for MathML 2.0//EN",
187                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isolat1.ent");
188         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
189                 "-//W3C//ENTITIES Added Latin 2 for MathML 2.0//EN",
190                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isolat2.ent");
191         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
192                 .put(
193                         "-//W3C//ENTITIES Numeric and Special Graphic for MathML 2.0//EN",
194                         "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isonum.ent");
195         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
196                 "-//W3C//ENTITIES Publishing for MathML 2.0//EN",
197                 "/net/sourceforge/jeuclid/mathml.2.0/iso8879/isopub.ent");
198         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
199                 "-//W3C//ENTITIES Extra for MathML 2.0//EN",
200                 "/net/sourceforge/jeuclid/mathml.2.0/mathml/mmlextra.ent");
201         ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put(
202                 "-//W3C//ENTITIES Aliases for MathML 2.0//EN",
203                 "/net/sourceforge/jeuclid/mathml.2.0/mathml/mmlalias.ent");
204         // CHECKSTYLE:ON
205 
206     }
207 }