@Test public void alterFile() throws Exception { String replacementGA = "com.rebuild:servlet-api"; String tomcatPath = " File target = tf.newFile(); FileUtils.copyFile( xmlFile, target ); Project project = new Project( target, TestUtils.getDummyModel() ); xmlManipulator.internalApplyChanges( project, new XMLState.XMLOperation( target.getName(), tomcatPath, replacementGA) ); Diff diff = DiffBuilder.compare( fromFile( xmlFile ) ).withTest( fromFile( target ) ).build(); assertTrue (diff.toString(), diff.hasDifferences()); String xpathForHamcrest = "*/*/*[starts-with(.,'com.rebuild') and local-name() = 'include']"; Iterable<Node> i = new JAXPXPathEngine( ).selectNodes( xpathForHamcrest, fromFile( target ).build() ); int count = 0; for ( Node anI : i ) { count++; assertTrue( anI.getTextContent().startsWith( "com.rebuild:servlet-api" ) ); } assertEquals(1, count); }
void internalApplyChanges( Project project, XMLState.XMLOperation operation ) throws ManipulationException { File target = new File( project.getPom().getParentFile(), operation.getFile() ); logger.info( "Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate() ); Document doc = xmlIO.parseXML( target ); try { NodeList nodeList = (NodeList) xPath.evaluate( operation.getXPath(), doc, XPathConstants.NODESET ); if ( nodeList.getLength() == 0 ) { if ( project.isIncrementalPME() ) { logger.warn ("Did not locate XML using XPath {}", operation.getXPath() ); return; } else { logger.error( "XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile() ); throw new ManipulationException( "Did not locate XML using XPath {}", operation.getXPath() ); } } for ( int i = 0; i < nodeList.getLength(); i++ ) { Node node = nodeList.item( i ); if ( isEmpty( operation.getUpdate() ) ) { node.getParentNode().removeChild( node ); } else { node.setTextContent( operation.getUpdate() ); } } xmlIO.writeXML( target, doc ); } catch ( XPathExpressionException e ) { logger.error( "Caught XML exception processing file {}, document context {} ", target, doc, e ); throw new ManipulationException( "Caught XML exception processing file", e ); } }
XMLManipulator implements Manipulator { void internalApplyChanges( Project project, XMLState.XMLOperation operation ) throws ManipulationException { File target = new File( project.getPom().getParentFile(), operation.getFile() ); logger.info( "Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate() ); Document doc = xmlIO.parseXML( target ); try { NodeList nodeList = (NodeList) xPath.evaluate( operation.getXPath(), doc, XPathConstants.NODESET ); if ( nodeList.getLength() == 0 ) { if ( project.isIncrementalPME() ) { logger.warn ("Did not locate XML using XPath {}", operation.getXPath() ); return; } else { logger.error( "XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile() ); throw new ManipulationException( "Did not locate XML using XPath {}", operation.getXPath() ); } } for ( int i = 0; i < nodeList.getLength(); i++ ) { Node node = nodeList.item( i ); if ( isEmpty( operation.getUpdate() ) ) { node.getParentNode().removeChild( node ); } else { node.setTextContent( operation.getUpdate() ); } } xmlIO.writeXML( target, doc ); } catch ( XPathExpressionException e ) { logger.error( "Caught XML exception processing file {}, document context {} ", target, doc, e ); throw new ManipulationException( "Caught XML exception processing file", e ); } } }
XMLManipulator implements Manipulator { void internalApplyChanges( Project project, XMLState.XMLOperation operation ) throws ManipulationException { File target = new File( project.getPom().getParentFile(), operation.getFile() ); logger.info( "Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate() ); Document doc = xmlIO.parseXML( target ); try { NodeList nodeList = (NodeList) xPath.evaluate( operation.getXPath(), doc, XPathConstants.NODESET ); if ( nodeList.getLength() == 0 ) { if ( project.isIncrementalPME() ) { logger.warn ("Did not locate XML using XPath {}", operation.getXPath() ); return; } else { logger.error( "XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile() ); throw new ManipulationException( "Did not locate XML using XPath {}", operation.getXPath() ); } } for ( int i = 0; i < nodeList.getLength(); i++ ) { Node node = nodeList.item( i ); if ( isEmpty( operation.getUpdate() ) ) { node.getParentNode().removeChild( node ); } else { node.setTextContent( operation.getUpdate() ); } } xmlIO.writeXML( target, doc ); } catch ( XPathExpressionException e ) { logger.error( "Caught XML exception processing file {}, document context {} ", target, doc, e ); throw new ManipulationException( "Caught XML exception processing file", e ); } } @Inject  XMLManipulator(XMLIO xmlIO); }
XMLManipulator implements Manipulator { void internalApplyChanges( Project project, XMLState.XMLOperation operation ) throws ManipulationException { File target = new File( project.getPom().getParentFile(), operation.getFile() ); logger.info( "Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate() ); Document doc = xmlIO.parseXML( target ); try { NodeList nodeList = (NodeList) xPath.evaluate( operation.getXPath(), doc, XPathConstants.NODESET ); if ( nodeList.getLength() == 0 ) { if ( project.isIncrementalPME() ) { logger.warn ("Did not locate XML using XPath {}", operation.getXPath() ); return; } else { logger.error( "XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile() ); throw new ManipulationException( "Did not locate XML using XPath {}", operation.getXPath() ); } } for ( int i = 0; i < nodeList.getLength(); i++ ) { Node node = nodeList.item( i ); if ( isEmpty( operation.getUpdate() ) ) { node.getParentNode().removeChild( node ); } else { node.setTextContent( operation.getUpdate() ); } } xmlIO.writeXML( target, doc ); } catch ( XPathExpressionException e ) { logger.error( "Caught XML exception processing file {}, document context {} ", target, doc, e ); throw new ManipulationException( "Caught XML exception processing file", e ); } } @Inject  XMLManipulator(XMLIO xmlIO); @Override void init( final ManipulationSession session ); @Override Set<Project> applyChanges( final List<Project> projects ); @Override int getExecutionIndex(); }
XMLManipulator implements Manipulator { void internalApplyChanges( Project project, XMLState.XMLOperation operation ) throws ManipulationException { File target = new File( project.getPom().getParentFile(), operation.getFile() ); logger.info( "Attempting to start XML update to file {} with xpath {} and replacement {}", target, operation.getXPath(), operation.getUpdate() ); Document doc = xmlIO.parseXML( target ); try { NodeList nodeList = (NodeList) xPath.evaluate( operation.getXPath(), doc, XPathConstants.NODESET ); if ( nodeList.getLength() == 0 ) { if ( project.isIncrementalPME() ) { logger.warn ("Did not locate XML using XPath {}", operation.getXPath() ); return; } else { logger.error( "XPath {} did not find any expressions within {} ", operation.getXPath(), operation.getFile() ); throw new ManipulationException( "Did not locate XML using XPath {}", operation.getXPath() ); } } for ( int i = 0; i < nodeList.getLength(); i++ ) { Node node = nodeList.item( i ); if ( isEmpty( operation.getUpdate() ) ) { node.getParentNode().removeChild( node ); } else { node.setTextContent( operation.getUpdate() ); } } xmlIO.writeXML( target, doc ); } catch ( XPathExpressionException e ) { logger.error( "Caught XML exception processing file {}, document context {} ", target, doc, e ); throw new ManipulationException( "Caught XML exception processing file", e ); } } @Inject  XMLManipulator(XMLIO xmlIO); @Override void init( final ManipulationSession session ); @Override Set<Project> applyChanges( final List<Project> projects ); @Override int getExecutionIndex();  }