diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/provider/FileMetadataElementProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/provider/FileMetadataElementProvider.java index cd87dcdb4b..e4212b0cec 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/provider/FileMetadataElementProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/file_metadata/provider/FileMetadataElementProvider.java @@ -33,64 +33,70 @@ public class FileMetadataElementProvider extends ExtensionElementProvider { - public static FileMetadataElementProvider TEST_INSTANCE = new FileMetadataElementProvider(); - @Override public FileMetadataElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException { FileMetadataElement.Builder builder = FileMetadataElement.builder(); - do { - XmlPullParser.TagEvent tagEvent = parser.nextTag(); - String name = parser.getName(); - if (tagEvent != XmlPullParser.TagEvent.START_ELEMENT) { - continue; - } - switch (name) { - case FileMetadataElement.ELEMENT: - parser.next(); - break; - case FileMetadataElement.ELEM_DATE: - builder.setModificationDate(ParserUtils.getDateFromNextText(parser)); - break; - case FileMetadataElement.ELEM_DESC: - String lang = ParserUtils.getXmlLang(parser); - builder.addDescription(ParserUtils.getRequiredNextText(parser), lang); - break; - case "dimensions": // was replaced with width and height - String dimensions = ParserUtils.getRequiredNextText(parser); - String[] split = dimensions.split("x"); - if (split.length != 2) { - throw new IllegalArgumentException("Invalid dimensions."); - } - builder.setWidth(Integer.parseInt(split[0])); - builder.setHeight(Integer.parseInt(split[1])); - break; - case FileMetadataElement.ELEM_WIDTH: - builder.setWidth(Integer.parseInt(ParserUtils.getRequiredNextText(parser))); - break; - case FileMetadataElement.ELEM_HEIGHT: - builder.setHeight(Integer.parseInt(ParserUtils.getRequiredNextText(parser))); - break; - case FileMetadataElement.ELEM_LENGTH: - builder.setLength(Long.parseLong(ParserUtils.getRequiredNextText(parser))); - break; - case FileMetadataElement.ELEM_MEDIA_TYPE: - builder.setMediaType(ParserUtils.getRequiredNextText(parser)); - break; - case FileMetadataElement.ELEM_NAME: - builder.setName(ParserUtils.getRequiredNextText(parser)); - break; - case FileMetadataElement.ELEM_SIZE: - builder.setSize(Long.parseLong(ParserUtils.getRequiredNextText(parser))); - break; - case HashElement.ELEMENT: - builder.addHash(HashElementProvider.INSTANCE.parse(parser, parser.getDepth(), xmlEnvironment)); - break; - case ThumbnailElement.ELEMENT: - ThumbnailElementProvider provider = new ThumbnailElementProvider(); - builder.addThumbnail(provider.parse(parser, parser.getDepth(), xmlEnvironment)); + + outerloop: while (true) { + XmlPullParser.Event event = parser.next(); + switch (event) { + case START_ELEMENT: + String name = parser.getName(); + switch (name) { + case FileMetadataElement.ELEMENT: + parser.next(); + break; + case FileMetadataElement.ELEM_DATE: + builder.setModificationDate(ParserUtils.getDateFromNextText(parser)); + break; + case FileMetadataElement.ELEM_DESC: + String lang = ParserUtils.getXmlLang(parser); + builder.addDescription(ParserUtils.getRequiredNextText(parser), lang); + break; + case "dimensions": // was replaced with width and height + String dimensions = ParserUtils.getRequiredNextText(parser); + String[] split = dimensions.split("x"); + if (split.length != 2) { + throw new IllegalArgumentException("Invalid dimensions."); + } + builder.setWidth(Integer.parseInt(split[0])); + builder.setHeight(Integer.parseInt(split[1])); + break; + case FileMetadataElement.ELEM_WIDTH: + builder.setWidth(Integer.parseInt(ParserUtils.getRequiredNextText(parser))); + break; + case FileMetadataElement.ELEM_HEIGHT: + builder.setHeight(Integer.parseInt(ParserUtils.getRequiredNextText(parser))); + break; + case FileMetadataElement.ELEM_LENGTH: + builder.setLength(Long.parseLong(ParserUtils.getRequiredNextText(parser))); + break; + case FileMetadataElement.ELEM_MEDIA_TYPE: + builder.setMediaType(ParserUtils.getRequiredNextText(parser)); + break; + case FileMetadataElement.ELEM_NAME: + builder.setName(ParserUtils.getRequiredNextText(parser)); + break; + case FileMetadataElement.ELEM_SIZE: + builder.setSize(Long.parseLong(ParserUtils.getRequiredNextText(parser))); + break; + case HashElement.ELEMENT: + builder.addHash(HashElementProvider.INSTANCE.parse(parser, parser.getDepth(), xmlEnvironment)); + break; + case ThumbnailElement.ELEMENT: + ThumbnailElementProvider provider = new ThumbnailElementProvider(); + builder.addThumbnail(provider.parse(parser, parser.getDepth(), xmlEnvironment)); + } + break; + case END_ELEMENT: + if (parser.getDepth() == initialDepth) break outerloop; + break; + default: + // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement. + break; } - } while (parser.getDepth() != initialDepth); + } return builder.build(); } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/thumbnails/provider/ThumbnailElementProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/thumbnails/provider/ThumbnailElementProvider.java index b21f7e8ea8..6fc1740022 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/thumbnails/provider/ThumbnailElementProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/thumbnails/provider/ThumbnailElementProvider.java @@ -22,6 +22,7 @@ import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.provider.ExtensionElementProvider; +import org.jivesoftware.smack.util.ParserUtils; import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smackx.thumbnails.element.ThumbnailElement; @@ -32,14 +33,14 @@ public ThumbnailElement parse(XmlPullParser parser, int initialDepth, XmlEnviron throws XmlPullParserException, IOException, SmackParsingException, ParseException { String uri = parser.getAttributeValue(ThumbnailElement.ELEM_URI); String mediaType = parser.getAttributeValue(ThumbnailElement.ELEM_MEDIA_TYPE); - String width = parser.getAttributeValue(ThumbnailElement.ELEM_WIDTH); - String height = parser.getAttributeValue(ThumbnailElement.ELEM_HEIGHT); + Integer width = ParserUtils.getIntegerAttribute(parser, ThumbnailElement.ELEM_WIDTH); + Integer height = ParserUtils.getIntegerAttribute(parser, ThumbnailElement.ELEM_HEIGHT); return new ThumbnailElement( uri, mediaType, - width == null ? null : Integer.parseInt(width), - height == null ? null : Integer.parseInt(height) + width, + height ); } } diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/file_metadata/FileMetadataElementTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/file_metadata/FileMetadataElementTest.java index c016bfced3..2295813082 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/file_metadata/FileMetadataElementTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/file_metadata/FileMetadataElementTest.java @@ -31,7 +31,6 @@ import org.jivesoftware.smackx.hashes.HashManager; import org.jivesoftware.smackx.hashes.element.HashElement; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; @@ -39,8 +38,28 @@ public class FileMetadataElementTest extends SmackTestSuite { - private static Date date; - private static FileMetadataElement metadataElement; + private static final Date date; + static { + try { + date = XmppDateTime.parseDate("2015-07-26T21:46:00+01:00"); + } catch (ParseException e) { + throw new IllegalStateException(e); + } + } + + private static final FileMetadataElement metadataElement = FileMetadataElement.builder() + .setModificationDate(date) + .setWidth(1920) + .setHeight(1080) + .addDescription("Picture of 24th XSF Summit") + .addDescription("Foto vom 24. XSF Summit", "de") + .addHash(new HashElement(HashManager.ALGORITHM.SHA_256, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=")) + .setLength(63000) + .setMediaType("text/plain") + .setName("text.txt") + .setSize(6144) + .build(); + private static final String expectedXml = "" + "2015-07-26T20:46:00.000+00:00" + "1920" + @@ -66,24 +85,6 @@ public class FileMetadataElementTest extends SmackTestSuite { "6144" + ""; - @BeforeAll - public static void setup() throws ParseException { - date = XmppDateTime.parseDate("2015-07-26T21:46:00+01:00"); - metadataElement = FileMetadataElement.builder() - .setModificationDate(date) - .setWidth(1920) - .setHeight(1080) - .addDescription("Picture of 24th XSF Summit") - .addDescription("Foto vom 24. XSF Summit", "de") - .addHash(new HashElement(HashManager.ALGORITHM.SHA_256, "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=")) - .setLength(63000) - .setMediaType("text/plain") - .setName("text.txt") - .setSize(6144) - .build(); - } - - @Test public void testSerialization() { assertXmlSimilar(expectedXml, metadataElement.toXML().toString()); @@ -105,6 +106,28 @@ public void testLegacyParsing(SmackTestUtil.XmlPullParserKind parserKind) throws assertEquals(metadataElement, parsed); } + @ParameterizedTest + @EnumSource(SmackTestUtil.XmlPullParserKind.class) + public void testParseUnknownExtension(SmackTestUtil.XmlPullParserKind parserKind) throws Exception { + final String xml = "" + + "2015-07-26T20:46:00.000+00:00" + + "1920" + + "1080" + + "foo" + + "Picture of 24th XSF Summit" + + "Foto vom 24. XSF Summit" + + "2XarmwTlNxDAMkvymloX3S5+VbylNrJt/l5QyPa+YoU=" + + "63000" + + "text/plain" + + "text.txt" + + "6144" + + ""; + + FileMetadataElement parsed = SmackTestUtil.parse(xml, FileMetadataElementProvider.class, parserKind); + + assertEquals(metadataElement, parsed); + } + @Test public void nameIsEscaped() { FileMetadataElement e = FileMetadataElement.builder().setName("/etc/passwd").build(); diff --git a/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java b/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java index 1714723580..d9f01636ea 100644 --- a/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java +++ b/smack-java8-full/src/main/java/org/jivesoftware/smackx/package-info.java @@ -370,6 +370,12 @@ * Allows sending a MUC invitation directly from the user to the contact with mediation by the room. * * + * Jingle Content Thumbnails + * XEP-0264 + * {@link org.jivesoftware.smackx.thumbnails.element} + * Defines a way for a client to supply a preview image for Jingle content. + * + * * Message Carbons * XEP-0280 * {@link org.jivesoftware.smackx.carbons} @@ -583,6 +589,12 @@ * Declare body elements of a message as ignorable fallback for naive legacy clients. * * + * File metadata element + * XEP-0446 + * {@link org.jivesoftware.smackx.file_metadata.element} + * Defines a generic file metadata element to be used in other specifications. + * + * * Google GCM JSON payload * *