Search in sources :

Example 6 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project maven-plugins by apache.

the class AntBuildWriter method writeBuildXml.

/**
 * Generate an generic <code>build.xml</code> if not already exist
 *
 * @throws IOException
 * @see #DEFAULT_BUILD_FILENAME
 */
private void writeBuildXml() throws IOException {
    File outputFile = new File(project.getBasedir(), DEFAULT_BUILD_FILENAME);
    if (outputFile.exists() && !overwrite) {
        return;
    }
    String encoding = "UTF-8";
    OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(outputFile), encoding);
    XMLWriter writer = new PrettyPrintXMLWriter(w, StringUtils.repeat(" ", DEFAULT_INDENTATION_SIZE), encoding, null);
    // ----------------------------------------------------------------------
    // <!-- comments -->
    // ----------------------------------------------------------------------
    AntBuildWriterUtil.writeAntVersionHeader(writer);
    // ----------------------------------------------------------------------
    // <project/>
    // ----------------------------------------------------------------------
    writer.startElement("project");
    writer.addAttribute("name", project.getArtifactId());
    writer.addAttribute("default", "package");
    writer.addAttribute("basedir", ".");
    XmlWriterUtil.writeLineBreak(writer);
    XmlWriterUtil.writeCommentText(writer, "Import " + DEFAULT_MAVEN_BUILD_FILENAME + " into the current project", 1);
    writer.startElement("import");
    writer.addAttribute("file", DEFAULT_MAVEN_BUILD_FILENAME);
    // import
    writer.endElement();
    XmlWriterUtil.writeLineBreak(writer, 1, 1);
    XmlWriterUtil.writeCommentText(writer, "Help target", 1);
    writer.startElement("target");
    writer.addAttribute("name", "help");
    writer.startElement("echo");
    writer.addAttribute("message", "Please run: $ant -projecthelp");
    // echo
    writer.endElement();
    // target
    writer.endElement();
    XmlWriterUtil.writeLineBreak(writer, 2);
    // project
    writer.endElement();
    XmlWriterUtil.writeLineBreak(writer);
    w.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) File(java.io.File) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)

Example 7 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project maven-plugins by apache.

the class DoapMojo method doWrite.

private void doWrite(MavenProject project, File outputFile, Writer w) throws MojoExecutionException {
    if (asfExtOptions.isIncluded()) {
        getLog().info("Generating an ASF DOAP file " + outputFile.getAbsolutePath());
    } else {
        getLog().info("Generating a pure DOAP file " + outputFile.getAbsolutePath());
    }
    XMLWriter writer = new PrettyPrintXMLWriter(w, project.getModel().getModelEncoding(), null);
    // ----------------------------------------------------------------------------
    // Convert POM to DOAP
    // ----------------------------------------------------------------------------
    DoapUtil.writeHeader(writer);
    // Heading
    DoapUtil.writeStartElement(writer, "rdf", "RDF");
    if (Arrays.binarySearch(Locale.getISOLanguages(), lang) < 0) {
        messages.addMessage(new String[] { "doapOptions", "lang" }, lang, UserMessages.INVALID_ISO_DATE);
        throw new MojoExecutionException(messages.getErrorMessages().get(0));
    }
    writer.addAttribute("xml:lang", lang);
    if (StringUtils.isEmpty(doapOptions.getXmlnsNamespaceURI())) {
        messages.addMessage(new String[] { "doapOptions", "xmlnsNamespaceURI" }, null, UserMessages.REQUIRED);
        throw new MojoExecutionException(messages.getErrorMessages().get(0));
    }
    writer.addAttribute("xmlns" + (StringUtils.isEmpty(doapOptions.getXmlnsPrefix()) ? "" : ":" + doapOptions.getXmlnsPrefix()), doapOptions.getXmlnsNamespaceURI());
    writer.addAttribute("xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    writer.addAttribute("xmlns:foaf", "http://xmlns.com/foaf/0.1/");
    if (asfExtOptions.isIncluded()) {
        if (StringUtils.isEmpty(asfExtOptions.getXmlnsPrefix())) {
            messages.addMessage(new String[] { "doapOptions", "xmlnsPrefix" }, null, UserMessages.REQUIRED);
            throw new MojoExecutionException(messages.getErrorMessages().get(0));
        }
        if (StringUtils.isEmpty(asfExtOptions.getXmlnsNamespaceURI())) {
            messages.addMessage(new String[] { "doapOptions", "xmlnsNamespaceURI" }, null, UserMessages.REQUIRED);
        }
        writer.addAttribute("xmlns" + (StringUtils.isEmpty(asfExtOptions.getXmlnsPrefix()) ? "" : ":" + asfExtOptions.getXmlnsPrefix()), asfExtOptions.getXmlnsNamespaceURI());
    }
    if (extOptions != null && extOptions.length > 0 && !extOptions[0].getExtensions().isEmpty()) {
        for (ExtOptions extOption : extOptions) {
            if (StringUtils.isEmpty(extOption.getXmlnsPrefix())) {
                messages.addMessage(new String[] { "extOptions", "extOption", "xmlnsPrefix" }, null, UserMessages.REQUIRED);
                throw new MojoExecutionException(messages.getErrorMessages().get(0));
            }
            if (StringUtils.isEmpty(extOption.getXmlnsNamespaceURI())) {
                messages.addMessage(new String[] { "extOptions", "extOption", "xmlnsNamespaceURI" }, null, UserMessages.REQUIRED);
                throw new MojoExecutionException(messages.getErrorMessages().get(0));
            }
            writer.addAttribute("xmlns" + (StringUtils.isEmpty(extOption.getXmlnsPrefix()) ? "" : ":" + extOption.getXmlnsPrefix()), extOption.getXmlnsNamespaceURI());
        }
    }
    // Project
    DoapUtil.writeStartElement(writer, doapOptions.getXmlnsPrefix(), "Project");
    boolean added = false;
    if (artifact != null) {
        String about = project.getUrl();
        if (StringUtils.isNotEmpty(about)) {
            try {
                new URL(about);
                writer.addAttribute("rdf:about", about);
                added = true;
            } catch (MalformedURLException e) {
            // ignore
            }
        }
        if (!added) {
            messages.getWarnMessages().add("The project's url defined from " + artifact.toConfiguration() + " is empty or not a valid URL, using <about/> parameter.");
        }
    }
    if (!added) {
        if (StringUtils.isNotEmpty(about)) {
            try {
                new URL(about);
                writer.addAttribute("rdf:about", about);
            } catch (MalformedURLException e) {
                messages.addMessage(new String[] { "about" }, about, UserMessages.INVALID_URL);
            }
            added = true;
        }
    }
    if (!added) {
        messages.addMessage(new String[] { "about" }, null, UserMessages.RECOMMENDED);
    }
    // name
    writeName(writer, project);
    // description
    writeDescription(writer, project);
    // implements
    writeImplements(writer);
    // Audience
    writeAudience(writer);
    // Vendor
    writeVendor(writer, project);
    // created
    writeCreated(writer, project);
    // homepage and old-homepage
    writeHomepage(writer, project);
    // Blog
    writeBlog(writer);
    // licenses
    writeLicenses(writer, project);
    // programming-language
    writeProgrammingLanguage(writer, project);
    // category
    writeCategory(writer, project);
    // os
    writeOS(writer, project);
    // Plateform
    writePlateform(writer);
    // Language
    writeLanguage(writer);
    // SCM
    writeSourceRepositories(writer, project);
    // bug-database
    writeBugDatabase(writer, project);
    // mailing list
    writeMailingList(writer, project);
    // download-page and download-mirror
    writeDownloadPage(writer, project);
    // screenshots
    writeScreenshots(writer, project);
    // service-endpoint
    writeServiceEndpoint(writer);
    // wiki
    writeWiki(writer, project);
    // Releases
    writeReleases(writer, project);
    // Developers
    List<Contributor> developers = project.getDevelopers();
    writeContributors(writer, developers);
    // Contributors
    List<Contributor> contributors = project.getContributors();
    writeContributors(writer, contributors);
    // Extra DOAP
    Map<Object, String> map = doapOptions.getExtra();
    writeExtra(writer, project, "Extra DOAP vocabulary.", map, doapOptions.getXmlnsPrefix());
    // ASFext
    writeASFext(writer, project);
    // Extra extensions
    writeExtensions(writer);
    // Project
    writer.endElement();
    writeOrganizations(writer);
    // rdf:RDF
    writer.endElement();
}
Also used : MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Contributor(org.apache.maven.model.Contributor) ExtOptions(org.apache.maven.plugin.doap.options.ExtOptions) ASFExtOptions(org.apache.maven.plugin.doap.options.ASFExtOptions) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) URL(java.net.URL)

Example 8 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project maven-plugins by apache.

the class DoapUtilTest method testWriteRdfResourceElement.

/**
 * Test method for {@link DoapUtil#writeRdfResourceElement(XMLWriter, String, String)}.
 *
 * @throws Exception if any
 */
public void testWriteRdfResourceElement() throws Exception {
    StringWriter w = new StringWriter();
    XMLWriter writer = new PrettyPrintXMLWriter(w);
    DoapUtil.writeRdfResourceElement(writer, null, "name", "value");
    w.close();
    assertEquals(w.toString(), "<name " + DoapUtil.RDF_RESOURCE + "=\"value\"/>");
    w = new StringWriter();
    writer = new PrettyPrintXMLWriter(w);
    try {
        DoapUtil.writeRdfResourceElement(writer, null, null, null);
        assertTrue("Null not catched", false);
    } catch (IllegalArgumentException e) {
        assertTrue("IllegalArgumentException catched", true);
    } finally {
        w.close();
    }
}
Also used : StringWriter(java.io.StringWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)

Example 9 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project che by eclipse.

the class EffectivePomWriter method getEffectivePom.

public static String getEffectivePom(MavenServerImpl server, final File pom, List<String> activeProfiles, List<String> inactiveProfiles) {
    StringWriter stringWriter = new StringWriter();
    try {
        MavenExecutionRequest request = server.newMavenRequest(pom, activeProfiles, inactiveProfiles, Collections.emptyList());
        server.runMavenRequest(request, () -> {
            try {
                ProjectBuilder builder = server.getMavenComponent(ProjectBuilder.class);
                ProjectBuildingResult projectBuildingResult = builder.build(new File(pom.getPath()), request.getProjectBuildingRequest());
                MavenProject project = projectBuildingResult.getProject();
                XMLWriter writer = new PrettyPrintXMLWriter(stringWriter, "    ");
                writeHeader(writer);
                writeEffectivePom(project, writer);
            } catch (ProjectBuildingException | MojoExecutionException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        });
    } catch (Exception e) {
        return null;
    }
    return stringWriter.toString();
}
Also used : ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) ProjectBuildingException(org.apache.maven.project.ProjectBuildingException) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StringWriter(java.io.StringWriter) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) File(java.io.File)

Example 10 with PrettyPrintXMLWriter

use of org.codehaus.plexus.util.xml.PrettyPrintXMLWriter in project intellij-community by JetBrains.

the class MavenEffectivePomDumper method evaluateEffectivePom.

// See org.apache.maven.plugins.help.EffectivePomMojo#execute from maven-help-plugin
@Nullable
public static String evaluateEffectivePom(final Maven3ServerEmbedder embedder, @NotNull final File file, @NotNull List<String> activeProfiles, @NotNull List<String> inactiveProfiles) throws RemoteException, MavenServerProcessCanceledException {
    final StringWriter w = new StringWriter();
    try {
        final MavenExecutionRequest request = embedder.createRequest(file, activeProfiles, inactiveProfiles, Collections.<String>emptyList());
        embedder.executeWithMavenSession(request, new Runnable() {

            @Override
            public void run() {
                try {
                    // copied from DefaultMavenProjectBuilder.buildWithDependencies
                    ProjectBuilder builder = embedder.getComponent(ProjectBuilder.class);
                    ProjectBuildingResult buildingResult = builder.build(new File(file.getPath()), request.getProjectBuildingRequest());
                    MavenProject project = buildingResult.getProject();
                    XMLWriter writer = new PrettyPrintXMLWriter(new PrintWriter(w), StringUtils.repeat(" ", XmlWriterUtil.DEFAULT_INDENTATION_SIZE), "\n", null, null);
                    writeHeader(writer);
                    writeEffectivePom(project, writer);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    } catch (Exception e) {
        return null;
    }
    return w.toString();
}
Also used : MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) XMLWriter(org.codehaus.plexus.util.xml.XMLWriter) PrettyPrintXMLWriter(org.codehaus.plexus.util.xml.PrettyPrintXMLWriter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RemoteException(java.rmi.RemoteException) ProjectBuilder(org.apache.maven.project.ProjectBuilder) MavenProject(org.apache.maven.project.MavenProject) ProjectBuildingResult(org.apache.maven.project.ProjectBuildingResult) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PrettyPrintXMLWriter (org.codehaus.plexus.util.xml.PrettyPrintXMLWriter)16 XMLWriter (org.codehaus.plexus.util.xml.XMLWriter)10 StringWriter (java.io.StringWriter)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 IOException (java.io.IOException)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 MavenProject (org.apache.maven.project.MavenProject)3 Artifact (org.apache.maven.artifact.Artifact)2 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)2 ProjectBuilder (org.apache.maven.project.ProjectBuilder)2 ProjectBuildingResult (org.apache.maven.project.ProjectBuildingResult)2 BufferedWriter (java.io.BufferedWriter)1 FileNotFoundException (java.io.FileNotFoundException)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1