View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc.repo.maven;
17  
18  import java.io.File;
19  import java.util.HashMap;
20  
21  import javax.xml.parsers.DocumentBuilder;
22  import javax.xml.parsers.DocumentBuilderFactory;
23  import javax.xml.transform.Result;
24  import javax.xml.transform.Source;
25  import javax.xml.transform.Transformer;
26  import javax.xml.transform.TransformerFactory;
27  import javax.xml.transform.dom.DOMSource;
28  import javax.xml.transform.stream.StreamResult;
29  
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.Node;
33  import org.w3c.dom.NodeList;
34  
35  /** Recursively migrate all the POMs to Argeo Distribution v1.3 */
36  public class ConvertPoms_01_03 implements Runnable {
37  	final String SPRING_SOURCE_PREFIX = "com.springsource";
38  
39  	private HashMap<String, String> artifactMapping = new HashMap<String, String>();
40  
41  	private File rootDir;
42  
43  	public ConvertPoms_01_03(String rootDirPath) {
44  		this(new File(rootDirPath));
45  	}
46  
47  	public ConvertPoms_01_03(File rootDir) {
48  		this.rootDir = rootDir;
49  
50  		artifactMapping.put("org.argeo.dep.jacob", "com.jacob");
51  		artifactMapping.put("org.argeo.dep.jacob.win32.x86",
52  				"com.jacob.win32.x86");
53  		artifactMapping.put("org.argeo.dep.osgi.activemq",
54  				"org.apache.activemq");
55  		artifactMapping.put("org.argeo.dep.osgi.activemq.optional",
56  				"org.apache.activemq.optional");
57  		artifactMapping.put("org.argeo.dep.osgi.activemq.xmpp",
58  				"org.apache.activemq.xmpp");
59  		artifactMapping.put("org.argeo.dep.osgi.aether", "org.eclipse.aether");
60  		artifactMapping.put("org.argeo.dep.osgi.boilerpipe",
61  				"de.l3s.boilerpipe");
62  		artifactMapping.put("org.argeo.dep.osgi.commons.cli",
63  				"org.apache.commons.cli");
64  		artifactMapping.put("org.argeo.dep.osgi.commons.exec",
65  				"org.apache.commons.exec");
66  		artifactMapping.put("org.argeo.dep.osgi.directory.shared.asn.codec",
67  				"org.apache.directory.shared.asn.codec");
68  		artifactMapping.put("org.argeo.dep.osgi.drewnoakes.metadata_extractor",
69  				"com.drewnoakes.metadata_extractor");
70  		artifactMapping.put("org.argeo.dep.osgi.geoapi", "org.opengis");
71  		artifactMapping.put("org.argeo.dep.osgi.geotools", "org.geotools");
72  		artifactMapping.put("org.argeo.dep.osgi.google.collections",
73  				"com.google.collections");
74  		artifactMapping.put("org.argeo.dep.osgi.hibernatespatial",
75  				"org.hibernatespatial");
76  		artifactMapping.put("org.argeo.dep.osgi.jackrabbit",
77  				"org.apache.jackrabbit");
78  		artifactMapping.put("org.argeo.dep.osgi.jai.imageio",
79  				"com.sun.media.jai.imageio");
80  		artifactMapping.put("org.argeo.dep.osgi.java3d", "javax.vecmath");
81  		artifactMapping.put("org.argeo.dep.osgi.jcr", "javax.jcr");
82  		artifactMapping.put("org.argeo.dep.osgi.jsr275", "javax.measure");
83  		artifactMapping.put("org.argeo.dep.osgi.jts", "com.vividsolutions.jts");
84  		artifactMapping.put("org.argeo.dep.osgi.mina.filter.ssl",
85  				"org.apache.mina.filter.ssl");
86  		artifactMapping.put("org.argeo.dep.osgi.modeshape", "org.modeshape");
87  		artifactMapping.put("org.argeo.dep.osgi.netcdf",
88  				"edu.ucar.unidata.netcdf");
89  		artifactMapping.put("org.argeo.dep.osgi.pdfbox", "org.apache.pdfbox");
90  		artifactMapping.put("org.argeo.dep.osgi.poi", "org.apache.poi");
91  		artifactMapping.put("org.argeo.dep.osgi.postgis.jdbc",
92  				"org.postgis.jdbc");
93  		artifactMapping.put("org.argeo.dep.osgi.springframework.ldap",
94  				"org.springframework.ldap");
95  		artifactMapping.put("org.argeo.dep.osgi.tagsoup",
96  				"org.ccil.cowan.tagsoup");
97  		artifactMapping.put("org.argeo.dep.osgi.tika", "org.apache.tika");
98  	}
99  
100 	public void run() {
101 		traverse(rootDir);
102 	}
103 
104 	protected void traverse(File dir) {
105 		for (File file : dir.listFiles()) {
106 			String fileName = file.getName();
107 			if (file.isDirectory() && !skipDirName(fileName)) {
108 				traverse(file);
109 			} else if (fileName.equals("pom.xml")) {
110 				processPom(file);
111 			}
112 		}
113 	}
114 
115 	protected Boolean skipDirName(String fileName) {
116 		return fileName.equals(".svn") || fileName.equals("target")
117 				|| fileName.equals("META-INF") || fileName.equals("src");
118 	}
119 
120 	protected void processPom(File pomFile) {
121 		try {
122 			Boolean wasChanged = false;
123 			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
124 					.newInstance();
125 			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
126 			Document doc = dBuilder.parse(pomFile);
127 			doc.getDocumentElement().normalize();
128 
129 			Element dependenciesElement = null;
130 			NodeList rootChildren = doc.getDocumentElement().getChildNodes();
131 			for (int temp = 0; temp < rootChildren.getLength(); temp++) {
132 				Node n = rootChildren.item(temp);
133 				if (n.getNodeName().equals("dependencies"))
134 					dependenciesElement = (Element) n;
135 			}
136 
137 			if (dependenciesElement != null) {
138 				stdOut("\n## " + pomFile);
139 				NodeList dependencyElements = dependenciesElement
140 						.getElementsByTagName("dependency");
141 
142 				for (int temp = 0; temp < dependencyElements.getLength(); temp++) {
143 					Element eElement = (Element) dependencyElements.item(temp);
144 					String groupId = getTagValue(eElement, "groupId");
145 					String artifactId = getTagValue(eElement, "artifactId");
146 					// stdOut(groupId + ":" + artifactId);
147 
148 					String newGroupId = null;
149 					String newArtifactId = null;
150 					if (groupId.startsWith("org.argeo.dep")) {
151 						newGroupId = "org.argeo.tp";
152 					} else if (!(groupId.startsWith("org.argeo")
153 							|| groupId.startsWith("com.capco")
154 							|| groupId.startsWith("com.agfa") || groupId
155 							.startsWith("org.ibboost"))) {
156 						newGroupId = "org.argeo.tp";
157 					}
158 
159 					if (artifactMapping.containsKey(artifactId)) {
160 						newArtifactId = artifactMapping.get(artifactId);
161 					} else if (artifactId.startsWith(SPRING_SOURCE_PREFIX)
162 							&& !artifactId.equals(SPRING_SOURCE_PREFIX
163 									+ ".json")) {
164 						newArtifactId = artifactId
165 								.substring(SPRING_SOURCE_PREFIX.length() + 1);
166 					}
167 
168 					// modify
169 					if (newGroupId != null || newArtifactId != null) {
170 						if (newGroupId == null)
171 							newGroupId = groupId;
172 						if (newArtifactId == null)
173 							newArtifactId = artifactId;
174 						stdOut(groupId + ":" + artifactId + " => " + newGroupId
175 								+ ":" + newArtifactId);
176 						setTagValue(eElement, "groupId", newGroupId);
177 						setTagValue(eElement, "artifactId", newArtifactId);
178 						wasChanged = true;
179 					}
180 				}
181 			}
182 
183 			if (wasChanged) {
184 				// pomFile.renameTo(new File(pomFile.getParentFile(),
185 				// "pom-old.xml"));
186 				// save in place
187 				Source source = new DOMSource(doc);
188 				Result result = new StreamResult(pomFile);
189 				Transformer xformer = TransformerFactory.newInstance()
190 						.newTransformer();
191 				xformer.transform(source, result);
192 			}
193 		} catch (Exception e) {
194 			throw new RuntimeException("Cannot process " + pomFile, e);
195 		}
196 
197 	}
198 
199 	private String getTagValue(Element eElement, String sTag) {
200 		NodeList nList = eElement.getElementsByTagName(sTag);
201 		if (nList.getLength() > 0) {
202 			NodeList nlList = nList.item(0).getChildNodes();
203 			Node nValue = (Node) nlList.item(0);
204 			return nValue.getNodeValue();
205 		} else
206 			return null;
207 	}
208 
209 	private void setTagValue(Element eElement, String sTag, String value) {
210 		NodeList nList = eElement.getElementsByTagName(sTag);
211 		if (nList.getLength() > 0) {
212 			NodeList nlList = nList.item(0).getChildNodes();
213 			Node nValue = (Node) nlList.item(0);
214 			nValue.setNodeValue(value);
215 		}
216 	}
217 
218 	public static void stdOut(Object obj) {
219 		System.out.println(obj);
220 	}
221 
222 	public static void main(String argv[]) {
223 		new ConvertPoms_01_03(argv[0]).run();
224 	}
225 
226 }