Java Code Examples for org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader#hasNext()

The following examples show how to use org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader#hasNext() . 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: PomHelper.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the project version from the pom.
 *
 * @param pom The pom.
 * @return the project version or <code>null</code> if the project version is not defined (i.e. inherited from
 *         parent version).
 * @throws XMLStreamException if something went wrong.
 */
public static String getProjectVersion( final ModifiedPomXMLEventReader pom )
    throws XMLStreamException
{
    Stack<String> stack = new Stack<String>();
    String path = "";
    final Pattern matchScopeRegex = Pattern.compile( "/project/version" );

    pom.rewind();

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartElement() )
        {
            stack.push( path );
            path = path + "/" + event.asStartElement().getName().getLocalPart();

            if ( matchScopeRegex.matcher( path ).matches() )
            {
                pom.mark( 0 );
            }
        }
        if ( event.isEndElement() )
        {
            if ( matchScopeRegex.matcher( path ).matches() )
            {
                pom.mark( 1 );
                if ( pom.hasMark( 0 ) && pom.hasMark( 1 ) )
                {
                    return pom.getBetween( 0, 1 ).trim();
                }
                pom.clearMark( 0 );
                pom.clearMark( 1 );
            }
            path = stack.pop();
        }
    }
    return null;
}
 
Example 2
Source File: RewriteWithStAXTest.java    From versions-maven-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplace()
    throws Exception
{
    String input = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n"
        + "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n"
        + "    <artifactId>mojo-&amp;sandbox-parent</artifactId>\n" + "    <version>5-SNAPSHOT</version>\r"
        + "  </parent>\r" + "<build/></project>";
    String expected = "<?xml version='1.0' encoding='utf-8'?>\n" + "<project>\n\r\n\r\n\r\n\r" + "  <parent>\r\n"
        + "    <groupId xmlns='foo'>org.codehaus.mojo</groupId>\n" + "    <artifactId>my-artifact</artifactId>\n"
        + "    <version>5-SNAPSHOT</version>\r" + "  </parent>\r" + "<build/></project>";

    StringBuilder output = new StringBuilder( input );

    XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
    inputFactory.setProperty( XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE );
    ModifiedPomXMLEventReader eventReader = new ModifiedPomXMLEventReader( output, inputFactory, null );
    while ( eventReader.hasNext() )
    {
        XMLEvent event = eventReader.nextEvent();
        if ( event instanceof StartElement
            && event.asStartElement().getName().getLocalPart().equals( "artifactId" ) )
        {
            eventReader.mark( 0 );
        }
        if ( event instanceof EndElement && event.asEndElement().getName().getLocalPart().equals( "artifactId" ) )
        {
            eventReader.mark( 1 );
            if ( eventReader.hasMark( 0 ) )
            {
                eventReader.replaceBetween( 0, 1, "my-artifact" );
            }
        }
    }

    assertEquals( expected, output.toString() );
}
 
Example 3
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Searches the pom re-defining the specified property to the specified version.
 *
 * @param pom The pom to modify.
 * @param profileId The profile in which to modify the property.
 * @param property The property to modify.
 * @param value The new value of the property.
 * @return <code>true</code> if a replacement was made.
 * @throws XMLStreamException if somethinh went wrong.
 */
public static boolean setPropertyVersion( final ModifiedPomXMLEventReader pom, final String profileId,
                                          final String property, final String value )
    throws XMLStreamException
{
    Stack<String> stack = new Stack<String>();
    String path = "";
    final Pattern propertyRegex;
    final Pattern matchScopeRegex;
    final Pattern projectProfileId;
    boolean inMatchScope = false;
    boolean madeReplacement = false;
    if ( profileId == null )
    {
        propertyRegex = Pattern.compile( "/project/properties/" + RegexUtils.quote( property ) );
        matchScopeRegex = Pattern.compile( "/project/properties" );
        projectProfileId = null;
    }
    else
    {
        propertyRegex = Pattern.compile( "/project/profiles/profile/properties/" + RegexUtils.quote( property ) );
        matchScopeRegex = Pattern.compile( "/project/profiles/profile" );
        projectProfileId = Pattern.compile( "/project/profiles/profile/id" );
    }

    pom.rewind();

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartElement() )
        {
            stack.push( path );
            path = path + "/" + event.asStartElement().getName().getLocalPart();

            if ( propertyRegex.matcher( path ).matches() )
            {
                pom.mark( 0 );
            }
            else if ( matchScopeRegex.matcher( path ).matches() )
            {
                // we're in a new match scope
                // reset any previous partial matches
                inMatchScope = profileId == null;
                pom.clearMark( 0 );
                pom.clearMark( 1 );
            }
            else if ( profileId != null && projectProfileId.matcher( path ).matches() )
            {
                String candidateId = pom.getElementText();
                path = stack.pop(); // since getElementText will be after the end element

                inMatchScope = profileId.trim().equals( candidateId.trim() );
            }
        }
        if ( event.isEndElement() )
        {
            if ( propertyRegex.matcher( path ).matches() )
            {
                pom.mark( 1 );
            }
            else if ( matchScopeRegex.matcher( path ).matches() )
            {
                if ( inMatchScope && pom.hasMark( 0 ) && pom.hasMark( 1 ) )
                {
                    pom.replaceBetween( 0, 1, value );
                    madeReplacement = true;
                }
                pom.clearMark( 0 );
                pom.clearMark( 1 );
                inMatchScope = false;
            }
            path = stack.pop();
        }
    }
    return madeReplacement;
}
 
Example 4
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Searches the pom re-defining a project value using the given pattern.
 *
 * @param pom The pom to modify.
 * @param pattern The pattern to look for.
 * @param value The new value of the property.
 * @return <code>true</code> if a replacement was made.
 * @throws XMLStreamException if something went wrong.
 */
public static boolean setProjectValue( final ModifiedPomXMLEventReader pom, String pattern, final String value )
    throws XMLStreamException
{
    Stack<String> stack = new Stack<String>();
    String path = "";
    final Pattern matchScopeRegex;
    boolean madeReplacement = false;
    matchScopeRegex = Pattern.compile( pattern );

    pom.rewind();

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartElement() )
        {
            stack.push( path );
            path = path + "/" + event.asStartElement().getName().getLocalPart();

            if ( matchScopeRegex.matcher( path ).matches() )
            {
                pom.mark( 0 );
            }
        }
        if ( event.isEndElement() )
        {
            if ( matchScopeRegex.matcher( path ).matches() )
            {
                pom.mark( 1 );
                if ( pom.hasMark( 0 ) && pom.hasMark( 1 ) )
                {
                    pom.replaceBetween( 0, 1, value );
                    madeReplacement = true;
                }
                pom.clearMark( 0 );
                pom.clearMark( 1 );
            }
            path = stack.pop();
        }
    }
    return madeReplacement;
}
 
Example 5
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Searches the pom re-defining the project version to the specified version.
 *
 * @param pom The pom to modify.
 * @param value The new value of the property.
 * @return <code>true</code> if a replacement was made.
 * @throws XMLStreamException if somethinh went wrong.
 */
public static boolean setProjectParentVersion( final ModifiedPomXMLEventReader pom, final String value )
    throws XMLStreamException
{
    Stack<String> stack = new Stack<String>();
    String path = "";
    final Pattern matchScopeRegex;
    boolean madeReplacement = false;
    matchScopeRegex = Pattern.compile( "/project/parent/version" );

    pom.rewind();

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartElement() )
        {
            stack.push( path );
            path = path + "/" + event.asStartElement().getName().getLocalPart();

            if ( matchScopeRegex.matcher( path ).matches() )
            {
                pom.mark( 0 );
            }
        }
        if ( event.isEndElement() )
        {
            if ( matchScopeRegex.matcher( path ).matches() )
            {
                pom.mark( 1 );
                if ( pom.hasMark( 0 ) && pom.hasMark( 1 ) )
                {
                    pom.replaceBetween( 0, 1, value );
                    madeReplacement = true;
                }
                pom.clearMark( 0 );
                pom.clearMark( 1 );
            }
            path = stack.pop();
        }
    }
    return madeReplacement;
}
 
Example 6
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the parent artifact from the pom.
 *
 * @param pom The pom.
 * @param helper The helper (used to create the artifact).
 * @return The parent artifact or <code>null</code> if no parent is specified.
 * @throws XMLStreamException if something went wrong.
 */
public static Artifact getProjectParent( final ModifiedPomXMLEventReader pom, VersionsHelper helper )
    throws XMLStreamException
{
    Stack<String> stack = new Stack<>();
    String path = "";
    final Pattern matchScopeRegex = Pattern.compile( "/project/parent((/groupId)|(/artifactId)|(/version))" );
    String groupId = null;
    String artifactId = null;
    String version = null;

    pom.rewind();

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartElement() )
        {
            stack.push( path );
            final String elementName = event.asStartElement().getName().getLocalPart();
            path = path + "/" + elementName;

            if ( matchScopeRegex.matcher( path ).matches() )
            {
                if ( "groupId".equals( elementName ) )
                {
                    groupId = pom.getElementText().trim();
                    path = stack.pop();
                }
                else if ( "artifactId".equals( elementName ) )
                {
                    artifactId = pom.getElementText().trim();
                    path = stack.pop();
                }
                else if ( "version".equals( elementName ) )
                {
                    version = pom.getElementText().trim();
                    path = stack.pop();
                }
            }
        }
        if ( event.isEndElement() )
        {
            path = stack.pop();
        }
    }
    if ( groupId == null || artifactId == null || version == null )
    {
        return null;
    }
    return helper.createDependencyArtifact( groupId, artifactId, VersionRange.createFromVersion( version ), "pom",
                                            null, null, false );
}
 
Example 7
Source File: PomHelper.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Reads imported POMs from the dependency management section.
 *
 * @param pom POM
 * @return a non-null list of {@link Dependency} for each imported POM
 * @throws XMLStreamException XML stream exception
 * @see <a href="https://github.com/mojohaus/versions-maven-plugin/issues/134">bug #134</a>
 * @since 2.4
 */
public static List<Dependency> readImportedPOMsFromDependencyManagementSection( ModifiedPomXMLEventReader pom )
    throws XMLStreamException
{
    List<Dependency> importedPOMs = new ArrayList<Dependency>();
    Stack<String> stack = new Stack<String>();

    String groupIdElement = "groupId";
    String artifactIdElement = "artifactId";
    String versionElement = "version";
    String typeElement = "type";
    String scopeElement = "scope";
    Set<String> recognizedElements =
        new HashSet<>( Arrays.asList( groupIdElement, artifactIdElement, versionElement, typeElement,
                                            scopeElement ) );
    Map<String, String> depData = new HashMap<>();

    pom.rewind();

    String depMgmtDependencyPath = "/project/dependencyManagement/dependencies/dependency";

    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();

        if ( event.isStartElement() )
        {
            final String elementName = event.asStartElement().getName().getLocalPart();
            String parent = "";
            if ( !stack.isEmpty() )
            {
                parent = stack.peek();
            }
            String currentPath = parent + "/" + elementName;
            stack.push( currentPath );

            if ( currentPath.startsWith( depMgmtDependencyPath ) && recognizedElements.contains( elementName ) )
            {
                final String elementText = pom.getElementText().trim();
                depData.put( elementName, elementText );
                stack.pop();
            }
        }
        if ( event.isEndElement() )
        {
            String path = stack.pop();
            if ( depMgmtDependencyPath.equals( path ) )
            {
                if ( "pom".equals( depData.get( typeElement ) ) && "import".equals( depData.get( scopeElement ) ) )
                {
                    Dependency dependency = new Dependency();
                    dependency.setGroupId( depData.get( groupIdElement ) );
                    dependency.setArtifactId( depData.get( artifactIdElement ) );
                    dependency.setVersion( depData.get( versionElement ) );
                    dependency.setType( depData.get( typeElement ) );
                    dependency.setScope( depData.get( scopeElement ) );
                    importedPOMs.add( dependency );
                }
                depData.clear();
            }
        }
    }
    return importedPOMs;
}
 
Example 8
Source File: DisplayPluginUpdatesMojo.java    From versions-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a set of Strings which correspond to the plugin coordinates where there is a version specified.
 *
 * @param pomContents The project to get the plugins with versions specified.
 * @param path Path that points to the source of the XML
 * @return a set of Strings which correspond to the plugin coordinates where there is a version specified.
 */
private Set<String> findPluginsWithVersionsSpecified( StringBuilder pomContents, String path )
    throws IOException, XMLStreamException
{
    Set<String> result = new HashSet<>();
    ModifiedPomXMLEventReader pom = newModifiedPomXER( pomContents, path );

    Pattern pathRegex = Pattern.compile( "/project(/profiles/profile)?"
        + "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin" );
    Stack<StackState> pathStack = new Stack<StackState>();
    StackState curState = null;
    while ( pom.hasNext() )
    {
        XMLEvent event = pom.nextEvent();
        if ( event.isStartDocument() )
        {
            curState = new StackState( "" );
            pathStack.clear();
        }
        else if ( event.isStartElement() )
        {
            String elementName = event.asStartElement().getName().getLocalPart();
            if ( curState != null && pathRegex.matcher( curState.path ).matches() )
            {
                if ( "groupId".equals( elementName ) )
                {
                    curState.groupId = pom.getElementText().trim();
                    continue;
                }
                else if ( "artifactId".equals( elementName ) )
                {
                    curState.artifactId = pom.getElementText().trim();
                    continue;

                }
                else if ( "version".equals( elementName ) )
                {
                    curState.version = pom.getElementText().trim();
                    continue;
                }
            }

            pathStack.push( curState );
            curState = new StackState( curState.path + "/" + elementName );
        }
        else if ( event.isEndElement() )
        {
            if ( curState != null && pathRegex.matcher( curState.path ).matches() )
            {
                if ( curState.artifactId != null && curState.version != null )
                {
                    if ( curState.groupId == null )
                    {
                        curState.groupId = PomHelper.APACHE_MAVEN_PLUGINS_GROUPID;
                    }
                    result.add( curState.groupId + ":" + curState.artifactId );
                }
            }
            curState = pathStack.pop();
        }
    }

    return result;

}