Java Code Examples for org.codehaus.plexus.util.xml.Xpp3Dom#getChildren()

The following examples show how to use org.codehaus.plexus.util.xml.Xpp3Dom#getChildren() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MavenConfigurationExtractor.java    From jkube with Eclipse Public License 2.0 7 votes vote down vote up
private static Map<String, Object> getElement(Xpp3Dom element) {

        final Map<String, Object> conf = new HashMap<>();

        final Xpp3Dom[] currentElements = element.getChildren();

        for (Xpp3Dom currentElement: currentElements) {
            if (isSimpleType(currentElement)) {

                if (isAListOfElements(conf, currentElement)) {
                    addAsList(conf, currentElement);
                } else {
                    conf.put(currentElement.getName(), currentElement.getValue());
                }
            } else {
                conf.put(currentElement.getName(), getElement(currentElement));
            }
        }

        return conf;

    }
 
Example 2
Source File: PluginExtractor.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the subset of the given configuration containing only the values accepted by the plugin/goal. The
 * configuration is modified in-place. The the extraction fail the configuration stays unchanged.
 *
 * @param mojo          the Wisdom mojo
 * @param plugin        the plugin object
 * @param goal          the goal / mojo
 * @param configuration the global configuration
 */
public static void extractEligibleConfigurationForGoal(AbstractWisdomMojo mojo,
                                                       Plugin plugin, String goal, Xpp3Dom configuration) {
    try {
        MojoDescriptor descriptor = mojo.pluginManager.getMojoDescriptor(plugin, goal,
                mojo.remoteRepos, mojo.repoSession);
        final List<Parameter> parameters = descriptor.getParameters();
        Xpp3Dom[] children = configuration.getChildren();
        if (children != null) {
            for (int i = children.length - 1; i >= 0; i--) {
                Xpp3Dom child = children[i];
                if (!contains(parameters, child.getName())) {
                    configuration.removeChild(i);
                }
            }
        }
    } catch (Exception e) {
        mojo.getLog().warn("Cannot extract the eligible configuration for goal " + goal + " from the " +
                "configuration");
        mojo.getLog().debug(e);
        // The configuration is not changed.
    }

}
 
Example 3
Source File: AbstracMavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Searches the Maven pom.xml for the given project nature.
 *
 * @param mavenProject a description of the Maven project
 * @param natureId the nature to check
 * @return {@code true} if the project
 */
protected boolean hasProjectNature(MavenProject mavenProject, String natureId) {
  if (natureId == GWTNature.NATURE_ID || getGwtMavenPlugin(mavenProject) != null) {
    return true;
  }
  // The use of the maven-eclipse-plugin is deprecated. The following code is
  // only for backward compatibility.
  Plugin plugin = getEclipsePlugin(mavenProject);
  if (plugin != null) {
    Xpp3Dom configuration = (Xpp3Dom) plugin.getConfiguration();
    if (configuration != null) {
      Xpp3Dom additionalBuildCommands = configuration.getChild("additionalProjectnatures");
      if (additionalBuildCommands != null) {
        for (Xpp3Dom projectNature : additionalBuildCommands.getChildren("projectnature")) {
          if (projectNature != null && natureId.equals(projectNature.getValue())) {
            return true;
          }
        }
      }
    }
  }
  return false;
}
 
Example 4
Source File: MavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get the GWT Maven plugin 2 <moduleShort=Name/>.
 *
 * @param mavenProject
 * @return the moduleName from configuration
 */
private String getGwtModuleShortName(MavenProject mavenProject) {
  if (!isGwtMavenPlugin2(mavenProject)) {
    return null;
  }

  Plugin gwtPlugin2 = getGwtMavenPlugin2(mavenProject);
  if (gwtPlugin2 == null) {
    return null;
  }

  Xpp3Dom gwtPluginConfig = (Xpp3Dom) gwtPlugin2.getConfiguration();
  if (gwtPluginConfig == null) {
    return null;
  }

  String moduleName = null;
  for (Xpp3Dom child : gwtPluginConfig.getChildren()) {
    if (child != null && GWT_MAVEN_MODULESHORTNAME.equals(child.getName())) {
      moduleName = child.getValue().trim();
    }
  }
  return moduleName;
}
 
Example 5
Source File: RequirePropertyDiverges.java    From extra-enforcer-rules with Apache License 2.0 6 votes vote down vote up
/**
 * As Xpp3Dom is very picky about the order of children while comparing, create a new list where the children
 * are added in alphabetical order. See <a href="https://jira.codehaus.org/browse/MOJO-1931">MOJO-1931</a>.
 *
 * @param originalListFromPom order not specified
 * @return a list where children's member are alphabetically sorted.
 */
private List<Xpp3Dom> createRuleListWithNameSortedChildren( final List<Xpp3Dom> originalListFromPom )
{
    final List<Xpp3Dom> listWithSortedEntries = new ArrayList<Xpp3Dom>( originalListFromPom.size() );
    for ( Xpp3Dom unsortedXpp3Dom : originalListFromPom )
    {
        final Xpp3Dom sortedXpp3Dom = new Xpp3Dom( getRuleName() );
        final SortedMap<String, Xpp3Dom> childrenMap = new TreeMap<String, Xpp3Dom>();
        final Xpp3Dom[] children = unsortedXpp3Dom.getChildren();
        for ( Xpp3Dom child : children )
        {
            childrenMap.put( child.getName(), child );
        }
        for ( Xpp3Dom entry : childrenMap.values() )
        {
            sortedXpp3Dom.addChild( entry );
        }
        listWithSortedEntries.add( sortedXpp3Dom );
    }
    return listWithSortedEntries;
}
 
Example 6
Source File: CatchAllExecutionHandler.java    From pipeline-maven-plugin with MIT License 6 votes vote down vote up
@Nonnull
@Override
protected List<String> getConfigurationParametersToReport(ExecutionEvent executionEvent) {

    MojoExecution mojoExecution = executionEvent.getMojoExecution();
    if (mojoExecution == null) {
        return Collections.emptyList();
    }

    Xpp3Dom configuration = mojoExecution.getConfiguration();
    List<String> parameters = new ArrayList<String>();
    for (Xpp3Dom configurationParameter : configuration.getChildren()) {
        parameters.add(configurationParameter.getName());
    }
    return parameters;
}
 
Example 7
Source File: MavenProjectUtil.java    From ci.maven with Apache License 2.0 6 votes vote down vote up
/**
 * Get directory and targetPath configuration values from the Maven WAR plugin
 * @param proj the Maven project
 * @return a Map of source and target directories corresponding to the configuration keys
 * or null if the war plugin or its webResources or resource elements are not used. 
 * The map will be empty if the directory element is not used. The value field of the 
 * map is null when the targetPath element is not used.
 */
public static Map<String,String> getWebResourcesConfiguration(MavenProject proj) {
    Xpp3Dom dom = proj.getGoalConfiguration("org.apache.maven.plugins", "maven-war-plugin", null, null);
    if (dom != null) {
        Xpp3Dom web = dom.getChild("webResources");
        if (web != null) {
            Xpp3Dom resources[] = web.getChildren("resource");
            if (resources != null) {
                Map<String, String> result = new HashMap<String, String>();
                for (int i = 0; i < resources.length; i++) {
                    Xpp3Dom dir = resources[i].getChild("directory");
                    if (dir != null) {
                        Xpp3Dom target = resources[i].getChild("targetPath");
                        if (target != null) {
                            result.put(dir.getValue(), target.getValue());
                        } else {
                            result.put(dir.getValue(), null);
                        }
                    }
                }
                return result;
            }
        }
    }
    return null;
}
 
Example 8
Source File: DevMojo.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private Xpp3Dom getPluginConfig(Plugin plugin) {
    Xpp3Dom configuration = MojoExecutor.configuration();
    Xpp3Dom pluginConfiguration = (Xpp3Dom) plugin.getConfiguration();
    if (pluginConfiguration != null) {
        //Filter out `test*` configurations
        for (Xpp3Dom child : pluginConfiguration.getChildren()) {
            if (!child.getName().startsWith("test")) {
                configuration.addChild(child);
            }
        }
    }
    return configuration;
}
 
Example 9
Source File: MavenProjectConfigurator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get the project launch locaiton
 *
 * @param config
 *          gwt-maven-maven config DOM
 * @return the {@link #ECLIPSE_LAUNCH_SRC_DIR_PROPERTY_KEY} value from the config if it exists,
 *         {@link #ECLIPSE_LAUNCH_SRC_DIR_DEFAULT} otherwise or if config is null
 */
private final boolean getLaunchFromHere(Xpp3Dom config) {
  if (config == null) {
    return ECLIPSE_LAUNCH_SRC_DIR_DEFAULT;
  }
  for (Xpp3Dom child : config.getChildren()) {
    if (child != null && ECLIPSE_LAUNCH_SRC_DIR_PROPERTY_KEY.equals(child.getName())) {
      return child.getValue() == null ? ECLIPSE_LAUNCH_SRC_DIR_DEFAULT
          : Boolean.parseBoolean(child.getValue().trim());
    }
  }
  return ECLIPSE_LAUNCH_SRC_DIR_DEFAULT;
}
 
Example 10
Source File: PathsToolchainFactory.java    From exec-maven-plugin with Apache License 2.0 5 votes vote down vote up
protected static Properties toProperties( final Xpp3Dom dom )
{
    final Properties props = new Properties();

    final Xpp3Dom[] children = dom.getChildren();
    for ( final Xpp3Dom child : children )
        props.put( child.getName(), child.getValue() );

    return props;
}
 
Example 11
Source File: SurefireConfigConverter.java    From pitest with Apache License 2.0 5 votes vote down vote up
private List<String> extract(String childname, Xpp3Dom config) {
  final Xpp3Dom subelement = config.getChild(childname);
  if (subelement != null) {
    List<String> result = new LinkedList<>();
    final Xpp3Dom[] children = subelement.getChildren();
    for (Xpp3Dom child : children) {
      result.add(child.getValue());
    }
    return result;
  }

  return Collections.emptyList();
}
 
Example 12
Source File: MavenModelUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static String findHandler(Xpp3Dom parent) {
    for (Xpp3Dom child : parent.getChildren("bindingFile")) { //NOI18N
        String bindingPath = child.getValue();
        if (bindingPath != null && bindingPath.endsWith("_handler.xml")) { //NOI18N
            return bindingPath;
        }
    }
    return null;
}
 
Example 13
Source File: LocationAwareMavenXpp3Writer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void writeXpp3DOM(XmlSerializer serializer, Xpp3Dom root, InputLocationTracker rootTracker) throws IOException {
    StringBuffer b = b(serializer);
    serializer.startTag(NAMESPACE, root.getName());
    //need to flush the inner writer, flush on serializer closes tag
    flush(serializer);
    int start = b.length() - root.getName().length() - 2;
    
    String[] attributeNames = root.getAttributeNames();
    for ( int i = 0; i < attributeNames.length; i++ )
    {
        String attributeName = attributeNames[i];
        serializer.attribute(NAMESPACE, attributeName, root.getAttribute( attributeName ));
    }
    
    boolean config = rootTracker != null ? rootTracker.getLocation(root.getName()) != null : true;
    
    Xpp3Dom[] children = root.getChildren();
    for ( int i = 0; i < children.length; i++ )
    {
        writeXpp3DOM(serializer, children[i], rootTracker != null ? rootTracker.getLocation(config ? root.getName() : root) : null);
    }

    String value = root.getValue();
    if ( value != null )
    {
        serializer.text( value );
    }

    serializer.endTag(NAMESPACE, root.getName()).flush();
    logLocation(rootTracker, config ? root.getName() : root, start, b.length());
    
}
 
Example 14
Source File: PluginPropertyUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Should handle both deprecated 2.x-style report section, and 3.x-style Site Plugin config.
 * https://jira.codehaus.org/browse/MSITE-484 and https://jira.codehaus.org/browse/MSITE-443 if and when implemented may require updates.
 */
private static @NonNull Iterable<ReportPlugin> getEffectiveReportPlugins(@NonNull MavenProject prj) {
    List<ReportPlugin> plugins = new ArrayList<ReportPlugin>();
    for (Plugin plug : prj.getBuildPlugins()) {
        if (Constants.GROUP_APACHE_PLUGINS.equals(plug.getGroupId()) && Constants.PLUGIN_SITE.equals(plug.getArtifactId())) {
            Xpp3Dom cfg = (Xpp3Dom) plug.getConfiguration(); // MNG-4862
            if (cfg == null) {
                continue;
            }
            Xpp3Dom reportPlugins = cfg.getChild("reportPlugins");
            if (reportPlugins == null) {
                continue;
            }
            for (Xpp3Dom plugin : reportPlugins.getChildren("plugin")) {
                ReportPlugin p = new ReportPlugin();
                Xpp3Dom groupId = plugin.getChild("groupId");
                if (groupId != null) {
                    p.setGroupId(groupId.getValue());
                }
                Xpp3Dom artifactId = plugin.getChild("artifactId");
                if (artifactId != null) {
                    p.setArtifactId(artifactId.getValue());
                }
                Xpp3Dom version = plugin.getChild("version");
                if (version != null) {
                    p.setVersion(version.getValue());
                }
                p.setConfiguration(plugin.getChild("configuration"));
                // XXX reportSets
                // maven-site-plugin does not appear to apply defaults from plugin.xml (unlike 2.x?)
                plugins.add(p);
            }
        }
    }
    @SuppressWarnings("deprecation") List<ReportPlugin> m2Plugins = prj.getReportPlugins();
    plugins.addAll(m2Plugins);
    return plugins;
}
 
Example 15
Source File: PluginPropertyUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static @NonNull ConfigurationBuilder<Properties> propertiesBuilder(final @NonNull String propertyParameter) {
    return new ConfigurationBuilder<Properties>() {
        @Override
        public Properties build(Xpp3Dom conf, ExpressionEvaluator eval) {
            if (conf != null) {

                Xpp3Dom source = conf.getChild(propertyParameter);
                if (source != null) {
                    Properties toRet = new Properties();
                    Xpp3Dom[] childs = source.getChildren();
                    for (Xpp3Dom ch : childs) {
                            String val = ch.getValue();
                            if (val == null) {
                                //#168036
                                //we have the "property" named element now.
                                if (ch.getChildCount() == 2) {
                                    Xpp3Dom nameDom = ch.getChild("name"); //NOI18N
                                    Xpp3Dom valueDom = ch.getChild("value"); //NOI18N
                                    if (nameDom != null && valueDom != null) {
                                        String name = nameDom.getValue();
                                        String value = valueDom.getValue();
                                        if (name != null && value != null) {
                                            toRet.put(name, value);  //NOI18N
                                        }
                                    }
                                }
                                // #153063, #187648
                                toRet.put(ch.getName(), "");
                                continue;
                            }
                            toRet.put(ch.getName(), val.trim());  //NOI18N
                    }
                    return toRet;
                }
            }
            return null;
        }
    };
}
 
Example 16
Source File: IteratorMojo.java    From iterator-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * This will copy the configuration from <b>src</b> to the result whereas the placeholder will be replaced with the
 * current value.
 */
private PlexusConfiguration copyConfiguration( Xpp3Dom src, String iteratorName, String value )
{

    XmlPlexusConfiguration dom = new XmlPlexusConfiguration( src.getName() );

    if ( src.getValue() == null )
    {
        dom.setValue( src.getValue() );
    }
    else
    {
        if ( src.getValue().contains( iteratorName ) )
        {
            dom.setValue( src.getValue().replaceAll( iteratorName, value ) );
        }
        else
        {
            dom.setValue( src.getValue() );
        }
    }

    for ( String attributeName : src.getAttributeNames() )
    {
        dom.setAttribute( attributeName, src.getAttribute( attributeName ) );
    }

    for ( Xpp3Dom child : src.getChildren() )
    {
        dom.addChild( copyConfiguration( child, iteratorName, value ) );
    }

    return dom;
}
 
Example 17
Source File: EarImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private MavenModule[] checkConfiguration(MavenProject prj, Object conf) {
    List<MavenModule> toRet = new ArrayList<MavenModule>();
    if (conf != null && conf instanceof Xpp3Dom) {
        ExpressionEvaluator eval = PluginPropertyUtils.createEvaluator(project);
        Xpp3Dom dom = (Xpp3Dom) conf;
        Xpp3Dom modules = dom.getChild("modules"); //NOI18N
        if (modules != null) {
            int index = 0;
            for (Xpp3Dom module : modules.getChildren()) {
                MavenModule mm = new MavenModule();
                mm.type = module.getName();
                if (module.getChildren() != null) {
                    for (Xpp3Dom param : module.getChildren()) {
                        String value = param.getValue();
                        if (value == null) {
                            continue;
                        }
                        try {
                            Object evaluated = eval.evaluate(value.trim());
                            value = evaluated != null ? ("" + evaluated) : value.trim();  //NOI18N
                        } catch (ExpressionEvaluationException e) {
                            //log silently
                        }
                        if ("groupId".equals(param.getName())) { //NOI18N
                            mm.groupId = value;
                        } else if ("artifactId".equals(param.getName())) { //NOI18N
                            mm.artifactId = value;
                        } else if ("classifier".equals(param.getName())) { //NOI18N
                            mm.classifier = value;
                        } else if ("uri".equals(param.getName())) { //NOI18N
                            mm.uri = value;
                        } else if ("bundleDir".equals(param.getName())) { //NOI18N
                            mm.bundleDir = value;
                        } else if ("bundleFileName".equals(param.getName())) { //NOI18N
                            mm.bundleFileName = value;
                        } else if ("excluded".equals(param.getName())) { //NOI18N
                            mm.excluded = Boolean.valueOf(value);
                        }
                    }
                }
                mm.pomIndex = index;
                index++;
                toRet.add(mm);
            }
        }
    }
    return toRet.toArray(new MavenModule[0]);
}
 
Example 18
Source File: PathsToolchainFactory.java    From exec-maven-plugin with Apache License 2.0 4 votes vote down vote up
public ToolchainPrivate createToolchain( final ToolchainModel model )
    throws MisconfiguredToolchainException
{
    if ( model == null )
        return null;

    final PathsToolchain pathsToolchain = new PathsToolchain( model, this.logger );
    final Properties provides = this.getProvidesProperties( model );
    for ( final Map.Entry<Object, Object> provide : provides.entrySet() )
    {
        final String key = (String) provide.getKey();
        final String value = (String) provide.getValue();
        if ( value == null )
            throw new MisconfiguredToolchainException( "Provides token '" + key
                + "' doesn't have any value configured." );

        pathsToolchain.addProvideToken( key, RequirementMatcherFactory.createExactMatcher( value ) );
    }

    final Xpp3Dom config = (Xpp3Dom) model.getConfiguration();
    if ( config == null )
        return pathsToolchain;

    final Xpp3Dom pathDom = config.getChild( "paths" );
    if ( pathDom == null )
        return pathsToolchain;

    final Xpp3Dom[] pathDoms = pathDom.getChildren( "path" );
    if ( pathDoms == null || pathDoms.length == 0 )
        return pathsToolchain;

    final List<String> paths = new ArrayList<String>( pathDoms.length );
    for ( final Xpp3Dom pathdom : pathDoms )
    {
        final String pathString = pathdom.getValue();

        if ( pathString == null )
            throw new MisconfiguredToolchainException( "path element is empty" );

        final String normalizedPath = FileUtils.normalize( pathString );
        final File file = new File( normalizedPath );
        if ( !file.exists() )
            throw new MisconfiguredToolchainException( "Non-existing path '" + file.getAbsolutePath() + "'" );

        paths.add( normalizedPath );
    }

    pathsToolchain.setPaths( paths );

    return pathsToolchain;
}
 
Example 19
Source File: ClasspathWorkspaceReader.java    From furnace with Eclipse Public License 1.0 4 votes vote down vote up
private List<File> createFoundModules(final File pomFile)
{
   try
   {
      List<File> result = new ArrayList<>();
      if (log.isLoggable(Level.FINE))
      {
         log.fine("Processing " + pomFile.getAbsolutePath() + " for classpath module resolution");
      }
      Xpp3Dom dom = null;
      try (FileReader reader = new FileReader(pomFile))
      {
         dom = Xpp3DomBuilder.build(reader);
      }
      Xpp3Dom modules = dom.getChild("modules");
      if (modules != null)
      {
         for (Xpp3Dom module : modules.getChildren())
         {
            result.add(new File(pomFile.getParent(), module.getValue()));
         }
      }

      if (result.isEmpty())
      {
         Xpp3Dom parent = dom.getChild("parent");
         if (parent != null)
         {
            Xpp3Dom relativePathNode = parent.getChild("relativePath");
            String relativePath = (relativePathNode == null) ? "../pom.xml" : relativePathNode.getValue();
            if (relativePath != null)
            {
                File parentPom = pomFile.getParentFile().toPath().resolve(relativePath).toFile();
                if (parentPom.isFile())
                    result = createFoundModules(parentPom);
            }
         }
      }

      return result;
   }
   catch (final Exception e)
   {
      throw new RuntimeException("Could not parse pom.xml: " + pomFile, e);
   }
}
 
Example 20
Source File: LooseEarApplication.java    From ci.maven with Apache License 2.0 4 votes vote down vote up
public String getModuleUri(Artifact artifact) throws Exception {
    String defaultUri = "/" + getModuleName(artifact);
    // both "jar" and "bundle" packaging type project are "jar" type dependencies
    // that will be packaged in the ear lib directory
    String type = artifact.getType();
    if (("jar".equals(type) || "bundle".equals(type)) && getEarDefaultLibBundleDir() != null) {
        defaultUri = "/" + getEarDefaultLibBundleDir() + defaultUri;
    }
    Xpp3Dom dom = project.getGoalConfiguration("org.apache.maven.plugins", "maven-ear-plugin", null, null);
    if (dom != null) {
        Xpp3Dom val = dom.getChild("modules");
        if (val != null) {
            Xpp3Dom[] modules = val.getChildren();
            if (modules != null) {
                for (int i = 0; i < modules.length; i++) {
                    if (artifact.getGroupId().equals(getConfigValue(modules[i].getChild("groupId")))
                            && artifact.getArtifactId().equals(getConfigValue(modules[i].getChild("artifactId")))) {
                        String uri = getConfigValue(modules[i].getChild("uri"));
                        if (uri != null) {
                            return uri;
                        } else {
                            String bundleDir = getConfigValue(modules[i].getChild("bundleDir"));
                            String bundleFileName = getConfigValue(modules[i].getChild("bundleFileName"));
                            if (bundleDir == null) {
                                if ("jar".equals(type) && getEarDefaultLibBundleDir() != null) {
                                    bundleDir = "/" + getEarDefaultLibBundleDir();
                                } else {
                                    bundleDir = "";
                                }
                            } else {
                                bundleDir = "/" + bundleDir;
                            }

                            // remove duplicate forward slashes. At this point, we know bundleDir starts
                            // with a slash or is empty
                            if (bundleDir.length() > 1 && bundleDir.charAt(0) == bundleDir.charAt(1)) {
                                StringBuilder sb = new StringBuilder(bundleDir);
                                do {
                                    sb.deleteCharAt(0);
                                } while (sb.length() > 1 && sb.charAt(0) == sb.charAt(1));
                                bundleDir = sb.toString();
                                if ("/".equals(bundleDir)) {
                                    bundleDir = "";
                                }
                            }
                            if (bundleFileName != null) {
                                return bundleDir + "/" + bundleFileName;
                            } else {
                                return bundleDir + "/" + getModuleName(artifact);
                            }
                        }
                    }
                }
            }
        }
    }
    return defaultUri;
}