@Test(expected = ManipulationException.class) public void testNotFound() throws Exception { String path = " File target = tf.newFile(); FileUtils.copyFile( xmlFile, target ); Project p = new Project( target, TestUtils.getDummyModel() ); xmlManipulator.internalApplyChanges( p, new XMLState.XMLOperation( target.getName(), path, null) ); }
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();  }