Skip to content

Commit e3fd6da

Browse files
YiWangYiWang
YiWang
authored and
YiWang
committed
adding the src code
1 parent 7a035e3 commit e3fd6da

File tree

306 files changed

+1428
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

306 files changed

+1428
-0
lines changed

pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.yiedr.util</groupId>
5+
<artifactId>java-xml2go</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
8+
<dependencies>
9+
10+
<dependency>
11+
<groupId>junit</groupId>
12+
<artifactId>junit</artifactId>
13+
<version>4.11</version>
14+
</dependency>
15+
16+
17+
18+
</dependencies>
19+
</project>

src/main/java/jaxb/JaxbContext.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package jaxb;
2+
3+
import javax.xml.bind.JAXBElement;
4+
5+
public class JaxbContext {
6+
7+
JAXBElement jaxbElement;
8+
9+
Class scope;
10+
11+
public JaxbContext(){
12+
13+
}
14+
15+
public JAXBElement getJaxbElement() {
16+
return jaxbElement;
17+
}
18+
19+
public void setJaxbElement(JAXBElement jaxbElement) {
20+
this.jaxbElement = jaxbElement;
21+
}
22+
23+
public Class getScope() {
24+
return scope;
25+
}
26+
27+
public void setScope(Class scope) {
28+
this.scope = scope;
29+
}
30+
31+
public boolean isRecursive(String s) {
32+
return this.scope.getSimpleName().equals(s);
33+
}
34+
}

src/main/java/jaxb/JaxbFactory.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package jaxb;
2+
3+
4+
import java.lang.reflect.Type;
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.Method;
7+
import java.lang.reflect.ParameterizedType;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
import javax.xml.namespace.QName;
12+
import javax.xml.bind.annotation.XmlElementDecl;
13+
14+
15+
public class JaxbFactory {
16+
17+
Class factoryClass;
18+
19+
Map<String, Class> QNameMap;
20+
21+
public JaxbFactory(Class objectFactory) throws IllegalArgumentException, IllegalAccessException {
22+
this.factoryClass = objectFactory;
23+
this.init();
24+
}
25+
26+
public Class getJaxbElementType(String name) {
27+
return this.QNameMap.get(name);
28+
}
29+
30+
private void init() throws IllegalArgumentException, IllegalAccessException {
31+
this.QNameMap = new HashMap<String, Class>();
32+
33+
Field[] fields = this.factoryClass.getDeclaredFields();
34+
for (Field field : fields) {
35+
String type = field.getType().getName();
36+
// this field is QName
37+
if (type.equals("javax.xml.namespace.QName")) {
38+
field.setAccessible(true);
39+
QName qName = new QName("");
40+
qName =(QName) field.get(qName);
41+
42+
String localPart = qName.getLocalPart();
43+
QNameMap.put(localPart, null);
44+
}
45+
}
46+
47+
Method[] methods = this.factoryClass.getDeclaredMethods();
48+
for(Method method : methods) {
49+
XmlElementDecl decl = method.getAnnotation(XmlElementDecl.class);
50+
51+
// this method is to create jaxb element
52+
if (decl != null && QNameMap.containsKey(decl.name())) {
53+
54+
if (!method.getReturnType().getName().equals("javax.xml.bind.JAXBElement")) {
55+
System.err.println("this method does not return JaxbElement");
56+
}
57+
58+
ParameterizedType param = (ParameterizedType) method.getGenericReturnType();
59+
Type paramType = param.getActualTypeArguments()[0];
60+
Class typeClass = (Class) paramType;
61+
62+
this.QNameMap.put(decl.name(), typeClass);
63+
}
64+
}
65+
}
66+
}

src/main/java/loader/Loader.java

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package loader;
2+
3+
import java.io.File;
4+
import java.io.FilenameFilter;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
import java.net.URLClassLoader;
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
import javax.xml.bind.annotation.XmlEnum;
12+
13+
public class Loader {
14+
15+
String folder;
16+
ClassLoader classLoader;
17+
Set<String> classNameTable = new HashSet<String>();
18+
Set<String> classFullNameTable = new HashSet<String>();
19+
Set<String> enumClassTable = new HashSet<String>();
20+
21+
public Loader(String folder) {
22+
File file = new File(folder);
23+
24+
File packageDir = new File(file.getAbsolutePath());
25+
this.buildClassNameTable(packageDir);
26+
27+
try {
28+
URL url = file.toURI().toURL();
29+
URL[] urls = new URL[] { url };
30+
classLoader = new URLClassLoader(urls);
31+
URLClassLoader f = new URLClassLoader(urls);
32+
33+
} catch (MalformedURLException e) {
34+
// TODO Auto-generated catch block
35+
e.printStackTrace();
36+
}
37+
}
38+
39+
/**
40+
* find all the java classes in dir folder
41+
*
42+
* @param dir
43+
*/
44+
private void buildClassNameTable(File dir) {
45+
FilenameFilter classFilter = new FilenameFilter() {
46+
public boolean accept(File dir, String name) {
47+
String lowercaseName = name.toLowerCase();
48+
if (lowercaseName.endsWith(".class")) {
49+
return true;
50+
} else {
51+
return false;
52+
}
53+
}
54+
};
55+
56+
String[] clsNames = dir.list(classFilter);
57+
for (String c : clsNames) {
58+
if (c.endsWith(".class")) {
59+
c = c.substring(0, c.length() - 6);
60+
}
61+
62+
this.classFullNameTable.add(c);
63+
64+
if (c.contains("$")) {
65+
c = c.replaceAll("\\$", "");
66+
}
67+
68+
this.classNameTable.add(c);
69+
}
70+
}
71+
72+
public Set<String> getClassNameTable() {
73+
return this.classNameTable;
74+
}
75+
76+
public Set<String> getClassFullNameTable() {
77+
return classFullNameTable;
78+
}
79+
80+
public Class load(String className) throws ClassNotFoundException {
81+
return this.classLoader.loadClass(className);
82+
}
83+
84+
public String getFolder() {
85+
return folder;
86+
}
87+
88+
public void setFolder(String folder) {
89+
this.folder = folder;
90+
}
91+
92+
public Set<String> getEnumClassTable() {
93+
return enumClassTable;
94+
}
95+
96+
public void setEnumClassTable(Set<String> enumClassTable) {
97+
this.enumClassTable = enumClassTable;
98+
}
99+
100+
}

src/main/java/main/Start.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main;
2+
3+
import java.io.File;
4+
import java.net.MalformedURLException;
5+
import java.net.URL;
6+
7+
import translator.Translator;
8+
9+
public class Start {
10+
11+
public void run(String folder, String pkg) throws ClassNotFoundException {
12+
Translator t = new Translator(folder, pkg);
13+
File dir = new File(folder);
14+
15+
for (String fname : dir.list()) {
16+
if (fname.endsWith(".class")) {
17+
fname = fname.replaceAll("\\.class", "");
18+
String src = t.go(pkg + "." + fname);
19+
System.out.println(src);
20+
}
21+
}
22+
}
23+
24+
public static void main(String[] args) throws ClassNotFoundException, MalformedURLException {
25+
26+
String classDir = args[0];
27+
String pkg = args[1];
28+
29+
Start s = new Start();
30+
s.run(classDir, pkg);
31+
32+
}
33+
34+
}

0 commit comments

Comments
 (0)