Skip to content

Commit fc2aaa6

Browse files
committed
added device support to single threaded parser
1 parent 35484ff commit fc2aaa6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/main/java/cz/mallat/uasparser/SingleThreadedUASparser.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SingleThreadedUASparser extends UASparser {
2323

2424
protected Map<Matcher, Long> compiledBrowserMatcherMap;
2525
protected Map<Matcher, Long> compiledOsMatcherMap;
26+
protected Map<Matcher, Long> compiledDeviceMatcherMap;
2627

2728
public SingleThreadedUASparser(InputStream inputStreamToDefinitionFile) throws IOException {
2829
super(inputStreamToDefinitionFile);
@@ -62,6 +63,24 @@ protected void preCompileOsRegMap() {
6263
this.compiledOsMatcherMap = compiledOsMatcherMap;
6364
}
6465

66+
/**
67+
* Precompile device regexes
68+
*/
69+
@Override
70+
protected void preCompileDeviceRegMap() {
71+
if (deviceRegMap == null) {
72+
return; // skip for older ini files
73+
}
74+
LinkedHashMap<Matcher, Long> compiledDeviceMatcherMap =
75+
new LinkedHashMap<Matcher, Long>(deviceRegMap.size());
76+
77+
for (Map.Entry<String, Long> entry : deviceRegMap.entrySet()) {
78+
Pattern pattern = new Pattern(entry.getKey(), Pattern.IGNORE_CASE | Pattern.DOTALL);
79+
compiledDeviceMatcherMap.put(pattern.matcher(), entry.getValue());
80+
}
81+
this.compiledDeviceMatcherMap = compiledDeviceMatcherMap;
82+
}
83+
6584
/**
6685
* Searches in the os regex table. if found a match copies the os data
6786
*
@@ -113,6 +132,27 @@ protected void processBrowserRegex(String useragent, UserAgentInfo retObj) {
113132
}
114133
}
115134

135+
/**
136+
* Searches in the devices regex table. if found a match copies the device data
137+
*
138+
* @param useragent
139+
* @param uaInfo
140+
*/
141+
@Override
142+
protected void processDeviceRegex(String useragent, UserAgentInfo uaInfo) {
143+
if (compiledDeviceMatcherMap == null || deviceMap == null) {
144+
return;
145+
}
146+
for (Map.Entry<Matcher, Long> entry : compiledDeviceMatcherMap.entrySet()) {
147+
Matcher matcher = entry.getKey();
148+
matcher.setTarget(useragent);
149+
if (matcher.find()) {
150+
uaInfo.setDeviceEntry(deviceMap.get(entry.getValue()));
151+
return;
152+
}
153+
}
154+
}
155+
116156
protected Set<Entry<Matcher, Long>> getOsMatcherSet() {
117157
return compiledOsMatcherMap.entrySet();
118158
}

0 commit comments

Comments
 (0)