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