blob: 3e4088ddcab57e6e39bcc6272d95dc87265d5b45 [file] [log] [blame]
Francesco Furfariec7e1752006-10-02 13:37:04 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Francesco Furfarid8bdb642006-04-04 23:33:40 +00009 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Francesco Furfarid8bdb642006-04-04 23:33:40 +000011 *
Francesco Furfariec7e1752006-10-02 13:37:04 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Francesco Furfarid8bdb642006-04-04 23:33:40 +000018 */
19
20package org.apache.felix.upnp.basedriver.tool;
21
22import java.io.File;
23
24import org.osgi.framework.BundleContext;
25import org.osgi.framework.Constants;
26import org.osgi.framework.ServiceReference;
27import org.osgi.service.upnp.UPnPDevice;
28
Francesco Furfarif2a67912006-07-17 17:08:02 +000029/*
Karl Paulsd312acc2007-06-18 20:38:33 +000030* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Francesco Furfarif2a67912006-07-17 17:08:02 +000031*/
Francesco Furfarid8bdb642006-04-04 23:33:40 +000032public class Util {
33
34 public static boolean isUPnPDevice(ServiceReference sr){
35 String[] aux = (String[]) sr.getProperty(Constants.OBJECTCLASS);
36 String val=UPnPDevice.class.getName();
37 int i;
38 for (i = 0; i < aux.length; i++) {
39 if(aux[i].equals(val)) break;
40 }
41 if(i==aux.length) return false;
42 aux = (String[]) sr.getProperty(org.osgi.service.device.Constants.DEVICE_CATEGORY);
43 val=UPnPDevice.DEVICE_CATEGORY;
44 for (i = 0; i < aux.length; i++) {
45 if(aux[i].equals(val))
46 return true;
47 }
48 return false;
49 }
50
51 public static final boolean isRootDevice(ServiceReference sr){
52 return (sr.getProperty(UPnPDevice.PARENT_UDN)==null)
53 && isUPnPDevice(sr);
54 }
55
56 public static final String getPropertyDefault(BundleContext bc, String propertyName, String defaultValue ){
57 String value = bc.getProperty(propertyName);
58 if(value == null)
59 return defaultValue;
60 return value;
61 }
62
63 public static String sanitizeFilename(String name){
64 return name.replaceAll("[:;\\,/*?<>\"|]","_");
65 }
66
67 public static boolean makeParentPath(String filePath){
68 int l=filePath.lastIndexOf(File.separator);
69 filePath=filePath.substring(0,l);
70 File p=new File(filePath);
71 if(p.exists())
72 return true;
73 return p.mkdirs();
74 }
75
76 /**
77 * @param d
78 */
79 public static boolean deleteRecursive(File d) {
80 if(!d.delete()){
81 if(d.isDirectory()){
82 File[] subs = d.listFiles();
83 for (int i = 0; i < subs.length; i++) {
84 if(!deleteRecursive(subs[i]))
85 return false;
86 }
87 return d.delete();
88 }else{
89 return false;
90 }
91 }
92 return true;
93 }
94
95}