Skip to content

Commit

Permalink
merge the changes version 0.9.10
Browse files Browse the repository at this point in the history
  • Loading branch information
AneelaSaleem06 committed Feb 20, 2020
1 parent c0fa624 commit de5c78a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
</includes>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -86,6 +85,20 @@
</build>

<dependencies>
<dependency>
<groupId>com.github.ua-parser</groupId>
<artifactId>uap-core</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.ua-parser</groupId>
<artifactId>uap-core</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/ua_parser/DeviceParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -29,7 +30,8 @@
* @author Steve Jiang (@sjiang) &lt;gh at iamsteve com&gt;
*/
public class DeviceParser {
List<DevicePattern> patterns;

private final List<DevicePattern> patterns;

public DeviceParser(List<DevicePattern> patterns) {
this.patterns = patterns;
Expand All @@ -55,12 +57,15 @@ public Device parse(String agentString) {
}
}

/**
* Constructs a thread-safe DeviceParser.
*/
public static DeviceParser fromList(List<Map<String,String>> configList) {
List<DevicePattern> configPatterns = new ArrayList<DevicePattern>();
for (Map<String,String> configMap : configList) {
configPatterns.add(DeviceParser.patternFromMap(configMap));
}
return new DeviceParser(configPatterns);
return new DeviceParser(new CopyOnWriteArrayList<>(configPatterns));
}

protected static DevicePattern patternFromMap(Map<String, String> configMap) {
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/ua_parser/OSParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@

package ua_parser;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Operating System parser using ua-parser. Extracts OS information from user agent strings.
*
* @author Steve Jiang (@sjiang) &lt;gh at iamsteve com&gt;
*/
public class OSParser {

private final List<OSPattern> patterns;

public OSParser(List<OSPattern> patterns) {
this.patterns = patterns;
}

/**
* Constructs a thread-safe OSParser.
*/
public static OSParser fromList(List<Map<String,String>> configList) {
List<OSPattern> configPatterns = new ArrayList<OSPattern>();

for (Map<String,String> configMap : configList) {
configPatterns.add(OSParser.patternFromMap(configMap));
}
return new OSParser(configPatterns);
return new OSParser(new CopyOnWriteArrayList<>(configPatterns));
}

public OS parse(String agentString) {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/ua_parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ public class Parser {
private OSParser osParser;
private DeviceParser deviceParser;

/**
* Creates a parser using the regular expression yaml file bundled in the jar.
* @throws IOException if there's a problem reading the file from the classpath
*/
public Parser() throws IOException {
this(Parser.class.getResourceAsStream(REGEX_YAML_PATH));
try (InputStream is = Parser.class.getResourceAsStream(REGEX_YAML_PATH)) {
initialize(is);
}
}

/**
* Creates a parser using the supplied regular expression yaml file.
* It is the responsibility of the caller to close the InputStream after construction.
* @param regexYaml the yaml file containing the regular expressions
*/
public Parser(InputStream regexYaml) {
initialize(regexYaml);
}
Expand All @@ -65,6 +76,7 @@ public OS parseOS(String agentString) {

private void initialize(InputStream regexYaml) {
Yaml yaml = new Yaml(new SafeConstructor());

@SuppressWarnings("unchecked")
Map<String,List<Map<String,String>>> regexConfig = (Map<String,List<Map<String,String>>>) yaml.load(regexYaml);

Expand Down
26 changes: 22 additions & 4 deletions src/main/java/ua_parser/UserAgentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -28,19 +29,23 @@
* @author Steve Jiang (@sjiang) &lt;gh at iamsteve com&gt;
*/
public class UserAgentParser {

private final List<UAPattern> patterns;

public UserAgentParser(List<UAPattern> patterns) {
this.patterns = patterns;
}

/**
* Constructs a thread-safe UserAgentParser
*/
public static UserAgentParser fromList(List<Map<String,String>> configList) {
List<UAPattern> configPatterns = new ArrayList<UAPattern>();

for (Map<String, String> configMap : configList) {
configPatterns.add(UserAgentParser.patternFromMap(configMap));
}
return new UserAgentParser(configPatterns);
return new UserAgentParser(new CopyOnWriteArrayList<>(configPatterns));
}

public UserAgent parse(String agentString) {
Expand Down Expand Up @@ -103,18 +108,31 @@ public UserAgent match(String agentString) {
if (v1Replacement != null) {
v1 = v1Replacement;
} else if (groupCount >= 2) {
v1 = matcher.group(2);
String group2 = matcher.group(2);
if (!isBlank(group2)) {
v1 = group2;
}
}

if (v2Replacement != null) {
v2 = v2Replacement;
} else if (groupCount >= 3) {
v2 = matcher.group(3);
String group3 = matcher.group(3);
if (!isBlank(group3)) {
v2 = group3;
}
if (groupCount >= 4) {
v3 = matcher.group(4);
String group4 = matcher.group(4);
if (!isBlank(group4)) {
v3 = group4;
}
}
}
return family == null ? null : new UserAgent(family, v1, v2, v3);
}

private boolean isBlank(String value) {
return value == null || value.isEmpty();
}
}
}

0 comments on commit de5c78a

Please sign in to comment.