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 | 5761bf2 | 2006-05-02 19:16: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 | 5761bf2 | 2006-05-02 19:16: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 | 5761bf2 | 2006-05-02 19:16:40 +0000 | [diff] [blame] | 18 | */
|
Francesco Furfari | 605e6e3 | 2006-04-04 22:53:50 +0000 | [diff] [blame] | 19 |
|
| 20 | package org.apache.felix.upnp.extra.util;
|
| 21 |
|
| 22 | import java.text.*;
|
| 23 | import java.util.*;
|
| 24 |
|
| 25 | //import org.apache.xerces.impl.dv.util.*;
|
| 26 | import org.apache.xerces.impl.dv.util.Base64;
|
| 27 | import org.apache.xerces.impl.dv.util.HexBin;
|
| 28 | import org.osgi.service.upnp.*;
|
| 29 |
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 30 | /*
|
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame^] | 31 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
|
Francesco Furfari | f2a6791 | 2006-07-17 17:08:02 +0000 | [diff] [blame] | 32 | */
|
Francesco Furfari | 605e6e3 | 2006-04-04 22:53:50 +0000 | [diff] [blame] | 33 | public class Converter {
|
| 34 |
|
| 35 | /**
|
| 36 | *
|
| 37 | * @param value Object that contain the value
|
| 38 | * @param upnpType String conating the UPnP Type of the Object
|
| 39 | * @return a String that contain the UPnP rappresentation of the value contained in Object
|
| 40 | * of type specified by typeUPnP
|
| 41 | */
|
| 42 | public static String toString(Object value,String upnpType) throws Exception{
|
| 43 | if((value==null)||(upnpType==null))
|
| 44 | throw new NullPointerException("Must be specified a valid value and upnpType");
|
| 45 |
|
| 46 | if(value instanceof Number){
|
| 47 | if(value instanceof Integer){
|
| 48 | return value.toString();
|
| 49 | }else if(value instanceof Float){
|
| 50 | return value.toString();
|
| 51 | }else if(value instanceof Long){
|
| 52 | if(upnpType.equals(UPnPStateVariable.TYPE_TIME)){
|
| 53 | long l = ((Long)value).longValue();
|
| 54 | if(l<0) throw new IllegalArgumentException(l+ "Must be greater than 0");
|
| 55 | Calendar c = Calendar.getInstance();
|
| 56 | c.set(Calendar.HOUR_OF_DAY,(int) (l/3600000));
|
| 57 | int x=(int) (l % 3600000);
|
| 58 | c.set(Calendar.MINUTE,(int) (x / 60000));
|
| 59 | c.set(Calendar.SECOND,(x % 60000)/1000);
|
| 60 | SimpleDateFormat sdt = new SimpleDateFormat("HH:mm:ss");
|
| 61 | return sdt.format(c.getTime());
|
| 62 | }else if(upnpType.equals(UPnPStateVariable.TYPE_TIME_TZ)){
|
| 63 | long l = ((Long)value).longValue();
|
| 64 | if(l<0) throw new IllegalArgumentException(l+ "Must be greater than 0");
|
| 65 | Calendar c = Calendar.getInstance();
|
| 66 | c.set(Calendar.HOUR_OF_DAY,(int) (l/3600000));
|
| 67 | int x=(int) (l % 3600000);
|
| 68 | c.set(Calendar.MINUTE,(int) (x / 60000));
|
| 69 | c.set(Calendar.SECOND,(x % 60000)/1000);
|
| 70 | SimpleDateFormat sdt = new SimpleDateFormat("HH:mm:ssZ");
|
| 71 | return sdt.format(c.getTime());
|
| 72 | }else{
|
| 73 | //Must be UPnPStateVariable.TYPE_UI4)
|
| 74 | return value.toString();
|
| 75 | }
|
| 76 | }else if(value instanceof Double){
|
| 77 | if(upnpType.equals(UPnPStateVariable.TYPE_FIXED_14_4)){
|
| 78 | return Long.toString(((Double)value).longValue())+"."+
|
| 79 | Integer.toString((int) (((((Double)value).doubleValue()*10000D) % 10000)));
|
| 80 | }else{
|
| 81 | //Must be UPnPStateVariable.TYPE_R8 or UPnPStateVariable.TYPE_NUMBER
|
| 82 | return value.toString();
|
| 83 | }
|
| 84 | }
|
| 85 | }else if(value instanceof Date){
|
| 86 | if(upnpType.equals("dateTime")){
|
| 87 | SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
|
| 88 | return sdt.format(value);
|
| 89 | }else if(upnpType.equals("dateTime.tz")){
|
| 90 | SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
| 91 | return sdt.format(value);
|
| 92 | }else if(upnpType.equals("date")){
|
| 93 | SimpleDateFormat sdt = new SimpleDateFormat("yyyy-MM-dd");
|
| 94 | return sdt.format(value);
|
| 95 | }
|
| 96 | }else if(value instanceof Boolean){
|
| 97 | //Must be UPnPStateVariable.TYPE_BOOLEAN
|
| 98 | if(((Boolean)value).booleanValue()){
|
| 99 | return "1";
|
| 100 | }else{
|
| 101 | return "0";
|
| 102 | }
|
| 103 | }else if(value instanceof Character){
|
| 104 | //Must be UPnPStateVariable.TYPE_CHAR
|
| 105 | return value.toString();
|
| 106 | }else if(value instanceof String){
|
| 107 | return value.toString();
|
| 108 | //Must be one of
|
| 109 | // UPnPStateVariable.TYPE_STRING or
|
| 110 | // UPnPStateVariable.TYPE_URI or
|
| 111 | // UPnPStateVariable.TYPE_UUID
|
| 112 | }else if(value instanceof byte[]){
|
| 113 | if(upnpType.equals("bin.hex")){
|
| 114 | return HexBin.encode((byte[]) value);
|
| 115 | }else if(upnpType.equals("bin.base64")){
|
| 116 | return Base64.encode((byte[]) value);
|
| 117 | }
|
| 118 | }
|
| 119 | throw new IllegalArgumentException("Invalid Binding");
|
| 120 | }
|
| 121 |
|
| 122 | /**
|
| 123 | *
|
| 124 | * @param value
|
| 125 | * @param upnpType
|
| 126 | * @return
|
| 127 | */
|
| 128 | public static Object parseString(String value,String upnpType) throws Exception{
|
| 129 | if (value ==null && upnpType.equals("string"))
|
| 130 | value = "";
|
| 131 | if((value==null)||(upnpType==null))
|
| 132 | throw new NullPointerException("Must be specified a valid value and upnpType");
|
| 133 |
|
| 134 | if (upnpType.equals("ui1") || upnpType.equals("ui2")
|
| 135 | || upnpType.equals("i1") || upnpType.equals("i2")
|
| 136 | || upnpType.equals("i4") || upnpType.equals("int")) {
|
| 137 |
|
| 138 | return new Integer(value);
|
| 139 | } else if (upnpType.equals("ui4")){
|
| 140 | return new Long(value);
|
| 141 | } else if(upnpType.equals("time")){
|
| 142 | String[] timeFormats=new String[]{"HH:mm:ss"};
|
| 143 | Date d=getDateValue(value,timeFormats,timeFormats);
|
| 144 |
|
| 145 | Calendar c = Calendar.getInstance();
|
| 146 | c.setTime(d);
|
| 147 | return new Long(
|
| 148 | c.get(Calendar.HOUR_OF_DAY)*3600000
|
| 149 | +c.get(Calendar.MINUTE)*60000
|
| 150 | +c.get(Calendar.SECOND)*1000
|
| 151 | );
|
| 152 | } else if(upnpType.equals("time.tz")) {
|
| 153 | String[] timeFormats=new String[]{"HH:mm:ssZ","HH:mm:ss"};
|
| 154 | Date d=getDateValue(value,timeFormats,timeFormats);
|
| 155 | TimeZone tz = TimeZone.getDefault();
|
| 156 | int dst = tz.getDSTSavings();
|
| 157 | Calendar c = Calendar.getInstance(tz);
|
| 158 | c.setTime(d);
|
| 159 |
|
| 160 | if(timeFormats[0].equals("HH:mm:ssZ")&&(dst!=0))
|
| 161 | c.add(Calendar.MILLISECOND,dst);
|
| 162 | return new Long(
|
| 163 | c.get(Calendar.HOUR_OF_DAY)*3600000
|
| 164 | +c.get(Calendar.MINUTE)*60000
|
| 165 | +c.get(Calendar.SECOND)*1000
|
| 166 | );
|
| 167 | } else if (upnpType.equals("r4") || upnpType.equals("float")) {
|
| 168 | return new Float(value);
|
| 169 | } else if (upnpType.equals("r8") || upnpType.equals("number")
|
| 170 | || upnpType.equals("fixed.14.4")){
|
| 171 | return new Double(value);
|
| 172 | } else if (upnpType.equals("char")) {
|
| 173 | return new Character(value.charAt(0));
|
| 174 | } else if (upnpType.equals("string") || upnpType.equals("uri")
|
| 175 | || upnpType.equals("uuid")) {
|
| 176 | return value;
|
| 177 | } else if (upnpType.equals("date")) {
|
| 178 | String[] timeFormats=new String[]{"yyyy-MM-dd"};
|
| 179 |
|
| 180 | Date d=getDateValue(value,timeFormats,timeFormats);
|
| 181 | return d;
|
| 182 | } else if (upnpType.equals("dateTime")) {
|
| 183 |
|
| 184 | String[] timeFormats=new String[]{
|
| 185 | "yyyy-MM-dd",
|
| 186 | "yyyy-MM-dd'T'HH:mm:ss"
|
| 187 | };
|
| 188 |
|
| 189 | Date d=getDateValue(value,timeFormats,timeFormats);
|
| 190 | return d;
|
| 191 | } else if (upnpType.equals("dateTime.tz")) {
|
| 192 |
|
| 193 | String[] timeFormats=new String[]{
|
| 194 | "yyyy-MM-dd",
|
| 195 | "yyyy-MM-dd'T'HH:mm:ss",
|
| 196 | "yyyy-MM-dd'T'HH:mm:ssZ"
|
| 197 | };
|
| 198 |
|
| 199 | Date d=getDateValue(value,timeFormats,timeFormats);
|
| 200 | return d;
|
| 201 | } else if (upnpType.equals("boolean")) {
|
| 202 | if(value.equalsIgnoreCase("yes") || value.equalsIgnoreCase("true")
|
| 203 | || value.equalsIgnoreCase("1"))
|
| 204 | return new Boolean(true);
|
| 205 | else
|
| 206 | return new Boolean(false);
|
| 207 | } else if (upnpType.equals("bin.base64")) {
|
| 208 | return Base64.decode(value);
|
| 209 | } else if (upnpType.equals("bin.hex")) {
|
| 210 | return HexBin.decode(value);
|
| 211 | }
|
| 212 | throw new IllegalArgumentException("Invalid Binding");
|
| 213 | }
|
| 214 |
|
| 215 | private static String normalizeTimeZone(String value){
|
| 216 | if(value.endsWith("Z")){
|
| 217 | value=value.substring(0,value.length()-1)+"+0000";
|
| 218 | }else if((value.length()>7)
|
| 219 | &&(value.charAt(value.length()-3)==':')
|
| 220 | &&((value.charAt(value.length()-6)=='-')||(value.charAt(value.length()-6)=='+'))){
|
| 221 |
|
| 222 | value=value.substring(0,value.length()-3)+value.substring(value.length()-2);
|
| 223 | }
|
| 224 | return value;
|
| 225 | }
|
| 226 |
|
| 227 | /**
|
| 228 | * @param value
|
| 229 | * @param timeFormats
|
| 230 | * @param choosedIndex
|
| 231 | * @return
|
| 232 | * @throws ParseException
|
| 233 | */
|
| 234 | private static Date getDateValue(String value, String[] timeFormats, String[] choosedIndex) throws ParseException {
|
| 235 | ParsePosition position = null;
|
| 236 | Date d;
|
| 237 | value=normalizeTimeZone(value);
|
| 238 | for (int i=0; i<timeFormats.length; i++) {
|
| 239 | position = new ParsePosition(0);
|
| 240 | SimpleDateFormat sdt = new SimpleDateFormat(timeFormats[i]);
|
| 241 | d=sdt.parse(value,position);
|
| 242 | if(d!=null){
|
| 243 | if(position.getIndex()>=value.length()){
|
| 244 | choosedIndex[0]=timeFormats[i];
|
| 245 | return d;
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 | throw new ParseException("Error parsing "+value,position.getIndex());
|
| 250 | }
|
| 251 | }
|