1 package org.codehaus.plexus.spring;
\r
4 * Licensed to the Apache Software Foundation (ASF) under one
\r
5 * or more contributor license agreements. See the NOTICE file
\r
6 * distributed with this work for additional information
\r
7 * regarding copyright ownership. The ASF licenses this file
\r
8 * to you under the Apache License, Version 2.0 (the
\r
9 * "License"); you may not use this file except in compliance
\r
10 * with the License. You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing,
\r
15 * software distributed under the License is distributed on an
\r
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
\r
17 * KIND, either express or implied. See the License for the
\r
18 * specific language governing permissions and limitations
\r
19 * under the License.
\r
22 import java.io.InputStream;
\r
24 import javax.xml.transform.Source;
\r
25 import javax.xml.transform.Transformer;
\r
26 import javax.xml.transform.TransformerFactory;
\r
27 import javax.xml.transform.dom.DOMResult;
\r
28 import javax.xml.transform.dom.DOMSource;
\r
29 import javax.xml.transform.stream.StreamSource;
\r
31 import org.dom4j.io.DOMReader;
\r
32 import org.dom4j.io.OutputFormat;
\r
33 import org.dom4j.io.XMLWriter;
\r
34 import org.springframework.beans.factory.BeanDefinitionStoreException;
\r
35 import org.springframework.beans.factory.xml.BeanDefinitionDocumentReader;
\r
36 import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
\r
37 import org.springframework.beans.factory.xml.XmlReaderContext;
\r
38 import org.w3c.dom.Document;
\r
41 * A Spring {@link BeanDefinitionDocumentReader} that converts on the fly the
\r
42 * Plexus components descriptor to a spring XML context.
\r
44 * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
\r
46 public class PlexusBeanDefinitionDocumentReader
\r
47 extends DefaultBeanDefinitionDocumentReader
\r
49 public void registerBeanDefinitions( Document doc, XmlReaderContext readerContext )
\r
51 doc = convertPlexusDescriptorToSpringBeans( doc );
\r
52 if ( Boolean.getBoolean( "plexus-spring.debug" ) )
\r
56 XMLWriter writer = new XMLWriter( System.out, OutputFormat.createPrettyPrint() );
\r
57 writer.write( new DOMReader().read( doc ) );
\r
59 catch ( Exception e )
\r
65 super.registerBeanDefinitions( doc, readerContext );
\r
68 protected Document convertPlexusDescriptorToSpringBeans( Document doc )
\r
70 if ( !"component-set".equals( doc.getDocumentElement().getNodeName() ) )
\r
77 Source xmlSource = new DOMSource( doc );
\r
78 InputStream is = getClass().getResourceAsStream( "PlexusBeanDefinitionDocumentReader.xsl" );
\r
79 Source xsltSource = new StreamSource( is );
\r
81 DOMResult transResult = new DOMResult();
\r
83 TransformerFactory tf = TransformerFactory.newInstance();
\r
84 Transformer t = tf.newTransformer( xsltSource );
\r
85 t.transform( xmlSource, transResult );
\r
87 return (Document) transResult.getNode();
\r
89 catch ( Exception e )
\r
91 throw new BeanDefinitionStoreException(
\r
92 "Failed to translate plexus component descriptor to Spring XML context", e );
\r