MetaDataUtils.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.workgroup.util;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Hashtable;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.jivesoftware.smack.util.StringUtils;
  26. import org.jivesoftware.smackx.workgroup.MetaData;
  27. import org.xmlpull.v1.XmlPullParser;
  28. import org.xmlpull.v1.XmlPullParserException;

  29. /**
  30.  * Utility class for meta-data parsing and writing.
  31.  *
  32.  * @author Matt Tucker
  33.  */
  34. public class MetaDataUtils {

  35.     /**
  36.      * Parses any available meta-data and returns it as a Map of String name/value pairs. The
  37.      * parser must be positioned at an opening meta-data tag, or the an empty map will be returned.
  38.      *
  39.      * @param parser the XML parser positioned at an opening meta-data tag.
  40.      * @return the meta-data.
  41.      * @throws XmlPullParserException if an error occurs while parsing the XML.
  42.      * @throws IOException            if an error occurs while parsing the XML.
  43.      */
  44.     public static Map<String, List<String>> parseMetaData(XmlPullParser parser) throws XmlPullParserException, IOException {
  45.         int eventType = parser.getEventType();

  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.             eventType = parser.nextTag();

  52.             // Keep parsing until we've gotten to end of meta-data.
  53.             while ((eventType != XmlPullParser.END_TAG)
  54.                     || (!parser.getName().equals(MetaData.ELEMENT_NAME))) {
  55.                 String name = parser.getAttributeValue(0);
  56.                 String value = parser.nextText();

  57.                 if (metaData.containsKey(name)) {
  58.                     List<String> values = metaData.get(name);
  59.                     values.add(value);
  60.                 }
  61.                 else {
  62.                     List<String> values = new ArrayList<String>();
  63.                     values.add(value);
  64.                     metaData.put(name, values);
  65.                 }

  66.                 eventType = parser.nextTag();
  67.             }

  68.             return metaData;
  69.         }

  70.         return Collections.emptyMap();
  71.     }

  72.     /**
  73.      * Serializes a Map of String name/value pairs into the meta-data XML format.
  74.      *
  75.      * @param metaData the Map of meta-data as Map&lt;String,List&lt;String>>
  76.      * @return the meta-data values in XML form.
  77.      */
  78.     public static String serializeMetaData(Map<String, List<String>> metaData) {
  79.         StringBuilder buf = new StringBuilder();
  80.         if (metaData != null && metaData.size() > 0) {
  81.             buf.append("<metadata xmlns=\"http://jivesoftware.com/protocol/workgroup\">");
  82.             for (Iterator<String> i = metaData.keySet().iterator(); i.hasNext();) {
  83.                 String key = i.next();
  84.                 List<String> value = metaData.get(key);
  85.                 for (Iterator<String> it = value.iterator(); it.hasNext();) {
  86.                     String v = it.next();
  87.                     buf.append("<value name=\"").append(key).append("\">");
  88.                     buf.append(StringUtils.escapeForXML(v));
  89.                     buf.append("</value>");
  90.                 }
  91.             }
  92.             buf.append("</metadata>");
  93.         }
  94.         return buf.toString();
  95.     }
  96. }