Java Code Examples for com.ximpleware.VTDGen#enableIgnoredWhiteSpace()

The following examples show how to use com.ximpleware.VTDGen#enableIgnoredWhiteSpace() . 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: AbstractVersionModifyingMojo.java    From revapi with Apache License 2.0 6 votes vote down vote up
void updateProjectParentVersion(MavenProject project, Version version) throws MojoExecutionException {
    try {
        VTDGen gen = new VTDGen();
        gen.enableIgnoredWhiteSpace(true);
        gen.parseFile(project.getFile().getAbsolutePath(), true);

        VTDNav nav = gen.getNav();
        AutoPilot ap = new AutoPilot(nav);
        ap.selectXPath("namespace-uri(.)");
        String ns = ap.evalXPathToString();

        nav.toElementNS(VTDNav.FIRST_CHILD, ns, "parent");
        nav.toElementNS(VTDNav.FIRST_CHILD, ns, "version");
        int pos = nav.getText();

        XMLModifier mod = new XMLModifier(nav);
        mod.updateToken(pos, version.toString());

        try (OutputStream out = new FileOutputStream(project.getFile())) {
            mod.output(out);
        }
    } catch (IOException | ModifyException | NavException | XPathParseException | TranscodeException e) {
        throw new MojoExecutionException("Failed to update the parent version of project " + project, e);
    }
}
 
Example 2
Source File: AbstractVersionModifyingMojo.java    From revapi with Apache License 2.0 4 votes vote down vote up
void updateProjectVersion(MavenProject project, Version version) throws MojoExecutionException {
    try {

        int indentationSize;
        try (BufferedReader rdr = new BufferedReader(new FileReader(project.getFile()))) {
            indentationSize = XmlUtil.estimateIndentationSize(rdr);
        }

        VTDGen gen = new VTDGen();
        gen.enableIgnoredWhiteSpace(true);
        gen.parseFile(project.getFile().getAbsolutePath(), true);

        VTDNav nav = gen.getNav();
        AutoPilot ap = new AutoPilot(nav);
        XMLModifier mod = new XMLModifier(nav);

        ap.selectXPath("/project/version");
        if (ap.evalXPath() != -1) {
            //found the version
            int textPos = nav.getText();
            mod.updateToken(textPos, version.toString());
        } else {
            if (!forceVersionUpdate && !project.getParent().getVersion().equals(version.toString())) {
                throw new MojoExecutionException("Project " + project.getArtifactId() + " inherits the version" +
                        " from the parent project (" + project.getParent().getVersion() + ") but should have a" +
                        " different version according to the configured rules (" + version.toString() + "). If" +
                        " you wish to insert an explicit version instead of inheriting it, set the " +
                        Props.forceVersionUpdate.NAME + " property to true.");
            }

            //place the version after the artifactId
            ap.selectXPath("/project/artifactId");
            if (ap.evalXPath() == -1) {
                throw new MojoExecutionException("Failed to find artifactId element in the pom.xml of project "
                        + project.getArtifact().getId() + " when trying to insert a version tag after it.");
            } else {
                StringBuilder versionLine = new StringBuilder();
                versionLine.append('\n');
                for (int i = 0; i < indentationSize; ++i) {
                    versionLine.append(' ');
                }
                versionLine.append("<version>").append(version.toString()).append("</version>");
                mod.insertAfterElement(versionLine.toString());
            }
        }

        try (OutputStream out = new FileOutputStream(project.getFile())) {
            mod.output(out);
        }
    } catch (IOException | ModifyException | NavException | XPathParseException | XPathEvalException | TranscodeException e) {
        throw new MojoExecutionException("Failed to update the version of project " + project, e);
    }
}
 
Example 3
Source File: ConvertToXmlConfigMojo.java    From revapi with Apache License 2.0 4 votes vote down vote up
private void updateAllConfigurationFiles(MavenProject project, Map<String, ModelNode> extensionSchemas,
                                         int indentationSize) throws Exception {
    VTDGen gen = new VTDGen();
    gen.enableIgnoredWhiteSpace(true);
    gen.parseFile(project.getFile().getAbsolutePath(), true);

    VTDNav nav = gen.getNav();
    XMLModifier mod = new XMLModifier(nav);

    AutoPilot ap = new AutoPilot(nav);

    ThrowingConsumer<String> update = xpath -> {
        ap.resetXPath();
        ap.selectXPath(xpath);

        while (ap.evalXPath() != -1) {
            int textPos = nav.getText();

            String configFile = nav.toString(textPos);

            File newFile = updateConfigurationFile(new File(configFile), extensionSchemas, indentationSize);
            if (newFile == null) {
                continue;
            }

            mod.updateToken(textPos, newFile.getPath());
        }
    };

    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/configuration/analysisConfigurationFiles/*[not(self::configurationFile)]");
    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/configuration/analysisConfigurationFiles/configurationFile[not(roots)]/path");

    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/executions/execution/configuration/analysisConfigurationFiles/*[not(self::configurationFile)]");
    update.accept("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']" +
            "/executions/execution/configuration/analysisConfigurationFiles/configurationFile[not(roots)]/path");

    try (OutputStream out = new FileOutputStream(project.getFile())) {
        mod.output(out);
    }
}
 
Example 4
Source File: ConvertToXmlConfigMojo.java    From revapi with Apache License 2.0 4 votes vote down vote up
private static void updateAllConfigurations(File pomXml, Map<String, ModelNode> extensionSchemas,
                                            int indentationSize) throws Exception {
    VTDGen gen = new VTDGen();
    gen.enableIgnoredWhiteSpace(true);
    gen.parseFile(pomXml.getAbsolutePath(), true);

    VTDNav nav = gen.getNav();
    XMLModifier mod = new XMLModifier(nav);

    Callable<Void> update = () -> {
        int textPos = nav.getText();
        String jsonConfig = nav.toRawString(textPos);

        PlexusConfiguration xml = convertToXml(extensionSchemas, jsonConfig);
        if (xml == null) {
            return null;
        }

        StringWriter pretty = new StringWriter();
        XmlUtil.toIndentedString(xml, indentationSize, nav.getTokenDepth(textPos), pretty);

        //remove the first indentation, because text is already indented
        String prettyXml = pretty.toString().substring(indentationSize * nav.getTokenDepth(textPos));

        mod.insertAfterElement(prettyXml);
        mod.remove();

        return null;
    };

    AutoPilot ap = new AutoPilot(nav);

    ap.selectXPath("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']/configuration/analysisConfiguration");
    while (ap.evalXPath() != -1) {
        update.call();
    }

    ap.resetXPath();

    ap.selectXPath("//plugin[groupId = 'org.revapi' and artifactId = 'revapi-maven-plugin']/executions/execution/configuration/analysisConfiguration");
    while (ap.evalXPath() != -1) {
        update.call();
    }

    try (OutputStream out = new FileOutputStream(pomXml)) {
        mod.output(out);
    }
}