|
|
|
@ -9,7 +9,6 @@ import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
import javax.xml.parsers.SAXParser;
|
|
|
|
|
import javax.xml.parsers.SAXParserFactory;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileFilter;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.net.JarURLConnection;
|
|
|
|
@ -24,11 +23,7 @@ public class BeanRelationUtil {
|
|
|
|
|
|
|
|
|
|
static class XMLReader extends DefaultHandler {
|
|
|
|
|
private BeanInfo firstInfo = null;
|
|
|
|
|
private Enum<?> firstHolder = null;
|
|
|
|
|
private BeanInfo secondInfo = null;
|
|
|
|
|
private Enum<?> secondHolder = null;
|
|
|
|
|
private RELATION_TYPE type = RELATION_TYPE.INVALID;
|
|
|
|
|
private boolean owner = false;
|
|
|
|
|
|
|
|
|
|
public void startElement(String uri, String localName, String nodeName, Attributes attributes) throws SAXException {
|
|
|
|
|
if (nodeName.equalsIgnoreCase("Class")) {
|
|
|
|
|
final String name = attributes.getValue("name");
|
|
|
|
@ -49,8 +44,8 @@ public class BeanRelationUtil {
|
|
|
|
|
if (firstSignName == null) {
|
|
|
|
|
throw new SAXException("Relation节点缺少field属性");
|
|
|
|
|
}
|
|
|
|
|
this.firstHolder = this.firstInfo.getHolder(firstSignName);
|
|
|
|
|
if (this.firstHolder == null) {
|
|
|
|
|
Enum<?> firstHolder = this.firstInfo.getHolder(firstSignName);
|
|
|
|
|
if (firstHolder == null) {
|
|
|
|
|
throw new SAXException("未定义枚举标识" + firstSignName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -58,46 +53,44 @@ public class BeanRelationUtil {
|
|
|
|
|
if (secondFactoryName == null) {
|
|
|
|
|
throw new SAXException("Relation节点缺少name属性");
|
|
|
|
|
}
|
|
|
|
|
this.secondInfo = BeanInfo.getBeanInfo(secondFactoryName);
|
|
|
|
|
if (this.secondInfo == null) {
|
|
|
|
|
BeanInfo secondInfo = BeanInfo.getBeanInfo(secondFactoryName);
|
|
|
|
|
if (secondInfo == null) {
|
|
|
|
|
throw new SAXException("未找到" + secondFactoryName + "的类定义");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String secondSignName = attributes.getValue("reverse");
|
|
|
|
|
Enum<?> secondHolder = null;
|
|
|
|
|
if (secondSignName != null) {
|
|
|
|
|
this.secondHolder = this.secondInfo.getHolder(secondSignName);
|
|
|
|
|
if (this.secondHolder == null) {
|
|
|
|
|
secondHolder = secondInfo.getHolder(secondSignName);
|
|
|
|
|
if (secondHolder == null) {
|
|
|
|
|
throw new SAXException(secondFactoryName + "类未定义枚举标识" + secondSignName);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.secondHolder = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String typeName = attributes.getValue("type");
|
|
|
|
|
if (typeName == null) {
|
|
|
|
|
throw new SAXException("Relation节点缺少type属性");
|
|
|
|
|
}
|
|
|
|
|
this.type = RELATION_TYPE.valueOf(typeName);
|
|
|
|
|
RELATION_TYPE type = RELATION_TYPE.valueOf(typeName);
|
|
|
|
|
|
|
|
|
|
String ownerName = attributes.getValue("owner");
|
|
|
|
|
if (ownerName == null) {
|
|
|
|
|
this.owner = false;
|
|
|
|
|
} else {
|
|
|
|
|
this.owner = ownerName.equalsIgnoreCase("true") ? true : false;
|
|
|
|
|
boolean owner = false;
|
|
|
|
|
if (ownerName != null) {
|
|
|
|
|
owner = ownerName.equalsIgnoreCase("true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RelationInfo firstData = this.getSignData(firstInfo, this.firstHolder);
|
|
|
|
|
firstData.setBeanInfo(this.secondInfo);
|
|
|
|
|
firstData.setType(this.type);
|
|
|
|
|
firstData.setOwner(this.owner);
|
|
|
|
|
firstData.setReverseHolder(this.secondHolder);
|
|
|
|
|
RelationInfo firstData = this.getSignData(firstInfo, firstHolder);
|
|
|
|
|
firstData.setBeanInfo(secondInfo);
|
|
|
|
|
firstData.setType(type);
|
|
|
|
|
firstData.setOwner(owner);
|
|
|
|
|
firstData.setReverseHolder(secondHolder);
|
|
|
|
|
|
|
|
|
|
if (this.secondHolder != null) {
|
|
|
|
|
RelationInfo secondData = this.getSignData(secondInfo, this.secondHolder);
|
|
|
|
|
if (secondHolder != null) {
|
|
|
|
|
RelationInfo secondData = this.getSignData(secondInfo, secondHolder);
|
|
|
|
|
secondData.setBeanInfo(this.firstInfo);
|
|
|
|
|
secondData.setOwner(false);
|
|
|
|
|
secondData.setReverseHolder(this.firstHolder);
|
|
|
|
|
switch (this.type) {
|
|
|
|
|
secondData.setReverseHolder(firstHolder);
|
|
|
|
|
switch (type) {
|
|
|
|
|
case MULTI_TO_MULTI:
|
|
|
|
|
secondData.setType(RELATION_TYPE.MULTI_TO_MULTI);
|
|
|
|
|
break;
|
|
|
|
@ -165,8 +158,7 @@ public class BeanRelationUtil {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
return;
|
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -176,13 +168,10 @@ public class BeanRelationUtil {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File[] dirFiles = dir.listFiles(new FileFilter() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(File file) {
|
|
|
|
|
return file.isDirectory() || file.getName().endsWith(".xml");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
File[] dirFiles = dir.listFiles(file -> file.isDirectory() || file.getName().endsWith(".xml"));
|
|
|
|
|
if (dirFiles == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (File file : dirFiles) {
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
@ -190,7 +179,7 @@ public class BeanRelationUtil {
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
loadXMLConfigure(file.getCanonicalPath());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -215,33 +204,30 @@ public class BeanRelationUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载本地开发环境中的xml配置文件。
|
|
|
|
|
* @param xmlPath
|
|
|
|
|
*/
|
|
|
|
|
private static void loadXMLConfigure(String xmlPath) {
|
|
|
|
|
SAXParserFactory sf = SAXParserFactory.newInstance();
|
|
|
|
|
try {
|
|
|
|
|
SAXParser sp = sf.newSAXParser();
|
|
|
|
|
sp.parse(new InputSource(xmlPath), new XMLReader());
|
|
|
|
|
} catch (ParserConfigurationException | SAXException | IOException e) {
|
|
|
|
|
} catch (ParserConfigurationException | SAXException | IOException ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载jar中的xml配置文件。
|
|
|
|
|
* @param is
|
|
|
|
|
*/
|
|
|
|
|
private static void loadXMLConfigure(InputStream is) {
|
|
|
|
|
SAXParserFactory sf = SAXParserFactory.newInstance();
|
|
|
|
|
try {
|
|
|
|
|
SAXParser sp = sf.newSAXParser();
|
|
|
|
|
sp.parse(new InputSource(is), new XMLReader());
|
|
|
|
|
} catch (ParserConfigurationException | SAXException | IOException e) {
|
|
|
|
|
} catch (ParserConfigurationException | SAXException | IOException ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将指定包下的类加载到内存中
|
|
|
|
|
* @param packName
|
|
|
|
|
*/
|
|
|
|
|
public static List<Class<?>> loadClass(String packName) {
|
|
|
|
|
List<Class<?>> clses = new ArrayList<>();
|
|
|
|
@ -274,13 +260,10 @@ public class BeanRelationUtil {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File[] dirFiles = dir.listFiles(new FileFilter() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(File file) {
|
|
|
|
|
return file.isDirectory() || file.getName().endsWith(".class");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
File[] dirFiles = dir.listFiles(file -> file.isDirectory() || file.getName().endsWith(".class"));
|
|
|
|
|
if (dirFiles == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (File file : dirFiles) {
|
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
|