MetaDataFragment.java 19.4 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
package org.jivesoftware.messenger;

import org.jivesoftware.util.XPPWriter;
import java.util.*;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.dom4j.*;

/**
 * <p>Storage of generic meta-data.</p>
 * <p>Meta-data is expected to be stored as XML. We use a simple
 * naming convention of meta-data key names: data is stored
 * heirarchically separated by dots. The last name may contain
 * a colon ':' character that is read as name:attribute.
 * For example setting X.Y.Z to someValue, would map to an XML snippet of:</p>
 * <pre>
 * &lt;X&gt;
 *     &lt;Y&gt;
 *         &lt;Z&gt;someValue&lt;/Z&gt;
 *     &lt;/Y&gt;
 * &lt;/X&gt;
 * </pre>
 * And X.Y.Z:key to anotherValue as:</p>
 * <pre>
 * &lt;X&gt;
 *     &lt;Y&gt;
 *         &lt;Z key="anotherValue" /&gt;
 *     &lt;/Y&gt;
 * &lt;/X&gt;
 * </pre>
 * <p>Some XML cannot be built or accessed using this naming
 * convention (e.g. a typical Roster reset packet). More complex XML
 * packet should be represented using the XMPPDOMFragment. The
 * MetaDataFragment class is designed to provide 80% of XML
 * manipulation capabilities with the simplest 20% of code and API size
 * making it convenient for meta-data, simple IQ packets, etc.</p>
 *
 * @author Iain Shigeoka
 * @see XMPPDOMFragment
 */
public class MetaDataFragment extends PayloadFragment {

    Element root = DocumentHelper.createElement("root");

    /**
     * Parsing the XML DOM every time we need a property is slow. Therefore,
     * we use a Map to cache property values that are accessed more than once.
     */
    private Map propertyCache = new HashMap();

    public void setName(String name) {
        this.name = name;
        ((Element)root.elements().get(0)).setName(name);
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
        QName qName = DocumentHelper.createQName(name, DocumentHelper.createNamespace("", namespace));
        ((Element)root.elements().get(0)).setQName(qName);
    }

    public void send(XMLStreamWriter xmlSerializer, int version) throws XMLStreamException {
        Iterator fragIter = null;
        if (fragments != null && !fragments.isEmpty()) {
            fragIter = getFragments();
        }

        XPPWriter.write((Element)root.elements().get(0), xmlSerializer, fragIter, version);
    }

    public XMPPFragment createDeepCopy() {
        MetaDataFragment meta = null;

        Iterator metaElements = root.elementIterator();
        if (metaElements.hasNext()) {
            meta = new MetaDataFragment((Element)metaElements.next());
            while (metaElements.hasNext()) {
                meta.root.add((Element)metaElements.next());
            }
        }
        else {
            meta = new MetaDataFragment(namespace, name);
        }
        Iterator fragmentIter = getFragments();
        while (fragmentIter.hasNext()) {
            meta.addFragment(((XMPPFragment)fragmentIter.next()).createDeepCopy());
        }

        return meta;
    }

    /**
     * Create an empty meta-data chunk.
     */
    public MetaDataFragment(String namespace, String name) {
        super(namespace, name);
        QName qName = DocumentHelper.createQName(name, DocumentHelper.createNamespace("", namespace));
        root.add(DocumentHelper.createElement(qName));
    }

    /**
     * Create a meta-data chunk based on a DOM tree.
     */
    public MetaDataFragment(Element root) {
        super(root.getNamespaceURI(), root.getName());
        this.root.add((Element)root.clone());
    }

    /**
     * Returns the value of the specified property. A <tt>null</tt> answer does not necessarily mean
Bill Lynch's avatar
Bill Lynch committed
123
     * that the property does not exist. Use {@link #includesProperty(String)} to find out whether
Matt Tucker's avatar
Matt Tucker committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
     * the property exists or not.
     *
     * @param name the name of the property to get.
     * @return the value of the specified property.
     */
    public synchronized String getProperty(String name) {
        String value = (String)propertyCache.get(name);

        if (value == null) {
            String[] propName = parsePropertyName(name);

            // Grab the attribute if there is one
            String lastName = propName[propName.length - 1];
            String attName = null;
            int attributeIndex = lastName.indexOf(':');
            if (attributeIndex >= 0) {
                propName[propName.length - 1] = lastName.substring(0, attributeIndex);
                attName = lastName.substring(attributeIndex + 1);
            }

            // Search for this property by traversing down the XML hierarchy.
            Element element = root;
            for (int i = 0; i < propName.length; i++) {
                element = element.element(propName[i]);
                if (element == null) {
                    break;
                }
            }
            if (element != null) {
                if (attName == null) {
                    value = element.getTextTrim();
                }
                else {
                    value = element.attributeValue(attName);
                }

                // At this point, we found a matching property, so return its value.
                // Empty strings are returned as null.
                if ("".equals(value)) {
                    value = null;
                }
                else {
                    // Add to cache so that getting property next time is fast.
                    propertyCache.put(name, value);
                }
            }
        }
        return value;
    }

    /**
     * Returns true if the specified property is included in the XML hierarchy. A property could
     * have a value associated or not. If the property has an associated value then
Bill Lynch's avatar
Bill Lynch committed
177
     * {@link #getProperty(String)} will return a String otherwise <tt>null</tt> will be answered.
Matt Tucker's avatar
Matt Tucker committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
     *
     * @param name the name of the property to find out.
     * @return true if the specified property is included in the XML hierarchy.
     */
    public synchronized boolean includesProperty(String name) {
        String value = (String)propertyCache.get(name);

        if (value == null) {
            String[] propName = parsePropertyName(name);

            // Grab the attribute if there is one
            String lastName = propName[propName.length - 1];
            String attName = null;
            int attributeIndex = lastName.indexOf(':');
            if (attributeIndex >= 0) {
                propName[propName.length - 1] = lastName.substring(0, attributeIndex);
                attName = lastName.substring(attributeIndex + 1);
            }

            // Search for this property by traversing down the XML hierarchy.
            Element element = root;
            for (int i = 0; i < propName.length; i++) {
                element = element.element(propName[i]);
                if (element == null) {
                    break;
                }
            }

            if (element != null) {
                if (attName == null){
                    // The property exists so return true
                    return true;
                } else {
                    // The property exists if the attribute exists in the element
                    return element.attribute(attName) != null;
                }
            }
            else {
                // The property does not exist so return false
                return false;
            }
        }
        return true;
    }

    /**
     * Return all values who's path matches the given property name as a String array,
     * or an empty array if the if there are no children. You MAY NOT use the atttribute
     * markup (using a ':' in the last element name) with this call.
     * <p/>
     * getProperties() allows you to retrieve several values with the same property name.
     * For example, consider the XML file entry:
     * <pre>
     * &lt;foo&gt;
     *     &lt;bar&gt;
     *         &lt;prop&gt;some value&lt;/prop&gt;
     *         &lt;prop&gt;other value&lt;/prop&gt;
     *         &lt;prop&gt;last value&lt;/prop&gt;
     *     &lt;/bar&gt;
     * &lt;/foo&gt;
     * </pre>
     * If you call getProperties("foo.bar.prop") will return a string array containing
     * {"some value", "other value", "last value"}.
     *
     * @param name the name of the property to retrieve
     * @return all child property values for the given node name.
     */
    public String[] getProperties(String name) {
        String[] propName = parsePropertyName(name);

        // Search for this property by traversing down the XML heirarchy, stopping one short.
        Element element = root;
        for (int i = 0; i < propName.length - 1; i++) {
            element = element.element(propName[i]);
            if (element == null) {
                // This node doesn't match this part of the property name which
                // indicates this property doesn't exist so return empty array.
                return new String[]{};
            }
        }
        // We found matching property, return names of children.
        Iterator iter = element.elementIterator(propName[propName.length - 1]);
        ArrayList props = new ArrayList();
        while (iter.hasNext()) {
            Element e = (Element)iter.next();
            props.add(e.getName());
        }
        String[] childrenNames = new String[props.size()];
        return (String[])props.toArray(childrenNames);
    }

    /**
     * Sets a property to an array of values.  You MAY NOT use the atttribute
     * markup (using a ':' in the last element name) with this call. Multiple values matching the
     * same property is mapped to an XML file as multiple elements containing each value.
     * For example, using the name "foo.bar.prop", and the value string array containing
     * {"some value", "other value", "last value"} would produce the following XML:
     * <pre>
     * &lt;foo&gt;
     *     &lt;bar&gt;
     *         &lt;prop&gt;some value&lt;/prop&gt;
     *         &lt;prop&gt;other value&lt;/prop&gt;
     *         &lt;prop&gt;last value&lt;/prop&gt;
     *     &lt;/bar&gt;
     * &lt;/foo&gt;
     * </pre>
     *
     * @param name   the name of the property.
     * @param values The array of values for the property (can be empty but not null)
     */
    public void setProperties(String name, String[] values) {
        String[] propName = parsePropertyName(name);
        setProperty(name, values[0]);
        // Search for this property by traversing down the XML heirarchy, stopping one short.
        Element element = root;
        for (int i = 0; i < propName.length - 1; i++) {
            element = element.element(propName[i]);
            if (element == null) {
                // This node doesn't match this part of the property name which
                // indicates this property doesn't exist so return empty array.
                return;
            }
        }
        String childName = propName[propName.length - 1];
        // We found matching property, clear all children.
        List toRemove = new ArrayList();
        Iterator iter = element.elementIterator(childName);
        while (iter.hasNext()) {
            toRemove.add(iter.next());
        }
        for (iter = toRemove.iterator(); iter.hasNext();) {
            element.remove((Element)iter.next());
        }
        // Add the new children
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                element.addElement(childName).setText(values[i]);
            }
        }
    }

    /**
     * Return all children property names of a parent property as a String array,
     * or an empty array if the if there are no children. You MAY NOT use the atttribute
     * markup (using a ':' in the last element name) with this call.
     * For example, given the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then
     * the child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and
     * <tt>C</tt>.
     *
     * @param parent the name of the parent property.
     * @return all child property values for the given parent.
     */
    public String[] getChildrenProperties(String parent) {
        String[] propName = parsePropertyName(parent);

        // Search for this property by traversing down the XML heirarchy.
        Element element = root;
        for (int i = 0; i < propName.length; i++) {
            element = element.element(propName[i]);
            if (element == null) {
                // This node doesn't match this part of the property name which
                // indicates this property doesn't exist so return empty array.
                return new String[]{};
            }
        }
        // We found matching property, return names of children.
        List children = element.elements();
        int childCount = children.size();
        String[] childrenNames = new String[childCount];
        for (int i = 0; i < childCount; i++) {
            childrenNames[i] = ((Element)children.get(i)).getName();
        }
        return childrenNames;
    }

    /**
     * Returns all recursive children of the given parent property or an empty string array
     * if no children exist. The list of children is depth-first so the array is optimized
     * for easy displaying.
     *
     * @param parent the parent property.
     * @return all recursive children of the given property in depth-first order or an empty
     *         string array if no children exist.
     */
    public String[] getRecursiveChildrenProperties(String parent) {
        String[] properties = getChildrenProperties(parent);
        if (properties.length == 0) {
            return properties;
        }
        else {
            List list = new ArrayList(15);
            for (int i = 0; i < properties.length; i++) {
                String propName = parent + "." + properties[i];
                list.add(propName);
                list.addAll(Arrays.asList(getRecursiveChildrenProperties(propName)));
            }
            return (String[])list.toArray(new String[]{});
        }
    }

    /**
     * Sets the value of the specified property. If the property doesn't
     * currently exist, it will be automatically created.
     *
     * @param name  the name of the property to set.
     * @param value the new value for the property.
     */
    public synchronized void setProperty(String name, String value) {
        if (name == null) return;
        if (value == null) value = "";

        // Set cache correctly with prop name and value.
        propertyCache.put(name, value);

        String[] propName = parsePropertyName(name);

        // Search for this property by traversing down the XML heirarchy.
        Element element = root;
        for (int i = 0; i < propName.length - 1; i++) {
            // If we don't find this part of the property in the XML heirarchy
            // we add it as a new node
            if (element.element(propName[i]) == null) {
                element.addElement(propName[i]);
            }
            element = element.element(propName[i]);
        }
        String lastName = propName[propName.length - 1];
        int attributeIndex = lastName.indexOf(':');
        if (attributeIndex >= 0) {
            String eleName = lastName.substring(0, attributeIndex);
            String attName = lastName.substring(attributeIndex + 1);
            // If we don't find this part of the property in the XML heirarchy
            // we add it as a new node
            if (element.element(eleName) == null) {
                element.addElement(eleName);
            }
            element.element(eleName).addAttribute(attName, value);
        }
        else {
            // If we don't find this part of the property in the XML heirarchy
            // we add it as a new node
            if (element.element(lastName) == null) {
                element.addElement(lastName);
            }
            // Set the value of the property in this node.
            element.element(lastName).setText(value);
        }
    }

    /**
     * Deletes the specified property. You may use the atttribute markup (using a ':' in the last
     * element name) with this call. This method removes both the containing text, and the
     * element itself along with any attributes associated with that element.</p>
     *
     * @param name the property to delete.
     */
    public synchronized void deleteProperty(String name) {
        // Remove property from cache.
        propertyCache.remove(name);

        String[] propName = parsePropertyName(name);

        // Search for this property by traversing down the XML heirarchy.
        Element element = root;
        for (int i = 0; i < propName.length - 1; i++) {
            element = element.element(propName[i]);
            // Can't find the property so return.
            if (element == null) {
                return;
            }
        }
        String lastName = propName[propName.length - 1];
        int attributeIndex = lastName.indexOf(':');
        if (attributeIndex >= 0) {
            String eleName = lastName.substring(0, attributeIndex);
            String attName = lastName.substring(attributeIndex + 1);
            Attribute attrib = element.element(eleName).attribute(attName);
            if (attrib != null) {
                element.element(eleName).remove(attrib);
            }
        }
        else {
            // Found the correct element to remove, so remove it...
            element.remove(element.element(lastName));
        }
    }

    /**
     * Returns an array representation of the given Jive property. Jive
     * properties are always in the format "prop.name.is.this" which would be
     * represented as an array of four Strings.
     *
     * @param name the name of the Jive property.
     * @return an array representation of the given Jive property.
     */
    private String[] parsePropertyName(String name) {
        List propName = new ArrayList(5);
        // Use a StringTokenizer to tokenize the property name.
        StringTokenizer tokenizer = new StringTokenizer(name, ".");
        while (tokenizer.hasMoreTokens()) {
            propName.add(tokenizer.nextToken());
        }
        return (String[])propName.toArray(new String[propName.size()]);
    }

    /**
     * <p>Tries to convert any given fragment into a metadata fragment.</p>
     *
     * @param fragment The fragment to convert
     * @return The converted fragment or null if the fragment could not be converted
     */
    public static MetaDataFragment convertToMetaData(XMPPFragment fragment) {
        MetaDataFragment meta = null;
        if (fragment instanceof MetaDataFragment) {
            meta = (MetaDataFragment)fragment;
        }
        else if (fragment instanceof XMPPDOMFragment) {
            XMPPDOMFragment dom = (XMPPDOMFragment)fragment;
            meta = new MetaDataFragment(dom.getRootElement());
            Iterator frags = dom.getFragments();
            while (frags.hasNext()) {
                meta.addFragment((XMPPFragment)frags.next());
            }
        }
        else {
            // TODO: as a last resort, should read the fragment in using an empty serializer and rebuild the dom tree
            throw new IllegalArgumentException();
        }
        return meta;
    }

    /**
     * <p>Tries to convert this fragment into a metadata fragment.</p>
     *
     * @return The converted fragment
     */
    public XMPPDOMFragment convertToDOMFragment() {
        XMPPDOMFragment dom = new XMPPDOMFragment(root.createCopy());
        return dom;
    }
}