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 2013/06/23 04:30:28 UTC

svn commit: r1495789 - in /incubator/devicemap/trunk/devicemapjava: ./ src/main/java/ src/main/java/org/apache/devicemap/classification/ src/main/java/org/apache/devicemap/client/ src/main/resources/

Author: rezan
Date: Sun Jun 23 02:30:28 2013
New Revision: 1495789

URL: http://svn.apache.org/r1495789
Log:
refactor

Added:
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/DeviceMapClient.java
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/FileLoader.java
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Loader.java
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Util.java
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/XMLParser.java
    incubator/devicemap/trunk/devicemapjava/src/main/resources/
Removed:
    incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/classification/
Modified:
    incubator/devicemap/trunk/devicemapjava/README
    incubator/devicemap/trunk/devicemapjava/src/main/java/Main.java
    incubator/devicemap/trunk/devicemapjava/test.sh

Modified: incubator/devicemap/trunk/devicemapjava/README
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/README?rev=1495789&r1=1495788&r2=1495789&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/README (original)
+++ incubator/devicemap/trunk/devicemapjava/README Sun Jun 23 02:30:28 2013
@@ -1,3 +1,3 @@
-Device Map Java API
+Device Map Java Client
 
 Refactor (rewrite) of original OpenDDR Java API

Modified: incubator/devicemap/trunk/devicemapjava/src/main/java/Main.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/main/java/Main.java?rev=1495789&r1=1495788&r2=1495789&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/main/java/Main.java (original)
+++ incubator/devicemap/trunk/devicemapjava/src/main/java/Main.java Sun Jun 23 02:30:28 2013
@@ -1,34 +1,18 @@
-import java.io.FileInputStream;
-import java.io.InputStreamReader;
-import java.util.Map;
-import org.apache.devicemap.classification.Loader;
+import org.apache.devicemap.client.DeviceMapClient;
 
 public class Main {
+    
     public static void main(String args[]) throws Exception {
-        System.out.println("main()");
+        System.out.println("DeviceMap Client");
         
-        Loader loader=new Loader();
+        DeviceMapClient client=new DeviceMapClient();
         
-        InputStreamReader ddin=new InputStreamReader(new FileInputStream("DeviceDataSource.xml"), "UTF-8");
-        InputStreamReader ddpin=new InputStreamReader(new FileInputStream("DeviceDataSourcePatch.xml"), "UTF-8");
-        InputStreamReader din=new InputStreamReader(new FileInputStream("BuilderDataSource.xml"), "UTF-8");
-        InputStreamReader dpin=new InputStreamReader(new FileInputStream("BuilderDataSource.xml"), "UTF-8");
+        long start=System.currentTimeMillis();
         
-        loader.loadDeviceData(ddin);
-        loader.loadDeviceData(ddpin);
+        client.loadFromFolder(("."));
         
-        loader.setParentAttributes();
+        long diff=System.currentTimeMillis()-start;
         
-        loader.loadDevicePatterns(din);
-        loader.loadDevicePatterns(dpin);
-        
-        ddin.close();
-        ddpin.close();
-        din.close();
-        dpin.close();
-        
-        Map<String,Map<String,String>> devices=loader.getDevices();
-        
-        System.out.println(devices.get(devices.keySet().iterator().next()));
+        System.out.println("Loaded "+client.getDeviceCount()+" devices in "+diff+"ms");
     }
 }
\ No newline at end of file

Added: incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/DeviceMapClient.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/DeviceMapClient.java?rev=1495789&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/DeviceMapClient.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/DeviceMapClient.java Sun Jun 23 02:30:28 2013
@@ -0,0 +1,46 @@
+/*
+   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.client;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ * @author Reza Naghibi
+ */
+public class DeviceMapClient {
+    
+    Map<String,Map<String,String>> devices;
+    
+    public DeviceMapClient() {
+        devices=new HashMap<String,Map<String,String>>();
+    }
+    
+    public void loadFromFolder(String folder) throws IOException {
+        devices=FileLoader.dataFolder(folder);
+    }
+    
+    public int getDeviceCount() {
+        return (devices==null)?-1:devices.size();
+    }
+
+}

Added: incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/FileLoader.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/FileLoader.java?rev=1495789&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/FileLoader.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/FileLoader.java Sun Jun 23 02:30:28 2013
@@ -0,0 +1,78 @@
+/*
+   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.client;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Map;
+
+/**
+ *
+ * @author Reza Naghibi
+ */
+public class FileLoader {
+    
+    /**
+     * 
+     * @param folder which contains device map data
+     * @return map of devices
+     * @throws IOException 
+     */
+    public static Map<String,Map<String,String>> dataFolder(String folder) throws IOException {
+        Loader loader=new Loader();
+        
+        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";
+
+        InputStreamReader ddin=new InputStreamReader(new FileInputStream(ddpath), "UTF-8");
+        loader.loadDeviceData(ddin);
+        ddin.close();
+        
+        try {
+            InputStreamReader ddpin=new InputStreamReader(new FileInputStream(ddppath), "UTF-8");
+            loader.loadDeviceData(ddpin);
+            ddpin.close();
+        } catch(FileNotFoundException ex) {
+            Util.log("WARNING: file not found "+ddppath+": "+ex.toString());
+        }
+
+        loader.setParentAttributes();
+
+        InputStreamReader bin=new InputStreamReader(new FileInputStream(bpath), "UTF-8");
+        loader.loadDevicePatterns(bin);
+        bin.close();
+        
+        try {
+            InputStreamReader bpin=new InputStreamReader(new FileInputStream(bppath), "UTF-8");
+            loader.loadDevicePatterns(bpin);
+            bpin.close();
+        } catch(FileNotFoundException ex) {
+            Util.log("WARNING: file not found "+bppath+": "+ex.toString());
+        }
+
+        return loader.getDevices();
+    }
+
+}

Added: incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Loader.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Loader.java?rev=1495789&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Loader.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Loader.java Sun Jun 23 02:30:28 2013
@@ -0,0 +1,166 @@
+/*
+   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.client;
+
+import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ * @author Reza Naghibi
+ */
+public class Loader {
+    
+    private Map<String,Map<String,String>> devices;
+    
+    public Loader() {
+        devices=new HashMap<String,Map<String,String>>();
+    }
+    
+    /*
+     * loads device data from an InputStreamReader
+     */
+    public void loadDeviceData(InputStreamReader in) {
+        XMLParser parser=new XMLParser(in);
+        String tag;
+        
+        try {
+            String id="";
+            String parent="";
+            Map<String,String> attributes=new HashMap<String,String>();
+            
+            while(!(tag=parser.getNextTag()).isEmpty()) {
+                if(tag.startsWith("<device ")) {
+                    id=XMLParser.getAttribute(tag,"id");
+                    parent=XMLParser.getAttribute(tag,"parentId");
+                } else if(tag.equals("</device>")) {
+                    if(!parent.isEmpty()) {
+                        attributes.put("parentId", parent);
+                    }
+                    
+                    if(!id.isEmpty()) {
+                        attributes.put("id", id);
+                        getDevices().put(id, attributes);
+                    }
+                    
+                    id="";
+                    parent="";
+                    attributes=new HashMap<String,String>();
+                } else if(tag.startsWith("<property ")) {
+                    String key=XMLParser.getAttribute(tag,"name");
+                    String value=XMLParser.getAttribute(tag,"value");
+                    
+                    attributes.put(key,value);
+                }
+            }
+        } catch(Exception ex) {
+            Util.log("ERROR: "+ex.toString(),ex);
+        }
+    }
+    
+    /*
+     * loads patterns from an InputStreamReader
+     */
+    public void loadDevicePatterns(InputStreamReader in) {
+        XMLParser parser=new XMLParser(in);
+        String tag;
+        
+        try {
+            String builder="";
+            String id="";
+            String patterns="";
+            
+            while(!(tag=parser.getNextTag()).isEmpty()) {
+                if(tag.startsWith("<builder ")) {
+                    builder=XMLParser.getAttribute(tag,"class");
+                } else if(tag.startsWith("<device ")) {
+                    id=XMLParser.getAttribute(tag,"id");
+                } else if(tag.equals("</device>")) {
+                    if(!id.isEmpty() && !patterns.isEmpty()) {
+                        Map<String,String> device=devices.get(id);
+                        
+                        if(device!=null) {
+                            device.put("pattern",patterns);
+                            device.put("builder",builder);
+                        } else {
+                            Util.log("ERROR: device not found: '"+id+"'");
+                        }
+                    }
+                    
+                    id="";
+                    patterns="";
+                    
+                } else if(tag.equals("<value>")) {
+                    String pattern=parser.getTagValue();
+                    
+                    if(!patterns.isEmpty()) {
+                        patterns+=",";
+                    }
+                    
+                    patterns+=pattern;
+                }
+            }
+        } catch(Exception ex) {
+            Util.log("ERROR: "+ex.toString(),ex);
+        }
+    }
+    
+    /**
+     * Sets attributes from parents
+     */
+    public void setParentAttributes() {
+        for(String device:devices.keySet()) {
+            Map<String,String> attributes=devices.get(device);
+            
+            mergeParent(attributes);
+        }
+    }
+    
+    private void mergeParent(Map<String,String> attributes) {
+        String parentId=attributes.get("parentId");
+        
+        if(parentId==null || parentId.isEmpty()) {
+            return;
+        }
+        
+        Map<String,String> parent=devices.get(parentId);
+        
+        if(parent==null || parent.isEmpty()) {
+            return;
+        }
+        
+        mergeParent(parent);
+        
+        for(String key:parent.keySet()) {
+            String value=parent.get(key);
+            
+            if(!attributes.containsKey(key)) {
+                attributes.put(key, value);
+            }
+        }
+    }
+
+    /**
+     * @return the devices
+     */
+    public Map<String,Map<String,String>> getDevices() {
+        return devices;
+    }
+}

Added: incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Util.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Util.java?rev=1495789&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Util.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/Util.java Sun Jun 23 02:30:28 2013
@@ -0,0 +1,43 @@
+/*
+   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.client;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ *
+ * @author Reza Naghibi
+ */
+public class Util {
+    
+    public static void log(String msg) {
+        log(msg, null);
+    }
+    
+    public static void log(String msg, Exception e) {
+        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();
+        }
+    }
+}

Added: incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/XMLParser.java
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/XMLParser.java?rev=1495789&view=auto
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/XMLParser.java (added)
+++ incubator/devicemap/trunk/devicemapjava/src/main/java/org/apache/devicemap/client/XMLParser.java Sun Jun 23 02:30:28 2013
@@ -0,0 +1,117 @@
+/*
+   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.client;
+
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+/**
+ *
+ * @author Reza Naghibi
+ */
+public class XMLParser {
+    
+    private InputStreamReader in;
+    
+    private char pre;
+
+    private XMLParser() {}
+    
+    public XMLParser(InputStreamReader in) {
+        this.in=in;
+        pre=0;
+    }
+    
+    public String getNextTag() throws IOException {
+        StringBuilder ret=new StringBuilder();
+        
+        int i;
+        boolean start=false;
+        
+        if(pre=='<') {
+            ret.append(pre);
+            pre=0;
+            start=true;
+        }
+        
+        while((i=in.read())!=-1) {
+            char c=(char)i;
+            if(c=='<') {
+                start=true;
+                ret.append(c);
+            } else if(c=='>') {
+                ret.append(c);
+                break;
+            } else if(start) {
+                ret.append(c);
+            }
+        }
+        
+        return ret.toString();
+    }
+    
+    public String getTagValue() throws IOException {
+        StringBuilder ret=new StringBuilder();
+        
+        int i;
+        
+        while((i=in.read())!=-1) {
+            char c=(char)i;
+            if(c=='<') {
+                pre='<';
+                break;
+            } else {
+                ret.append(c);
+            }
+        }
+        
+        return ret.toString().trim();
+    }
+    
+    public static String getAttribute(String tag, String name) {
+        int retpos=tag.toLowerCase().indexOf(name.toLowerCase()+"=");
+        
+        if(retpos==-1)
+            return "";
+
+        String ret=tag.substring(retpos+name.length()+1);
+
+        if(ret.startsWith("\"")) {
+            ret=ret.substring(1);
+            int endpos=ret.indexOf("\"");
+            
+            if(endpos==-1) {
+                return "";
+            }
+            
+            ret=ret.substring(0, endpos);
+        } else {
+            int endpos=ret.indexOf(" ");
+            
+            if(endpos==-1) {
+                return "";
+            }
+            
+            ret=ret.substring(0, endpos);
+        }
+        
+        return ret;
+    }
+}

Modified: incubator/devicemap/trunk/devicemapjava/test.sh
URL: http://svn.apache.org/viewvc/incubator/devicemap/trunk/devicemapjava/test.sh?rev=1495789&r1=1495788&r2=1495789&view=diff
==============================================================================
--- incubator/devicemap/trunk/devicemapjava/test.sh (original)
+++ incubator/devicemap/trunk/devicemapjava/test.sh Sun Jun 23 02:30:28 2013
@@ -1,4 +1,4 @@
-#!/bin/bash
-
-java -cp "target/devicemapclient.jar" Main
-
+#!/bin/bash
+
+java -cp "target/devicemapclient.jar" Main
+