1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.extras.spring.config; |
12 | |
|
13 | |
import org.mule.config.MuleDtdResolver; |
14 | |
import org.mule.umo.transformer.UMOTransformer; |
15 | |
|
16 | |
import java.io.IOException; |
17 | |
|
18 | |
import javax.xml.transform.Source; |
19 | |
import javax.xml.transform.Transformer; |
20 | |
import javax.xml.transform.TransformerConfigurationException; |
21 | |
import javax.xml.transform.TransformerException; |
22 | |
import javax.xml.transform.TransformerFactory; |
23 | |
import javax.xml.transform.dom.DOMResult; |
24 | |
import javax.xml.transform.dom.DOMSource; |
25 | |
import javax.xml.transform.stream.StreamSource; |
26 | |
|
27 | |
import org.dom4j.io.DOMReader; |
28 | |
import org.springframework.beans.BeansException; |
29 | |
import org.springframework.beans.FatalBeanException; |
30 | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
31 | |
import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
32 | |
import org.springframework.beans.factory.xml.BeansDtdResolver; |
33 | |
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
34 | |
import org.springframework.core.io.ClassPathResource; |
35 | |
import org.springframework.core.io.Resource; |
36 | |
import org.w3c.dom.Document; |
37 | |
import org.xml.sax.EntityResolver; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public class MuleBeanDefinitionReader extends XmlBeanDefinitionReader |
45 | |
{ |
46 | 0 | private int contextCount = 0; |
47 | 0 | private int configCount = 0; |
48 | 0 | private MuleDtdResolver dtdResolver = null; |
49 | |
|
50 | |
public MuleBeanDefinitionReader(BeanDefinitionRegistry beanDefinitionRegistry, int configCount) |
51 | |
{ |
52 | 0 | super(beanDefinitionRegistry); |
53 | |
|
54 | 0 | setDocumentReaderClass(MuleBeanDefinitionDocumentReader.class); |
55 | 0 | setResourceLoader(new MuleResourceLoader()); |
56 | |
|
57 | 0 | setValidationMode(VALIDATION_DTD); |
58 | 0 | setEntityResolver(createEntityResolver()); |
59 | 0 | this.configCount = configCount; |
60 | |
|
61 | |
|
62 | 0 | ((DefaultListableBeanFactory)beanDefinitionRegistry).registerCustomEditor(UMOTransformer.class, |
63 | |
new TransformerEditor()); |
64 | 0 | } |
65 | |
|
66 | |
public int registerBeanDefinitions(Document document, Resource resource) throws BeansException |
67 | |
{ |
68 | |
try |
69 | |
{ |
70 | 0 | Document newDocument = transformDocument(document); |
71 | 0 | return super.registerBeanDefinitions(newDocument, resource); |
72 | |
} |
73 | 0 | catch (Exception e) |
74 | |
{ |
75 | 0 | throw new FatalBeanException("Failed to read config resource: " + resource, e); |
76 | |
} |
77 | |
finally |
78 | |
{ |
79 | 0 | incConfigCount(); |
80 | |
} |
81 | |
} |
82 | |
|
83 | |
public static Transformer createTransformer(Source source) throws TransformerConfigurationException |
84 | |
{ |
85 | 0 | TransformerFactory factory = TransformerFactory.newInstance(); |
86 | 0 | Transformer transformer = factory.newTransformer(source); |
87 | 0 | return transformer; |
88 | |
} |
89 | |
|
90 | |
protected Document transformDocument(Document document) throws IOException, TransformerException |
91 | |
{ |
92 | 0 | if (getXslResource() != null) |
93 | |
{ |
94 | 0 | Transformer transformer = createTransformer(createXslSource()); |
95 | 0 | DOMResult result = new DOMResult(); |
96 | 0 | transformer.setParameter("firstContext", Boolean.valueOf(isFirstContext())); |
97 | 0 | transformer.transform(new DOMSource(document), result); |
98 | 0 | if (logger.isDebugEnabled()) |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 0 | String xml = new DOMReader().read((Document)result.getNode()).asXML(); |
103 | 0 | if (logger.isDebugEnabled()) |
104 | |
{ |
105 | 0 | logger.debug("Transformed document is:\n" + xml); |
106 | |
} |
107 | |
} |
108 | 0 | catch (Exception e) |
109 | |
{ |
110 | 0 | e.printStackTrace(); |
111 | 0 | } |
112 | |
} |
113 | 0 | return (Document)result.getNode(); |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 0 | return document; |
118 | |
} |
119 | |
|
120 | |
} |
121 | |
|
122 | |
protected Source createXslSource() throws IOException |
123 | |
{ |
124 | 0 | return new StreamSource(getXslResource().getInputStream(), getXslResource().getURL().toString()); |
125 | |
} |
126 | |
|
127 | |
protected ClassPathResource getXslResource() |
128 | |
{ |
129 | 0 | String xsl = dtdResolver.getXslForDtd(); |
130 | 0 | if (xsl != null) |
131 | |
{ |
132 | 0 | return new ClassPathResource(xsl); |
133 | |
} |
134 | |
else |
135 | |
{ |
136 | 0 | return null; |
137 | |
} |
138 | |
} |
139 | |
|
140 | |
protected EntityResolver createEntityResolver() |
141 | |
{ |
142 | 0 | if (dtdResolver == null) |
143 | |
{ |
144 | 0 | MuleDtdResolver muleSpringResolver = new MuleDtdResolver("mule-spring-configuration.dtd", |
145 | |
"mule-to-spring.xsl", new BeansDtdResolver()); |
146 | 0 | dtdResolver = new MuleDtdResolver("mule-configuration.dtd", "mule-to-spring.xsl", |
147 | |
muleSpringResolver); |
148 | |
} |
149 | 0 | return dtdResolver; |
150 | |
} |
151 | |
|
152 | |
public boolean isFirstContext() |
153 | |
{ |
154 | 0 | return contextCount == 0; |
155 | |
} |
156 | |
|
157 | |
private void incConfigCount() |
158 | |
{ |
159 | 0 | contextCount++; |
160 | 0 | if (contextCount >= configCount) |
161 | |
{ |
162 | 0 | contextCount = 0; |
163 | |
} |
164 | 0 | } |
165 | |
} |