Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addressing #41 #55

Merged
merged 2 commits into from
Aug 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,22 @@ public boolean getEpisode() {
public void setEpisode(boolean episode) {
this.episode = episode;
}

/**
* If <b>true</b>, adds <b>if-exists="true"</b> in <b>bindings</b> elements.
* See https://github.com/highsource/maven-jaxb2-plugin/issues/41 for more
* information
*/
@Parameter(property = "maven.xjc2.applyEpisodeHackForUnusedSchemaBindings", defaultValue = "false")
private boolean applyEpisodeHackForUnusedSchemaBindings;

public boolean getApplyEpisodeHackForUnusedSchemaBindings() {
return this.applyEpisodeHackForUnusedSchemaBindings;
}

public void setApplyEpisodeHackForUnusedSchemaBindings(boolean applyEpisodeHackForUnusedSchemaBindings) {
this.applyEpisodeHackForUnusedSchemaBindings = applyEpisodeHackForUnusedSchemaBindings;
}

/**
* If true, marks generated classes using a @Generated annotation - i.e.
Expand Down
35 changes: 35 additions & 0 deletions plugin-core/src/main/java/org/jvnet/jaxb2/maven2/RawXJC2Mojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -34,6 +35,14 @@
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
Expand Down Expand Up @@ -491,13 +500,39 @@ protected void doExecute() throws MojoExecutionException {
MessageFormat.format(
"Refreshing the generated directory [{0}].",
getGenerateDirectory().getAbsolutePath()));
setupEpisodeHackForUnusedSchemaBindings();
buildContext.refresh(getGenerateDirectory());
}

if (getVerbose()) {
getLog().info("Finished execution.");
}
}


private void setupEpisodeHackForUnusedSchemaBindings() throws MojoExecutionException {
if(!getApplyEpisodeHackForUnusedSchemaBindings()) {
getLog().debug("allowImporWithUnusedSchemaBindingsInEpisode disabled");
return;
}
File episodeFile = getEpisodeFile();
if(!episodeFile.canWrite()) {
getLog().debug(MessageFormat.format("Episode file [{0}] not writable, applyEpisodeHackForUnusedSchemaBindings configuration aborted", episodeFile));
return;
}
try {
File tempEpisode = File.createTempFile("maven.xjc2", "episode");
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(getClass().getResourceAsStream("/episodeHackForUnusedSchemaBindings.xslt")));
transformer.transform(new StreamSource(episodeFile), new StreamResult(tempEpisode));
FileUtils.copyFile(tempEpisode, episodeFile);
FileUtils.forceDelete(tempEpisode);
getLog().info(MessageFormat.format("Episode file [{0}] transformed to allow import with unused schema bindings", episodeFile));
} catch(IOException e) {
throw new MojoExecutionException(MessageFormat.format("IO error transforming [{0}] to allow import with unused schema bindings", episodeFile), e);
} catch (TransformerException e) {
throw new MojoExecutionException(MessageFormat.format("Unexpected transformation error in [{0}] to allow import with unused schema bindings", episodeFile), e);
}
}

private URILastModifiedResolver uriLastModifiedResolver;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jaxp="http://java.sun.com/xml/ns/jaxb"
version="1.0">
<!-- this template is applied by default to all nodes and attributes -->
<xsl:template match="@*|node()">
<!-- just copy all my attributes and child nodes, except if there's a better template for some of them -->
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="jaxp:bindings[@scd='x-schema::tns']">
<xsl:copy>
<xsl:attribute name="if-exists">true</xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>