You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devicemap.apache.org by re...@apache.org on 2014/07/05 03:33:37 UTC
svn commit: r1607963 - in
/incubator/devicemap/trunk/devicemap/java/classifier/src:
main/java/org/apache/devicemap/ main/java/org/apache/devicemap/cmd/
main/java/org/apache/devicemap/data/
main/java/org/apache/devicemap/loaders/ main/java/org/apache/de...
Author: rezan
Date: Sat Jul 5 01:33:36 2014
New Revision: 1607963
URL: http://svn.apache.org/r1607963
Log:
refactoring and cleanup
Added:
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/FileLoader.java
- copied, changed from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/FileLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/JarLoader.java
- copied, changed from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/JarLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/NOOPLoader.java
- copied, changed from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/NOOPLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/URLLoader.java
- copied, changed from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/URLLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientJarTest.java
- copied, changed from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientSimpleTest.java
Removed:
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/FileLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/JarLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/NOOPLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/URLLoader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientSimpleTest.java
Modified:
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Constants.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Util.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/cmd/Main.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Device.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/Loader.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/LoaderFactory.java
incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/parser/XMLParser.java
incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientTest.java
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Constants.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Constants.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Constants.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Constants.java Sat Jul 5 01:33:36 2014
@@ -25,6 +25,6 @@ package org.apache.devicemap;
*/
public class Constants {
- public static final String version="0.9";
+ public static final String version="1.0";
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/DeviceMapClient.java Sat Jul 5 01:33:36 2014
@@ -1,26 +1,26 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
-
package org.apache.devicemap;
import org.apache.devicemap.data.Device;
import org.apache.devicemap.loaders.LoaderFactory;
+import org.apache.devicemap.loaders.LoaderFactory.LoaderOption;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -33,41 +33,34 @@ import java.util.Map;
* @author Werner Keil
*/
public class DeviceMapClient {
-
+
//indexes
- Map<String,Device> devices;
- final Map<String,List<Device>> patterns;
-
+ private Map<String, Device> devices;
+ private final Map<String, List<Device>> patterns;
+
public DeviceMapClient() {
- devices=new HashMap<String,Device>();
- patterns=new HashMap<String,List<Device>>();
+ devices = new HashMap<String, Device>();
+ patterns = new HashMap<String, List<Device>>();
}
- public void loadFromResource(String option, String res) throws IOException{
- devices = LoaderFactory.getLoader(option).getData(res);
- createIndex();
- }
-
- //load DDR indexes from a folder
- public void loadFromFolder(String folder) throws IOException {
- devices=LoaderFactory.getLoader("-l").getData(folder);
-
+ public void loadData(LoaderOption option, String path) throws IOException {
+ devices = LoaderFactory.getLoader(option).getData(path);
createIndex();
}
-
+
private void createIndex() {
- for(Device device:devices.values()) {
- for(List<String> patternset:device.getPatterns().getPatterns()) {
- for(int i=0;i<patternset.size();i++) {
- String pattern=patternset.get(i);
-
+ for (Device device : devices.values()) {
+ for (List<String> patternset : device.getPatterns().getPatterns()) {
+ for (int i = 0; i < patternset.size(); i++) {
+ String pattern = patternset.get(i);
+
//duplicate
- if(patterns.get(pattern)!=null) {
- if(i==(patternset.size()-1) && !patterns.get(pattern).contains(device)) {
+ if (patterns.get(pattern) != null) {
+ if (i == (patternset.size() - 1) && !patterns.get(pattern).contains(device)) {
patterns.get(pattern).add(device);
}
} else {
- List<Device> single=new ArrayList<Device>();
+ List<Device> single = new ArrayList<Device>();
single.add(device);
patterns.put(pattern, single);
}
@@ -75,74 +68,74 @@ public class DeviceMapClient {
}
}
}
-
- public Map<String,String> classify(String text) {
- Map<String,List<Device>> hits=new HashMap<String,List<Device>>();
- Device winner=null;
- String winnerStr="";
-
- if(text==null) {
+
+ public Map<String, String> classify(String text) {
+ Map<String, List<Device>> hits = new HashMap<String, List<Device>>();
+ Device winner = null;
+ String winnerStr = "";
+
+ if (text == null) {
return null;
}
-
- Util.debugLog("classify: '"+text+"'");
-
- String[] parts=text.split(" |-|_|/|\\\\|\\[|\\]|\\(|\\)|;");
-
+
+ Util.debugLog("classify: '" + text + "'");
+
+ String[] parts = text.split(" |-|_|/|\\\\|\\[|\\]|\\(|\\)|;");
+
//generate ngrams upto size 4
- for(int i=0;i<parts.length;i++) {
- if(parts[i].isEmpty()) {
+ for (int i = 0; i < parts.length; i++) {
+ if (parts[i].isEmpty()) {
continue;
}
- String pattern="";
- for(int j=0;j<4 && (j+i)<parts.length;j++) {
- pattern+=Util.normalize(parts[i+j]);
-
- List<Device> dlist=patterns.get(pattern);
-
- if(dlist!=null) {
+ String pattern = "";
+ for (int j = 0; j < 4 && (j + i) < parts.length; j++) {
+ pattern += Util.normalize(parts[i + j]);
+
+ List<Device> dlist = patterns.get(pattern);
+
+ if (dlist != null) {
hits.put(pattern, dlist);
-
- for(Device device:dlist) {
- Util.debugLog("Hit found: "+pattern+" => "+device.getId()+" "+device.getPatterns());
+
+ for (Device device : dlist) {
+ Util.debugLog("Hit found: " + pattern + " => " + device.getId() + " " + device.getPatterns());
}
}
}
}
-
+
//look for the strongest hit
- for(String hit:hits.keySet()) {
- for(Device device:hits.get(hit)) {
- if(!device.getPatterns().isValid(hits.keySet())) {
+ for (String hit : hits.keySet()) {
+ for (Device device : hits.get(hit)) {
+ if (!device.getPatterns().isValid(hits.keySet())) {
continue;
}
- Util.debugLog("Hit candidate: "+hit+" => "+device.getId());
+ Util.debugLog("Hit candidate: " + hit + " => " + device.getId());
- if(winner!=null) {
- if("simple".equals(winner.getType()) && !"simple".equals(device.getType())) {
- winner=device;
- winnerStr=hit;
- } else if(hit.length()>winnerStr.length() && !"simple".equals(device.getType())) {
- winner=device;
- winnerStr=hit;
+ if (winner != null) {
+ if ("simple".equals(winner.getType()) && !"simple".equals(device.getType())) {
+ winner = device;
+ winnerStr = hit;
+ } else if (hit.length() > winnerStr.length() && !"simple".equals(device.getType())) {
+ winner = device;
+ winnerStr = hit;
}
} else {
- winner=device;
- winnerStr=hit;
+ winner = device;
+ winnerStr = hit;
}
}
}
-
- if(winner!=null) {
- Util.debugLog("Result: "+winner);
-
+
+ if (winner != null) {
+ Util.debugLog("Result: " + winner);
+
return winner.getAttributes();
} else {
return null;
}
}
-
+
public int getDeviceCount() {
return devices.size();
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Util.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Util.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Util.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/Util.java Sat Jul 5 01:33:36 2014
@@ -1,20 +1,20 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
package org.apache.devicemap;
@@ -27,45 +27,45 @@ import java.util.Date;
* @author Reza Naghibi
*/
public class Util {
-
+
/*
* normalizes a pattern
*/
public static String normalize(String p) {
- if(p==null) {
+ if (p == null) {
return p;
}
-
- p=p.toLowerCase().trim();
-
- p=p.replaceAll("\\[bb\\]","b");
-
- StringBuilder ret=new StringBuilder();
-
- for(int i=0;i<p.length();i++) {
- Character c=p.charAt(i);
- if(Character.isLetter(c) || Character.isDigit(c)) {
+
+ p = p.toLowerCase().trim();
+
+ p = p.replaceAll("\\[bb\\]", "b");
+
+ StringBuilder ret = new StringBuilder();
+
+ for (int i = 0; i < p.length(); i++) {
+ Character c = p.charAt(i);
+ if (Character.isLetter(c) || Character.isDigit(c)) {
ret.append(c);
}
}
-
+
return ret.toString();
}
-
+
public static void debugLog(String msg) {
debugLog(msg, null);
}
-
+
public static void debugLog(String msg, Exception e) {
- if(System.getProperty("debug") == null) {
+ if (System.getProperty("debug") == null) {
return;
}
-
- String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS").format(new Date());
-
- System.out.println(date+" [dmapjclient] "+msg);
-
- if(e!=null) {
+
+ String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS").format(new Date());
+
+ System.out.println(date + " [dmapjclient] " + msg);
+
+ if (e != null) {
e.printStackTrace();
}
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/cmd/Main.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/cmd/Main.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/cmd/Main.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/cmd/Main.java Sat Jul 5 01:33:36 2014
@@ -1,20 +1,20 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
package org.apache.devicemap.cmd;
@@ -25,127 +25,123 @@ import java.io.FileReader;
import java.util.Map;
import org.apache.devicemap.Constants;
import org.apache.devicemap.DeviceMapClient;
+import org.apache.devicemap.loaders.LoaderFactory.LoaderOption;
/**
*
* @author Reza Naghibi
*/
public class Main {
-
+
public static void main(String[] args) throws Exception {
-
- System.out.println("DeviceMap Java Client "+Constants.version);
-
- System.setProperty("debug","true");
-
- String resourceFolder=null;
- String parameter=null;
- String option=null;
-
- for(int i=0;i<args.length;i++)
- {
- //-l [resource folder]
- if((args[i].equals("-l") || args[i].equals("-u")) && args.length>(i+1)) {
- option=args[i];
- i++;
- resourceFolder=args[i];
- } else if(args[i].equals("-j")) {
- option=args[i];
- resourceFolder="";
- } else if(args[i].startsWith("-h") || args[i].startsWith("--h")) {
- System.out.println("Usage: "+Main.class.getName()+" [OPTIONS] [FILE|STRING]\n");
+
+ System.out.println("DeviceMap Java Client " + Constants.version);
+
+ System.setProperty("debug", "true");
+
+ String loaderPath = "";
+ LoaderOption option = LoaderOption.NOOP;
+ String parameter = null;
+
+ for (int i = 0; i < args.length; i++) {
+ if (args[i].equals("-l")) {
+ option = LoaderOption.FOLDER;
+ if (args.length > (++i)) {
+ loaderPath = args[i];
+ }
+ } else if (args[i].equals("-u")) {
+ option = LoaderOption.URL;
+ if (args.length > (++i)) {
+ loaderPath = args[i];
+ }
+ } else if (args[i].equals("-j")) {
+ option = LoaderOption.JAR;
+ } else if (args[i].startsWith("-h") || args[i].startsWith("--h")) {
+ System.out.println("Usage: " + Main.class.getName() + " [OPTIONS] [FILE|STRING]\n");
System.out.println(" -l <path> load Device Map resouces from folder");
System.out.println(" -j load Device Map resouces from jar file in classpath");
System.out.println(" -u <url> load Device Map resouces from URL");
System.out.println(" FILE text file of strings");
System.out.println(" STRING test string");
-
+
return;
+ } //[test string] | [test file]
+ else {
+ parameter = args[i];
}
- //[test string] | [test file]
- else
- parameter=args[i];
}
-
- DeviceMapClient client=new DeviceMapClient();
- Map<String,String> m;
-
- if(option!=null && !option.isEmpty()) {
- long start=System.currentTimeMillis();
-
- client.loadFromResource(option,resourceFolder);
-
- long diff=System.currentTimeMillis()-start;
-
- System.out.println("Loaded "+client.getDeviceCount()+" devices with "+client.getPatternCount()+" patterns in "+diff+"ms");
- }
-
- String test="Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0";
- long startn=System.nanoTime();
+ DeviceMapClient client = new DeviceMapClient();
+
+ long start = System.currentTimeMillis();
- m=client.classify(test);
+ client.loadData(option, loaderPath);
- long diffn=(System.nanoTime()-startn)/1000;
+ long diff = System.currentTimeMillis() - start;
- System.out.println("Test lookup: '"+(m!=null?m.get("id"):"unknown")+"' time: "+diffn+"usec");
-
- if(parameter==null);
- else if((new File(parameter)).exists())
- {
- System.out.println("Text file: "+parameter);
+ System.out.println("Loaded " + client.getDeviceCount() + " devices with " + client.getPatternCount() + " patterns in " + diff + "ms");
- int count=0;
- int total=0;
+ String test = "Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0";
- BufferedReader in=new BufferedReader(new FileReader(parameter));
+ long startn = System.nanoTime();
+
+ Map<String, String> m = client.classify(test);
+
+ long diffn = (System.nanoTime() - startn) / 1000;
+
+ System.out.println("Test lookup: '" + (m != null ? m.get("id") : "unknown") + "' time: " + diffn + "usec");
+
+ if (parameter == null) {
+ } else if ((new File(parameter)).exists()) {
+ System.out.println("Text file: " + parameter);
+
+ int count = 0;
+ int total = 0;
+
+ BufferedReader in = new BufferedReader(new FileReader(parameter));
String line;
- while((line=in.readLine())!=null)
- {
- System.out.println("Text: '"+line+"'");
+ while ((line = in.readLine()) != null) {
+ System.out.println("Text: '" + line + "'");
- startn=System.nanoTime();
+ startn = System.nanoTime();
- m=client.classify(line);
+ m = client.classify(line);
- diffn=System.nanoTime()-startn;
+ diffn = System.nanoTime() - startn;
- total+=diffn;
+ total += diffn;
count++;
- System.out.println("Text lookup "+count+": '"+(m!=null?m.get("id"):"unknown")+"' time: "+(diffn/1000)+"usec");
+ System.out.println("Text lookup " + count + ": '" + (m != null ? m.get("id") : "unknown") + "' time: " + (diffn / 1000) + "usec");
}
in.close();
-
- if(count==0) {
- count=1;
+
+ if (count == 0) {
+ count = 1;
}
- total/=count;
+ total /= count;
- System.out.println("TOTAL lookups: "+count+", average time: "+(diffn/1000)+"usec");
- }
- else
- {
- System.out.println("Text: '"+parameter+"'");
+ System.out.println("TOTAL lookups: " + count + ", average time: " + (total / 1000) + "usec");
+ } else {
+ System.out.println("Text: '" + parameter + "'");
- startn=System.nanoTime();
+ startn = System.nanoTime();
- m=client.classify(parameter);
+ m = client.classify(parameter);
- diffn=System.nanoTime()-startn;
+ diffn = System.nanoTime() - startn;
- System.out.println("Text lookup: '"+(m!=null?m.get("id"):"unknown")+"' time: "+(diffn/1000)+"usec");
+ System.out.println("Text lookup: '" + (m != null ? m.get("id") : "unknown") + "' time: " + (diffn / 1000) + "usec");
- if(m!=null) {
+ if (m != null) {
System.out.print("DeviceMap attributes => ");
- for(String key:m.keySet())
- {
- String value=m.get(key);
- System.out.print(key+": '"+value+"' ");
+ for (String key : m.keySet()) {
+ String value = m.get(key);
+ System.out.print(key + ": '" + value + "' ");
}
System.out.println("");
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Device.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Device.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Device.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Device.java Sat Jul 5 01:33:36 2014
@@ -1,22 +1,21 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
-
package org.apache.devicemap.data;
import java.util.Map;
@@ -26,27 +25,28 @@ import java.util.Map;
* @author Reza Naghibi
*/
public class Device {
-
+
private String id;
-
+
private String parentId;
-
+
private String type;
-
- private Pattern pattern;
-
- private Map<String,String> attributes;
-
+
+ private final Pattern pattern;
+
+ private Map<String, String> attributes;
+
public Device() {
- pattern=new Pattern();
+ pattern = new Pattern();
}
-
+
+ @Override
public String toString() {
- return "id='"+id+"'," +
- "parentId='"+parentId+"',"+
- "type='"+type+"',"+
- "pattern="+pattern+","+
- "attribytes="+attributes+".";
+ return "id='" + id + "',"
+ + "parentId='" + parentId + "',"
+ + "type='" + type + "',"
+ + "pattern=" + pattern + ","
+ + "attribytes=" + attributes + ".";
}
public String getId() {
@@ -77,11 +77,11 @@ public class Device {
return pattern;
}
- public Map<String,String> getAttributes() {
+ public Map<String, String> getAttributes() {
return attributes;
}
- public void setAttributes(Map<String,String> attributes) {
+ public void setAttributes(Map<String, String> attributes) {
this.attributes = attributes;
}
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/data/Pattern.java Sat Jul 5 01:33:36 2014
@@ -1,22 +1,21 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
-
package org.apache.devicemap.data;
import java.util.ArrayList;
@@ -28,64 +27,65 @@ import java.util.Set;
* @author Reza Naghibi
*/
public class Pattern {
-
+
//all patterns strings in the inner list need to match (AND)
//only 1 outer list of patterns need to match (OR)
- private List<List<String>> patterns;
-
+ private final List<List<String>> patterns;
+
public Pattern() {
- patterns=new ArrayList<List<String>>();
+ patterns = new ArrayList<List<String>>();
}
-
+
+ @Override
public String toString() {
return patterns.toString();
}
-
+
/*
* add the patterns together as an inner list (AND)
*/
public void setAndPattern(List<String> patterns) {
this.patterns.add(patterns);
}
-
+
/*
* add each pattern on its own list (OR)
*/
public void setOrPattern(List<String> patterns) {
- for(String pattern:patterns) {
+ for (String pattern : patterns) {
setPattern(pattern);
}
}
-
+
/*
* add a single pattern (OR)
*/
public void setPattern(String pattern) {
- List<String> single=new ArrayList<String>();
+ List<String> single = new ArrayList<String>();
single.add(pattern);
patterns.add(single);
}
-
+
public List<List<String>> getPatterns() {
return patterns;
}
-
+
/*
* does the set of patterns match this object?
*/
public boolean isValid(Set<String> patterns) {
- boolean found=false;
+ boolean found = false;
patterns:
- for(List<String> patternset:getPatterns()) {
- for(String pattern:patternset) {
- if(!patterns.contains(pattern)) {
+ for (List<String> patternset : getPatterns()) {
+ for (String pattern : patternset) {
+ if (!patterns.contains(pattern)) {
continue patterns;
}
}
- found=true;
+ found = true;
break;
}
-
+
return found;
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/Loader.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/Loader.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/Loader.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/Loader.java Sat Jul 5 01:33:36 2014
@@ -1,24 +1,24 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
-
package org.apache.devicemap.loaders;
+import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
@@ -33,157 +33,153 @@ import org.apache.devicemap.parser.XMLPa
* @author Reza Naghibi
*/
public class Loader {
-
- private Map<String,Device> devices;
-
+
+ private final Map<String, Device> devices;
+
public Loader() {
- devices=new HashMap<String,Device>();
+ devices = new HashMap<String, Device>();
}
-
+
/*
* loads device data from an InputStreamReader
*/
- public void loadDeviceData(Reader in) {
- XMLParser parser=new XMLParser(in);
+ public void loadDeviceData(Reader in) throws IOException {
+ XMLParser parser = new XMLParser(in);
String tag;
-
- try {
- Device device=new Device();
- Map<String,String> attributes=new HashMap<String,String>();
-
- while(!(tag=parser.getNextTag()).isEmpty()) {
- //new device found
- if(tag.startsWith("<device ")) {
- device.setId(XMLParser.getAttribute(tag,"id"));
- device.setParentId(XMLParser.getAttribute(tag,"parentId"));
- } else if(tag.equals("</device>")) {
-
- //add the device
- if(device.getId()!=null && !device.getId().isEmpty()) {
- attributes.put("id", device.getId());
- device.setAttributes(attributes);
- devices.put(device.getId(), device);
- }
-
- //reset the device
- device=new Device();
- attributes=new HashMap<String,String>();
- } else if(tag.startsWith("<property ")) {
- //add the property to the device
- String key=XMLParser.getAttribute(tag,"name");
- String value=XMLParser.getAttribute(tag,"value");
-
- attributes.put(key,value);
+
+ Device device = new Device();
+ Map<String, String> attributes = new HashMap<String, String>();
+
+ while (!(tag = parser.getNextTag()).isEmpty()) {
+ //new device found
+ if (tag.startsWith("<device ")) {
+ device.setId(XMLParser.getAttribute(tag, "id"));
+ device.setParentId(XMLParser.getAttribute(tag, "parentId"));
+ } else if (tag.equals("</device>")) {
+
+ //add the device
+ if (device.getId() != null && !device.getId().isEmpty()) {
+ attributes.put("id", device.getId());
+ device.setAttributes(attributes);
+ devices.put(device.getId(), device);
}
+
+ //reset the device
+ device = new Device();
+ attributes = new HashMap<String, String>();
+ } else if (tag.startsWith("<property ")) {
+ //add the property to the device
+ String key = XMLParser.getAttribute(tag, "name");
+ String value = XMLParser.getAttribute(tag, "value");
+
+ attributes.put(key, value);
}
- } catch(Exception ex) {
- Util.debugLog("ERROR: "+ex.toString(),ex);
}
}
-
+
/*
* loads patterns from an InputStreamReader
*/
- public void loadDevicePatterns(Reader in) {
- XMLParser parser=new XMLParser(in);
+ public void loadDevicePatterns(Reader in) throws IOException {
+ XMLParser parser = new XMLParser(in);
String tag;
-
+
try {
- String builder="";
- Device device=null;
- String id="";
- List<String> patterns=new ArrayList<String>();
-
- while(!(tag=parser.getNextTag()).isEmpty()) {
+ String builder = "";
+ Device device = null;
+ String id = "";
+ List<String> patterns = new ArrayList<String>();
+
+ while (!(tag = parser.getNextTag()).isEmpty()) {
//new builder found
- if(tag.startsWith("<builder ")) {
- builder=XMLParser.getAttribute(tag,"class");
-
- if(builder.lastIndexOf(".")>=0) {
- builder=builder.substring(builder.lastIndexOf(".")+1);
+ if (tag.startsWith("<builder ")) {
+ builder = XMLParser.getAttribute(tag, "class");
+
+ if (builder.lastIndexOf(".") >= 0) {
+ builder = builder.substring(builder.lastIndexOf(".") + 1);
}
- } else if(tag.startsWith("<device ")) {
+ } else if (tag.startsWith("<device ")) {
//new device found
- device=devices.get(XMLParser.getAttribute(tag,"id"));
- } else if(tag.equals("</device>")) {
+ device = devices.get(XMLParser.getAttribute(tag, "id"));
+ } else if (tag.equals("</device>")) {
//add the device
- if(device!=null) {
+ if (device != null) {
//TwoStep is an AND pattern, also index the unigram
- if(builder.equals("TwoStepDeviceBuilder")) {
+ if (builder.equals("TwoStepDeviceBuilder")) {
device.getPatterns().setAndPattern(patterns);
-
- String unigram="";
-
- for(String pattern:patterns) {
- if(pattern.contains(unigram)) {
- unigram=pattern;
+
+ String unigram = "";
+
+ for (String pattern : patterns) {
+ if (pattern.contains(unigram)) {
+ unigram = pattern;
} else {
- unigram+=pattern;
+ unigram += pattern;
}
}
-
+
device.getPatterns().setPattern(unigram);
} else {
device.getPatterns().setOrPattern(patterns);
}
-
- if(builder.equals("SimpleDeviceBuilder")) {
+
+ if (builder.equals("SimpleDeviceBuilder")) {
device.setType("simple");
} else {
device.setType("weak");
}
} else {
- Util.debugLog("ERROR: device not found: '"+id+"'");
+ Util.debugLog("ERROR: device not found: '" + id + "'");
}
-
+
//reset the device
- device=null;
- id="";
- patterns=new ArrayList<String>();
- } else if(tag.equals("<value>")) {
+ device = null;
+ id = "";
+ patterns = new ArrayList<String>();
+ } else if (tag.equals("<value>")) {
//add the pattern to the device
- String pattern=Util.normalize(parser.getTagValue());
-
- if(pattern.isEmpty()) {
+ String pattern = Util.normalize(parser.getTagValue());
+
+ if (pattern.isEmpty()) {
continue;
}
-
+
patterns.add(pattern);
}
}
- } catch(Exception ex) {
- Util.debugLog("ERROR: "+ex.toString(),ex);
+ } catch (Exception ex) {
+ Util.debugLog("ERROR: " + ex.toString(), ex);
}
}
-
+
/**
* Sets attributes from parents
*/
public void setParentAttributes() {
- for(Device device:devices.values()) {
+ for (Device device : devices.values()) {
mergeParent(device);
}
}
-
+
private void mergeParent(Device device) {
- String parentId=device.getParentId();
-
- if(parentId==null) {
+ String parentId = device.getParentId();
+
+ if (parentId == null) {
return;
}
-
- Device parent=devices.get(parentId);
-
- if(parent==null) {
+
+ Device parent = devices.get(parentId);
+
+ if (parent == null) {
return;
}
-
+
mergeParent(parent);
-
- for(String key:parent.getAttributes().keySet()) {
- String value=parent.getAttributes().get(key);
-
- if(!device.getAttributes().containsKey(key)) {
+
+ for (String key : parent.getAttributes().keySet()) {
+ String value = parent.getAttributes().get(key);
+
+ if (!device.getAttributes().containsKey(key)) {
device.getAttributes().put(key, value);
}
}
@@ -192,7 +188,7 @@ public class Loader {
/**
* @return the devices
*/
- public Map<String,Device> getDevices() {
+ public Map<String, Device> getDevices() {
return devices;
}
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/LoaderFactory.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/LoaderFactory.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/LoaderFactory.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/LoaderFactory.java Sat Jul 5 01:33:36 2014
@@ -1,60 +1,51 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
package org.apache.devicemap.loaders;
+import org.apache.devicemap.loaders.types.JarLoader;
+import org.apache.devicemap.loaders.types.FileLoader;
+import org.apache.devicemap.loaders.types.NOOPLoader;
+import org.apache.devicemap.loaders.types.URLLoader;
+
+/**
+ *
+ * @author kalyanar
+ * @author Reza Naghibi
+ */
public class LoaderFactory {
- private static enum LoaderOption{
- JAR("-j"),FOLDER("-l"),URL("-u"),NOOP("-n");
- private String loaderType;
- LoaderOption(String loaderType){
- this.loaderType = loaderType;
- }
- String type(){
- return this.loaderType;
- }
+
+ public static enum LoaderOption {
+ JAR, FOLDER, URL, NOOP;
};
-
-
-public static final ResourceLoader getLoader(String option){
- LoaderOption lOption = getLoaderOption(option);
- switch(lOption){
- case JAR:{
- return new JarLoader(new Loader());
- }
- case FOLDER:{
- return new FileLoader(new Loader());
- }
- case URL:{
- return new URLLoader(new Loader());
- }
- case NOOP:{
- return new NOOPLoader();
- }
- }
- return new NOOPLoader();
-}
-private static LoaderOption getLoaderOption(String option){
- for(LoaderOption lOption:LoaderOption.values()){
- if(lOption.type().equals(option)){
- return lOption;
+
+ public static ResourceLoader getLoader(LoaderOption option) {
+ switch (option) {
+ case JAR: {
+ return new JarLoader(new Loader());
+ }
+ case FOLDER: {
+ return new FileLoader(new Loader());
+ }
+ case URL: {
+ return new URLLoader(new Loader());
+ }
}
+ return new NOOPLoader();
}
- return LoaderOption.NOOP;
-}
}
Copied: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/FileLoader.java (from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/FileLoader.java)
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/FileLoader.java?p2=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/FileLoader.java&p1=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/FileLoader.java&r1=1607956&r2=1607963&rev=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/FileLoader.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/FileLoader.java Sat Jul 5 01:33:36 2014
@@ -1,25 +1,23 @@
/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
*/
+package org.apache.devicemap.loaders.types;
-package org.apache.devicemap.loaders;
-
-import org.apache.devicemap.loaders.Loader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@@ -27,80 +25,81 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
+
import org.apache.devicemap.data.Device;
import org.apache.devicemap.Util;
+import org.apache.devicemap.loaders.Loader;
+import org.apache.devicemap.loaders.ResourceLoader;
/**
*
* @author Reza Naghibi
*/
-public class FileLoader implements ResourceLoader{
-
+public class FileLoader implements ResourceLoader {
+
private final Loader loader;
-
-
- public FileLoader(Loader loader){
+
+ public FileLoader(Loader loader) {
this.loader = loader;
}
-
+
/**
- *
- * @param folder which contains device map data
+ *
+ * @param path which contains device map data
* @return map of devices
- * @throws IOException
+ * @throws IOException
*/
- public Map<String,Device> getData(String folder) throws IOException {
-
-
- String ddpath=folder+File.separatorChar+"DeviceDataSource.xml";
- String ddppath=folder+File.separatorChar+"DeviceDataSourcePatch.xml";
- String bpath=folder+File.separatorChar+"BuilderDataSource.xml";
- String bppath=folder+File.separatorChar+"BuilderDataSourcePatch.xml";
-
- long start=System.currentTimeMillis();
-
- BufferedReader ddin=new BufferedReader(new InputStreamReader(new FileInputStream(ddpath), "UTF-8"));
+ @Override
+ public Map<String, Device> getData(String path) throws IOException {
+ String ddpath = path + File.separatorChar + "DeviceDataSource.xml";
+ String ddppath = path + File.separatorChar + "DeviceDataSourcePatch.xml";
+ String bpath = path + File.separatorChar + "BuilderDataSource.xml";
+ String bppath = path + File.separatorChar + "BuilderDataSourcePatch.xml";
+
+ long start = System.currentTimeMillis();
+
+ BufferedReader ddin = new BufferedReader(new InputStreamReader(new FileInputStream(ddpath), "UTF-8"));
loader.loadDeviceData(ddin);
ddin.close();
-
- long diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+ddpath+" in "+diff+"ms");
-
+
+ long diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + ddpath + " in " + diff + "ms");
+
try {
- start=System.currentTimeMillis();
-
- BufferedReader ddpin=new BufferedReader(new InputStreamReader(new FileInputStream(ddppath), "UTF-8"));
+ start = System.currentTimeMillis();
+
+ BufferedReader ddpin = new BufferedReader(new InputStreamReader(new FileInputStream(ddppath), "UTF-8"));
loader.loadDeviceData(ddpin);
ddpin.close();
-
- diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+ddppath+" in "+diff+"ms");
- } catch(FileNotFoundException ex) {
- Util.debugLog("WARNING: file not found "+ddppath+": "+ex.toString());
+
+ diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + ddppath + " in " + diff + "ms");
+ } catch (FileNotFoundException ex) {
+ Util.debugLog("WARNING: file not found " + ddppath + ": " + ex.toString());
}
loader.setParentAttributes();
- start=System.currentTimeMillis();
-
- BufferedReader bin=new BufferedReader(new InputStreamReader(new FileInputStream(bpath), "UTF-8"));
+ start = System.currentTimeMillis();
+
+ BufferedReader bin = new BufferedReader(new InputStreamReader(new FileInputStream(bpath), "UTF-8"));
loader.loadDevicePatterns(bin);
bin.close();
-
- diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+bpath+" in "+diff+"ms");
-
+
+ diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + bpath + " in " + diff + "ms");
+
try {
- start=System.currentTimeMillis();
-
- BufferedReader bpin=new BufferedReader(new InputStreamReader(new FileInputStream(bppath), "UTF-8"));
+ start = System.currentTimeMillis();
+
+ BufferedReader bpin = new BufferedReader(new InputStreamReader(new FileInputStream(bppath), "UTF-8"));
loader.loadDevicePatterns(bpin);
bpin.close();
-
- diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+bppath+" in "+diff+"ms");
- } catch(FileNotFoundException ex) {
- Util.debugLog("WARNING: file not found "+bppath+": "+ex.toString());
+
+ diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + bppath + " in " + diff + "ms");
+ } catch (FileNotFoundException ex) {
+ Util.debugLog("WARNING: file not found " + bppath + ": " + ex.toString());
}
return loader.getDevices();
Copied: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/JarLoader.java (from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/JarLoader.java)
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/JarLoader.java?p2=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/JarLoader.java&p1=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/JarLoader.java&r1=1607956&r2=1607963&rev=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/JarLoader.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/JarLoader.java Sat Jul 5 01:33:36 2014
@@ -16,16 +16,19 @@
specific language governing permissions and limitations
under the License.
*/
-package org.apache.devicemap.loaders;
+package org.apache.devicemap.loaders.types;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import org.apache.devicemap.Util;
import org.apache.devicemap.data.Device;
+import org.apache.devicemap.loaders.Loader;
+import org.apache.devicemap.loaders.ResourceLoader;
/**
*
@@ -42,32 +45,33 @@ public class JarLoader implements Resour
@Override
public Map<String, Device> getData(String path) throws IOException {
- if(path==null) {
- path="";
+ if (path == null) {
+ path = "";
}
-
- String ddpath = path+"/devicedata/DeviceDataSource.xml";
- String ddppath = path+"/devicedata/DeviceDataSourcePatch.xml";
- String bpath = path+"/devicedata/BuilderDataSource.xml";
- String bppath = path+"/devicedata/BuilderDataSourcePatch.xml";
+
+ String ddpath = path + "/devicedata/DeviceDataSource.xml";
+ String ddppath = path + "/devicedata/DeviceDataSourcePatch.xml";
+ String bpath = path + "/devicedata/BuilderDataSource.xml";
+ String bppath = path + "/devicedata/BuilderDataSourcePatch.xml";
long start = System.currentTimeMillis();
- BufferedReader ddin = new BufferedReader(new InputStreamReader(JarLoader.class.getResourceAsStream(ddpath), "UTF-8"));
+ BufferedReader ddin = getReader(ddpath);
loader.loadDeviceData(ddin);
ddin.close();
long diff = System.currentTimeMillis() - start;
Util.debugLog("Loaded " + ddpath + " in " + diff + "ms");
+
try {
start = System.currentTimeMillis();
- BufferedReader ddpin = new BufferedReader(new InputStreamReader(JarLoader.class.getResourceAsStream(ddppath), "UTF-8"));
+ BufferedReader ddpin = getReader(ddppath);
loader.loadDeviceData(ddpin);
ddpin.close();
diff = System.currentTimeMillis() - start;
Util.debugLog("Loaded " + ddppath + " in " + diff + "ms");
- } catch (FileNotFoundException ex) {
+ } catch (IOException ex) {
Util.debugLog("WARNING: file not found " + ddppath + ": " + ex.toString());
}
@@ -75,7 +79,7 @@ public class JarLoader implements Resour
start = System.currentTimeMillis();
- BufferedReader bin = new BufferedReader(new InputStreamReader(JarLoader.class.getResourceAsStream(bpath), "UTF-8"));
+ BufferedReader bin = getReader(bpath);
loader.loadDevicePatterns(bin);
bin.close();
@@ -85,7 +89,7 @@ public class JarLoader implements Resour
try {
start = System.currentTimeMillis();
- BufferedReader bpin = new BufferedReader(new InputStreamReader(JarLoader.class.getResourceAsStream(bppath), "UTF-8"));
+ BufferedReader bpin = getReader(bppath);
loader.loadDevicePatterns(bpin);
bpin.close();
@@ -97,4 +101,14 @@ public class JarLoader implements Resour
return loader.getDevices();
}
+
+ private BufferedReader getReader(String path) throws IOException {
+ InputStream in = JarLoader.class.getResourceAsStream(path);
+
+ if (in == null) {
+ throw new IOException("Jar resource not found: " + path);
+ }
+
+ return new BufferedReader(new InputStreamReader(in, "UTF-8"));
+ }
}
Copied: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/NOOPLoader.java (from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/NOOPLoader.java)
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/NOOPLoader.java?p2=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/NOOPLoader.java&p1=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/NOOPLoader.java&r1=1607956&r2=1607963&rev=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/NOOPLoader.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/NOOPLoader.java Sat Jul 5 01:33:36 2014
@@ -1,16 +1,16 @@
-package org.apache.devicemap.loaders;
+package org.apache.devicemap.loaders.types;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import org.apache.devicemap.data.Device;
+import org.apache.devicemap.loaders.ResourceLoader;
public class NOOPLoader implements ResourceLoader {
@Override
- public Map<String, Device> getData(String path) throws IOException {
-
+ public Map<String, Device> getData(String path) {
return Collections.emptyMap();
}
Copied: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/URLLoader.java (from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/URLLoader.java)
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/URLLoader.java?p2=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/URLLoader.java&p1=incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/URLLoader.java&r1=1607956&r2=1607963&rev=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/URLLoader.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/loaders/types/URLLoader.java Sat Jul 5 01:33:36 2014
@@ -1,9 +1,6 @@
-package org.apache.devicemap.loaders;
+package org.apache.devicemap.loaders.types;
import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
@@ -11,74 +8,77 @@ import java.util.Map;
import org.apache.devicemap.Util;
import org.apache.devicemap.data.Device;
+import org.apache.devicemap.loaders.Loader;
+import org.apache.devicemap.loaders.ResourceLoader;
+
/**
- *
+ *
* @author kalyanar
* @author Reza Naghibi
*
*/
public class URLLoader implements ResourceLoader {
+
private final Loader loader;
-
-
- public URLLoader(Loader loader){
+
+ public URLLoader(Loader loader) {
this.loader = loader;
}
-
+
@Override
public Map<String, Device> getData(String path) throws IOException {
- String ddpath=path+"/DeviceDataSource.xml";
- String ddppath=path+"/DeviceDataSourcePatch.xml";
- String bpath=path+"/BuilderDataSource.xml";
- String bppath=path+"/BuilderDataSourcePatch.xml";
- long start=System.currentTimeMillis();
-
- BufferedReader ddin=new BufferedReader(new InputStreamReader(new URL(ddpath).openStream(), "UTF-8"));
+ String ddpath = path + "/DeviceDataSource.xml";
+ String ddppath = path + "/DeviceDataSourcePatch.xml";
+ String bpath = path + "/BuilderDataSource.xml";
+ String bppath = path + "/BuilderDataSourcePatch.xml";
+
+ long start = System.currentTimeMillis();
+
+ BufferedReader ddin = new BufferedReader(new InputStreamReader(new URL(ddpath).openStream(), "UTF-8"));
loader.loadDeviceData(ddin);
ddin.close();
-
- long diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+ddpath+" in "+diff+"ms");
-
+
+ long diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + ddpath + " in " + diff + "ms");
+
try {
- start=System.currentTimeMillis();
-
- BufferedReader ddpin=new BufferedReader(new InputStreamReader(new URL(ddppath).openStream(), "UTF-8"));
+ start = System.currentTimeMillis();
+
+ BufferedReader ddpin = new BufferedReader(new InputStreamReader(new URL(ddppath).openStream(), "UTF-8"));
loader.loadDeviceData(ddpin);
ddpin.close();
-
- diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+ddppath+" in "+diff+"ms");
- } catch(FileNotFoundException ex) {
- Util.debugLog("WARNING: file not found "+ddppath+": "+ex.toString());
+
+ diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + ddppath + " in " + diff + "ms");
+ } catch (IOException ex) {
+ Util.debugLog("WARNING: file not found " + ddppath + ": " + ex.toString());
}
loader.setParentAttributes();
- start=System.currentTimeMillis();
-
- BufferedReader bin=new BufferedReader(new InputStreamReader(new URL(bpath).openStream(), "UTF-8"));
+ start = System.currentTimeMillis();
+
+ BufferedReader bin = new BufferedReader(new InputStreamReader(new URL(bpath).openStream(), "UTF-8"));
loader.loadDevicePatterns(bin);
bin.close();
-
- diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+bpath+" in "+diff+"ms");
-
+
+ diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + bpath + " in " + diff + "ms");
+
try {
- start=System.currentTimeMillis();
-
- BufferedReader bpin=new BufferedReader(new InputStreamReader(new URL(bppath).openStream(), "UTF-8"));
+ start = System.currentTimeMillis();
+
+ BufferedReader bpin = new BufferedReader(new InputStreamReader(new URL(bppath).openStream(), "UTF-8"));
loader.loadDevicePatterns(bpin);
bpin.close();
-
- diff=System.currentTimeMillis()-start;
- Util.debugLog("Loaded "+bppath+" in "+diff+"ms");
- } catch(FileNotFoundException ex) {
- Util.debugLog("WARNING: file not found "+bppath+": "+ex.toString());
+
+ diff = System.currentTimeMillis() - start;
+ Util.debugLog("Loaded " + bppath + " in " + diff + "ms");
+ } catch (IOException ex) {
+ Util.debugLog("WARNING: file not found " + bppath + ": " + ex.toString());
}
return loader.getDevices();
-
}
}
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/parser/XMLParser.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/parser/XMLParser.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/parser/XMLParser.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/main/java/org/apache/devicemap/parser/XMLParser.java Sat Jul 5 01:33:36 2014
@@ -29,13 +29,10 @@ import java.io.Reader;
*/
public class XMLParser {
- private Reader in;
+ private final Reader in;
private char pre;
- @SuppressWarnings("unused")
- private XMLParser() {}
-
public XMLParser(Reader in) {
this.in=in;
pre=0;
Copied: incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientJarTest.java (from r1607956, incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientSimpleTest.java)
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientJarTest.java?p2=incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientJarTest.java&p1=incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientSimpleTest.java&r1=1607956&r2=1607963&rev=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientSimpleTest.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientJarTest.java Sat Jul 5 01:33:36 2014
@@ -19,18 +19,19 @@
package org.apache.devicemap;
import java.util.Map;
+import org.apache.devicemap.loaders.LoaderFactory.LoaderOption;
import org.junit.Assert;
import org.junit.Test;
/**
- *
+ * @author Reza Naghibi
*/
-public class DeviceMapClientSimpleTest {
+public class DeviceMapClientJarTest {
@Test
- public void DeviceMapClientTest() throws Exception {
+ public void DeviceMapClientJarTest() throws Exception {
DeviceMapClient client=new DeviceMapClient();
- client.loadFromResource("-j","");
+ client.loadData(LoaderOption.JAR,"");
String test="Mozilla/5.0 (Linux; U; Android 2.2; en; HTC Aria A6380 Build/ERE27) AppleWebKit/540.13+ (KHTML, like Gecko) Version/3.1 Mobile Safari/524.15.0";
Modified: incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientTest.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientTest.java?rev=1607963&r1=1607962&r2=1607963&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientTest.java (original)
+++ incubator/devicemap/trunk/devicemap/java/classifier/src/test/java/org/apache/devicemap/DeviceMapClientTest.java Sat Jul 5 01:33:36 2014
@@ -23,6 +23,7 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
+import org.apache.devicemap.loaders.LoaderFactory.LoaderOption;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -31,7 +32,7 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
- *
+ * @author Reza Naghibi
*/
@RunWith(value = Parameterized.class)
public class DeviceMapClientTest {
@@ -41,7 +42,7 @@ public class DeviceMapClientTest {
@BeforeClass
public static void setupDeviceMapClient() throws Exception {
client=new DeviceMapClient();
- client.loadFromResource("-j","");
+ client.loadData(LoaderOption.JAR,"");
}
@Parameters