1 /*
2 * The contents of this file are subject to the terms of the Common Development and
3 * Distribution License (the License). You may not use this file except in compliance with the
4 * License.
5 *
6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7 * specific language governing permission and limitations under the License.
8 *
9 * When distributing Covered Software, include this CDDL Header Notice in each file and include
10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11 * Header, with the fields enclosed by brackets [] replaced by your own identifying
12 * information: "Portions copyright [year] [name of copyright owner]".
13 *
14 * Copyright 2015-2016 ForgeRock AS.
15 */
16
17 package org.forgerock.util.xml;
18
19 import java.io.InputStream;
20 import java.io.StringReader;
21
22 import org.xml.sax.InputSource;
23 import org.xml.sax.helpers.DefaultHandler;
24
25 /**
26 * This is a custom XML handler to load the dtds from the classpath This should
27 * be used by all the xml parsing document builders to set the default entity
28 * resolvers. This will avoid to have the dtds having specified in a fixed
29 * directory that will get replaced during installation This will need to
30 * specify the dtds as follows jar://com/sun/identity/sm/sms.dtd Bundle all the
31 * dtds along with the jar files and
32 */
33 public class XMLHandler extends DefaultHandler {
34
35 /* FIXME: does this class need to be non-final and public? */
36
37 /**
38 * Creates a new XML handler.
39 */
40 public XMLHandler() {
41 // No impl.
42 }
43
44 @Override
45 public InputSource resolveEntity(final String aPublicID, final String aSystemID) {
46 final String sysid = aSystemID.trim();
47
48 if (sysid.toLowerCase().startsWith("jar://")) {
49 final String dtdname = sysid.substring(5);
50 final InputStream is = getClass().getResourceAsStream(dtdname);
51 if (is != null) {
52 return new InputSource(is);
53 }
54 }
55
56 /*
57 * make sure that we do NOT return null here, as xerces would fall back
58 * to the default entity resolver and try to resolve the entity with
59 * that
60 */
61 return new InputSource(new StringReader(""));
62 }
63 }