001/**
002 *
003 * Copyright 2018 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.ox.provider;
018
019import java.io.IOException;
020import java.text.ParseException;
021import java.util.Date;
022
023import org.jivesoftware.smack.packet.XmlEnvironment;
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.ox.element.PublicKeysListElement;
030
031import org.pgpainless.key.OpenPgpV4Fingerprint;
032
033public final class PublicKeysListElementProvider extends ExtensionElementProvider<PublicKeysListElement> {
034
035    public static final PublicKeysListElementProvider TEST_INSTANCE = new PublicKeysListElementProvider();
036
037    @Override
038    public PublicKeysListElement parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment)
039                    throws XmlPullParserException, IOException, ParseException {
040
041        PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
042
043        while (true) {
044
045            XmlPullParser.TagEvent tag = parser.nextTag();
046            String name;
047
048            switch (tag) {
049                case START_ELEMENT:
050                    name = parser.getName();
051                    if (PublicKeysListElement.PubkeyMetadataElement.ELEMENT.equals(name)) {
052                        String finger = parser.getAttributeValue(null,
053                                PublicKeysListElement.PubkeyMetadataElement.ATTR_V4_FINGERPRINT);
054                        String dt = parser.getAttributeValue(null,
055                                PublicKeysListElement.PubkeyMetadataElement.ATTR_DATE);
056                        OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(finger);
057                        Date date = ParserUtils.getDateFromXep82String(dt);
058                        builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, date));
059                    }
060                    break;
061
062                case END_ELEMENT:
063                    name = parser.getName();
064                    if (name.equals(PublicKeysListElement.ELEMENT)) {
065                        return builder.build();
066                    }
067                    break;
068
069                default:
070                    // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
071                    break;
072            }
073        }
074    }
075}