001: package org.geotools.maven.xmlcodegen;
002:
003: import java.io.File;
004: import java.net.URL;
005: import java.net.URLClassLoader;
006: import java.util.ArrayList;
007: import java.util.Arrays;
008: import java.util.HashMap;
009: import java.util.HashSet;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import org.apache.maven.artifact.Artifact;
014: import org.apache.maven.artifact.factory.DefaultArtifactFactory;
015: import org.apache.maven.artifact.repository.ArtifactRepository;
016: import org.apache.maven.plugin.MojoExecutionException;
017: import org.apache.maven.plugin.MojoFailureException;
018: import org.apache.maven.project.ProjectUtils;
019: import org.eclipse.xsd.XSDSchema;
020: import org.eclipse.xsd.util.XSDSchemaLocator;
021:
022: /**
023: * Generates the bindings and utility classes used to parse xml documents
024: * for a particular schema.
025: *
026: * @goal generate
027: *
028: * @author Justin Deoliveira, The Open Planning Project
029: *
030: */
031: public class BindingGeneratorMojo extends AbstractGeneratorMojo {
032:
033: /**
034: * Flag controlling wether an interface containg all the element, attribute, and
035: * type names from the schema should be generated. Inclusion / exclusion filters
036: * do not apply.
037: *
038: * @parameter expression="true"
039: */
040: boolean generateNames;
041:
042: /**
043: * Flag controlling wether the binding configuration ( {@link org.geotools.xml.BindingConfiguration} )
044: * should be generated, default is true.
045: *
046: * @parameter expression="true"
047: */
048: boolean generateBindingConfiguration;
049:
050: /**
051: * Flag controlling wether a schema locator ( {@link XSDSchemaLocator} )
052: * should be generated, the default is false.
053: *
054: * @parameter expression="false"
055: */
056: boolean generateSchemaLocationResolver;
057:
058: /**
059: * Flag controlling wether a parser configuration ( {@link org.geotools.xml.Configuration} )
060: * the default is true.
061: *
062: * @parameter expression="true"
063: */
064: boolean generateConfiguration;
065:
066: /**
067: * Flag controlling wether bindings for attributes should be generated, default is
068: * false.
069: *
070: * @parameter expression="false"
071: */
072: boolean generateAttributeBindings;
073:
074: /**
075: * Flag controlling wether bindings for eleements should be generated, default is
076: * false.
077: *
078: * @parameter expression="false"
079: */
080: boolean generateElementBindings;
081:
082: /**
083: * Flag controlling wether bindings for types should be generated, default is
084: * true.
085: *
086: * @parameter expression="true"
087: */
088: boolean generateTypeBindings;
089:
090: /**
091: * List of names of attributes, elements, and types to include, if unset all will
092: * be generated.
093: *
094: * @parameter
095: */
096: String[] includes;
097:
098: /**
099: * List of constructor arguments that should be supplied to generated bindings
100: *
101: * @parameter
102: */
103: BindingConstructorArgument[] bindingConstructorArguments;
104:
105: /**
106: * The base class for complex bindings. If unspecified {@link org.geotools.xml.AbstractComplexBinding}
107: * is used.
108: *
109: * @parameter default="org.geotools.xml.AbstractComplexBinding"
110: *
111: */
112: Class complexBindingBaseClass;
113:
114: /**
115: * The base class for simple bindings. If unspecified {@link org.geotools.xml.AbstractSimpleBinding}
116: * is used.
117: *
118: * @parameter default="org.geotools.xml.AbstractSimpleBinding"
119: */
120: Class simpleBindingBaseClass;
121:
122: public void execute() throws MojoExecutionException,
123: MojoFailureException {
124:
125: XSDSchema xsdSchema = schema();
126: if (xsdSchema == null) {
127: return;
128: }
129:
130: BindingGenerator generator = new BindingGenerator();
131: generator
132: .setGeneratingBindingConfiguration(generateBindingConfiguration);
133: generator.setGeneratingBindingInterface(generateNames);
134: generator.setGenerateAttributes(generateAttributeBindings);
135: generator.setGenerateElements(generateElementBindings);
136: generator.setGenerateTypes(generateTypeBindings);
137: generator.setOverwriting(overwriteExistingFiles);
138: generator.setLocation(outputDirectory.getAbsolutePath());
139:
140: if (destinationPackage != null) {
141: generator.setPackageBase(destinationPackage);
142: }
143:
144: //list of urls to use as class loading locations
145: List urls = new ArrayList();
146: try {
147: List l = project.getCompileClasspathElements();
148: for (Iterator i = l.iterator(); i.hasNext();) {
149: String element = (String) i.next();
150: File d = new File(element);
151:
152: if (d.exists() && d.isDirectory()) {
153: urls.add(d.toURL());
154: }
155: }
156: } catch (Exception e) {
157: getLog().error(e);
158: return;
159: }
160:
161: ClassLoader cl = new URLClassLoader((URL[]) urls
162: .toArray(new URL[urls.size()]));
163: if (bindingConstructorArguments != null) {
164: HashMap map = new HashMap();
165:
166: for (int i = 0; i < bindingConstructorArguments.length; i++) {
167: String name = bindingConstructorArguments[i].getName();
168: String type = bindingConstructorArguments[i].getType();
169: Class clazz = null;
170:
171: try {
172: clazz = cl.loadClass(type);
173: } catch (ClassNotFoundException e) {
174: getLog().error("Could not locate class:" + type);
175: return;
176: }
177:
178: map.put(name, clazz);
179: }
180:
181: generator.setBindingConstructorArguments(map);
182: }
183:
184: if (includes != null && includes.length > 0) {
185: HashSet included = new HashSet(Arrays.asList(includes));
186: getLog().info("Including: " + included);
187: generator.setIncluded(included);
188: }
189:
190: getLog().info("Generating bindings...");
191: generator.generate(xsdSchema);
192:
193: //schema location resolver
194: if (generateSchemaLocationResolver) {
195: SchemaLocationResolverGenerator slrg = new SchemaLocationResolverGenerator();
196: getLog().info(
197: "Generating schema location resolver to "
198: + outputDirectory.getAbsolutePath());
199: slrg.setLocation(outputDirectory.getAbsolutePath());
200: slrg.setOverwriting(overwriteExistingFiles);
201: slrg.setPackageBase(destinationPackage);
202: slrg.setSchemaLookupDirectories(schemaLookupDirectories);
203: slrg.generate(xsdSchema);
204: }
205:
206: //parser configuration
207: if (generateConfiguration) {
208: getLog().info("Generating parser configuration...");
209: ConfigurationGenerator cg = new ConfigurationGenerator();
210: cg.setLocation(outputDirectory.getAbsolutePath());
211: cg.setOverwriting(overwriteExistingFiles);
212: cg.setPackageBase(destinationPackage);
213: cg.setSchemaLookupDirectories(schemaLookupDirectories);
214: cg.generate(xsdSchema);
215: }
216: }
217:
218: }
|