| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
@ThreadSafe |
| 44 | |
public class ResourceEntityResolver implements EntityResolver { |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public static final String MML2_SYSTEMID = "http://www.w3.org/TR/MathML2/dtd/mathml2.dtd"; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 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 | 209 | private static final Map<String, String> PUBLIC_ID_TO_INTERNAL = new HashMap<String, String>(); |
| 59 | |
|
| 60 | 209 | private static final Map<String, String> PUBLIC_ID_TO_SYSYEM = new HashMap<String, String>(); |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | 1518 | public ResourceEntityResolver() { |
| 66 | |
|
| 67 | 1518 | } |
| 68 | |
|
| 69 | |
|
| 70 | |
public InputSource resolveEntity(final String publicId, |
| 71 | |
final String systemId) { |
| 72 | 72314 | InputSource retval = null; |
| 73 | |
|
| 74 | 72314 | String mappedPath = ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL |
| 75 | |
.get(publicId); |
| 76 | |
|
| 77 | 72314 | if ((mappedPath == null) |
| 78 | |
&& (systemId |
| 79 | |
.startsWith(ResourceEntityResolver.MML1_SYSTEMID_PATH))) { |
| 80 | 0 | mappedPath = "/net/sourceforge/jeuclid/mathml.1.0.1" |
| 81 | |
+ systemId |
| 82 | |
.substring(ResourceEntityResolver.MML1_SYSTEMID_PATH |
| 83 | |
.length()); |
| 84 | |
} |
| 85 | 72314 | if (mappedPath != null) { |
| 86 | 72314 | retval = this.loadMappedResource(publicId, systemId, mappedPath); |
| 87 | |
} |
| 88 | 72314 | return retval; |
| 89 | |
} |
| 90 | |
|
| 91 | |
private InputSource loadMappedResource(final String publicId, |
| 92 | |
final String systemId, final String mappedPath) { |
| 93 | 72314 | InputSource retval = null; |
| 94 | 72314 | final InputStream resourceStream = ResourceEntityResolver.class |
| 95 | |
.getResourceAsStream(mappedPath); |
| 96 | 72314 | if (resourceStream != null) { |
| 97 | 72314 | retval = new InputSource(resourceStream); |
| 98 | 72314 | retval.setPublicId(publicId); |
| 99 | 72314 | String mappedSystemId = ResourceEntityResolver.PUBLIC_ID_TO_SYSYEM |
| 100 | |
.get(publicId); |
| 101 | 72314 | if (mappedSystemId == null) { |
| 102 | 69179 | mappedSystemId = systemId; |
| 103 | |
} |
| 104 | 72314 | retval.setSystemId(mappedSystemId); |
| 105 | |
} |
| 106 | 72314 | return retval; |
| 107 | |
} |
| 108 | |
|
| 109 | |
static { |
| 110 | |
|
| 111 | 209 | 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 | 209 | ResourceEntityResolver.PUBLIC_ID_TO_INTERNAL.put( |
| 116 | |
ResourceEntityResolver.MML2_PUBLICID, |
| 117 | |
"/net/sourceforge/jeuclid/mathml.2.0/mathml2.dtd"); |
| 118 | |
|
| 119 | 209 | ResourceEntityResolver.PUBLIC_ID_TO_SYSYEM.put( |
| 120 | |
ResourceEntityResolver.MML2_PUBLICID, |
| 121 | |
ResourceEntityResolver.MML2_SYSTEMID); |
| 122 | |
|
| 123 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | 209 | 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 | |
|
| 205 | |
|
| 206 | 209 | } |
| 207 | |
} |