001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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.workgroup.util;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.Collections;
022import java.util.Hashtable;
023import java.util.List;
024import java.util.Map;
025
026import org.jivesoftware.smack.util.StringUtils;
027import org.jivesoftware.smackx.workgroup.MetaData;
028
029import org.xmlpull.v1.XmlPullParser;
030import org.xmlpull.v1.XmlPullParserException;
031
032/**
033 * Utility class for meta-data parsing and writing.
034 *
035 * @author Matt Tucker
036 */
037public class MetaDataUtils {
038
039    /**
040     * Parses any available meta-data and returns it as a Map of String name/value pairs. The
041     * parser must be positioned at an opening meta-data tag, or the an empty map will be returned.
042     *
043     * @param parser the XML parser positioned at an opening meta-data tag.
044     * @return the meta-data.
045     * @throws XmlPullParserException if an error occurs while parsing the XML.
046     * @throws IOException            if an error occurs while parsing the XML.
047     */
048    public static Map<String, List<String>> parseMetaData(XmlPullParser parser) throws XmlPullParserException, IOException {
049        int eventType = parser.getEventType();
050
051        // If correctly positioned on an opening meta-data tag, parse meta-data.
052        if ((eventType == XmlPullParser.START_TAG)
053                && parser.getName().equals(MetaData.ELEMENT_NAME)
054                && parser.getNamespace().equals(MetaData.NAMESPACE)) {
055            Map<String, List<String>> metaData = new Hashtable<>();
056
057            eventType = parser.nextTag();
058
059            // Keep parsing until we've gotten to end of meta-data.
060            while ((eventType != XmlPullParser.END_TAG)
061                    || (!parser.getName().equals(MetaData.ELEMENT_NAME))) {
062                String name = parser.getAttributeValue(0);
063                String value = parser.nextText();
064
065                if (metaData.containsKey(name)) {
066                    List<String> values = metaData.get(name);
067                    values.add(value);
068                }
069                else {
070                    List<String> values = new ArrayList<>();
071                    values.add(value);
072                    metaData.put(name, values);
073                }
074
075                eventType = parser.nextTag();
076            }
077
078            return metaData;
079        }
080
081        return Collections.emptyMap();
082    }
083
084    /**
085     * Serializes a Map of String name/value pairs into the meta-data XML format.
086     *
087     * @param metaData the Map of meta-data as Map&lt;String,List&lt;String&gt;&gt;
088     * @return the meta-data values in XML form.
089     */
090    public static String serializeMetaData(Map<String, List<String>> metaData) {
091        StringBuilder buf = new StringBuilder();
092        if (metaData != null && metaData.size() > 0) {
093            buf.append("<metadata xmlns=\"http://jivesoftware.com/protocol/workgroup\">");
094            for (String key : metaData.keySet()) {
095                List<String> value = metaData.get(key);
096                for (String v : value) {
097                    buf.append("<value name=\"").append(key).append("\">");
098                    buf.append(StringUtils.escapeForXmlText(v));
099                    buf.append("</value>");
100                }
101            }
102            buf.append("</metadata>");
103        }
104        return buf.toString();
105    }
106}