1 4 package com.tc.config.schema.beanfactory; 5 6 import org.apache.xmlbeans.XmlError; 7 import org.apache.xmlbeans.XmlException; 8 import org.apache.xmlbeans.XmlOptions; 9 import org.xml.sax.SAXException ; 10 11 import com.tc.config.Loader; 12 import com.tc.util.Assert; 13 import com.terracottatech.config.TcConfigDocument; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import javax.xml.parsers.ParserConfigurationException ; 21 22 25 public class TerracottaDomainConfigurationDocumentBeanFactory implements ConfigBeanFactory { 26 27 public TerracottaDomainConfigurationDocumentBeanFactory() { 28 } 30 31 public BeanWithErrors createBean(InputStream in, String sourceDescription) throws IOException , SAXException , 32 ParserConfigurationException , XmlException { 33 Assert.assertNotBlank(sourceDescription); 34 35 List errors = new ArrayList (); 36 XmlOptions options = createXmlOptions(errors, sourceDescription); 37 Loader configLoader = new Loader(); 38 39 TcConfigDocument document = configLoader.parse(in, options); 40 document.validate(options); 41 return new BeanWithErrors(document, (XmlError[]) errors.toArray(new XmlError[errors.size()])); 42 } 43 44 public static XmlOptions createXmlOptions(List errors, String sourceDescription) 45 throws SAXException , ParserConfigurationException { 46 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); 47 Thread.currentThread().setContextClassLoader( 48 TerracottaDomainConfigurationDocumentBeanFactory.class 49 .getClassLoader()); 50 try { 51 XmlOptions options = new XmlOptions(); 52 options = options.setLoadLineNumbers(); 53 options = options.setDocumentSourceName(sourceDescription); 54 options = options.setErrorListener(errors); 55 return options; 56 57 } finally { 58 Thread.currentThread().setContextClassLoader(contextClassLoader); 59 } 60 61 } 62 } 63 | Popular Tags |