KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > tools > DTDEntityResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: DTDEntityResolver.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.tools;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import org.apache.fop.render.afp.exceptions.FontRuntimeException;
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28
29 /**
30  * An entity resolver for both DOM and SAX models of the SAX document.
31  * <p>
32  * The entity resolver only handles queries for the DTD. It will find any URI
33  * with a recognised public id and return an {@link org.xml.sax.InputSource}.
34  * <p>
35  * @author <a HREF="mailto:joe@exubero.com">Joe Schmetzer</a>
36  */

37 public class DTDEntityResolver implements EntityResolver JavaDoc {
38
39     /** Public ID for the AFP fonts 1.0 DTD. */
40     public static final String JavaDoc AFP_DTD_1_0_ID = "-//APACHE/DTD AFP Installed Font Definition DTD 1.0//EN";
41
42     /** Resource location for the AFP fonts 1.0 DTD. */
43     public static final String JavaDoc AFP_DTD_1_0_RESOURCE = "afp-fonts-1.0.dtd";
44
45     /** Public ID for the AFP fonts 1.1 DTD. */
46     public static final String JavaDoc AFP_DTD_1_1_ID = "-//APACHE/DTD AFP Installed Font Definition DTD 1.1//EN";
47
48     /** Resource location for the AFP fonts 1.1 DTD. */
49     public static final String JavaDoc AFP_DTD_1_1_RESOURCE = "afp-fonts-1.1.dtd";
50
51     /** Public ID for the AFP fonts 1.2 DTD. */
52     public static final String JavaDoc AFP_DTD_1_2_ID = "-//APACHE/DTD AFP Installed Font Definition DTD 1.2//EN";
53
54     /** Resource location for the AFP fonts 1.2 DTD. */
55     public static final String JavaDoc AFP_DTD_1_2_RESOURCE = "afp-fonts-1.2.dtd";
56
57     /**
58      * Resolve the combination of system and public identifiers.
59      * If this resolver recognises the publicId, it will handle the resolution
60      * from the classpath, otherwise it will return null and allow the default
61      * resolution to occur.
62      *
63      * @param publicId the public identifier to use
64      * @param systemId the system identifier to resolve
65      * @return An input source to the entity or null if not handled
66      * @throws IOException an error reading the stream
67      */

68     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
69     throws IOException JavaDoc {
70
71         URL JavaDoc resource = null;
72         if ( AFP_DTD_1_2_ID.equals(publicId) ) {
73             resource = getResource( AFP_DTD_1_2_RESOURCE );
74         } else if ( AFP_DTD_1_1_ID.equals(publicId) ) {
75             resource = getResource( AFP_DTD_1_1_RESOURCE );
76         } else if ( AFP_DTD_1_0_ID.equals(publicId) ) {
77             throw new FontRuntimeException(
78                 "The AFP Installed Font Definition 1.0 DTD is not longer supported" );
79         } else if( systemId != null && systemId.indexOf("afp-fonts.dtd") >= 0 ) {
80             throw new FontRuntimeException(
81                 "The AFP Installed Font Definition DTD must be specified using the public id" );
82         } else {
83             return null;
84         }
85
86         InputSource JavaDoc inputSource = new InputSource JavaDoc( resource.openStream() );
87         inputSource.setPublicId( publicId );
88         inputSource.setSystemId( systemId );
89
90         return inputSource;
91     }
92
93     /**
94      * Returns the URL of a resource on the classpath
95      * @param resourceName the path to the resource relative to the root of the
96      * classpath.
97      * @return the URL of the required resource
98      * @throws FontRuntimeException if the resource could not be found.
99      */

100     private URL JavaDoc getResource(String JavaDoc resourcePath) {
101         ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
102         if (cl == null) {
103             cl = ClassLoader.getSystemClassLoader();
104         }
105
106         URL JavaDoc resource = cl.getResource( resourcePath );
107         if (resource == null) {
108             throw new FontRuntimeException( "Resource " + resourcePath +
109                 " could not be found on the classpath" );
110         }
111
112         return resource;
113     }
114 }
115
Popular Tags