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.LinkedHashMap; 023import java.util.List; 024import java.util.Map; 025 026import org.jivesoftware.smack.util.StringUtils; 027import org.jivesoftware.smack.xml.XmlPullParser; 028import org.jivesoftware.smack.xml.XmlPullParserException; 029 030import org.jivesoftware.smackx.workgroup.MetaData; 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 @SuppressWarnings("MixedMutabilityReturnType") 049 public static Map<String, List<String>> parseMetaData(XmlPullParser parser) throws XmlPullParserException, IOException { 050 XmlPullParser.Event eventType = parser.getEventType(); 051 052 // If correctly positioned on an opening meta-data tag, parse meta-data. 053 if ((eventType == XmlPullParser.Event.START_ELEMENT) 054 && parser.getName().equals(MetaData.ELEMENT_NAME) 055 && parser.getNamespace().equals(MetaData.NAMESPACE)) { 056 Map<String, List<String>> metaData = new LinkedHashMap<>(); 057 058 eventType = parser.next(); 059 060 // Keep parsing until we've gotten to end of meta-data. 061 while ((eventType != XmlPullParser.Event.END_ELEMENT) 062 || !parser.getName().equals(MetaData.ELEMENT_NAME)) { 063 String name = parser.getAttributeValue(0); 064 String value = parser.nextText(); 065 066 if (metaData.containsKey(name)) { 067 List<String> values = metaData.get(name); 068 values.add(value); 069 } 070 else { 071 List<String> values = new ArrayList<>(); 072 values.add(value); 073 metaData.put(name, values); 074 } 075 076 eventType = parser.next(); 077 } 078 079 return metaData; 080 } 081 082 return Collections.emptyMap(); 083 } 084 085 /** 086 * Serializes a Map of String name/value pairs into the meta-data XML format. 087 * 088 * @param metaData the Map of meta-data as Map<String, List<String>> 089 * @return the meta-data values in XML form. 090 */ 091 public static String serializeMetaData(Map<String, List<String>> metaData) { 092 StringBuilder buf = new StringBuilder(); 093 if (metaData != null && metaData.size() > 0) { 094 buf.append("<metadata xmlns=\"http://jivesoftware.com/protocol/workgroup\">"); 095 for (String key : metaData.keySet()) { 096 List<String> value = metaData.get(key); 097 for (String v : value) { 098 buf.append("<value name=\"").append(key).append("\">"); 099 buf.append(StringUtils.escapeForXmlText(v)); 100 buf.append("</value>"); 101 } 102 } 103 buf.append("</metadata>"); 104 } 105 return buf.toString(); 106 } 107}