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