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