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.util.Date;
021
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.parsing.SmackParsingException.SmackTextParseException;
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) throws XmlPullParserException, IOException, SmackTextParseException {
039
040        PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
041
042        while (true) {
043
044            XmlPullParser.TagEvent tag = parser.nextTag();
045            String name;
046
047            switch (tag) {
048                case START_ELEMENT:
049                    name = parser.getName();
050                    if (PublicKeysListElement.PubkeyMetadataElement.ELEMENT.equals(name)) {
051                        String finger = parser.getAttributeValue(null,
052                                PublicKeysListElement.PubkeyMetadataElement.ATTR_V4_FINGERPRINT);
053                        String dt = parser.getAttributeValue(null,
054                                PublicKeysListElement.PubkeyMetadataElement.ATTR_DATE);
055                        OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(finger);
056                        Date date = ParserUtils.getDateFromXep82String(dt);
057                        builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, date));
058                    }
059                    break;
060
061                case END_ELEMENT:
062                    name = parser.getName();
063                    if (name.equals(PublicKeysListElement.ELEMENT)) {
064                        return builder.build();
065                    }
066                    break;
067
068                default:
069                    // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
070                    break;
071            }
072        }
073    }
074}