001/** 002 * 003 * Copyright 2020 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.file_metadata.provider; 018 019import java.io.IOException; 020import java.text.ParseException; 021 022import org.jivesoftware.smack.packet.XmlEnvironment; 023import org.jivesoftware.smack.parsing.SmackParsingException; 024import org.jivesoftware.smack.provider.ExtensionElementProvider; 025import org.jivesoftware.smack.util.ParserUtils; 026import org.jivesoftware.smack.xml.XmlPullParser; 027import org.jivesoftware.smack.xml.XmlPullParserException; 028import org.jivesoftware.smackx.file_metadata.element.FileMetadataElement; 029import org.jivesoftware.smackx.hashes.element.HashElement; 030import org.jivesoftware.smackx.hashes.provider.HashElementProvider; 031import org.jivesoftware.smackx.thumbnails.element.ThumbnailElement; 032import org.jivesoftware.smackx.thumbnails.provider.ThumbnailElementProvider; 033 034public class FileMetadataElementProvider extends ExtensionElementProvider<FileMetadataElement> { 035 036 @Override 037 public FileMetadataElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) 038 throws XmlPullParserException, IOException, SmackParsingException, ParseException { 039 FileMetadataElement.Builder builder = FileMetadataElement.builder(); 040 041 outerloop: while (true) { 042 XmlPullParser.Event event = parser.next(); 043 switch (event) { 044 case START_ELEMENT: 045 String name = parser.getName(); 046 switch (name) { 047 case FileMetadataElement.ELEMENT: 048 parser.next(); 049 break; 050 case FileMetadataElement.ELEM_DATE: 051 builder.setModificationDate(ParserUtils.getDateFromNextText(parser)); 052 break; 053 case FileMetadataElement.ELEM_DESC: 054 String lang = ParserUtils.getXmlLang(parser); 055 builder.addDescription(ParserUtils.getRequiredNextText(parser), lang); 056 break; 057 case "dimensions": // was replaced with width and height 058 String dimensions = ParserUtils.getRequiredNextText(parser); 059 String[] split = dimensions.split("x"); 060 if (split.length != 2) { 061 throw new IllegalArgumentException("Invalid dimensions."); 062 } 063 builder.setWidth(Integer.parseInt(split[0])); 064 builder.setHeight(Integer.parseInt(split[1])); 065 break; 066 case FileMetadataElement.ELEM_WIDTH: 067 builder.setWidth(Integer.parseInt(ParserUtils.getRequiredNextText(parser))); 068 break; 069 case FileMetadataElement.ELEM_HEIGHT: 070 builder.setHeight(Integer.parseInt(ParserUtils.getRequiredNextText(parser))); 071 break; 072 case FileMetadataElement.ELEM_LENGTH: 073 builder.setLength(Long.parseLong(ParserUtils.getRequiredNextText(parser))); 074 break; 075 case FileMetadataElement.ELEM_MEDIA_TYPE: 076 builder.setMediaType(ParserUtils.getRequiredNextText(parser)); 077 break; 078 case FileMetadataElement.ELEM_NAME: 079 builder.setName(ParserUtils.getRequiredNextText(parser)); 080 break; 081 case FileMetadataElement.ELEM_SIZE: 082 builder.setSize(Long.parseLong(ParserUtils.getRequiredNextText(parser))); 083 break; 084 case HashElement.ELEMENT: 085 builder.addHash(HashElementProvider.INSTANCE.parse(parser, parser.getDepth(), xmlEnvironment)); 086 break; 087 case ThumbnailElement.ELEMENT: 088 ThumbnailElementProvider provider = new ThumbnailElementProvider(); 089 builder.addThumbnail(provider.parse(parser, parser.getDepth(), xmlEnvironment)); 090 } 091 break; 092 case END_ELEMENT: 093 if (parser.getDepth() == initialDepth) break outerloop; 094 break; 095 default: 096 // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. 097 break; 098 } 099 } 100 return builder.build(); 101 } 102}