|
| 1 | +package org.codehaus.mojo.flatten; |
| 2 | + |
| 3 | +import java.io.ByteArrayInputStream; |
| 4 | +import java.io.ByteArrayOutputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.function.BiConsumer; |
| 12 | + |
| 13 | +import javax.xml.parsers.DocumentBuilder; |
| 14 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 15 | +import javax.xml.parsers.ParserConfigurationException; |
| 16 | + |
| 17 | +import org.apache.maven.plugin.MojoExecutionException; |
| 18 | +import org.apache.maven.plugin.logging.Log; |
| 19 | +import org.w3c.dom.Document; |
| 20 | +import org.w3c.dom.Node; |
| 21 | +import org.w3c.dom.NodeList; |
| 22 | +import org.w3c.dom.bootstrap.DOMImplementationRegistry; |
| 23 | +import org.w3c.dom.ls.DOMImplementationLS; |
| 24 | +import org.w3c.dom.ls.LSOutput; |
| 25 | +import org.w3c.dom.ls.LSSerializer; |
| 26 | +import org.xml.sax.SAXException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Helper class to keep the comments how they have been in the original pom.xml While reading with |
| 30 | + * {@link org.apache.maven.model.io.xpp3.MavenXpp3Writer} the comments are not placed into the |
| 31 | + * {@link org.apache.maven.model.Model} and so {@link org.apache.maven.model.io.xpp3.MavenXpp3Writer} is not able to |
| 32 | + * re-write those comments. |
| 33 | + * |
| 34 | + * Workaround (maybe until core is fixed) is to remember all the comments and restore them after MavenXpp3Writer has |
| 35 | + * created the new flattened pom.xml. |
| 36 | + * |
| 37 | + * Current restriction on non-unique child nodes is that this class finds the node back due to the position in the file, |
| 38 | + * that may lead to mis-re-added comments e.g. on multiple added dependencies (but for e.g. resolveCiFriendliesOnly the |
| 39 | + * nodes keep stable) |
| 40 | + * |
| 41 | + */ |
| 42 | +class KeepCommentsInPom |
| 43 | +{ |
| 44 | + |
| 45 | + /** |
| 46 | + * Create an instance with collected current comments of the passed pom.xml file. |
| 47 | + */ |
| 48 | + static KeepCommentsInPom create(Log aLog, File aOriginalPomFile) throws MojoExecutionException |
| 49 | + { |
| 50 | + KeepCommentsInPom tempKeepCommentsInPom = new KeepCommentsInPom(); |
| 51 | + tempKeepCommentsInPom.setLog(aLog); |
| 52 | + tempKeepCommentsInPom.loadComments(aOriginalPomFile); |
| 53 | + return tempKeepCommentsInPom; |
| 54 | + } |
| 55 | + |
| 56 | + private Log log; |
| 57 | + |
| 58 | + /** |
| 59 | + * The unique path list for an original node (the comments are stored via the referenced previousSibling) |
| 60 | + */ |
| 61 | + private Map<String, Node> commentsPaths; |
| 62 | + |
| 63 | + /** |
| 64 | + * |
| 65 | + */ |
| 66 | + KeepCommentsInPom() |
| 67 | + { |
| 68 | + super(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * load all current comments and text fragments from xml file |
| 73 | + * |
| 74 | + * @param anOriginalPomFile the pom.xml |
| 75 | + */ |
| 76 | + private void loadComments(File anOriginalPomFile) throws MojoExecutionException |
| 77 | + { |
| 78 | + commentsPaths = new HashMap<>(); |
| 79 | + DocumentBuilderFactory tempDBF = DocumentBuilderFactory.newInstance(); |
| 80 | + DocumentBuilder tempDB; |
| 81 | + try |
| 82 | + { |
| 83 | + tempDB = tempDBF.newDocumentBuilder(); |
| 84 | + Document tempPom = tempDB.parse(anOriginalPomFile); |
| 85 | + Node tempNode = tempPom.getDocumentElement(); |
| 86 | + walkOverNodes(tempNode, ".", (node, nodePath) -> |
| 87 | + { |
| 88 | + // collectNodesByPathNames |
| 89 | + commentsPaths.put(nodePath, node); |
| 90 | + }); |
| 91 | + } catch (ParserConfigurationException | SAXException | IOException e) |
| 92 | + { |
| 93 | + throw new MojoExecutionException("Cannot load comments from " + anOriginalPomFile, e); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Walk over the pom hierarchy of the Document. |
| 99 | + * |
| 100 | + * @param Node the current Node |
| 101 | + * @param String the unique path in the parent |
| 102 | + * @param aConsumer Function to be called with the toBeCollected/found node. |
| 103 | + */ |
| 104 | + private void walkOverNodes(Node aNode, String aParentPath, BiConsumer<Node, String> aConsumer) |
| 105 | + { |
| 106 | + String tempNodeName = aNode.getNodeName(); |
| 107 | + if (log.isDebugEnabled()) |
| 108 | + { |
| 109 | + log.debug("walkOverNodes: aParentPath=" + aParentPath + " tempNodeName=" + tempNodeName); |
| 110 | + } |
| 111 | + String tempNodePath = aParentPath + "\t" + tempNodeName; |
| 112 | + aConsumer.accept(aNode, tempNodePath); |
| 113 | + NodeList tempChilds = aNode.getChildNodes(); |
| 114 | + // Copy the childs as aConsumer may change the node sequence (add a comment) |
| 115 | + List<Node> tempCopiedChilds = new ArrayList<>(); |
| 116 | + Map<String, Integer> tempChildWithSameName = new HashMap<>(); |
| 117 | + for (int i = 0; i < tempChilds.getLength(); i++) |
| 118 | + { |
| 119 | + Node tempItem = tempChilds.item(i); |
| 120 | + if (tempItem.getNodeType() != Node.TEXT_NODE && tempItem.getNodeType() != Node.COMMENT_NODE) |
| 121 | + { |
| 122 | + // Take real nodes to find them back by number |
| 123 | + String tempChildNodeName = tempItem.getNodeName(); |
| 124 | + Integer tempChildWithSameNameCount = tempChildWithSameName.get(tempChildNodeName); |
| 125 | + if (tempChildWithSameNameCount == null) |
| 126 | + { |
| 127 | + tempChildWithSameNameCount = 1; |
| 128 | + } else |
| 129 | + { |
| 130 | + tempChildWithSameNameCount += 1; |
| 131 | + } |
| 132 | + tempChildWithSameName.put(tempChildNodeName, tempChildWithSameNameCount); |
| 133 | + tempCopiedChilds.add(tempItem); |
| 134 | + } |
| 135 | + } |
| 136 | + Map<String, Integer> tempChildWithSameNameCounters = new HashMap<>(); |
| 137 | + for (int i = 0; i < tempCopiedChilds.size(); i++) |
| 138 | + { |
| 139 | + Node tempCopiedChild = tempCopiedChilds.get(i); |
| 140 | + String tempChildNodeName = tempCopiedChild.getNodeName(); |
| 141 | + if (tempChildWithSameName.get(tempChildNodeName) > 1) |
| 142 | + { |
| 143 | + Integer tempChildWithSameNameCounter = tempChildWithSameNameCounters.get(tempChildNodeName); |
| 144 | + if (tempChildWithSameNameCounter == null) |
| 145 | + { |
| 146 | + tempChildWithSameNameCounter = 1; |
| 147 | + } else |
| 148 | + { |
| 149 | + tempChildWithSameNameCounter += 1; |
| 150 | + } |
| 151 | + tempChildWithSameNameCounters.put(tempChildNodeName, tempChildWithSameNameCounter); |
| 152 | + // add a counter to find back the correct node. |
| 153 | + walkOverNodes(tempCopiedChild, tempNodePath + "\t" + tempChildWithSameNameCounter, aConsumer); |
| 154 | + } else |
| 155 | + { |
| 156 | + // unique child names |
| 157 | + walkOverNodes(tempCopiedChild, tempNodePath, aConsumer); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @param String the XML written by {@link org.apache.maven.model.io.xpp3.MavenXpp3Writer} |
| 164 | + */ |
| 165 | + public String restoreOriginalComments(String anXml, String aModelEncoding) throws MojoExecutionException |
| 166 | + { |
| 167 | + DocumentBuilderFactory tempDBF = DocumentBuilderFactory.newInstance(); |
| 168 | + DocumentBuilder tempDB; |
| 169 | + try |
| 170 | + { |
| 171 | + tempDB = tempDBF.newDocumentBuilder(); |
| 172 | + String tempEncoding = aModelEncoding == null ? "UTF-8" : aModelEncoding; // default encoding UTF-8 when |
| 173 | + // nothing in pom model. |
| 174 | + Document tempPom = tempDB.parse(new ByteArrayInputStream(anXml.getBytes(tempEncoding))); |
| 175 | + Node tempNode = tempPom.getDocumentElement(); |
| 176 | + walkOverNodes(tempNode, ".", (newNode, nodePath) -> |
| 177 | + { |
| 178 | + Node tempOriginalNode = commentsPaths.get(nodePath); |
| 179 | + if (tempOriginalNode != null) |
| 180 | + { |
| 181 | + String tempOriginalNodeName = tempOriginalNode.getNodeName(); |
| 182 | + if (tempOriginalNodeName.equals(newNode.getNodeName())) |
| 183 | + { |
| 184 | + // found matching node |
| 185 | + Node tempRefChild = newNode; |
| 186 | + Node tempPotentialCommentOrText = tempOriginalNode.getPreviousSibling(); |
| 187 | + while (tempPotentialCommentOrText != null |
| 188 | + && tempPotentialCommentOrText.getNodeType() == Node.TEXT_NODE) |
| 189 | + { |
| 190 | + // skip text in the original xml node |
| 191 | + tempPotentialCommentOrText = tempPotentialCommentOrText.getPreviousSibling(); |
| 192 | + } |
| 193 | + while (tempPotentialCommentOrText != null |
| 194 | + && tempPotentialCommentOrText.getNodeType() == Node.COMMENT_NODE) |
| 195 | + { |
| 196 | + // copy the node to be able to call previoussibling for next element |
| 197 | + Node tempRefPrevious = tempRefChild.getPreviousSibling(); |
| 198 | + String tempWhitespaceTextBeforeRefNode = null; |
| 199 | + if (tempRefPrevious != null && tempRefPrevious.getNodeType() == Node.TEXT_NODE) |
| 200 | + { |
| 201 | + tempWhitespaceTextBeforeRefNode = tempRefPrevious.getNodeValue(); |
| 202 | + } |
| 203 | + Node tempNewComment; |
| 204 | + tempNewComment = tempPom.createComment(tempPotentialCommentOrText.getNodeValue()); |
| 205 | + tempRefChild.getParentNode().insertBefore(tempNewComment, tempRefChild); |
| 206 | + // copy the whitespaces between comment and refNode |
| 207 | + if (tempWhitespaceTextBeforeRefNode != null) |
| 208 | + { |
| 209 | + tempRefChild.getParentNode().insertBefore( |
| 210 | + tempPom.createTextNode(tempWhitespaceTextBeforeRefNode), tempRefChild); |
| 211 | + } |
| 212 | + |
| 213 | + tempRefChild = tempNewComment; |
| 214 | + |
| 215 | + tempPotentialCommentOrText = tempPotentialCommentOrText.getPreviousSibling(); |
| 216 | + while (tempPotentialCommentOrText != null |
| 217 | + && tempPotentialCommentOrText.getNodeType() == Node.TEXT_NODE) |
| 218 | + { |
| 219 | + // skip text in the original xml node |
| 220 | + tempPotentialCommentOrText = tempPotentialCommentOrText.getPreviousSibling(); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + }); |
| 226 | + return writeDocumentToString(tempPom); |
| 227 | + } catch (ParserConfigurationException | SAXException | IOException | ClassNotFoundException |
| 228 | + | InstantiationException | IllegalAccessException | ClassCastException e) |
| 229 | + { |
| 230 | + throw new MojoExecutionException("Cannot add comments", e); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Use an LSSerializer to keep whitespaces added by MavenXpp3Writer |
| 236 | + * |
| 237 | + * @param Document the pom to write to String. |
| 238 | + */ |
| 239 | + private String writeDocumentToString(Document aPom) |
| 240 | + throws ClassNotFoundException, InstantiationException, IllegalAccessException |
| 241 | + { |
| 242 | + DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); |
| 243 | + DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); |
| 244 | + LSOutput output = impl.createLSOutput(); |
| 245 | + output.setEncoding("UTF-8"); |
| 246 | + ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
| 247 | + output.setByteStream(outStream); |
| 248 | + LSSerializer writer = impl.createLSSerializer(); |
| 249 | + writer.write(aPom, output); |
| 250 | + return new String(outStream.toByteArray()); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * @see #log |
| 255 | + */ |
| 256 | + public Log getLog() |
| 257 | + { |
| 258 | + return log; |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * @see #log |
| 263 | + */ |
| 264 | + public void setLog(Log aLog) |
| 265 | + { |
| 266 | + log = aLog; |
| 267 | + } |
| 268 | + |
| 269 | +} |
0 commit comments