001 /*
002 * Copyright 2002 - 2007 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: ResourceEntityResolver.java,v 2bab6eb875e8 2010/08/11 16:45:50 max $ */
018
019 package net.sourceforge.jeuclid;
020
021 import java.io.InputStream;
022 import java.util.HashMap;
023 import java.util.Map;
024
025 import javax.annotation.concurrent.ThreadSafe;
026
027 import org.xml.sax.EntityResolver;
028 import org.xml.sax.InputSource;
029
030 /**
031 * Entity Resolver for standard MathML entities.
032 * <p>
033 * This class contains support for resolving all entities which are in the
034 * default MathML namespaces. It currently has support for
035 * <ul>
036 * <li>MathML 1.0.1</li>
037 * <li>OpenOffice MathML 1.0.1</li>
038 * <li>MathML 2.0</li>
039 * </ul>
040 *
041 * @version $Revision: 2bab6eb875e8 $
042 */
043 @ThreadSafe
044 public class ResourceEntityResolver implements EntityResolver {
045
046 /**
047 * The system ID for mathML.
048 */
049 public static final String MML2_SYSTEMID = "http://www.w3.org/TR/MathML2/dtd/mathml2.dtd";
050
051 /**
052 * The public ID for mathML.
053 */
054 public static final String MML2_PUBLICID = "-//W3C//DTD MathML 2.0//EN";
055
056 private static final String MML1_SYSTEMID_PATH = "http://www.w3.org/Math/DTD/mathml1";
057
058 private static final Map<String, String> PUBLIC_ID_TO_INTERNAL = new HashMap<String, String>();
059
060 private static final Map<String, String> PUBLIC_ID_TO_SYSYEM = new HashMap<String, String>();
061
062 /**
063 * Default constructor.
064 */
065 public ResourceEntityResolver() {
066 // Empty on purpose.
067 }
068
069 /** {@inheritDoc} */
070 public InputSource resolveEntity(final String publicId,
071 final String systemId) {
072 InputSource retval = null;
073
074 String mappedPath = ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL
075 .get(publicId);
076
077 if ((mappedPath == null)
078 && (systemId
079 .startsWith(ResourceEntityResolver.MML1_SYSTEMID_PATH))) {
080 mappedPath = "/net/sourceforge/jeuclid/mathml.1.0.1"
081 + systemId
082 .substring(ResourceEntityResolver.MML1_SYSTEMID_PATH
083 .length());
084 }
085 if (mappedPath != null) {
086 retval = this.loadMappedResource(publicId, systemId, mappedPath);
087 }
088 return retval;
089 }
090
091 private InputSource loadMappedResource(final String publicId,
092 final String systemId, final String mappedPath) {
093 InputSource retval = null;
094 final InputStream resourceStream = ResourceEntityResolver.class
095 .getResourceAsStream(mappedPath);
096 if (resourceStream != null) {
097 retval = new InputSource(resourceStream);
098 retval.setPublicId(publicId);
099 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 }