/opup/src/old/java/com/atlassian/labs/opup/VersionChanger.java
Java | 254 lines | 210 code | 43 blank | 1 comment | 26 complexity | f7f0c5426d87fdcccdbdb28e1394c2a6 MD5 | raw file
Possible License(s): BSD-3-Clause
- package com.atlassian.labs.opup;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.HashMap;
- import java.util.Map;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Result;
- import javax.xml.transform.Source;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerConfigurationException;
- import javax.xml.transform.TransformerException;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.TransformerFactoryConfigurationError;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.apache.commons.io.IOUtils;
- import org.apache.maven.artifact.versioning.ArtifactVersion;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class VersionChanger
- {
- private final DocumentBuilder docBuild;
- private final Transformer identity;
- private final Map<String, ArtifactVersion> newVersions = new HashMap<String, ArtifactVersion>();
- private final Map<String, ArtifactVersion> newProperties = new HashMap<String, ArtifactVersion>();
- private String key;
- public VersionChanger() throws ParserConfigurationException, TransformerConfigurationException,
- TransformerFactoryConfigurationError
- {
- docBuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- identity = TransformerFactory.newInstance().newTransformer();
- identity.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- }
- public void setKey(String k)
- {
- this.key = k;
- }
- public String getKey()
- {
- return key;
- }
- /* XXX Duplicate method during refactoring */
- static String groupAndArtifact(String coords)
- {
- String s = coords;
- int sc = s.indexOf(':', s.indexOf(':') + 1);
- if (sc >= 0)
- {
- s = s.substring(0, sc);
- }
- return s;
- }
- public void setNewVersion(String coords, ArtifactVersion version)
- {
- newVersions.put(groupAndArtifact(coords), version);
- }
- public void setNewPropertyValue(String name, ArtifactVersion version)
- {
- newProperties.put(name, version);
- }
- public boolean process(InputStream in, OutputStream out) throws SAXException, IOException, TransformerException
- {
- Document doc = docBuild.parse(in);
- boolean changed = process(doc);
- Source source = new DOMSource(doc);
- Result target = new StreamResult(out);
- identity.transform(source, target);
- out.write('\n');
- return changed;
- }
- public boolean process(Document doc)
- {
- boolean changed = false;
- Element project = doc.getDocumentElement();
- NodeList nl = project.getElementsByTagName("dependency");
- for (int i = 0; i < nl.getLength(); i++)
- {
- Element d = (Element) nl.item(i);
- String groupId = getValue(d, "groupId");
- String artifactId = getValue(d, "artifactId");
- String currentVersion = getValue(d, "version");
- ArtifactVersion nv = newVersions.get(groupId + ":" + artifactId);
- if (nv != null)
- {
- NodeList nl2 = d.getElementsByTagName("version");
- if (nl2.getLength() > 0)
- {
- nl2.item(0).setTextContent(nv.toString());
- if (!nv.toString().equals(currentVersion))
- {
- changed = true;
- }
- }
- }
- }
- nl = project.getElementsByTagName("properties");
- if (nl.getLength() > 0)
- {
- Element props = (Element) nl.item(0);
- NodeList nl2 = props.getElementsByTagName("*");
- for (int i = 0; i < nl2.getLength(); i++)
- {
- Element p = (Element) nl2.item(i);
- ArtifactVersion nv = newProperties.get(p.getTagName());
- if (nv != null)
- {
- if (!p.getTextContent().equals(nv.toString()))
- {
- p.setTextContent(nv.toString());
- changed = true;
- }
- }
- }
- }
- return changed;
- }
- private static String getValue(Element e, String name)
- {
- NodeList nl = e.getElementsByTagName(name);
- if (nl.getLength() > 0)
- {
- return nl.item(0).getTextContent();
- }
- else
- {
- return null;
- }
- }
- public void changeAll(Iterable<File> poms) throws IOException, SAXException, TransformerException
- {
- for (File pom : poms)
- {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- File newVersion = null;
- InputStream in = new FileInputStream(pom);
- try
- {
- if (process(in, out))
- {
- File t = new File(pom.getPath() + ".opup-tmp");
- FileOutputStream fout = new FileOutputStream(t);
- try
- {
- IOUtils.copy(new ByteArrayInputStream(out.toByteArray()), fout);
- newVersion = t;
- }
- finally
- {
- fout.close();
- }
- }
- }
- finally
- {
- in.close();
- }
- if (newVersion != null)
- {
- if (!newVersion.renameTo(pom)) {
- System.err.println("Failed to rename " + newVersion);
- }
- }
- }
- }
- public boolean isEmpty()
- {
- return newVersions.isEmpty() && newProperties.isEmpty();
- }
- public String toString()
- {
- StringBuilder sb = new StringBuilder();
- if (!newVersions.isEmpty())
- {
- for (Map.Entry<String, ArtifactVersion> e : newVersions.entrySet())
- {
- if (sb.length() > 0)
- {
- sb.append(", ");
- }
- sb.append(e.getKey() + " " + e.getValue());
- }
- }
- if (!newProperties.isEmpty())
- {
- for (Map.Entry<String, ArtifactVersion> e : newProperties.entrySet())
- {
- if (sb.length() > 0)
- {
- sb.append(", ");
- }
- sb.append(e.getKey() + " " + e.getValue());
- }
- }
- if (sb.length() == 0)
- {
- sb.append("No changes.");
- }
- return sb.toString();
- }
- }