PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/opup/src/old/java/com/atlassian/labs/opup/VersionChanger.java

https://bitbucket.org/jwalton/opup
Java | 254 lines | 210 code | 43 blank | 1 comment | 26 complexity | f7f0c5426d87fdcccdbdb28e1394c2a6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package com.atlassian.labs.opup;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import javax.xml.parsers.DocumentBuilder;
  13. import javax.xml.parsers.DocumentBuilderFactory;
  14. import javax.xml.parsers.ParserConfigurationException;
  15. import javax.xml.transform.OutputKeys;
  16. import javax.xml.transform.Result;
  17. import javax.xml.transform.Source;
  18. import javax.xml.transform.Transformer;
  19. import javax.xml.transform.TransformerConfigurationException;
  20. import javax.xml.transform.TransformerException;
  21. import javax.xml.transform.TransformerFactory;
  22. import javax.xml.transform.TransformerFactoryConfigurationError;
  23. import javax.xml.transform.dom.DOMSource;
  24. import javax.xml.transform.stream.StreamResult;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.maven.artifact.versioning.ArtifactVersion;
  27. import org.w3c.dom.Document;
  28. import org.w3c.dom.Element;
  29. import org.w3c.dom.NodeList;
  30. import org.xml.sax.SAXException;
  31. public class VersionChanger
  32. {
  33. private final DocumentBuilder docBuild;
  34. private final Transformer identity;
  35. private final Map<String, ArtifactVersion> newVersions = new HashMap<String, ArtifactVersion>();
  36. private final Map<String, ArtifactVersion> newProperties = new HashMap<String, ArtifactVersion>();
  37. private String key;
  38. public VersionChanger() throws ParserConfigurationException, TransformerConfigurationException,
  39. TransformerFactoryConfigurationError
  40. {
  41. docBuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  42. identity = TransformerFactory.newInstance().newTransformer();
  43. identity.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  44. }
  45. public void setKey(String k)
  46. {
  47. this.key = k;
  48. }
  49. public String getKey()
  50. {
  51. return key;
  52. }
  53. /* XXX Duplicate method during refactoring */
  54. static String groupAndArtifact(String coords)
  55. {
  56. String s = coords;
  57. int sc = s.indexOf(':', s.indexOf(':') + 1);
  58. if (sc >= 0)
  59. {
  60. s = s.substring(0, sc);
  61. }
  62. return s;
  63. }
  64. public void setNewVersion(String coords, ArtifactVersion version)
  65. {
  66. newVersions.put(groupAndArtifact(coords), version);
  67. }
  68. public void setNewPropertyValue(String name, ArtifactVersion version)
  69. {
  70. newProperties.put(name, version);
  71. }
  72. public boolean process(InputStream in, OutputStream out) throws SAXException, IOException, TransformerException
  73. {
  74. Document doc = docBuild.parse(in);
  75. boolean changed = process(doc);
  76. Source source = new DOMSource(doc);
  77. Result target = new StreamResult(out);
  78. identity.transform(source, target);
  79. out.write('\n');
  80. return changed;
  81. }
  82. public boolean process(Document doc)
  83. {
  84. boolean changed = false;
  85. Element project = doc.getDocumentElement();
  86. NodeList nl = project.getElementsByTagName("dependency");
  87. for (int i = 0; i < nl.getLength(); i++)
  88. {
  89. Element d = (Element) nl.item(i);
  90. String groupId = getValue(d, "groupId");
  91. String artifactId = getValue(d, "artifactId");
  92. String currentVersion = getValue(d, "version");
  93. ArtifactVersion nv = newVersions.get(groupId + ":" + artifactId);
  94. if (nv != null)
  95. {
  96. NodeList nl2 = d.getElementsByTagName("version");
  97. if (nl2.getLength() > 0)
  98. {
  99. nl2.item(0).setTextContent(nv.toString());
  100. if (!nv.toString().equals(currentVersion))
  101. {
  102. changed = true;
  103. }
  104. }
  105. }
  106. }
  107. nl = project.getElementsByTagName("properties");
  108. if (nl.getLength() > 0)
  109. {
  110. Element props = (Element) nl.item(0);
  111. NodeList nl2 = props.getElementsByTagName("*");
  112. for (int i = 0; i < nl2.getLength(); i++)
  113. {
  114. Element p = (Element) nl2.item(i);
  115. ArtifactVersion nv = newProperties.get(p.getTagName());
  116. if (nv != null)
  117. {
  118. if (!p.getTextContent().equals(nv.toString()))
  119. {
  120. p.setTextContent(nv.toString());
  121. changed = true;
  122. }
  123. }
  124. }
  125. }
  126. return changed;
  127. }
  128. private static String getValue(Element e, String name)
  129. {
  130. NodeList nl = e.getElementsByTagName(name);
  131. if (nl.getLength() > 0)
  132. {
  133. return nl.item(0).getTextContent();
  134. }
  135. else
  136. {
  137. return null;
  138. }
  139. }
  140. public void changeAll(Iterable<File> poms) throws IOException, SAXException, TransformerException
  141. {
  142. for (File pom : poms)
  143. {
  144. ByteArrayOutputStream out = new ByteArrayOutputStream();
  145. File newVersion = null;
  146. InputStream in = new FileInputStream(pom);
  147. try
  148. {
  149. if (process(in, out))
  150. {
  151. File t = new File(pom.getPath() + ".opup-tmp");
  152. FileOutputStream fout = new FileOutputStream(t);
  153. try
  154. {
  155. IOUtils.copy(new ByteArrayInputStream(out.toByteArray()), fout);
  156. newVersion = t;
  157. }
  158. finally
  159. {
  160. fout.close();
  161. }
  162. }
  163. }
  164. finally
  165. {
  166. in.close();
  167. }
  168. if (newVersion != null)
  169. {
  170. if (!newVersion.renameTo(pom)) {
  171. System.err.println("Failed to rename " + newVersion);
  172. }
  173. }
  174. }
  175. }
  176. public boolean isEmpty()
  177. {
  178. return newVersions.isEmpty() && newProperties.isEmpty();
  179. }
  180. public String toString()
  181. {
  182. StringBuilder sb = new StringBuilder();
  183. if (!newVersions.isEmpty())
  184. {
  185. for (Map.Entry<String, ArtifactVersion> e : newVersions.entrySet())
  186. {
  187. if (sb.length() > 0)
  188. {
  189. sb.append(", ");
  190. }
  191. sb.append(e.getKey() + " " + e.getValue());
  192. }
  193. }
  194. if (!newProperties.isEmpty())
  195. {
  196. for (Map.Entry<String, ArtifactVersion> e : newProperties.entrySet())
  197. {
  198. if (sb.length() > 0)
  199. {
  200. sb.append(", ");
  201. }
  202. sb.append(e.getKey() + " " + e.getValue());
  203. }
  204. }
  205. if (sb.length() == 0)
  206. {
  207. sb.append("No changes.");
  208. }
  209. return sb.toString();
  210. }
  211. }