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.Iterator;
024import java.util.List;
025import java.util.Map;
026
027import org.jivesoftware.smack.util.StringUtils;
028import org.jivesoftware.smackx.workgroup.MetaData;
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<String, List<String>>();
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<String>();
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>>
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 (Iterator<String> i = metaData.keySet().iterator(); i.hasNext();) {
095                String key = i.next();
096                List<String> value = metaData.get(key);
097                for (Iterator<String> it = value.iterator(); it.hasNext();) {
098                    String v = it.next();
099                    buf.append("<value name=\"").append(key).append("\">");
100                    buf.append(StringUtils.escapeForXML(v));
101                    buf.append("</value>");
102                }
103            }
104            buf.append("</metadata>");
105        }
106        return buf.toString();
107    }
108}