Author: jochen Date: Tue Sep 5 05:36:42 2006 New Revision: 440331 URL: http://svn.apache.org/viewvc?view=rev&rev=440331 Log: Added the EntityResolverFactory.
Added: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/EntityResolverFactory.java Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/pom.xml webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/ISchemaCollection.java webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeMojo.java webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollection.java webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollectionProcessor.java webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/test/java/org/apache/ws/jaxme/maven/plugins/BaseTest.java Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/pom.xml URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/pom.xml?view=diff&rev=440331&r1=440330&r2=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/pom.xml (original) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/pom.xml Tue Sep 5 05:36:42 2006 @@ -141,6 +141,11 @@ <version>0.5.2-SNAPSHOT</version> </dependency> <dependency> + <groupId>org.apache.ws.jaxme</groupId> + <artifactId>jaxme2-rt</artifactId> + <version>0.5.2-SNAPSHOT</version> + </dependency> + <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>2.0.4</version> Added: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/EntityResolverFactory.java URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/EntityResolverFactory.java?view=auto&rev=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/EntityResolverFactory.java (added) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/EntityResolverFactory.java Tue Sep 5 05:36:42 2006 @@ -0,0 +1,125 @@ +/* + * Copyright 2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ws.jaxme.maven.plugins; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.ext.EntityResolver2; + + +/** + * A factory for entity resolvers. + */ +public class EntityResolverFactory { + private abstract static class EntityResolver2Impl implements EntityResolver2 { + public InputSource getExternalSubset(String name, String baseURI) + throws SAXException, IOException { + return null; + } + public InputSource resolveEntity(String pName, String pPublicId, String pBaseURI, String pSystemId) + throws SAXException, IOException { + return resolveEntity(pPublicId, pSystemId); + } + } + + private static class FilesEntityResolver extends EntityResolver2Impl { + private final String prefix; + FilesEntityResolver(String pPrefix) { + prefix = pPrefix; + } + public InputSource resolveEntity(String pPublicId, String pSystemId) + throws SAXException, IOException { + if (pSystemId == null) { + return null; + } + File file = new File(prefix + pSystemId); + if (!file.exists()) { + return null; + } + final InputSource isource; + try { + isource = new InputSource(new FileInputStream(file)); + } catch (IOException e) { + throw new SAXException("Failed to open file " + file.getPath() + ": " + e.getMessage(), e); + } + isource.setSystemId(file.toURI().toURL().toExternalForm()); + return isource; + } + } + + private static class ClasspathEntityResolver extends EntityResolver2Impl { + private final String prefix; + ClasspathEntityResolver(String pPrefix) { + prefix = pPrefix == null ? "" : pPrefix; + } + public InputSource resolveEntity(String pPublicId, String pSystemId) throws SAXException, IOException { + if (pSystemId == null) { + return null; + } + URL url = Thread.currentThread().getContextClassLoader().getResource(prefix + pSystemId); + if (url == null) { + return null; + } + final InputSource isource; + try { + isource = new InputSource(url.openStream()); + } catch (IOException e) { + throw new SAXException("Failed to open URL " + url.toExternalForm() + ": " + e.getMessage(), e); + } + isource.setSystemId(url.toExternalForm()); + return isource; + } + } + + static EntityResolver getEntityResolver(String pMode) + throws MojoFailureException, MojoExecutionException { + if (pMode == null || pMode.length() == 0 || "files".equals(pMode)) { + return null; + } + if (pMode.startsWith("files:")) { + final String prefix = pMode.substring("files:".length()); + return new FilesEntityResolver(prefix); + } + if ("classpath".equals(pMode)) { + return new ClasspathEntityResolver(null); + } + if (pMode.startsWith("classpath:")) { + final String prefix = pMode.substring("classpath:".length()); + return new ClasspathEntityResolver(prefix); + } + if (pMode.startsWith("class:")) { + final String className = pMode.substring("class:".length()); + try { + return (EntityResolver) Thread.currentThread().getContextClassLoader().loadClass(className).newInstance(); + } catch (ClassNotFoundException e) { + throw new MojoExecutionException("Failed to load resolver class " + className + ": " + e.getMessage(), e); + } catch (InstantiationException e) { + throw new MojoExecutionException("Failed to instantiate resolver class " + className + ": " + e.getMessage(), e); + } catch (IllegalAccessException e) { + throw new MojoExecutionException("Illegal access to resolver class " + className + ": " + e.getMessage(), e); + } + } + throw new MojoFailureException("Invalid value for resolver mode: " + pMode); + } +} Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/ISchemaCollection.java URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/ISchemaCollection.java?view=diff&rev=440331&r1=440330&r2=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/ISchemaCollection.java (original) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/ISchemaCollection.java Tue Sep 5 05:36:42 2006 @@ -97,7 +97,7 @@ * behaviour. A good example is the * <code>org.apache.ws.jaxme.pm.generator.jdbc.JaxMeJdbcSG</code>. */ - String[] getSgFactoryChain(); + String[] getSgFactoryChains(); /** * The target directory for source files. Defaults to @@ -121,8 +121,7 @@ * Returns a set of properties, which are being set on the * generator. */ - public Map getProperties(); - + Map getProperties(); /** * Returns a prefix, where to add the schema files to the generated jar @@ -130,5 +129,19 @@ * files are being added. Use "" or "/" to add schema files to the * jar files root directory. */ - public String getSchemaTargetPrefix(); + String getSchemaTargetPrefix(); + + /** + * Returns a mechanism for entity resolving. The following modes + * are available: + * <table> + * <tr><td>files</td><td>Entities are resolved in the local file system. + * This is the default</td></tr> + * <tr><td>classpath</td><td>Entities are resolved in the classpath.</td></tr> + * <tr><td>class:<classname></td><td> + * Entities are resolved through a SAX entity resolver, which is an instance + * of the given class</td></tr> + * </table> + */ + String getEntityResolverMode(); } Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeMojo.java URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeMojo.java?view=diff&rev=440331&r1=440330&r2=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeMojo.java (original) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/JaxMeMojo.java Tue Sep 5 05:36:42 2006 @@ -19,9 +19,14 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -38,7 +43,7 @@ * @goal jaxme * @phase generate-sources * @description Runs the JaxMe binding compiler and generates the source files. - * @requiresDependencyResolution test + * @requiresDependencyResolution compile */ public class JaxMeMojo extends AbstractMojo implements ISchemaCollection { /** This property specifies a set of external @@ -99,14 +104,7 @@ */ private FileSet[] produces; - /** The Maven project. - * @parameter expression="${project}" - * @required - * @readonly - */ - private MavenProject project; - - /** The properties being set on the generator. + /** The properties being set on the generator. * @parameter */ private Map properties; @@ -147,7 +145,7 @@ * <code>org.apache.ws.jaxme.pm.generator.jdbc.JaxMeJdbcSG</code>. * @parameter */ - private String[] sgFactoryChain; + private String[] sgFactoryChains; /** The target directory for source files. Defaults to * @parameter expression="${project.build.directory}/generated-sources/jaxme/java" @@ -173,6 +171,21 @@ private SchemaCollection[] schemaCollections; /** + * Returns a mechanism for entity resolving. The following modes + * are available: + * <table> + * <tr><td>files</td><td>Entities are resolved in the local file system. + * This is the default</td></tr> + * <tr><td>classpath</td><td>Entities are resolved in the classpath.</td></tr> + * <tr><td>class:<classname></td><td> + * Entities are resolved through a SAX entity resolver, which is an instance + * of the given class</td></tr> + * </table> + * @parameter default-value="files" + */ + private String entityResolverMode; + + /** * Returns a prefix, where to add the schema files to the generated jar * file, if you want that. Defaults to null, in which case no schema * files are being added. Use "" or "/" to add schema files to the @@ -181,6 +194,27 @@ */ private String schemaTargetPrefix; + /** The Maven project. + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + /** + * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}" + * @required + * @readonly + */ + private ArtifactFactory artifactFactory; + + /** + * @parameter expression="${plugin.artifacts}" + * @required + * @read-only + */ + private List pluginArtifacts; + public FileSet[] getBindings() { return bindings; } @@ -230,8 +264,8 @@ return schemaReader; } - public String[] getSgFactoryChain() { - return sgFactoryChain; + public String[] getSgFactoryChains() { + return sgFactoryChains; } public String getSrcTarget() { @@ -250,6 +284,10 @@ return schemaTargetPrefix; } + public String getEntityResolverMode() { + return entityResolverMode; + } + private ClassLoader getClassLoader(ClassLoader pParent) throws MojoFailureException { List clElements = getClasspathElements(); if (clElements == null && clElements.size() == 0) { @@ -298,6 +336,36 @@ } } + private void addPluginArtifacts() { + Set projectArtifacts = new HashSet(project.getDependencyArtifacts()); + for (Iterator iter = pluginArtifacts.iterator(); iter.hasNext(); ) { + Artifact pluginArtifact = (Artifact) iter.next(); + if (pluginArtifact.getGroupId().startsWith("org.apache.ws.jaxme")) { + boolean found = false; + for (Iterator iter2 = projectArtifacts.iterator(); iter2.hasNext(); ) { + Artifact projectArtifact = (Artifact) iter2.next(); + if (pluginArtifact.getGroupId().equals(projectArtifact.getGroupId()) && + pluginArtifact.getArtifactId().equals(projectArtifact.getArtifactId())) { + found = true; + break; + } + } + if (!found) { + Artifact artifact = artifactFactory.createArtifactWithClassifier(pluginArtifact.getGroupId(), + pluginArtifact.getArtifactId(), pluginArtifact.getVersion(), + pluginArtifact.getType(), pluginArtifact.getClassifier()); + if ("jaxme2-rt".equals(artifact.getArtifactId())) { + artifact.setScope(Artifact.SCOPE_RUNTIME); + } else { + artifact.setScope(Artifact.SCOPE_COMPILE); + } + projectArtifacts.add(artifact); + } + } + } + project.setDependencyArtifacts(projectArtifacts); + } + public void execute() throws MojoExecutionException, MojoFailureException { ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClassLoader(SchemaReader.class.getClassLoader())); @@ -320,16 +388,19 @@ } } if (!isEmpty(schemaArray)) { + getLog().debug("Processing implicit schemaCollection"); configure(this); processor.process(this); } if (collections != null) { for (int i = 0; i < collections.length; i++) { + getLog().debug("Processing schemaCollection " + i); configure(collections[i]); processor.process(collections[i]); } } processor.fixProjectSettings(); + addPluginArtifacts(); } finally { Thread.currentThread().setContextClassLoader(oldCl); LoggerAccess.setLoggerFactory(lf); Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollection.java URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollection.java?view=diff&rev=440331&r1=440330&r2=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollection.java (original) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollection.java Tue Sep 5 05:36:42 2006 @@ -31,6 +31,7 @@ private String schemaTargetPrefix; private boolean extension, removingOldOutput, validating; private Map properties; + private String entityResolverMode; /** * Sets a set of external binding files, which are @@ -135,11 +136,11 @@ * behaviour. A good example is the * <code>org.apache.ws.jaxme.pm.generator.jdbc.JaxMeJdbcSG</code>. */ - public void sgFactoryChain(String[] pSgFactoryChain) { + public void setSgFactoryChains(String[] pSgFactoryChain) { sgFactoryChain = pSgFactoryChain; } - public String[] getSgFactoryChain() { + public String[] getSgFactoryChains() { return sgFactoryChain; } @@ -221,5 +222,25 @@ public String getSchemaTargetPrefix() { return schemaTargetPrefix; + } + + public String getEntityResolverMode() { + return entityResolverMode; + } + + /** + * Returns a mechanism for entity resolving. The following modes + * are available: + * <table> + * <tr><td>files</td><td>Entities are resolved in the local file system. + * This is the default</td></tr> + * <tr><td>classpath</td><td>Entities are resolved in the classpath.</td></tr> + * <tr><td>class:<classname></td><td> + * Entities are resolved through a SAX entity resolver, which is an instance + * of the given class</td></tr> + * </table> + */ + public void setEntityResolverMode(String entityResolverMode) { + this.entityResolverMode = entityResolverMode; } } Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollectionProcessor.java URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollectionProcessor.java?view=diff&rev=440331&r1=440330&r2=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollectionProcessor.java (original) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/main/java/org/apache/ws/jaxme/maven/plugins/SchemaCollectionProcessor.java Tue Sep 5 05:36:42 2006 @@ -296,7 +296,7 @@ final SchemaReader result = newSchemaReaderInstance(pCollection); log.debug("Schema reader class: " + result.getClass().getName()); - String[] chains = pCollection.getSgFactoryChain(); + String[] chains = pCollection.getSgFactoryChains(); if (chains != null) { for (int i = 0; i < chains.length; i++) { Class c = getSgFactoryChainClass(chains[i]); @@ -337,6 +337,7 @@ removeOldOutput(pCollection, pProducedFiles); Generator g = new GeneratorImpl(); + g.setEntityResolver(EntityResolverFactory.getEntityResolver(pCollection.getEntityResolverMode())); for (int i = 0; i < pBindingFiles.length; i++) { File f = pBindingFiles[i].getFile(); try { Modified: webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/test/java/org/apache/ws/jaxme/maven/plugins/BaseTest.java URL: http://svn.apache.org/viewvc/webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/test/java/org/apache/ws/jaxme/maven/plugins/BaseTest.java?view=diff&rev=440331&r1=440330&r2=440331 ============================================================================== --- webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/test/java/org/apache/ws/jaxme/maven/plugins/BaseTest.java (original) +++ webservices/jaxme/trunk/ws-jaxme/maven-jaxme-plugin/src/test/java/org/apache/ws/jaxme/maven/plugins/BaseTest.java Tue Sep 5 05:36:42 2006 @@ -17,7 +17,9 @@ import java.io.File; import java.util.ArrayList; +import java.util.HashSet; +import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.model.Build; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; @@ -42,10 +44,18 @@ return build; } }; + project.setDependencyArtifacts(new HashSet()); setVariableValueToObject(mojo, "project", project); setVariableValueToObject(mojo, "srcTarget", "target/classes/generated-sources/jaxme/java"); setVariableValueToObject(mojo, "resourceTarget", "target/classes/generated-sources/jaxme/resources"); setVariableValueToObject(mojo, "classpathElements", new ArrayList()); + setVariableValueToObject(mojo, "pluginArtifacts", new ArrayList()); + ArtifactFactory factory = (ArtifactFactory) lookup(ArtifactFactory.class.getName()); + if (factory == null) { + throw new NullPointerException("Missing ArtifactFactory"); + } + setVariableValueToObject(mojo, "artifactFactory", factory); + return mojo; } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]