@@ -48,17 +48,17 @@ private DeviceParser(List<DeviceParser.DevicePattern> patterns) {
4848 this .patterns = patterns ;
4949 }
5050
51- public String parse (String agentString ) {
51+ public Device parse (String agentString ) {
5252 if (agentString == null ) {
5353 return null ;
5454 }
55- String device = null ;
55+ Device device = null ;
5656 for (final DeviceParser .DevicePattern p : this .patterns ) {
5757 if ((device = p .match (agentString )) != null ) {
5858 break ;
5959 }
6060 }
61- if (device == null ) device = "Other" ;
61+ if (device == null ) device = new Device ( "Other" , null , null ) ;
6262 return device ;
6363 }
6464
@@ -69,7 +69,7 @@ private static DeviceParser.DevicePattern patternFromMap(Map<String, String> con
6969 }
7070 Pattern pattern = "i" .equals (configMap .get ("regex_flag" )) // no other flags used (by now)
7171 ? Pattern .compile (regex , Pattern .CASE_INSENSITIVE ) : Pattern .compile (regex );
72- return new DeviceParser .DevicePattern (pattern , configMap .get ("device_replacement" ));
72+ return new DeviceParser .DevicePattern (pattern , configMap .get ("device_replacement" ), configMap . get ( "brand_replacement" ), configMap . get ( "model_replacement" ) );
7373 }
7474
7575 private static final class DevicePattern {
@@ -79,37 +79,75 @@ private static final class DevicePattern {
7979 private final Matcher matcher ;
8080
8181 private final String deviceReplacement ;
82+ private final String brandReplacement ;
83+ private final String modelReplacement ;
8284
83- DevicePattern (Pattern pattern , String deviceReplacement ) {
85+ DevicePattern (Pattern pattern , String deviceReplacement , String brandReplacement , String modelReplacement ) {
8486 this .matcher = pattern .matcher ("" );
8587 this .deviceReplacement = deviceReplacement ;
88+ this .brandReplacement = brandReplacement ;
89+ this .modelReplacement = modelReplacement ;
8690 }
8791
88- public synchronized String match (final CharSequence agentString ) {
92+ public synchronized Device match (final CharSequence agentString ) {
8993 this .matcher .reset (agentString );
9094 if (!this .matcher .find ()) {
9195 return null ;
9296 }
93- String device = null ;
97+ String family = null ;
98+ String brand = null ;
99+ String model = null ;
94100 if (this .deviceReplacement != null ) {
95101 if (this .deviceReplacement .contains ("$" )) {
96- device = this .deviceReplacement ;
102+ family = this .deviceReplacement ;
97103 for (String substitution : DevicePattern
98104 .getSubstitutions (this .deviceReplacement )) {
99105 int i = Integer .parseInt (substitution .substring (1 ));
100106 final String replacement = this .matcher .groupCount () >= i &&
101107 this .matcher .group (i ) != null
102108 ? Matcher .quoteReplacement (this .matcher .group (i )) : "" ;
103- device = device .replaceFirst ('\\' + substitution , replacement );
109+ family = family .replaceFirst ('\\' + substitution , replacement );
104110 }
105- device = device .trim ();
111+ family = family .trim ();
106112 } else {
107- device = this .deviceReplacement ;
113+ family = this .deviceReplacement ;
108114 }
109115 } else if (this .matcher .groupCount () >= 1 ) {
110- device = this .matcher .group (1 );
116+ family = this .matcher .group (1 );
111117 }
112- return device ;
118+ if (this .brandReplacement != null ) {
119+ if (this .brandReplacement .contains ("$" )) {
120+ brand = this .brandReplacement ;
121+ for (String substitution : DevicePattern
122+ .getSubstitutions (this .brandReplacement )) {
123+ int i = Integer .parseInt (substitution .substring (1 ));
124+ final String replacement = this .matcher .groupCount () >= i &&
125+ this .matcher .group (i ) != null
126+ ? Matcher .quoteReplacement (this .matcher .group (i )) : "" ;
127+ brand = brand .replaceFirst ('\\' + substitution , replacement );
128+ }
129+ brand = brand .trim ();
130+ } else {
131+ brand = this .brandReplacement ;
132+ }
133+ }
134+ if (this .modelReplacement != null ) {
135+ if (this .modelReplacement .contains ("$" )) {
136+ model = this .modelReplacement ;
137+ for (String substitution : DevicePattern
138+ .getSubstitutions (this .modelReplacement )) {
139+ int i = Integer .parseInt (substitution .substring (1 ));
140+ final String replacement = this .matcher .groupCount () >= i &&
141+ this .matcher .group (i ) != null
142+ ? Matcher .quoteReplacement (this .matcher .group (i )) : "" ;
143+ model = model .replaceFirst ('\\' + substitution , replacement );
144+ }
145+ model = model .trim ();
146+ } else {
147+ model = this .modelReplacement ;
148+ }
149+ }
150+ return new Device (family , brand , model );
113151 }
114152
115153 private static Iterable <String > getSubstitutions (String deviceReplacement ) {
0 commit comments