001/** 002 * 003 * Copyright 2017 Paul Schaub 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.jingle_filetransfer.element; 018 019import java.io.File; 020import java.util.Date; 021 022import org.jivesoftware.smack.util.XmlStringBuilder; 023import org.jivesoftware.smackx.hashes.element.HashElement; 024import org.jivesoftware.smackx.jingle.element.JingleContentDescriptionChildElement; 025 026/** 027 * Content of type File. 028 */ 029public class JingleFileTransferChild extends JingleContentDescriptionChildElement { 030 public static final String ELEMENT = "file"; 031 public static final String ELEM_DATE = "date"; 032 public static final String ELEM_DESC = "desc"; 033 public static final String ELEM_MEDIA_TYPE = "media-type"; 034 public static final String ELEM_NAME = "name"; 035 public static final String ELEM_SIZE = "size"; 036 037 private final Date date; 038 private final String desc; 039 private final HashElement hash; 040 private final String mediaType; 041 private final String name; 042 private final int size; 043 private final Range range; 044 045 public JingleFileTransferChild(Date date, String desc, HashElement hash, String mediaType, String name, int size, Range range) { 046 this.date = date; 047 this.desc = desc; 048 this.hash = hash; 049 this.mediaType = mediaType; 050 this.name = name; 051 this.size = size; 052 this.range = range; 053 } 054 055 public Date getDate() { 056 return date; 057 } 058 059 public String getDescription() { 060 return desc; 061 } 062 063 public HashElement getHash() { 064 return hash; 065 } 066 067 public String getMediaType() { 068 return mediaType; 069 } 070 071 public String getName() { 072 return name; 073 } 074 075 public int getSize() { 076 return size; 077 } 078 079 public Range getRange() { 080 return range; 081 } 082 083 @Override 084 public String getElementName() { 085 return ELEMENT; 086 } 087 088 @Override 089 public CharSequence toXML(String enclosingNamespace) { 090 XmlStringBuilder sb = new XmlStringBuilder(this); 091 sb.rightAngleBracket(); 092 093 sb.optElement(ELEM_DATE, date); 094 sb.optElement(ELEM_DESC, desc); 095 sb.optElement(ELEM_MEDIA_TYPE, mediaType); 096 sb.optElement(ELEM_NAME, name); 097 sb.optElement(range); 098 if (size > 0) { 099 sb.element(ELEM_SIZE, Integer.toString(size)); 100 } 101 sb.optElement(hash); 102 sb.closeElement(this); 103 return sb; 104 } 105 106 public static Builder getBuilder() { 107 return new Builder(); 108 } 109 110 public static final class Builder { 111 private Date date; 112 private String desc; 113 private HashElement hash; 114 private String mediaType; 115 private String name; 116 private int size; 117 private Range range; 118 119 private Builder() { 120 } 121 122 public Builder setDate(Date date) { 123 this.date = date; 124 return this; 125 } 126 127 public Builder setDescription(String desc) { 128 this.desc = desc; 129 return this; 130 } 131 132 public Builder setHash(HashElement hash) { 133 this.hash = hash; 134 return this; 135 } 136 137 public Builder setMediaType(String mediaType) { 138 this.mediaType = mediaType; 139 return this; 140 } 141 142 public Builder setName(String name) { 143 this.name = name; 144 return this; 145 } 146 147 public Builder setSize(int size) { 148 this.size = size; 149 return this; 150 } 151 152 public Builder setRange(Range range) { 153 this.range = range; 154 return this; 155 } 156 157 public JingleFileTransferChild build() { 158 return new JingleFileTransferChild(date, desc, hash, mediaType, name, size, range); 159 } 160 161 public Builder setFile(File file) { 162 return setDate(new Date(file.lastModified())) 163 .setName(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("/") + 1)) 164 .setSize((int) file.length()); 165 } 166 } 167}