KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > config > MuleDtdResolver


1 /*
2  * $Id: MuleDtdResolver.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.config;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.mule.util.IOUtils;
19 import org.mule.util.StringUtils;
20 import org.xml.sax.EntityResolver JavaDoc;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 /**
25  * <code>MuleDtdResolver</code> attempts to locate the mule-configuration.dtd on
26  * the classpath, regardless of the DOCTYPE declaration. If the dtd is not found, it
27  * defaults to trying to download it using the systemId. <p/> This resolve is
28  * responsible for associating an Xsl document if any with the Dtd. It also allows
29  * for a delegate Entity resolver and delegate Xsl. This allows Configuration
30  * builders to mix Mule Xml configuration with other document based configuration and
31  * apply transformers to each of the configuration types (if necessary) before
32  * constucting a Mule instance. <p/> Note that its up to the Configuration builder
33  * implementation to do the actual transformations this Resolver simply associates
34  * Xsl reosurces with dtds
35  *
36  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
37  * @version $Revision: 4259 $
38  */

39 public class MuleDtdResolver implements EntityResolver JavaDoc
40 {
41     /**
42      * logger used by this class
43      */

44     protected static final Log logger = LogFactory.getLog(MuleDtdResolver.class);
45
46     public static final String JavaDoc DEFAULT_MULE_DTD = "mule-configuration.dtd";
47     private String JavaDoc dtdName = null;
48
49     // Maybe the dtd should go in the META-INF??
50
private static final String JavaDoc SEARCH_PATH = "";
51
52     private EntityResolver JavaDoc delegate;
53     private String JavaDoc xsl;
54     private static String JavaDoc currentXsl;
55
56     public MuleDtdResolver()
57     {
58         this(DEFAULT_MULE_DTD);
59     }
60
61     public MuleDtdResolver(String JavaDoc dtdName)
62     {
63         this(dtdName, null, null);
64     }
65
66     public MuleDtdResolver(String JavaDoc dtdName, String JavaDoc xsl)
67     {
68         this(dtdName, xsl, null);
69     }
70
71     public MuleDtdResolver(String JavaDoc dtdName, EntityResolver JavaDoc delegate)
72     {
73         this(dtdName, null, delegate);
74     }
75
76     public MuleDtdResolver(String JavaDoc dtdName, String JavaDoc xsl, EntityResolver JavaDoc delegate)
77     {
78         this.dtdName = dtdName;
79         this.delegate = delegate;
80         this.xsl = xsl;
81         if (logger.isDebugEnabled())
82         {
83             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
84             buffer.append("Created Mule Dtd Resolver: ");
85             buffer.append("dtd=").append(dtdName).append(", ");
86             buffer.append("xsl=").append(xsl).append(", ");
87             buffer.append("delegate resolver=").append(delegate).append(", ");
88             logger.debug(buffer.toString());
89         }
90     }
91
92     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws IOException JavaDoc, SAXException JavaDoc
93     {
94         logger.debug("Trying to resolve XML entity with public ID: " + publicId + " and system ID: "
95                      + systemId);
96
97         InputSource JavaDoc source = null;
98         currentXsl = null;
99         if (delegate != null)
100         {
101             source = delegate.resolveEntity(publicId, systemId);
102         }
103         if ((source == null) && StringUtils.isNotBlank(systemId) && systemId.endsWith(".dtd"))
104         {
105             String JavaDoc[] tokens = systemId.split("/");
106             String JavaDoc dtdFile = tokens[tokens.length - 1];
107             logger.debug("Looking on classpath for " + SEARCH_PATH + dtdFile);
108
109             InputStream JavaDoc is = IOUtils.getResourceAsStream(SEARCH_PATH + dtdFile, getClass(), /* tryAsFile */
110                 true, /* tryAsUrl */false);
111             if (is != null)
112             {
113                 source = new InputSource JavaDoc(is);
114                 source.setPublicId(publicId);
115                 source.setSystemId(systemId);
116                 logger.debug("Found on classpath mule DTD: " + systemId);
117                 currentXsl = xsl;
118                 return source;
119             }
120             logger.debug("Could not find dtd resource on classpath: " + SEARCH_PATH + dtdFile);
121         }
122         return source;
123     }
124
125     public String JavaDoc getXslForDtd()
126     {
127         return currentXsl;
128     }
129 }
130
Popular Tags