Skip to content

Commit

Permalink
[resolves smartics#37] Configurable namespace in the generated module…
Browse files Browse the repository at this point in the history
….xml files
  • Loading branch information
ppalaga committed Sep 14, 2017
1 parent dc4740d commit afecb4b
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -55,6 +56,7 @@
import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector;
import org.eclipse.aether.util.graph.selector.OptionalDependencySelector;
import org.eclipse.aether.util.graph.selector.ScopeDependencySelector;
import org.jdom2.Namespace;

import de.smartics.maven.plugin.jboss.modules.aether.Mapper;
import de.smartics.maven.plugin.jboss.modules.aether.MavenRepository;
Expand All @@ -73,6 +75,7 @@
import de.smartics.maven.plugin.jboss.modules.domain.SlotStrategy;
import de.smartics.maven.plugin.jboss.modules.domain.TransitiveDependencyResolver;
import de.smartics.maven.plugin.jboss.modules.parser.ModulesXmlLocator;
import de.smartics.maven.plugin.jboss.modules.xml.ModuleXmlBuilder;

/**
* Generates a archive containing modules from a BOM project.
Expand Down Expand Up @@ -368,6 +371,12 @@ public final class JBossModulesArchiveMojo extends AbstractMojo
@Parameter(defaultValue = "false")
private boolean generateFeaturePackDefinition;

/**
* The namespace to use in the generated module.xml files.
*/
@Parameter(defaultValue = "urn:jboss:module:1.1")
private String targetNamespace;

// ****************************** Initializer *******************************

// ****************************** Constructors ******************************
Expand Down Expand Up @@ -604,6 +613,8 @@ private ExecutionContext createContext(final List<Dependency> dependencies)
builder.with(moduleMap);
builder.withIgnoreOptionalDependencies(ignoreOptionalDependencies);
builder.withGenerateFeaturePackDefinition(generateFeaturePackDefinition);
Namespace ns = StringUtils.isBlank(targetNamespace) ? ModuleXmlBuilder.MODULE_NS_1_1 : Namespace.getNamespace(targetNamespace);
builder.withTargetNamespace(ns);

if (verbose)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import org.apache.maven.plugin.logging.Log;
import org.eclipse.aether.graph.Dependency;
import org.eclipse.aether.resolution.DependencyResolutionException;
import org.jdom2.Namespace;

import de.smartics.maven.plugin.jboss.modules.descriptor.ModuleDescriptor;
import de.smartics.maven.plugin.jboss.modules.util.Arg;
import de.smartics.maven.plugin.jboss.modules.xml.ModuleXmlBuilder;
import edu.emory.mathcs.backport.java.util.Collections;

/**
Expand Down Expand Up @@ -77,6 +79,8 @@ public final class ExecutionContext
*/
private Boolean generateFeaturePackDefinition;

private final Namespace targetNamespace;

// ****************************** Initializer *******************************

// ****************************** Constructors ******************************
Expand All @@ -91,6 +95,7 @@ private ExecutionContext(final Builder builder)
this.moduleMap = builder.moduleMap;
this.ignoreOptionalDependencies = builder.ignoreOptionalDependencies;
this.generateFeaturePackDefinition = builder.generateFeaturePackDefinition;
this.targetNamespace = builder.targetNamespace;
}

// ****************************** Inner Classes *****************************
Expand Down Expand Up @@ -146,6 +151,8 @@ public static final class Builder
*/
private Boolean generateFeaturePackDefinition;

private Namespace targetNamespace = ModuleXmlBuilder.MODULE_NS_1_1;

// ***************************** Initializer ******************************

// ***************************** Constructors *****************************
Expand Down Expand Up @@ -255,6 +262,18 @@ public Builder withGenerateFeaturePackDefinition(final Boolean generateFeaturePa
return this;
}

/**
* Sets the namespace to use during the generation of module.xml files.
*
* @param targetNamespace a module.xml namespace
* @return a reference to this builder.
*/
public Builder withTargetNamespace(final Namespace targetNamespace)
{
this.targetNamespace = targetNamespace;
return this;
}

// --- business -----------------------------------------------------------

/**
Expand Down Expand Up @@ -408,6 +427,11 @@ public ModuleDescriptor getModule(final Dependency dependency)
return module;
}

public Namespace getTargetNamespace()
{
return targetNamespace;
}

// --- object basics --------------------------------------------------------

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package de.smartics.maven.plugin.jboss.modules.parser;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
Expand All @@ -26,16 +25,15 @@

import de.smartics.maven.plugin.jboss.modules.descriptor.ApplyToDependencies;
import de.smartics.maven.plugin.jboss.modules.descriptor.ApplyToModule;
import de.smartics.maven.plugin.jboss.modules.descriptor.ArtifactClusion;
import de.smartics.maven.plugin.jboss.modules.descriptor.ArtifactMatcher;
import de.smartics.maven.plugin.jboss.modules.descriptor.DependenciesDescriptor;
import de.smartics.maven.plugin.jboss.modules.descriptor.DependenciesDescriptor.Builder;
import de.smartics.maven.plugin.jboss.modules.descriptor.ArtifactClusion;
import de.smartics.maven.plugin.jboss.modules.descriptor.Directives;
import de.smartics.maven.plugin.jboss.modules.descriptor.ModuleClusion;
import de.smartics.maven.plugin.jboss.modules.descriptor.ModuleDescriptor;
import de.smartics.maven.plugin.jboss.modules.descriptor.ModuleMatcher;
import de.smartics.maven.plugin.jboss.modules.descriptor.ModulesDescriptor;
import de.smartics.maven.plugin.jboss.modules.xml.ModuleXmlBuilder;

/**
* The worker to do the parsing on a given document. It provides internal state
Expand Down Expand Up @@ -266,35 +264,17 @@ private void parseApply(final Builder builder,
final Element importElement = applyElement.getChild("imports", NS);
if (importElement != null)
{
adjustNamespaces(importElement);
final String imports = outputter.outputString(importElement);
builder.withImportsXml(imports);
}
final Element exportElement = applyElement.getChild("exports", NS);
if (exportElement != null)
{
adjustNamespaces(exportElement);
final String exports = outputter.outputString(exportElement);
builder.withExportsXml(exports);
}
}

private void adjustNamespaces(final Element element)
{
element.setNamespace(null);
final List<Namespace> namespaces =
new ArrayList<Namespace>(element.getAdditionalNamespaces());
for (final Namespace namespace : namespaces)
{
element.removeNamespaceDeclaration(namespace);
}
element.setNamespace(ModuleXmlBuilder.NS);
for (final Element child : element.getChildren())
{
adjustNamespaces(child);
}
}

private void parseApplyToModule(final Element applyToModuleElement)
{
if (applyToModuleElement == null)
Expand All @@ -304,7 +284,6 @@ private void parseApplyToModule(final Element applyToModuleElement)

final ApplyToModule.Builder mBuilder = new ApplyToModule.Builder();

adjustNamespaces(applyToModuleElement);
final XMLOutputter outputter = new XMLOutputter(Format.getCompactFormat());
for (final Element child : applyToModuleElement.getChildren())
{
Expand All @@ -325,7 +304,7 @@ private void handleChild(final ApplyToModule.Builder mBuilder,
else if ("properties".equals(elementName))
{
for (final Element propertyElement : child.getChildren("property",
ModuleXmlBuilder.NS))
NS))
{
final String name = propertyElement.getAttributeValue("name");
final String fragment = outputter.outputString(propertyElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,35 +255,17 @@ private void parseApply(final Builder builder,
final Element importElement = dependenciesElement.getChild("imports", NS);
if (importElement != null)
{
adjustNamespaces(importElement);
final String imports = outputter.outputString(importElement);
builder.withImportsXml(imports);
}
final Element exportElement = dependenciesElement.getChild("exports", NS);
if (exportElement != null)
{
adjustNamespaces(exportElement);
final String exports = outputter.outputString(exportElement);
builder.withExportsXml(exports);
}
}

private void adjustNamespaces(final Element element)
{
element.setNamespace(null);
final List<Namespace> namespaces =
new ArrayList<Namespace>(element.getAdditionalNamespaces());
for (final Namespace namespace : namespaces)
{
element.removeNamespaceDeclaration(namespace);
}
element.setNamespace(ModuleXmlBuilder.NS);
for (final Element child : element.getChildren())
{
adjustNamespaces(child);
}
}

private void parseApplyToModule(final Element applyToModuleElement)
{
if (applyToModuleElement == null)
Expand All @@ -293,7 +275,6 @@ private void parseApplyToModule(final Element applyToModuleElement)

final ApplyToModule.Builder mBuilder = new ApplyToModule.Builder();

adjustNamespaces(applyToModuleElement);
final XMLOutputter outputter = new XMLOutputter(Format.getCompactFormat());
for (final Element child : applyToModuleElement.getChildren())
{
Expand All @@ -314,7 +295,7 @@ private void handleChild(final ApplyToModule.Builder mBuilder,
else if ("properties".equals(elementName))
{
for (final Element propertyElement : child.getChildren("property",
ModuleXmlBuilder.NS))
NS))
{
final String name = propertyElement.getAttributeValue("name");
final String fragment = outputter.outputString(propertyElement);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2013-2017 smartics, Kronseder & Reiner GmbH
*
* 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 de.smartics.maven.plugin.jboss.modules.util;

import java.util.ArrayList;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.Namespace;

public class XmlUtils
{

private XmlUtils()
{
}

public static void adjustNamespaces(final Element element, Namespace ns)
{
element.setNamespace(null);
final List<Namespace> namespaces =
new ArrayList<Namespace>(element.getAdditionalNamespaces());
for (final Namespace namespace : namespaces)
{
element.removeNamespaceDeclaration(namespace);
}
element.setNamespace(ns);
for (final Element child : element.getChildren())
{
adjustNamespaces(child, ns);
}
}

}

0 comments on commit afecb4b

Please sign in to comment.