001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: DTDEntityResolver.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.afp.tools;
021:
022: import java.io.IOException;
023: import java.net.URL;
024:
025: import org.apache.fop.render.afp.exceptions.FontRuntimeException;
026: import org.xml.sax.EntityResolver;
027: import org.xml.sax.InputSource;
028:
029: /**
030: * An entity resolver for both DOM and SAX models of the SAX document.
031: * <p>
032: * The entity resolver only handles queries for the DTD. It will find any URI
033: * with a recognised public id and return an {@link org.xml.sax.InputSource}.
034: * <p>
035: * @author <a href="mailto:joe@exubero.com">Joe Schmetzer</a>
036: */
037: public class DTDEntityResolver implements EntityResolver {
038:
039: /** Public ID for the AFP fonts 1.0 DTD. */
040: public static final String AFP_DTD_1_0_ID = "-//APACHE/DTD AFP Installed Font Definition DTD 1.0//EN";
041:
042: /** Resource location for the AFP fonts 1.0 DTD. */
043: public static final String AFP_DTD_1_0_RESOURCE = "afp-fonts-1.0.dtd";
044:
045: /** Public ID for the AFP fonts 1.1 DTD. */
046: public static final String AFP_DTD_1_1_ID = "-//APACHE/DTD AFP Installed Font Definition DTD 1.1//EN";
047:
048: /** Resource location for the AFP fonts 1.1 DTD. */
049: public static final String AFP_DTD_1_1_RESOURCE = "afp-fonts-1.1.dtd";
050:
051: /** Public ID for the AFP fonts 1.2 DTD. */
052: public static final String AFP_DTD_1_2_ID = "-//APACHE/DTD AFP Installed Font Definition DTD 1.2//EN";
053:
054: /** Resource location for the AFP fonts 1.2 DTD. */
055: public static final String AFP_DTD_1_2_RESOURCE = "afp-fonts-1.2.dtd";
056:
057: /**
058: * Resolve the combination of system and public identifiers.
059: * If this resolver recognises the publicId, it will handle the resolution
060: * from the classpath, otherwise it will return null and allow the default
061: * resolution to occur.
062: *
063: * @param publicId the public identifier to use
064: * @param systemId the system identifier to resolve
065: * @return An input source to the entity or null if not handled
066: * @throws IOException an error reading the stream
067: */
068: public InputSource resolveEntity(String publicId, String systemId)
069: throws IOException {
070:
071: URL resource = null;
072: if (AFP_DTD_1_2_ID.equals(publicId)) {
073: resource = getResource(AFP_DTD_1_2_RESOURCE);
074: } else if (AFP_DTD_1_1_ID.equals(publicId)) {
075: resource = getResource(AFP_DTD_1_1_RESOURCE);
076: } else if (AFP_DTD_1_0_ID.equals(publicId)) {
077: throw new FontRuntimeException(
078: "The AFP Installed Font Definition 1.0 DTD is not longer supported");
079: } else if (systemId != null
080: && systemId.indexOf("afp-fonts.dtd") >= 0) {
081: throw new FontRuntimeException(
082: "The AFP Installed Font Definition DTD must be specified using the public id");
083: } else {
084: return null;
085: }
086:
087: InputSource inputSource = new InputSource(resource.openStream());
088: inputSource.setPublicId(publicId);
089: inputSource.setSystemId(systemId);
090:
091: return inputSource;
092: }
093:
094: /**
095: * Returns the URL of a resource on the classpath
096: * @param resourceName the path to the resource relative to the root of the
097: * classpath.
098: * @return the URL of the required resource
099: * @throws FontRuntimeException if the resource could not be found.
100: */
101: private URL getResource(String resourcePath) {
102: ClassLoader cl = Thread.currentThread().getContextClassLoader();
103: if (cl == null) {
104: cl = ClassLoader.getSystemClassLoader();
105: }
106:
107: URL resource = cl.getResource(resourcePath);
108: if (resource == null) {
109: throw new FontRuntimeException("Resource " + resourcePath
110: + " could not be found on the classpath");
111: }
112:
113: return resource;
114: }
115: }
|