Java Code Examples for org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader#hasMark()
The following examples show how to use
org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader#hasMark() .
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 |
/** * 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 |
@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-&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 |
/** * 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 |
/** * 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 |
/** * 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; }