public static void setCollectionValues( Xpp3Dom parent, String nodeName, String childName, Collection<String> values) { Xpp3Dom node = parent.getChild(nodeName); if (node != null) { for (int i = 0; i < parent.getChildCount(); i++) { Xpp3Dom existing = parent.getChild(i); if (StringUtils.equals(nodeName, existing.getName())) { parent.removeChild(i); break; } } node = null; } node = new Xpp3Dom(nodeName); parent.addChild(node); for (String childVal : values) { Xpp3Dom childNode = new Xpp3Dom(childName); node.addChild(childNode); childNode.setValue(childVal); } }
private Xpp3Dom toDom(Map<Object, Object> map) { Xpp3Dom dom = new Xpp3Dom("configuration"); for (Map.Entry<Object, Object> entry : map.entrySet()) { if (entry.getValue() instanceof Xpp3Dom) { Xpp3Dom child = new Xpp3Dom((Xpp3Dom) entry.getValue(), entry.getKey().toString()); dom.addChild(child); } else { Xpp3Dom child = new Xpp3Dom(entry.getKey().toString()); child.setValue(entry.getValue().toString()); dom.addChild(child); } } return dom; }
public void setUnits(final List<Unit> units) { removeChild(dom, "units"); final Xpp3Dom unitsDom = new Xpp3Dom("units"); for (final Unit unit : units) { unitsDom.addChild(unit.getDom()); } unitsDom.setAttribute("size", Integer.toString(units.size())); dom.addChild(unitsDom); }
/** * Recursively convert PLEXUS config to Xpp3Dom. * * @param config The config to convert * @return The Xpp3Dom document * @see #execute(String,String,Properties) */ private Xpp3Dom toXppDom(final PlexusConfiguration config) { final Xpp3Dom result = new Xpp3Dom(config.getName()); result.setValue(config.getValue(null)); for (final String name : config.getAttributeNames()) { try { result.setAttribute(name, config.getAttribute(name)); } catch (final PlexusConfigurationException ex) { throw new IllegalArgumentException(ex); } } for (final PlexusConfiguration child : config.getChildren()) { result.addChild(this.toXppDom(child)); } return result; }
public static void setNodeValue(Xpp3Dom parent, String name, String value) { // if we do not have a current value, then just return without setting the node; if (value == null) { return; } Xpp3Dom node = parent.getChild(name); if (node == null) { node = new Xpp3Dom(name); parent.addChild(node); } node.setValue(value); }
/** * Recuresively convert Properties to Xpp3Dom. * * @param config The config to convert * @param name High-level name of it * @return The Xpp3Dom document * @see #execute(String,String,Properties) * @checkstyle ExecutableStatementCountCheck (100 lines) */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private Xpp3Dom toXppDom(final Properties config, final String name) { final Xpp3Dom xpp = new Xpp3Dom(name); for (final Map.Entry<?, ?> entry : config.entrySet()) { if (entry.getValue() instanceof String) { final Xpp3Dom child = new Xpp3Dom(entry.getKey().toString()); child.setValue(config.getProperty(entry.getKey().toString())); xpp.addChild(child); } else if (entry.getValue() instanceof String[]) { final Xpp3Dom child = new Xpp3Dom(entry.getKey().toString()); for (final String val : String[].class.cast(entry.getValue())) { final Xpp3Dom row = new Xpp3Dom(entry.getKey().toString()); row.setValue(val); child.addChild(row); } xpp.addChild(child); } else if (entry.getValue() instanceof Collection) { final Xpp3Dom child = new Xpp3Dom(entry.getKey().toString()); for (final Object val : Collection.class.cast(entry.getValue())) { final Xpp3Dom row = new Xpp3Dom(entry.getKey().toString()); if (val != null) { row.setValue(val.toString()); child.addChild(row); } } xpp.addChild(child); } else if (entry.getValue() instanceof Properties) { xpp.addChild( this.toXppDom(Properties.class.cast(entry.getValue()), entry.getKey().toString())); } else { throw new IllegalArgumentException( String.format("Invalid properties value at '%s'", entry.getKey().toString())); } } return xpp; }
public void updateModel( Model model, String yangVersion, List<CodeGeneratorConfig> generators, String yangRoot) { // Model model = super.getModel(); model.setBuild(new Build()); Plugin plugin = new Plugin(); plugin.setGroupId("org.opendaylight.yangtools"); plugin.setArtifactId("yang-maven-plugin"); plugin.setVersion(yangVersion); for (CodeGeneratorConfig genConf : generators) { Dependency dependency = new Dependency(); dependency.setGroupId(genConf.getGroupId()); dependency.setArtifactId(genConf.getArtifactId()); dependency.setVersion(genConf.getVersion()); dependency.setType("jar"); plugin.addDependency(dependency); } PluginExecution pluginExecution = new PluginExecution(); pluginExecution.setId("generate-sources"); pluginExecution.addGoal("generate-sources"); Xpp3Dom config = new Xpp3Dom("configuration"); Xpp3Dom codeGenerators = new Xpp3Dom("codeGenerators"); for (CodeGeneratorConfig genConf : generators) { Xpp3Dom generator = new Xpp3Dom("generator"); generator.addChild(createSingleParameter("codeGeneratorClass", genConf.getGenClassName())); generator.addChild(createSingleParameter("outputBaseDir", genConf.getGenOutputDirectory())); codeGenerators.addChild(generator); } config.addChild(createSingleParameter("yangFilesRootDir", yangRoot)); config.addChild(codeGenerators); config.addChild(createSingleParameter("inspectDependencies", "false")); pluginExecution.setConfiguration(config); plugin.addExecution(pluginExecution); model.getBuild().addPlugin(plugin); model.addPluginRepository( createRepoParameter( "opendaylight-release", "http://nexus.opendaylight.org/content/repositories/opendaylight.release/")); model.addPluginRepository( createRepoParameter( "opendaylight-snapshot", "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/")); model.addRepository( createRepoParameter( "opendaylight-release", "http://nexus.opendaylight.org/content/repositories/opendaylight.release/")); model.addRepository( createRepoParameter( "opendaylight-snapshot", "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/")); model.getProperties().put("maven.compiler.source", "1.7"); model.getProperties().put("maven.compiler.target", "1.7"); Dependency dependency2 = new Dependency(); dependency2.setGroupId("org.opendaylight.yangtools"); dependency2.setArtifactId("yang-binding"); dependency2.setVersion(yangVersion); dependency2.setType("jar"); model.addDependency(dependency2); }
public boolean performFinish() { try { if (customMediatorModel.getSelectedOption().equals("new.mediator")) { project = createNewProject(); IFolder srcFolder = ProjectUtils.getWorkspaceFolder(project, "src", "main", "java"); JavaUtils.addJavaSupportAndSourceFolder(project, srcFolder); /*create the new Java project*/ String className = customMediatorModel.getMediatorClassName(); String packageName = customMediatorModel.getMediatorClassPackageName(); IJavaProject iJavaProject = JavaCore.create(project); IPackageFragmentRoot root = iJavaProject.getPackageFragmentRoot(srcFolder); IPackageFragment sourcePackage = root.createPackageFragment(packageName, false, null); /*get the Mediator class template*/ String template = CustomMediatorClassTemplate.getClassTemplete(packageName, className); ICompilationUnit cu = sourcePackage.createCompilationUnit(className + ".java", template, false, null); project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); try { IEditorPart javaEditor = JavaUI.openInEditor(cu); JavaUI.revealInEditor(javaEditor, (IJavaElement) cu); } catch (Exception e) { log.error(e); } } else { project = customMediatorModel.getMediatorProject(); project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); } File pomfile = project.getFile("pom.xml").getLocation().toFile(); getModel().getMavenInfo().setPackageName("bundle"); if (!pomfile.exists()) { createPOM(pomfile); addDependancies(project); } MavenProject mavenProject = MavenUtils.getMavenProject(pomfile); boolean pluginExists = MavenUtils.checkOldPluginEntry( mavenProject, "org.apache.felix", "maven-bundle-plugin", "2.3.4"); if (!pluginExists) { Plugin plugin = MavenUtils.createPluginEntry( mavenProject, "org.apache.felix", "maven-bundle-plugin", "2.3.4", true); Xpp3Dom configurationNode = MavenUtils.createMainConfigurationNode(plugin); Xpp3Dom instructionNode = MavenUtils.createXpp3Node("instructions"); Xpp3Dom bundleSymbolicNameNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-SymbolicName"); Xpp3Dom bundleNameNode = MavenUtils.createXpp3Node(instructionNode, "Bundle-Name"); ; Xpp3Dom exportPackageNode = MavenUtils.createXpp3Node(instructionNode, "Export-Package"); Xpp3Dom dynamicImportNode = MavenUtils.createXpp3Node(instructionNode, "DynamicImport-Package"); bundleSymbolicNameNode.setValue(project.getName()); bundleNameNode.setValue(project.getName()); if (customMediatorModel.getMediatorClassPackageName() != null && !customMediatorModel.getMediatorClassPackageName().trim().isEmpty()) { exportPackageNode.setValue(customMediatorModel.getMediatorClassPackageName()); } else { IJavaProject javaProject = JavaCore.create(project); if (null != javaProject) { StringBuffer sb = new StringBuffer(); for (IPackageFragment pkg : javaProject.getPackageFragments()) { if (pkg.getKind() == IPackageFragmentRoot.K_SOURCE) { if (pkg.hasChildren()) { sb.append(pkg.getElementName()).append(","); } } } exportPackageNode.setValue(sb.toString().replaceAll(",$", "")); } } dynamicImportNode.setValue("*"); configurationNode.addChild(instructionNode); MavenUtils.saveMavenProject(mavenProject, pomfile); } project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); ProjectUtils.addNatureToProject(project, false, MEDIATOR_PROJECT_NATURE); MavenUtils.updateWithMavenEclipsePlugin( pomfile, new String[] {JDT_BUILD_COMMAND}, new String[] {MEDIATOR_PROJECT_NATURE, JDT_PROJECT_NATURE}); customMediatorModel.addToWorkingSet(project); project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); refreshDistProjects(); } catch (CoreException e) { log.error(e); } catch (Exception e) { log.error(e); } return true; }
public void verifyWriter() throws IOException, DocumentException { String expectedXml = FileUtils.fileRead(getTestFile("src/test/verifiers/dom4j/expected.xml")); expectedXml = expectedXml.replaceAll("(\r\n)|(\r)", "\n"); // ---------------------------------------------------------------------- // Build the model thats going to be written. // ---------------------------------------------------------------------- Model expected = new Model(); expected.setExtend("/foo/bar"); expected.setName("Maven"); expected.setModelVersion("4.0.0"); MailingList mailingList = new MailingList(); mailingList.setName("Mailing list"); mailingList.setSubscribe("Super Subscribe"); mailingList.setUnsubscribe("Duper Unsubscribe"); mailingList.setArchive("?ber Archive"); expected.addMailingList(mailingList); Scm scm = new Scm(); String connection = "connection"; String developerConnection = "developerConnection"; String url = "url"; scm.setConnection(connection); scm.setDeveloperConnection(developerConnection); scm.setUrl(url); expected.setScm(scm); Build build = new Build(); build.setSourceDirectory("src/main/java"); build.setUnitTestSourceDirectory("src/test/java"); SourceModification sourceModification = new SourceModification(); sourceModification.setClassName("excludeEclipsePlugin"); sourceModification.setDirectory("foo"); sourceModification.addExclude("de/abstrakt/tools/codegeneration/eclipse/*.java"); build.addSourceModification(sourceModification); expected.setBuild(build); Component component = new Component(); component.setName("component1"); expected.addComponent(component); component = new Component(); component.setName("component2"); component.setComment("comment2"); expected.addComponent(component); Component c2 = new Component(); c2.setName("sub"); c2.setComment("subcomment"); component.getComponents().add(c2); component = new Component(); component.setName("component3"); Xpp3Dom xpp3Dom = new Xpp3Dom("custom"); Xpp3Dom child = new Xpp3Dom("foo"); child.setValue("bar"); xpp3Dom.addChild(child); child = new Xpp3Dom("bar"); child.setAttribute("att1", "value"); child.setValue("baz"); xpp3Dom.addChild(child); child = new Xpp3Dom("el1"); xpp3Dom.addChild(child); Xpp3Dom el1 = child; child = new Xpp3Dom("el2"); child.setValue("text"); el1.addChild(child); component.setCustom(xpp3Dom); expected.addComponent(component); component = new Component(); component.setName("component4"); expected.addComponent(component); Properties properties = new Properties(); properties.setProperty("name", "value"); component.setFlatProperties(properties); properties = new Properties(); properties.setProperty("key", "theValue"); component.setProperties(properties); Repository repository = new Repository(); repository.setId("foo"); expected.addRepository(repository); // ---------------------------------------------------------------------- // Write out the model // ---------------------------------------------------------------------- MavenDom4jWriter writer = new MavenDom4jWriter(); StringWriter buffer = new StringWriter(); writer.write(buffer, expected); String actualXml = buffer.toString(); actualXml = actualXml.replaceAll("(\r\n)|(\r)", "\n"); // System.out.println( expectedXml ); // // System.err.println( actualXml ); Assert.assertEquals(expectedXml.trim(), actualXml.trim()); MavenDom4jReader reader = new MavenDom4jReader(); Model actual = reader.read(new StringReader(actualXml)); Assert.assertNotNull("Actual", actual); assertModel(expected, actual); buffer = new StringWriter(); writer.write(buffer, actual); Assert.assertEquals( expectedXml.trim(), buffer.toString().trim().replaceAll("(\r\n)|(\r)", "\n")); }
/** * If enabled, grab the execution root pom (which will be the topmost POM in terms of directory * structure). Check for the presence of the project-sources-maven-plugin in the base build * (/project/build/plugins/). Inject a new plugin execution for creating project sources if this * plugin has not already been declared in the base build section. */ @Override public Set<Project> applyChanges(final List<Project> projects, final ManipulationSession session) throws ManipulationException { final ProjectSourcesInjectingState state = session.getState(ProjectSourcesInjectingState.class); // This manipulator will only run if its enabled *and* at least one other manipulator is // enabled. if (state.isEnabled() && session.anyStateEnabled(State.activeByDefault)) { for (final Project project : projects) { if (project.isExecutionRoot()) { logger.info("Examining {} to apply sources/metadata plugins.", project); final Model model = project.getModel(); Build build = model.getBuild(); if (build == null) { build = new Build(); model.setBuild(build); } boolean changed = false; final Map<String, Plugin> pluginMap = build.getPluginsAsMap(); if (state.isProjectSourcesPluginEnabled() && !pluginMap.containsKey(PROJECT_SOURCES_COORD)) { final PluginExecution execution = new PluginExecution(); execution.setId(PROJECT_SOURCES_EXEC_ID); execution.setPhase(INITIALIZE_PHASE); execution.setGoals(Collections.singletonList(PROJECT_SOURCES_GOAL)); final Plugin plugin = new Plugin(); plugin.setGroupId(PROJECT_SOURCES_GID); plugin.setArtifactId(PROJECT_SOURCES_AID); plugin.setVersion(state.getProjectSourcesPluginVersion()); plugin.addExecution(execution); build.addPlugin(plugin); changed = true; } if (state.isBuildMetadataPluginEnabled() && !pluginMap.containsKey(BMMP_COORD)) { final PluginExecution execution = new PluginExecution(); execution.setId(BMMP_EXEC_ID); execution.setPhase(VALIDATE_PHASE); execution.setGoals(Collections.singletonList(BMMP_GOAL)); final Xpp3Dom xml = new Xpp3Dom("configuration"); final Map<String, Object> config = new HashMap<>(); config.put("createPropertiesReport", true); config.put("hideCommandLineInfo", false); config.put("hideJavaOptsInfo", false); config.put("activateOutputFileMapping", true); config.put("addJavaRuntimeInfo", true); // Default name is build.properties but we currently prefer build.metadata. config.put("propertiesOutputFile", "build.metadata"); // Deactivate features we don't want. config.put("createXmlReport", false); config.put("addLocallyModifiedTagToFullVersion", false); config.put("addToGeneratedSources", false); config.put("validateCheckout", false); config.put("forceNewProperties", true); config.put("addBuildDateToFullVersion", false); config.put("addHostInfo", false); config.put("addBuildDateInfo", false); config.put("addOsInfo", false); config.put("addMavenExecutionInfo", false); config.put("addToFilters", false); final Xpp3Dom additionalLocations = new Xpp3Dom("addToLocations"); final Xpp3Dom additionalLocation = new Xpp3Dom("addToLocation"); xml.addChild(additionalLocations); additionalLocations.addChild(additionalLocation); additionalLocation.setValue("${session.executionRootDirectory}"); for (final Map.Entry<String, Object> entry : config.entrySet()) { final Xpp3Dom child = new Xpp3Dom(entry.getKey()); if (entry.getValue() != null) { child.setValue(entry.getValue().toString()); } xml.addChild(child); } execution.setConfiguration(xml); final Plugin plugin = new Plugin(); plugin.setGroupId(BMMP_GID); plugin.setArtifactId(BMMP_AID); plugin.setVersion(state.getBuildMetadataPluginVersion()); plugin.addExecution(execution); build.addPlugin(plugin); changed = true; } if (changed) { return Collections.singleton(project); } } } } return Collections.emptySet(); }