first working version
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@921350 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ef083f8a30
commit
900d8f5b8d
2 changed files with 103 additions and 9 deletions
|
@ -1,13 +1,23 @@
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBElement;
|
import javax.xml.bind.JAXBElement;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.bind.Unmarshaller;
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
import org.apache.tuscany.SourceExplore.ObjectFactory;
|
import org.apache.tuscany.SourceExplore.ObjectFactory;
|
||||||
|
import org.apache.tuscany.SourceExplore.Package;
|
||||||
import org.apache.tuscany.SourceExplore.Projects;
|
import org.apache.tuscany.SourceExplore.Projects;
|
||||||
import org.apache.tuscany.SourceExplore.Projects2;
|
import org.apache.tuscany.SourceExplore.Projects2;
|
||||||
import org.apache.tuscany.SourceExplore.Projects2.Project;
|
import org.apache.tuscany.SourceExplore.Projects2.Project;
|
||||||
|
@ -20,15 +30,17 @@ public class HashSPISource {
|
||||||
|
|
||||||
ObjectFactory fac = new ObjectFactory();
|
ObjectFactory fac = new ObjectFactory();
|
||||||
|
|
||||||
|
|
||||||
|
String sourceRoot = "c:/Dev2/SCA";
|
||||||
|
String inputXML = "src/main/resources/projects.xml";
|
||||||
|
|
||||||
JAXBContext jaxbContext = JAXBContext
|
JAXBContext jaxbContext = JAXBContext
|
||||||
.newInstance("org.apache.tuscany.SourceExplore");
|
.newInstance("org.apache.tuscany.SourceExplore");
|
||||||
Unmarshaller m = jaxbContext.createUnmarshaller();
|
Unmarshaller um = jaxbContext.createUnmarshaller();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
File inputFile = new File("src/main/resources/projects.xml").getAbsoluteFile();
|
File inputFile = new File(inputXML).getAbsoluteFile();
|
||||||
Projects p = (Projects)m.unmarshal(inputFile);
|
Projects p = (Projects)um.unmarshal(inputFile);
|
||||||
List<JAXBElement<String>> data = p.getProjectOrPackage();
|
List<JAXBElement<String>> data = p.getProjectOrPackage();
|
||||||
System.out.println(p);
|
System.out.println(p);
|
||||||
|
|
||||||
|
@ -38,11 +50,12 @@ public class HashSPISource {
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < data.size(); i++) {
|
for (int i = 0; i < data.size(); i++) {
|
||||||
|
Project proj = null;
|
||||||
if(!"project".equals(data.get(i).getName().getLocalPart())) {
|
if(!"project".equals(data.get(i).getName().getLocalPart())) {
|
||||||
throw new Exception("Expecting a project element " + i);
|
throw new Exception("Expecting a project element " + i);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("project: " + data.get(i).getValue());
|
System.out.println("project: " + data.get(i).getValue());
|
||||||
Project proj = fac.createProjects2Project();
|
proj = fac.createProjects2Project();
|
||||||
proj.setName(data.get(i).getValue());
|
proj.setName(data.get(i).getValue());
|
||||||
p2.getProject().add(proj);
|
p2.getProject().add(proj);
|
||||||
}
|
}
|
||||||
|
@ -51,13 +64,94 @@ public class HashSPISource {
|
||||||
throw new Exception("Expecting a package element " + i);
|
throw new Exception("Expecting a package element " + i);
|
||||||
} else {
|
} else {
|
||||||
System.out.println("package: " + data.get(i).getValue());
|
System.out.println("package: " + data.get(i).getValue());
|
||||||
|
Package pkg = fac.createPackage();
|
||||||
|
pkg.setName(data.get(i).getValue());
|
||||||
|
proj.getPackage().add(pkg);
|
||||||
|
processPackage(sourceRoot, proj, pkg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
|
FileOutputStream fos = new FileOutputStream(new File("src/main/resources/out.xml").getAbsoluteFile());
|
||||||
|
Marshaller m = jaxbContext.createMarshaller();
|
||||||
|
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||||
|
m.marshal(p2, fos);
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void processPackage(String sourceRoot, Project proj, Package pkg) {
|
||||||
|
|
||||||
|
ObjectFactory fac = new ObjectFactory();
|
||||||
|
|
||||||
|
String pkgPath = pkg.getName().replace('.', '/' );
|
||||||
|
String folderPath = sourceRoot +"/" + proj.getName() +"/src/main/java/" + pkgPath;
|
||||||
|
File folder = new File(folderPath);
|
||||||
|
File[] contents = folder.listFiles();
|
||||||
|
|
||||||
|
List<org.apache.tuscany.SourceExplore.File> files = pkg.getFile();
|
||||||
|
if(contents!= null) {
|
||||||
|
for (File file : contents) {
|
||||||
|
if(ofInterest(file)) {
|
||||||
|
org.apache.tuscany.SourceExplore.File f = fac.createFile();
|
||||||
|
f.setName(file.getPath());
|
||||||
|
String digest = md5(file);
|
||||||
|
f.setDigest(digest);
|
||||||
|
files.add(f);
|
||||||
|
}
|
||||||
|
System.out.println(file.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String md5(File f) {
|
||||||
|
MessageDigest digest = null;
|
||||||
|
try {
|
||||||
|
digest = MessageDigest.getInstance("MD5");
|
||||||
|
} catch (NoSuchAlgorithmException e1) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream is = null;
|
||||||
|
String output = null;
|
||||||
|
try {
|
||||||
|
is = new FileInputStream(f);
|
||||||
|
} catch (FileNotFoundException e1) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
|
int read = 0;
|
||||||
|
try {
|
||||||
|
while( (read = is.read(buffer)) > 0) {
|
||||||
|
digest.update(buffer, 0, read);
|
||||||
|
}
|
||||||
|
byte[] md5sum = digest.digest();
|
||||||
|
BigInteger bigInt = new BigInteger(1, md5sum);
|
||||||
|
output = bigInt.toString(16);
|
||||||
|
System.out.println("MD5: " + output);
|
||||||
|
}
|
||||||
|
catch(IOException e) {
|
||||||
|
throw new RuntimeException("Unable to process file for MD5", e);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
try {
|
||||||
|
is.close();
|
||||||
|
}
|
||||||
|
catch(IOException e) {
|
||||||
|
throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean ofInterest(File file) {
|
||||||
|
boolean result = false;
|
||||||
|
|
||||||
|
if(!result) result = file.getPath().endsWith(".java");
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
<element name="project">
|
<element name="project">
|
||||||
<complexType>
|
<complexType>
|
||||||
<sequence maxOccurs="unbounded" minOccurs="0">
|
<sequence maxOccurs="unbounded" minOccurs="0">
|
||||||
<element name="package"><complexType></complexType></element>
|
<element name="package" type="tns:Package"></element>
|
||||||
</sequence>
|
</sequence>
|
||||||
<attribute name="name" type="string"></attribute>
|
<attribute name="name" type="string"></attribute>
|
||||||
</complexType>
|
</complexType>
|
||||||
|
|
Loading…
Add table
Reference in a new issue