1 package xdoclet.sdk.xtag.migrator;
2
3 import xdoclet.plugins.VelocityPlugin;
4 import xdoclet.XDocletException;
5 import xdoclet.MetadataProvider;
6 import xdoclet.util.betwixt.BetwixtConfigurer;
7 import xdoclet.sdk.xgg.binding.NamingUtils;
8 import xdoclet.sdk.xgg.XGGPojo;
9
10 import java.util.Collection;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.io.File;
14 import java.io.IOException;
15 import java.beans.IntrospectionException;
16
17 import org.apache.commons.betwixt.io.BeanReader;
18 import org.apache.commons.betwixt.XMLIntrospector;
19 import org.apache.commons.logging.LogFactory;
20 import org.xml.sax.SAXException;
21 import org.xml.sax.EntityResolver;
22 import org.xml.sax.InputSource;
23
24 /***
25 * This plugin turns XDoclet 1.2 style xtags.xml into XDoclet 2.0 style
26 * XTag beans.
27 *
28 * @bean.class name="xtagxml2bean"
29 * displayName="xtag.xml 1.2 migration plugin"
30 * shortDescription="A plugin that turns XDoclet 1.2 style xtags.xml into XDoclet 2.0 style beans."
31 *
32 * @bean.attribute name="xdoclet-class" value="xdoclet.XDoclet"
33 *
34 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Hellesøy</a>
35 * @version $Revision: 1.8 $
36 */
37 public class XTagXml2BeanPlugin extends VelocityPlugin {
38
39 private File _xtagXml;
40
41 public XTagXml2BeanPlugin() {
42 setTemplatePath( "xdoclet/sdk/xtag/migrator/XTagXml2Bean.vm");
43 setFileName("{0}Tag.java");
44 XGGPojo.setVersion("xtags_1_1.dtd");
45 }
46
47 public void setXtagXml( File xtagXml ) {
48 _xtagXml = xtagXml;
49 }
50
51 /***
52 * Called by template to get the java class name for a tag.
53 *
54 * @param tagName
55 * @return
56 */
57 public String getJavaClassName( String tagName ) {
58 return NamingUtils.getJavaClassName( tagName );
59 }
60
61 public MetadataProvider getMetadataProvider() {
62 return new XTagXml2BeanMetadataProvider();
63 }
64
65 private class XTagXml2BeanMetadataProvider implements MetadataProvider {
66 /***
67 * Creates a Collection of {@link xdoclet.sdk.xtag.migrator.Tag}.
68 */
69 public Collection createMetadataCollection() throws XDocletException {
70 if( _xtagXml == null ) {
71 throw new XDocletException("xtagXml must be set.");
72 }
73 if( !_xtagXml.exists() ) {
74 throw new XDocletException( _xtagXml.getAbsolutePath() + " doesn't exist.");
75 }
76 Collection result = new ArrayList();
77
78 // Turn it into a bean model with Betwixt.
79 // See http://jakarta.apache.org/commons/betwixt/overview.html
80 XMLIntrospector introspector = new XMLIntrospector();
81 BetwixtConfigurer.configureIntrospector( introspector, null );
82
83 BeanReader beanReader = new BeanReader();
84 beanReader.setXMLIntrospector( introspector );
85
86 try {
87 // Register the xtags.xml beans
88 beanReader.registerBeanClass(Condition.class);
89 beanReader.registerBeanClass(ConditionDescription.class);
90 beanReader.registerBeanClass(ConditionParameter.class);
91 beanReader.registerBeanClass(Deprecated.class);
92 beanReader.registerBeanClass(Level.class);
93 beanReader.registerBeanClass(Mandatory.class);
94 beanReader.registerBeanClass(Xmlname.class);
95 beanReader.registerBeanClass(Namespace.class);
96 beanReader.registerBeanClass(Options.class);
97 beanReader.registerBeanClass(Option.class);
98 beanReader.registerBeanClass(OptionSets.class);
99 beanReader.registerBeanClass(OptionSet.class);
100 beanReader.registerBeanClass(Parameter.class);
101 beanReader.registerBeanClass(Tag.class);
102 beanReader.registerBeanClass(Tags.class);
103 beanReader.registerBeanClass(Type.class);
104 beanReader.registerBeanClass(Unique.class);
105 beanReader.registerBeanClass(UsageDescription.class);
106 beanReader.registerBeanClass(Xdoclet.class);
107 beanReader.registerBeanClass(Xmldefault.class);
108
109 // Set a new EntityResolver to find the DTD offline.
110 beanReader.setEntityResolver(new EntityResolver() {
111 public InputSource resolveEntity(String publicId,
112 String systemId)
113 throws SAXException, IOException {
114 return new InputSource(getClass().getResourceAsStream("xtags_1_1.dtd"));
115 }
116 });
117
118 Xdoclet xdoclet = (Xdoclet) beanReader.parse(_xtagXml);
119
120 // DEBUG
121 /*
122 System.out.println("xdoclet.xml");
123 BeanWriter beanWriter = new BeanWriter(System.out);
124
125 // Configure Betwixt
126 beanWriter.getXMLIntrospector().setAttributesForPrimitives(true);
127 beanWriter.getXMLIntrospector().setWrapCollectionsInElement(false);
128 beanWriter.enablePrettyPrint();
129 beanWriter.setWriteIDs(false);
130 beanWriter.write(xdoclet);
131 beanWriter.flush();
132 */
133
134 Collection namespaceCollection = xdoclet.getNamespaceCollection();
135 for (Iterator iterator = namespaceCollection.iterator(); iterator.hasNext();) {
136 Namespace namespace = (Namespace) iterator.next();
137 Tags tags = namespace.getTags();
138 Collection tagCollection = tags.getTagCollection();
139 result.addAll( tagCollection );
140 }
141 return result;
142 } catch (IOException e) {
143 LogFactory.getLog(XTagXml2BeanPlugin.class).error(e.getMessage(), e);
144 throw new XDocletException(e);
145 } catch (SAXException e) {
146 LogFactory.getLog(XTagXml2BeanPlugin.class).error(e.getMessage(), e);
147 throw new XDocletException(e);
148 } catch (IntrospectionException e) {
149 LogFactory.getLog(XTagXml2BeanPlugin.class).error(e.getMessage(), e);
150 throw new XDocletException(e);
151 }
152 }
153
154 public String getFilenameSubstitutionValue(Object o) throws XDocletException {
155 Tag tag = (Tag) o;
156 Xmlname xmlName = tag.getXmlname();
157 String tagName = xmlName.getPcData();
158 return NamingUtils.getJavaClassName( tagName );
159 /*
160 int dot = tagName.lastIndexOf('.');
161 if( dot != -1 ) {
162 tagName = tagName.substring( dot );
163 }
164 String className = NamingUtils.getJavaClassName(tagName);
165 return className;
166 */
167 }
168
169 public String getPackageName(Object o) {
170 return XTagXml2BeanPlugin.this.getPackageName();
171 }
172
173 public void cleanup() throws XDocletException {
174 }
175
176 public void setClasspath( String classpath ) throws XDocletException {
177 }
178
179 }
180 }
This page was automatically generated by Maven