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