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 static org.xmlpull.v1.XmlPullParser.END_TAG;
020import static org.xmlpull.v1.XmlPullParser.START_TAG;
021
022import java.util.Date;
023
024import org.jivesoftware.smack.provider.ExtensionElementProvider;
025import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
026
027import org.jxmpp.util.XmppDateTime;
028import org.pgpainless.key.OpenPgpV4Fingerprint;
029import org.xmlpull.v1.XmlPullParser;
030
031public final class PublicKeysListElementProvider extends ExtensionElementProvider<PublicKeysListElement> {
032
033    public static final PublicKeysListElementProvider TEST_INSTANCE = new PublicKeysListElementProvider();
034
035    @Override
036    public PublicKeysListElement parse(XmlPullParser parser, int initialDepth) throws Exception {
037
038        PublicKeysListElement.Builder builder = PublicKeysListElement.builder();
039
040        while (true) {
041
042            int tag = parser.nextTag();
043            String name = parser.getName();
044
045            switch (tag) {
046                case START_TAG:
047
048                    if (PublicKeysListElement.PubkeyMetadataElement.ELEMENT.equals(name)) {
049                        String finger = parser.getAttributeValue(null,
050                                PublicKeysListElement.PubkeyMetadataElement.ATTR_V4_FINGERPRINT);
051                        String dt = parser.getAttributeValue(null,
052                                PublicKeysListElement.PubkeyMetadataElement.ATTR_DATE);
053                        OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(finger);
054                        Date date = XmppDateTime.parseXEP0082Date(dt);
055                        builder.addMetadata(new PublicKeysListElement.PubkeyMetadataElement(fingerprint, date));
056                    }
057                    break;
058
059                case END_TAG:
060
061                    if (name.equals(PublicKeysListElement.ELEMENT)) {
062                        return builder.build();
063                    }
064            }
065        }
066    }
067}