Java Code Examples for org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader#rewind()
The following examples show how to use
org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader#rewind() .
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: 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 3
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 4
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; }
Example 5
Source File: PomHelper.java From versions-maven-plugin with Apache License 2.0 | 4 votes |
/** * 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 6
Source File: PomHelper.java From versions-maven-plugin with Apache License 2.0 | 4 votes |
/** * 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; }