Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 1 | /* |
| 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 Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 9 | * |
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 11 | * |
Francesco Furfari | ec7e175 | 2006-10-02 13:37:04 +0000 | [diff] [blame] | 12 | * 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 Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | package org.apache.felix.upnp.basedriver.tool; |
| 21 | |
| 22 | import java.io.File; |
| 23 | |
| 24 | import org.osgi.framework.BundleContext; |
| 25 | import org.osgi.framework.Constants; |
| 26 | import org.osgi.framework.ServiceReference; |
| 27 | import org.osgi.service.upnp.UPnPDevice; |
| 28 | |
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 29 | /* |
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 30 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 31 | */ |
Francesco Furfari | d8bdb64 | 2006-04-04 23:33:40 +0000 | [diff] [blame] | 32 | public 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 | } |