提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.netsdk.lib; |
H |
2 |
|
|
3 |
import org.xml.sax.Attributes; |
|
4 |
import org.xml.sax.SAXException; |
|
5 |
import org.xml.sax.helpers.DefaultHandler; |
|
6 |
|
|
7 |
import javax.xml.parsers.ParserConfigurationException; |
|
8 |
import javax.xml.parsers.SAXParser; |
|
9 |
import javax.xml.parsers.SAXParserFactory; |
|
10 |
import java.io.IOException; |
|
11 |
import java.io.InputStream; |
|
12 |
import java.util.*; |
|
13 |
|
|
14 |
/** |
|
15 |
* @author 47081 |
|
16 |
* @version 1.0 |
|
17 |
* @description |
|
18 |
* @date 2021/3/10 |
|
19 |
*/ |
|
20 |
public class DynamicParseUtil { |
|
21 |
private DynamicLibParseHandler handler; |
|
22 |
private SAXParserFactory saxParserFactory; |
|
23 |
private SAXParser saxParser; |
|
24 |
|
|
25 |
/** |
|
26 |
* 适配各系统动态库名称大小写不同,以及lib前缀造成的找不到库的问题 |
|
27 |
* |
|
28 |
* @param currentSystem 当前系统:win64,win32,linux64,linux32,mac64 |
|
29 |
* @param libName 动态库名称 |
|
30 |
* @return |
|
31 |
*/ |
|
32 |
public String compareLibName(String currentSystem, String libName) { |
|
33 |
String dynamicLibName = libName; |
|
34 |
List<String> libs = handler.getLibsBySystem(currentSystem); |
|
35 |
if (currentSystem.toLowerCase().contains("win")) { |
|
36 |
return findLibs(libs, libName); |
|
37 |
} |
|
38 |
if (libName.startsWith("lib")) { |
|
39 |
dynamicLibName = libName.substring(3); |
|
40 |
} |
|
41 |
return findLibs(libs, dynamicLibName); |
|
42 |
} |
|
43 |
|
|
44 |
private String findLibs(List<String> libs, String libName) { |
|
45 |
for (String lib : libs) { |
|
46 |
if (libName.equalsIgnoreCase(lib)) { |
|
47 |
return lib; |
|
48 |
} |
|
49 |
} |
|
50 |
return ""; |
|
51 |
} |
|
52 |
|
|
53 |
public List<String> getLibsSystem(String system) { |
|
54 |
return handler.getLibsBySystem(system); |
|
55 |
} |
|
56 |
|
|
57 |
private DynamicParseUtil() throws ParserConfigurationException { |
|
58 |
// 获取SAX分析器的工厂实例,专门负责创建SAXParser分析器 |
|
59 |
saxParserFactory = SAXParserFactory.newInstance(); |
|
60 |
// 获取SAXParser分析器的实例 |
|
61 |
try { |
|
62 |
saxParser = saxParserFactory.newSAXParser(); |
|
63 |
handler = new DynamicLibParseHandler(); |
|
64 |
} catch (Exception e) { |
|
65 |
e.printStackTrace(); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
public DynamicParseUtil(InputStream inputSteam) |
|
70 |
throws ParserConfigurationException, IOException, SAXException { |
|
71 |
this(); |
|
72 |
saxParser.parse(inputSteam, handler); |
|
73 |
} |
|
74 |
|
|
75 |
/** xml解析handler */ |
|
76 |
private class DynamicLibParseHandler extends DefaultHandler { |
|
77 |
private HashMap<String, List<String>> dynamics = new HashMap<String, List<String>>(); |
|
78 |
private List<String> systems = |
|
79 |
Arrays.asList("win64", "win32", "linux64", "linux32", "mac64", "linuxARM"); |
|
80 |
private String currentDynamicSystem; |
|
81 |
private List<String> libs; |
|
82 |
|
|
83 |
public List<String> getLibsBySystem(String system) { |
|
84 |
return dynamics.get(system); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public void startDocument() throws SAXException { |
|
89 |
super.startDocument(); |
|
90 |
} |
|
91 |
|
|
92 |
@Override |
|
93 |
public void startElement(String uri, String localName, String qName, Attributes attributes) |
|
94 |
throws SAXException { |
|
95 |
super.startElement(uri, localName, qName, attributes); |
|
96 |
if (systems.contains(qName)) { |
|
97 |
currentDynamicSystem = qName; |
|
98 |
if (libs == null) { |
|
99 |
libs = new ArrayList<String>(); |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
@Override |
|
105 |
public void endElement(String uri, String localName, String qName) throws SAXException { |
|
106 |
super.endElement(uri, localName, qName); |
|
107 |
if (systems.contains(qName)) { |
|
108 |
// 保存到hashmap中 |
|
109 |
dynamics.put(currentDynamicSystem, libs); |
|
110 |
// 清除libs |
|
111 |
libs = null; |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
@Override |
|
116 |
public void characters(char[] ch, int start, int length) throws SAXException { |
|
117 |
super.characters(ch, start, length); |
|
118 |
String lib = new String(ch, start, length); |
|
119 |
if (!lib.trim().isEmpty()) { |
|
120 |
libs.add(lib); |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
} |