blob: 5be1c1a426fded80f86771eb01f4117f2b5250e5 [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * Copyright 2003-2007 Jive Software.
3 *
4 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.jivesoftware.smackx.workgroup.util;
18
19import org.jivesoftware.smackx.workgroup.MetaData;
20import org.jivesoftware.smack.util.StringUtils;
21import org.xmlpull.v1.XmlPullParser;
22import org.xmlpull.v1.XmlPullParserException;
23
24import java.io.IOException;
25import java.util.*;
26
27/**
28 * Utility class for meta-data parsing and writing.
29 *
30 * @author Matt Tucker
31 */
32public class MetaDataUtils {
33
34 /**
35 * Parses any available meta-data and returns it as a Map of String name/value pairs. The
36 * parser must be positioned at an opening meta-data tag, or the an empty map will be returned.
37 *
38 * @param parser the XML parser positioned at an opening meta-data tag.
39 * @return the meta-data.
40 * @throws XmlPullParserException if an error occurs while parsing the XML.
41 * @throws IOException if an error occurs while parsing the XML.
42 */
43 public static Map<String, List<String>> parseMetaData(XmlPullParser parser) throws XmlPullParserException, IOException {
44 int eventType = parser.getEventType();
45
46 // If correctly positioned on an opening meta-data tag, parse meta-data.
47 if ((eventType == XmlPullParser.START_TAG)
48 && parser.getName().equals(MetaData.ELEMENT_NAME)
49 && parser.getNamespace().equals(MetaData.NAMESPACE)) {
50 Map<String, List<String>> metaData = new Hashtable<String, List<String>>();
51
52 eventType = parser.nextTag();
53
54 // Keep parsing until we've gotten to end of meta-data.
55 while ((eventType != XmlPullParser.END_TAG)
56 || (!parser.getName().equals(MetaData.ELEMENT_NAME))) {
57 String name = parser.getAttributeValue(0);
58 String value = parser.nextText();
59
60 if (metaData.containsKey(name)) {
61 List<String> values = metaData.get(name);
62 values.add(value);
63 }
64 else {
65 List<String> values = new ArrayList<String>();
66 values.add(value);
67 metaData.put(name, values);
68 }
69
70 eventType = parser.nextTag();
71 }
72
73 return metaData;
74 }
75
76 return Collections.emptyMap();
77 }
78
79 /**
80 * Serializes a Map of String name/value pairs into the meta-data XML format.
81 *
82 * @param metaData the Map of meta-data as Map&lt;String,List&lt;String>>
83 * @return the meta-data values in XML form.
84 */
85 public static String serializeMetaData(Map<String, List<String>> metaData) {
86 StringBuilder buf = new StringBuilder();
87 if (metaData != null && metaData.size() > 0) {
88 buf.append("<metadata xmlns=\"http://jivesoftware.com/protocol/workgroup\">");
89 for (Iterator<String> i = metaData.keySet().iterator(); i.hasNext();) {
90 String key = i.next();
91 List<String> value = metaData.get(key);
92 for (Iterator<String> it = value.iterator(); it.hasNext();) {
93 String v = it.next();
94 buf.append("<value name=\"").append(key).append("\">");
95 buf.append(StringUtils.escapeForXML(v));
96 buf.append("</value>");
97 }
98 }
99 buf.append("</metadata>");
100 }
101 return buf.toString();
102 }
103}