You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by ke...@apache.org on 2010/07/16 21:32:59 UTC
svn commit: r964916 - in /incubator/oodt/trunk/metadata/src:
main/java/org/apache/oodt/cas/metadata/ test/org/apache/oodt/cas/metadata/
Author: kelly
Date: Fri Jul 16 19:32:59 2010
New Revision: 964916
URL: http://svn.apache.org/viewvc?rev=964916&view=rev
Log:
"pge" needed even more stuff out of "metadata", so bring in updated implementations and tests of Metadata and SerializableMetadata
Modified:
incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/Metadata.java
incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/SerializableMetadata.java
incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestMetadata.java
incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestSerializableMetadata.java
Modified: incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/Metadata.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/Metadata.java?rev=964916&r1=964915&r2=964916&view=diff
==============================================================================
--- incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/Metadata.java (original)
+++ incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/Metadata.java Fri Jul 16 19:32:59 2010
@@ -1,491 +1,579 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
package org.apache.oodt.cas.metadata;
//JDK imports
-import java.util.Iterator;
-import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Hashtable;
-import java.util.Vector;
-import java.util.Map;
import java.util.List;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.NodeList;
-import org.xml.sax.InputSource;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import java.net.URLEncoder;
-import java.io.UnsupportedEncodingException;
-import java.io.InputStream;
-
-//OODT imports
-import org.apache.oodt.commons.xml.XMLUtils;
+import java.util.Map;
+import java.util.Stack;
+import java.util.StringTokenizer;
+import java.util.Vector;
/**
+ *
+ * Metadata is a {@link Map} of <code>String</code> keys mapped to Object
+ * values. So, each key can map to potentially many values, but also can map to
+ * null, or to a single value.
+ *
* @author mattmann
+ * @author bfoster
* @version $Revision$
*
- * <p>
- * Metadata is a {@link Map} of <code>String</code> keys mapped to
- * <code>Vector</code> values. So, each key can map to potentially many
- * values, but also can map to null, or to a single value.
- * </p>
- *
*/
public class Metadata {
- /* the map of elementName=>Elements */
- protected Map elementMap = null;
+ private Group root;
- /* our log stream */
- protected static final Logger LOG = Logger.getLogger(Metadata.class
- .getName());
-
- /**
- * <p>
- * Constructs a new Metadata
- * </p>
- */
- public Metadata() {
- elementMap = new Hashtable();
- }
-
- /**
- * @deprecated
- * <p>
- * Constructs a new Metadata from a given InputStream.
- *
- * @param is
- * The InputStream to read.
- */
- public Metadata(InputStream is) throws Exception {
- if (is == null) {
- throw new Exception(
- "Unable to parse metadata stream: stream not set!");
- }
-
- try {
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- factory.setNamespaceAware(true);
- DocumentBuilder parser = factory.newDocumentBuilder();
- elementMap = new Hashtable();
- parse(parser.parse(new InputSource(is)));
- } catch (Exception e) {
- throw new Exception("Unable to parse metadata stream.", e);
- }
- }
-
- /**
- * <p>
- * Merges the existing Hashtable of metadata with the specified new
- * Hashtable of metadata.
- * </p>
- *
- * @param metadata
- * The metadata to merge the internal metadata with.
- */
- public void addMetadata(Hashtable metadata) {
- addMetadata(metadata, false);
- }
-
- /**
- * <p>
- * Merges the existing Hashtable of metadata with the specified new
- * Hashtable of metadata, replacing an existing metadata value if a new
- * metadata value with same key is specified, if the replace parameter is
- * set to true.
- *
- * Otherwise, this function behaves the same as
- * {@link #addMetadata(Hashtable)}.
- * </p>
- *
- * @param metadata
- * The metadata to merge the internal metadata with.
- * @param replace
- * If set to true, then an existing metadata value is replaced if
- * a new metadata value with the same key is specified.
- * Otherwise, if set to false then this function behaves as
- * {@link #addMetadata(Hashtable)}.
- * @since OODT-143
- */
- public void addMetadata(Hashtable metadata, boolean replace) {
- // iterate through the keys and add values whereever possible, otherwise
- // replace them
- for (Iterator i = metadata.keySet().iterator(); i.hasNext();) {
- String key = (String) i.next();
- List values = null;
-
- if (elementMap.get(key) != null) {
- values = (List) elementMap.get(key);
- if (replace) {
- values.clear();
- }
- } else {
- values = new Vector();
- elementMap.put(key, values);
- }
-
- if (metadata.get(key) != null) {
- Object val = metadata.get(key);
-
- if (val instanceof List) {
- values.addAll((List) val);
- } else if (val instanceof String) {
- values.add((String) val);
- }
- }
- }
- }
-
- /**
- * Replace our metadata with all metadata in the given metadata.
- * Keys in our metadata will be overwritten by the given's. New
- * keys that don't yet exist in ours will get added.
- *
- * @param metadata Metadata from which to replace our own.
- */
- public void replaceMetadata(Metadata metadata) {
- this.replaceMetadata(new Hashtable(metadata.elementMap));
- }
-
- /**
- * <p>
- * Replaces the internal hashtable of metadata with the specified
- * {@link Hashtable}.
- * </p>
- *
- * @param metadata
- * The metadata to replace the existing internal {@Hashtable}
- * with.
- */
- public void replaceMetadata(Hashtable metadata) {
- elementMap = new Hashtable();
- for (Iterator i = metadata.keySet().iterator(); i.hasNext();) {
- String key = (String) i.next();
- Object val = metadata.get(key);
-
- List values = new Vector();
- if (val instanceof String) {
- values.add((String) val);
- } else if (val instanceof List) {
- values.addAll((List) val);
- }
-
- elementMap.put(key, values);
- }
- }
-
- /**
- * <p>
- * Adds a single String value to this metadata key. If the key already
- * exists, this value is appended to that list of metadata values. If the
- * key doesn't exist yet, a new list of metadata values is created and then
- * this value is inserted first, in order.
- * </p>
- *
- * @param key
- * The key to add the value for.
- * @param value
- * The value to add.
- */
- public void addMetadata(String key, String value) {
- if (elementMap.get(key) != null) {
- // append this value to the existing key
- List values = (List) elementMap.get(key);
- values.add(value);
- } else {
- List values = new Vector();
- values.add(value);
- elementMap.put(key, values);
- }
- }
-
- /**
- * <p>
- * Adds a list of values to this metadata key. If the key already exists
- * then the list of values is appended to the existing list of values for
- * the key, in order. If the key doesn't exist, the list of String values
- * are set as the value list for this key.
- * </p>
- *
- * @param key
- * The key to set the values for.
- * @param values
- * The ordered list of values for this String key. Values must be
- * basic Strings.
- */
- public void addMetadata(String key, List values) {
- if (elementMap.get(key) != null) {
- // get the existing values
- List existingValues = (List) elementMap.get(key);
- existingValues.addAll(values);
- } else {
- elementMap.put(key, values);
- }
- }
-
- /**
- * <p>
- * Removes the existing value list for the specified <code>key</code>,
- * and replaces it with the specified {@link List} of <code>values</code>.
- * </p>
- *
- * @param key
- * The key to replace the metadata for.
- * @param values
- * The new metadata values for this key.
- */
- public void replaceMetadata(String key, List values) {
- Object value = removeMetadata(key);
- value = null;
- elementMap.put(key, values);
- }
-
- /**
- * <p>
- * Removes the existing value list for the specified <code>key</code>,
- * and replaces it with the specified String value.
- * </p>
- *
- * @param key
- * The key to replace the metadata for.
- * @param value
- * The new metadata value for this key.
- */
- public void replaceMetadata(String key, String value) {
- Object val = removeMetadata(key);
- val = null;
- List values = new Vector();
- values.add(value);
- elementMap.put(key, values);
- }
-
- /**
- * <p>
- * Removes the value of the specified metadata key.
- * </p>
- *
- * @param key
- * The key to remove the value from.
- * @return The removed value.
- */
- public Object removeMetadata(String key) {
- return elementMap.remove(key);
- }
-
- /**
- * <p>
- * Gets all metadata String values mapped to the specified <code>key</code>.
- * </p>
- *
- * @param key
- * The key to obtain multi-valued metadata for.
- * @return all metadata String values mapped to the specified
- * <code>key</code>.
- */
- public List getAllMetadata(String key) {
- if (elementMap.get(key) != null) {
- return (List) elementMap.get(key);
- } else
- return null;
- }
-
- /**
- * <p>
- * Gets a single <code>String</code> value mapped to the specified
- * <code>key</code>.
- * </p>
- *
- * @param key
- * The key to obtain the single valued metadata for.
- * @return A single <code>String</code> value mapped to the specified
- * <code>key</code>.
- */
- public String getMetadata(String key) {
- if (elementMap.get(key) != null) {
- List values = (List) elementMap.get(key);
- if (values.size() == 0) {
- return null;
- } else {
- return (String) values.get(0);
- }
- } else
- return null;
- }
-
- /**
- *
- * @return The internal {@link Hashtable} representation of the metadata.
- */
- public Hashtable getHashtable() {
- return (Hashtable) elementMap;
- }
-
- /**
- * <p>
- * Test for existence of the specified key in the metadata element map.
- * </p>
- *
- * @param key
- * The key to check for existance of.
- * @return True if the key exists in the Metadata, false otherwise.
- */
- public boolean containsKey(String key) {
- return elementMap.containsKey(key);
- }
-
- /**
- * <p>
- * Checks to see whether or not a particular key is a multi-valued key
- * field.
- * </p>
- *
- * @param key
- * The key to perform the multi-valued check on.
- * @return True if the key is multi-valued, False, otherwise.
- */
- public boolean isMultiValued(String key) {
- if (elementMap.get(key) != null) {
- List values = (List) elementMap.get(key);
-
- if (values != null) {
- return values.size() > 1;
- } else
- return false;
- } else
+ public Metadata() {
+ this.root = this.createNewRoot();
+ }
+
+ public Metadata(Metadata metadata) {
+ this();
+ this.addMetadata(metadata);
+ }
+
+ /**
+ * Adds (Appends if key exists) from given metadata into this metadata
+ *
+ * @param metadata
+ * Metadata to add metadata from
+ */
+ public void addMetadata(Metadata metadata) {
+ for (String key : metadata.getAllKeys())
+ this.addMetadata(key, metadata.getAllMetadata(key));
+ }
+
+ public void addMetadata(String group, Metadata metadata) {
+ if (group == null) {
+ this.addMetadata(metadata);
+ } else {
+ for (String key : metadata.getAllKeys())
+ this.addMetadata(group + "/" + key, metadata.getAllMetadata(key));
+ }
+ }
+
+ /**
+ * Adds (Replaces if key exists) from given Metadata into this Metadata
+ *
+ * @param metadata
+ */
+ public void replaceMetadata(Metadata metadata) {
+ for (String key : metadata.getAllKeys())
+ this.replaceMetadata(key, metadata.getAllMetadata(key));
+ }
+
+ public void replaceMetadata(String group, Metadata metadata) {
+ if (group == null) {
+ this.replaceMetadata(metadata);
+ } else {
+ for (String key : metadata.getAllKeys())
+ this.replaceMetadata(group + "/" + key, metadata.getAllMetadata(key));
+ }
+ }
+
+ /**
+ * Adds key (Appends if key exists)
+ *
+ * @param key
+ * Key to be added
+ * @param value
+ * Value of key to be added
+ */
+ public void addMetadata(String key, String value) {
+ this.getGroup(key).addValue(value);
+ }
+
+ /**
+ * Adds key (Replaces if key exists)
+ *
+ * @param key
+ * Key to be added
+ * @param value
+ * Value of key to be added
+ */
+ public void replaceMetadata(String key, String value) {
+ this.getGroup(key).setValue(value);
+ }
+
+ /**
+ * Adds key (Appends if key exists)
+ *
+ * @param key
+ * Key to be added
+ * @param values
+ * Values of key to be added
+ */
+ public void addMetadata(String key, List<String> values) {
+ this.getGroup(key).addValues(values);
+ }
+
+ /**
+ * Adds key (Replaces if key exists)
+ *
+ * @param key
+ * Key to be added
+ * @param values
+ * Values of key to be added
+ */
+ public void replaceMetadata(String key, List<String> values) {
+ this.getGroup(key).setValues(values);
+ }
+
+ /**
+ * Removes key
+ *
+ * @param key
+ * Key to remove
+ */
+ public void removeMetadata(String key) {
+ Group removeGroup = this.getGroup(key, false);
+ if (removeGroup != null && removeGroup.hasValues()) {
+ if (removeGroup.getChildren().size() > 0)
+ removeGroup.clearValues();
+ else
+ removeGroup.getParent().removeChild(removeGroup.getName());
+ }
+ }
+
+ /**
+ * Removes key
+ *
+ * @param key
+ * Key to remove
+ */
+ public void removeMetadataGroup(String group) {
+ Group removeGroup = this.getGroup(group, false);
+ if (removeGroup != null && removeGroup.getChildren().size() > 0)
+ removeGroup.getParent().removeChild(removeGroup.getName());
+ }
+
+ /**
+ * Checks if keys exists
+ *
+ * @param key
+ * Key to check for
+ * @return True if key exists, false otherwise
+ */
+ public boolean containsKey(String key) {
+ Group group = this.getGroup(key, false);
+ return group != null && group.hasValues();
+ }
+
+ /**
+ * Checks if key has more than one value
+ *
+ * @param key
+ * Key to check for
+ * @return True is key exists and has more than one value, false otherwise
+ */
+ public boolean isMultiValued(String key) {
+ Group group = this.getGroup(key, false);
+ return group != null && group.getValues().size() > 1;
+ }
+
+ /**
+ * Creates a Metadata from the given group
+ *
+ * @param group
+ * The Group to grab
+ * @return Metadata containing group and all keys below it
+ */
+ public Metadata getSubMetadata(String group) {
+ Metadata m = new Metadata();
+ Group newRoot = this.getGroup(group, false);
+ if (newRoot != null)
+ m.root.addChildren(newRoot.clone().getChildren());
+ return m;
+ }
+
+ /**
+ * Gets the first value for the given key
+ *
+ * @param key
+ * The key for which the first value will be returned
+ * @return First value for given key, or null if key does not exist
+ */
+ public String getMetadata(String key) {
+ Group group = this.getGroup(key, false);
+ if (group != null)
+ return group.getValue();
+ else
+ return null;
+ }
+
+ /**
+ * Gets all values for give key
+ *
+ * @param key
+ * The key for which all values will be return
+ * @return All values for given key, or null if key does not exist
+ */
+ public List<String> getAllMetadata(String key) {
+ Group group = this.getGroup(key, false);
+ if (group != null)
+ return new Vector<String>(group.getValues());
+ else
+ return null;
+ }
+
+ /**
+ * Gets All key in and below given group
+ *
+ * @param group
+ * The group in question
+ * @return All keys for the given group and below
+ */
+ public List<String> getKeys(String group) {
+ Group foundGroup = this.getGroup(group);
+ if (foundGroup != null)
+ return this.getKeys(foundGroup);
+ else
+ return new Vector<String>();
+ }
+
+ /**
+ * Gets all keys in this Metadata
+ *
+ * @return All keys in this Metadata
+ */
+ public List<String> getKeys() {
+ return this.getKeys(this.root);
+ }
+
+ protected List<String> getKeys(Group group) {
+ Vector<String> keys = new Vector<String>();
+ for (Group child : group.getChildren())
+ if (child.hasValues())
+ keys.add(child.getFullPath());
+ return keys;
+ }
+
+ /**
+ * Gets All key in and below given group
+ *
+ * @param group
+ * The group in question
+ * @return All keys for the given group and below
+ */
+ public List<String> getAllKeys(String group) {
+ Group foundGroup = this.getGroup(group);
+ if (foundGroup != null)
+ return this.getAllKeys(foundGroup);
+ else
+ return new Vector<String>();
+ }
+
+ /**
+ * Gets all keys in this Metadata
+ *
+ * @return All keys in this Metadata
+ */
+ public List<String> getAllKeys() {
+ return this.getAllKeys(this.root);
+ }
+
+ protected List<String> getAllKeys(Group group) {
+ Vector<String> keys = new Vector<String>();
+ for (Group child : group.getChildren()) {
+ if (child.hasValues())
+ keys.add(child.getFullPath());
+ keys.addAll(this.getAllKeys(child));
+ }
+ return keys;
+ }
+
+ /**
+ * Get all keys whose leaf key name is equal to the given arg
+ * @param key leaf key name
+ * @return list of keys with the given leaf key name
+ */
+ public List<String> getAllKeysWithName(String key) {
+ List<String> keys = new Vector<String>();
+ Stack<Group> stack = new Stack<Group>();
+ stack.add(this.root);
+ while (!stack.empty()) {
+ Group curGroup = stack.pop();
+ if (curGroup.getName().equals(key) && curGroup.hasValues())
+ keys.add(curGroup.getFullPath());
+ stack.addAll(curGroup.getChildren());
+ }
+ return keys;
+ }
+
+ /**
+ * Gets Values in root group
+ *
+ * @return All Values in root group
+ */
+ public List<String> getValues() {
+ Vector<String> values = new Vector<String>();
+ for (String key : this.getKeys())
+ values.addAll(this.getAllMetadata(key));
+ return values;
+ }
+
+ /**
+ * Gets values in given group
+ *
+ * @param group
+ * Group in question
+ * @return Values in given group
+ */
+ public List<String> getValues(String group) {
+ Vector<String> values = new Vector<String>();
+ for (String key : this.getKeys(group))
+ values.addAll(this.getAllMetadata(key));
+ return values;
+ }
+
+ /**
+ * Gets all values in this Metadata
+ *
+ * @return All values in this Metadata
+ */
+ public List<String> getAllValues() {
+ Vector<String> values = new Vector<String>();
+ for (String key : this.getAllKeys())
+ values.addAll(this.getAllMetadata(key));
+ return values;
+ }
+
+ /**
+ * Gets All values in and below given group
+ *
+ * @param group
+ * Group in question
+ * @return All values in and below given group
+ */
+ public List<String> getAllValues(String group) {
+ Vector<String> values = new Vector<String>();
+ for (String key : this.getAllKeys(group))
+ values.addAll(this.getAllMetadata(key));
+ return values;
+ }
+
+ public void addMetadata(Hashtable<String, Object> metadata) {
+ addMetadata(metadata, false);
+ }
+
+ /**
+ * Takes a hashtable of String keys and Object values. Values of type List
+ * must be a List of Strings; all other values will have its toString() method
+ * invoked.
+ * @param metadata Hashtable based metadata to add
+ * @param replace If true, existing keys will be replaced, other values will be
+ * combined.
+ */
+ public void addMetadata(Hashtable<String, Object> metadata, boolean replace) {
+ // for back compat: the old method allowed us to give it a
+ // Hashtable<String,String> and it still worked
+ for (String key : metadata.keySet()) {
+ List<String> vals = (metadata.get(key) instanceof List) ? (List<String>) metadata
+ .get(key)
+ : Collections.singletonList(metadata.get(key).toString());
+ if (replace) {
+ this.replaceMetadata(key, vals);
+ } else {
+ this.addMetadata(key, vals);
+ }
+ }
+ }
+
+ public void replaceMetadata(Hashtable<String, Object> metadata) {
+ this.root = this.createNewRoot();
+ this.addMetadata(metadata);
+ }
+
+ public boolean containsGroup(String group) {
+ return this.getGroup(group, false) != null;
+ }
+
+ public List<String> getGroups() {
+ return this.getGroups(this.root);
+ }
+
+ public List<String> getGroups(String group) {
+ return this.getGroups(this.getGroup(group));
+ }
+
+ protected List<String> getGroups(Group group) {
+ Vector<String> groupNames = new Vector<String>();
+ for (Group child : group.getChildren())
+ groupNames.add(child.getName());
+ return groupNames;
+ }
+
+ protected Group getGroup(String key) {
+ return getGroup(key, true);
+ }
+
+ protected Group getGroup(String key, boolean create) {
+ if (key == null)
+ return this.root;
+ StringTokenizer tokenizer = new StringTokenizer(key, "/");
+ Group curGroup = this.root;
+ while (tokenizer.hasMoreTokens()) {
+ String groupName = tokenizer.nextToken();
+ Group childGroup = curGroup.getChild(groupName);
+ if (childGroup == null) {
+ if (!create)
+ return null;
+ childGroup = new Group(groupName);
+ curGroup.addChild(childGroup);
+ }
+ curGroup = childGroup;
+ }
+ return curGroup;
+ }
+
+ protected Group createNewRoot() {
+ return new Group(Group.ROOT_GROUP_NAME);
+ }
+
+ protected class Group {
+
+ private static final String ROOT_GROUP_NAME = "root";
+
+ private String name;
+ private List<String> values;
+ private Group parent;
+ private Map<String, Group> children;
+
+ public Group(String name) {
+ this.name = name;
+ this.values = new Vector<String>();
+ this.children = new HashMap<String, Group>();
+ }
+
+ public String getName() {
+ return this.name;
+ }
+
+ public String getFullPath() {
+ if (this.parent != null && !this.parent.getName().equals(ROOT_GROUP_NAME))
+ return this.parent.getFullPath() + "/" + this.name;
+ else
+ return this.name;
+ }
+
+ public Group getParent() {
+ return this.parent;
+ }
+
+ public void setValue(String value) {
+ this.values.clear();
+ this.values.add(value);
+ }
+
+ public void setValues(List<String> values) {
+ this.values.clear();
+ this.values.addAll(values);
+ }
+
+ public void clearValues() {
+ this.values.clear();
+ }
+
+ public void addValue(String value) {
+ this.values.add(value);
+ }
+
+ public void addValues(List<String> value) {
+ this.values.addAll(value);
+ }
+
+ public boolean hasValues() {
+ return this.values.size() > 0;
+ }
+
+ public String getValue() {
+ if (this.hasValues())
+ return this.values.get(0);
+ else
+ return null;
+ }
+
+ public List<String> getValues() {
+ return this.values;
+ }
+
+ public void addChild(Group child) {
+ this.children.put(child.getName(), child);
+ child.parent = this;
+ }
+
+ public void addChildren(List<Group> children) {
+ for (Group child : children)
+ this.addChild(child);
+ }
+
+ public List<Group> getChildren() {
+ return new Vector<Group>(this.children.values());
+ }
+
+ public void removeChild(String name) {
+ this.children.remove(name);
+ }
+
+ public Group getChild(String name) {
+ return this.children.get(name);
+ }
+
+ @Override
+ public Group clone() {
+ Group clone = new Group(this.name);
+ clone.setValues(this.values);
+ for (Group child : this.children.values())
+ clone.addChild(child.clone());
+ return clone;
+ }
+
+ @Override
+ public String toString() {
+ return this.getFullPath();
+ }
+
+ }
+
+ public Hashtable<String, Object> getHashtable() {
+ Hashtable<String, Object> table = new Hashtable<String, Object>();
+ for (String key : this.getAllKeys())
+ table.put(key, this.getAllMetadata(key));
+ return table;
+ }
+
+ public boolean equals(Object obj) {
+ if (obj instanceof Metadata) {
+ Metadata compMet = (Metadata) obj;
+ if (this.getKeys().equals(compMet.getKeys())) {
+ for (String key : this.getKeys())
+ if (!this.getAllMetadata(key).equals(compMet.getAllMetadata(key)))
return false;
- }
-
- public boolean equals(Object obj) {
- if (obj instanceof Metadata) {
- return ((Metadata) obj).elementMap.size() == this.elementMap.size()
- && ((Metadata) obj).elementMap.entrySet().containsAll(this.elementMap.entrySet());
- }else
- return false;
- }
-
- /**
- * @deprecated
- * <p>
- * @see org.apache.oodt.cas.metadata.SerializableMetadata for new toXML usage.
- * Returns an XML representation of this Metadata as an
- * <code>org.w3c.Document</code>.
- * </p>
- *
- * @return an XML representation of this Metadata as an
- * <code>org.w3c.Document</code>.
- * @throws Exception
- * If any error occurs.
- */
- public Document toXML() throws Exception {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(true);
- Document document = null;
-
- try {
- DocumentBuilder builder = factory.newDocumentBuilder();
- document = builder.newDocument();
-
- Element root = (Element) document.createElement("cas:metadata");
- // FIXME: change namespace URI?
- root.setAttribute("xmlns:cas", "http://oodt.jpl.nasa.gov/1.0/cas");
- document.appendChild(root);
-
- // now add the set of metadata elements in the properties object
- for (Iterator i = elementMap.keySet().iterator(); i.hasNext();) {
- String elemName = null;
-
- try {
- elemName = (String) i.next();
- List elemValues = (List) elementMap.get(elemName);
-
- Element metadataElem = document.createElement("keyval");
- Element keyElem = document.createElement("key");
- keyElem.appendChild(document.createTextNode(URLEncoder
- .encode(elemName, "UTF-8")));
-
- metadataElem.appendChild(keyElem);
-
- String type = "scalar";
-
- if (elemValues.size() > 1) {
- type = "vector";
- }
-
- metadataElem.setAttribute("type", type);
-
- for (Iterator j = elemValues.iterator(); j.hasNext();) {
- String elemValue = (String) j.next();
- Element valElem = document.createElement("val");
- if (elemValue == null) {
- throw new Exception("Attempt to write null value "
- + "for property: [" + elemName
- + "]: val: [" + elemValue + "]");
- }
- valElem.appendChild(document.createTextNode(URLEncoder
- .encode(elemValue, "UTF-8")));
- metadataElem.appendChild(valElem);
- }
- root.appendChild(metadataElem);
- } catch (UnsupportedEncodingException e) {
- LOG.log(Level.WARNING, "Error encoding metadata "
- + elementMap + " to xml file!");
- } catch (Exception e) {
- e.printStackTrace();
- throw e;
- }
-
- }
-
- } catch (ParserConfigurationException pce) {
- LOG.log(Level.WARNING, "Error generating metadata xml file!: "
- + pce.getMessage());
- throw new Exception("Error generating metadata xml file!: "
- + pce.getMessage());
- }
-
- return document;
- }
-
- /**
- * @deprecated
- * @param document
- * @throws Exception
- */
- private void parse(Document document) throws Exception {
-
- Element root = document.getDocumentElement();
-
- NodeList keyValElems = root.getElementsByTagName("keyval");
-
- for (int i = 0; i < keyValElems.getLength(); i++) {
- Element keyValElem = (Element) keyValElems.item(i);
-
- String elemName = XMLUtils.read(keyValElem, "key");
- List elemValues = XMLUtils.readMany(keyValElem, "val");
- elementMap.put(elemName, elemValues);
- }
+ return true;
+ } else {
+ return false;
+ }
+ } else {
+ return false;
}
+ }
}
Modified: incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/SerializableMetadata.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/SerializableMetadata.java?rev=964916&r1=964915&r2=964916&view=diff
==============================================================================
--- incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/SerializableMetadata.java (original)
+++ incubator/oodt/trunk/metadata/src/main/java/org/apache/oodt/cas/metadata/SerializableMetadata.java Fri Jul 16 19:32:59 2010
@@ -1,20 +1,17 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
package org.apache.oodt.cas.metadata;
@@ -26,8 +23,6 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.net.URLEncoder;
-import java.util.Hashtable;
-import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
@@ -61,7 +56,13 @@ public class SerializableMetadata extend
private String xmlEncoding;
private boolean useCDATA;
-
+
+ public SerializableMetadata() {
+ super();
+ this.xmlEncoding = "UTF-8";
+ this.useCDATA = false;
+ }
+
/**
* Accepts any encoding which is supported by java.net.URLEncoder If
* useCDATA is set true then element text will be wrapped in a CDATA tag.
@@ -83,18 +84,22 @@ public class SerializableMetadata extend
this.useCDATA = useCDATA;
}
- /**
- * @deprecated
- *
- * @see org.apache.oodt.cas.metadata.Metadata#init(java.io.InputStream)
- */
- public SerializableMetadata(InputStream is, String xmlEncoding,
- boolean useCDATA) throws Exception {
- super(is);
+ public SerializableMetadata(Metadata metadata) {
+ this(metadata, "UTF-8", false);
+ }
+
+ public SerializableMetadata(InputStream inputStream) throws IOException {
+ this(inputStream, "UTF-8", false);
+ }
+
+ public SerializableMetadata(InputStream inputStream, String xmlEncoding,
+ boolean useCDATA) throws IOException {
this.xmlEncoding = xmlEncoding;
this.useCDATA = useCDATA;
+ this.loadMetadataFromXmlStream(inputStream);
}
+
/**
* Builds a SerializableMetadata object from a Metadata object
*
@@ -103,7 +108,7 @@ public class SerializableMetadata extend
*/
public SerializableMetadata(Metadata metadata, String xmlEncoding,
boolean useCDATA) {
- this.replaceMetadata(metadata.getHashtable());
+ this.replaceMetadata(metadata);
this.xmlEncoding = xmlEncoding;
this.useCDATA = useCDATA;
}
@@ -166,48 +171,35 @@ public class SerializableMetadata extend
Document document = factory.newDocumentBuilder().newDocument();
Element root = (Element) document.createElement("cas:metadata");
- // FIXME: change namespace URI?
root.setAttribute("xmlns:cas", "http://oodt.jpl.nasa.gov/1.0/cas");
document.appendChild(root);
// now add the set of metadata elements in the properties object
- for (Iterator i = this.elementMap.keySet().iterator(); i.hasNext();) {
- String elemName = null;
-
- elemName = (String) i.next();
- List elemValues = (List) this.elementMap.get(elemName);
-
+ for (String key : this.getAllKeys()) {
Element metadataElem = document.createElement("keyval");
Element keyElem = document.createElement("key");
if (this.useCDATA)
- keyElem.appendChild(document.createCDATASection(elemName));
+ keyElem.appendChild(document.createCDATASection(key));
else
- keyElem.appendChild(document.createTextNode(URLEncoder
- .encode(elemName, this.xmlEncoding)));
+ keyElem.appendChild(document.createTextNode(URLEncoder.encode(key, this.xmlEncoding)));
+
metadataElem.appendChild(keyElem);
- String type = "scalar";
-
- if (elemValues.size() > 1) {
- type = "vector";
- }
-
- metadataElem.setAttribute("type", type);
+ metadataElem.setAttribute("type", "vector");
- for (Iterator j = elemValues.iterator(); j.hasNext();) {
- String elemValue = (String) j.next();
+ for (String value : this.getAllMetadata(key)) {
Element valElem = document.createElement("val");
- if (elemValue == null) {
+ if (value == null) {
throw new Exception("Attempt to write null value "
- + "for property: [" + elemName + "]: val: ["
- + elemValue + "]");
+ + "for property: [" + key + "]: val: ["
+ + value + "]");
}
if (this.useCDATA)
valElem.appendChild(document
- .createCDATASection(elemValue));
+ .createCDATASection(value));
else
valElem.appendChild(document.createTextNode(URLEncoder
- .encode(elemValue, this.xmlEncoding)));
+ .encode(value, this.xmlEncoding)));
metadataElem.appendChild(valElem);
}
root.appendChild(metadataElem);
@@ -236,7 +228,6 @@ public class SerializableMetadata extend
.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
- elementMap = new Hashtable();
Element root = parser.parse(new InputSource(in))
.getDocumentElement();
@@ -247,9 +238,9 @@ public class SerializableMetadata extend
String elemName = XMLUtils.read(keyValElem, "key",
this.xmlEncoding);
- List elemValues = XMLUtils.readMany(keyValElem, "val",
+ List<String> elemValues = XMLUtils.readMany(keyValElem, "val",
this.xmlEncoding);
- elementMap.put(elemName, elemValues);
+ this.addMetadata(elemName, elemValues);
}
} catch (Exception e) {
throw new IOException(
Modified: incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestMetadata.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestMetadata.java?rev=964916&r1=964915&r2=964916&view=diff
==============================================================================
--- incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestMetadata.java (original)
+++ incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestMetadata.java Fri Jul 16 19:32:59 2010
@@ -1,20 +1,17 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
package org.apache.oodt.cas.metadata;
@@ -22,188 +19,150 @@ package org.apache.oodt.cas.metadata;
import junit.framework.TestCase;
//JDK imports
-import java.io.ByteArrayOutputStream;
-import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Vector;
import java.util.Hashtable;
-//OODT imports
-import org.apache.oodt.commons.xml.XMLUtils;
-
/**
+ * Test Case Suite for the Metadata class.
+ *
* @author mattmann
* @author bfoster
- * @version $Revision$
- *
- * <p>
- * Test Case Suite for the Metadata class.
- * </p>
*
*/
public class TestMetadata extends TestCase {
- private Metadata metadata = null;
+ private Metadata metadata = null;
- /**
- * <p>
- * Default Constructor
- * </p>
- */
- public TestMetadata() {
- super();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see junit.framework.TestCase#setUp()
- */
- protected void setUp() throws Exception {
- metadata = new Metadata();
- }
-
- public void testWriteRead() {
- metadata.addMetadata("Name1", "Value1");
- metadata.addMetadata("Name2", "Value2");
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
-
- try {
- XMLUtils.writeXmlToStream(metadata.toXML(), out);
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
-
- Metadata metadata2 = null;
- try {
- metadata2 = new Metadata(
- new ByteArrayInputStream(out.toByteArray()));
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
-
- assertNotNull(metadata2);
- assertNotNull(metadata2.getHashtable());
-
- assertEquals(2, metadata2.getHashtable().size());
- assertNotNull(metadata2.getMetadata("Name1"));
- assertEquals("Value1", metadata2.getMetadata("Name1"));
- assertNotNull(metadata2.getMetadata("Name2"));
- assertEquals("Value2", metadata2.getMetadata("Name2"));
- }
-
- public void testAddMany() {
- List counting = new Vector();
- counting.add("1");
- counting.add("2");
- counting.add("3");
- metadata.addMetadata("ManyTest", counting);
-
- assertNotNull(metadata.getAllMetadata("ManyTest"));
- assertEquals(3, metadata.getAllMetadata("ManyTest").size());
-
- // test ordering
- assertEquals("1", (String) metadata.getAllMetadata("ManyTest").get(0));
- assertEquals("2", (String) metadata.getAllMetadata("ManyTest").get(1));
- assertEquals("3", (String) metadata.getAllMetadata("ManyTest").get(2));
- }
-
- public void testAddHashtable() {
- Hashtable testHash = new Hashtable();
- testHash.put("key1", "val1");
- testHash.put("key2", "val2");
-
- metadata = new Metadata();
- metadata.addMetadata("key3", "val3");
- metadata.addMetadata(testHash);
-
- assertNotNull(metadata.getMetadata("key1"));
- assertNotNull(metadata.getMetadata("key2"));
-
- assertEquals("val1", metadata.getMetadata("key1"));
- assertEquals("val2", metadata.getMetadata("key2"));
-
- assertNotNull(metadata.getMetadata("key3"));
- assertEquals("val3", metadata.getMetadata("key3"));
- }
-
- public void testReplace() {
- Hashtable testHash = new Hashtable();
- testHash.put("key1", "val1");
- testHash.put("key2", "val2");
-
- metadata = new Metadata();
- metadata.addMetadata("blah", "blah2");
-
- assertNotNull(metadata.getMetadata("blah"));
- assertNull(metadata.getMetadata("key1"));
- assertNull(metadata.getMetadata("key2"));
-
- metadata.replaceMetadata(testHash);
-
- assertNull(metadata.getMetadata("blah"));
- assertNotNull(metadata.getMetadata("key1"));
- assertNotNull(metadata.getMetadata("key2"));
-
- assertEquals("val1", metadata.getMetadata("key1"));
- assertEquals("val2", metadata.getMetadata("key2"));
-
- metadata.replaceMetadata("key1", "val2");
- metadata.replaceMetadata("key2", "val1");
-
- assertEquals("val2", metadata.getMetadata("key1"));
- assertEquals("val1", metadata.getMetadata("key2"));
-
- List twoValues = new Vector();
- twoValues.add("firstVal");
- twoValues.add("secondVal");
-
- metadata.replaceMetadata("key1", twoValues);
- assertNotNull(metadata.getMetadata("key1"));
- assertEquals(2, metadata.getAllMetadata("key1").size());
-
- assertEquals("firstVal", (String) metadata.getAllMetadata("key1")
- .get(0));
- assertEquals("secondVal", (String) metadata.getAllMetadata("key1").get(
- 1));
-
- }
-
- /**
- * Ensure that replacing a Metadata instance with a Metadata instance works.
- */
- public void testReplacementWithMetadata() {
- Metadata a = new Metadata();
- Metadata b = new Metadata();
- a.addMetadata("a", "1");
- b.addMetadata("a", "2");
- b.addMetadata("b", "3");
- a.replaceMetadata(b);
- assertEquals("2", a.getMetadata("a"));
- assertEquals("3", a.getMetadata("b"));
- }
-
- public void testEquals() {
- Metadata m1 = new Metadata();
- m1.addMetadata("key1", "val1");
- m1.addMetadata("key2", "val2");
- m1.addMetadata("key2", "val3");
-
- Metadata m2 = new Metadata();
- m2.addMetadata("key1", "val1");
- m2.addMetadata("key2", "val2");
- m2.addMetadata("key2", "val3");
-
- assertEquals(m1, m2);
- assertEquals(m2, m1);
-
- m2.removeMetadata("key1");
-
- assertFalse(m1.equals(m2));
- assertFalse(m2.equals(m1));
- }
+ /**
+ * <p>
+ * Default Constructor
+ * </p>
+ */
+ public TestMetadata() {
+ super();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ metadata = new Metadata();
+ }
+
+ public void testAddMany() {
+ List counting = new Vector();
+ counting.add("1");
+ counting.add("2");
+ counting.add("3");
+ metadata.addMetadata("ManyTest", counting);
+
+ assertNotNull(metadata.getAllMetadata("ManyTest"));
+ assertEquals(3, metadata.getAllMetadata("ManyTest").size());
+
+ // test ordering
+ assertEquals("1", (String) metadata.getAllMetadata("ManyTest").get(0));
+ assertEquals("2", (String) metadata.getAllMetadata("ManyTest").get(1));
+ assertEquals("3", (String) metadata.getAllMetadata("ManyTest").get(2));
+ }
+
+ public void testAddHashtable() {
+ Hashtable testHash = new Hashtable();
+ testHash.put("key1", "val1");
+ testHash.put("key2", "val2");
+
+ metadata = new Metadata();
+ metadata.addMetadata("key3", "val3");
+ metadata.addMetadata(testHash);
+
+ assertNotNull(metadata.getMetadata("key1"));
+ assertNotNull(metadata.getMetadata("key2"));
+
+ assertEquals("val1", metadata.getMetadata("key1"));
+ assertEquals("val2", metadata.getMetadata("key2"));
+
+ assertNotNull(metadata.getMetadata("key3"));
+ assertEquals("val3", metadata.getMetadata("key3"));
+ }
+
+ public void testReplace() {
+ Hashtable testHash = new Hashtable();
+ testHash.put("key1", "val1");
+ testHash.put("key2", "val2");
+
+ metadata = new Metadata();
+ metadata.addMetadata("blah", "blah2");
+
+ assertNotNull(metadata.getMetadata("blah"));
+ assertNull(metadata.getMetadata("key1"));
+ assertNull(metadata.getMetadata("key2"));
+
+ metadata.replaceMetadata(testHash);
+
+ assertNull(metadata.getMetadata("blah"));
+ assertNotNull(metadata.getMetadata("key1"));
+ assertNotNull(metadata.getMetadata("key2"));
+
+ assertEquals("val1", metadata.getMetadata("key1"));
+ assertEquals("val2", metadata.getMetadata("key2"));
+
+ metadata.replaceMetadata("key1", "val2");
+ metadata.replaceMetadata("key2", "val1");
+
+ assertEquals("val2", metadata.getMetadata("key1"));
+ assertEquals("val1", metadata.getMetadata("key2"));
+
+ List twoValues = new Vector();
+ twoValues.add("firstVal");
+ twoValues.add("secondVal");
+
+ metadata.replaceMetadata("key1", twoValues);
+ assertNotNull(metadata.getMetadata("key1"));
+ assertEquals(2, metadata.getAllMetadata("key1").size());
+
+ assertEquals("firstVal", (String) metadata.getAllMetadata("key1").get(0));
+ assertEquals("secondVal", (String) metadata.getAllMetadata("key1").get(1));
+
+ }
+
+ public void testGetAllKeysWithName() {
+ Metadata m1 = new Metadata();
+ m1.addMetadata("Group1/key1", "val1");
+ m1.addMetadata("Group1/key2", "val2");
+ m1.addMetadata("Group2/key2", "val3");
+ m1.addMetadata("Group2/key2/key3", "val3");
+ m1.addMetadata("Group1/sub1/key2", "val3");
+ m1.addMetadata("Group1/sub2/key2/key3", "val3");
+ List<String> keys = m1.getAllKeysWithName("key2");
+ assertEquals(keys.size(), 3);
+ assertEquals(keys.get(0), "Group2/key2");
+ assertEquals(keys.get(1), "Group1/sub1/key2");
+ assertEquals(keys.get(2), "Group1/key2");
+ keys = m1.getAllKeysWithName("key1");
+ assertEquals(keys.size(), 1);
+ assertEquals(keys.get(0), "Group1/key1");
+ }
+
+ public void testEquals() {
+ Metadata m1 = new Metadata();
+ m1.addMetadata("key1", "val1");
+ m1.addMetadata("key2", "val2");
+ m1.addMetadata("key2", "val3");
+
+ Metadata m2 = new Metadata();
+ m2.addMetadata("key1", "val1");
+ m2.addMetadata("key2", "val2");
+ m2.addMetadata("key2", "val3");
+
+ assertEquals(m1, m2);
+ assertEquals(m2, m1);
+
+ m2.removeMetadata("key1");
+
+ assertFalse(m1.equals(m2));
+ assertFalse(m2.equals(m1));
+ }
}
Modified: incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestSerializableMetadata.java
URL: http://svn.apache.org/viewvc/incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestSerializableMetadata.java?rev=964916&r1=964915&r2=964916&view=diff
==============================================================================
--- incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestSerializableMetadata.java (original)
+++ incubator/oodt/trunk/metadata/src/test/org/apache/oodt/cas/metadata/TestSerializableMetadata.java Fri Jul 16 19:32:59 2010
@@ -1,24 +1,23 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
+// Licensed to the Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE.txt file distributed with this work for
+// additional information regarding copyright ownership. The ASF licenses this
+// file to you under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
package org.apache.oodt.cas.metadata;
//JDK imports
+import org.apache.oodt.commons.xml.XMLUtils;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
@@ -29,110 +28,140 @@ import junit.framework.TestCase;
/**
*
+ *
+ * Test class for SerializableMetadata.
+ *
* @author bfoster
- * @version $Revision$
+ * @author mattmann
*
- * <p>
- * Test class for SerializableMetadata
- * </p>.
*/
public class TestSerializableMetadata extends TestCase {
- private String[] encodings = new String[] { "UTF-8", "iso-8859-1",
- "windows-1252", "UTF-16", "US-ASCII" };
+ private String[] encodings = new String[] { "UTF-8", "iso-8859-1",
+ "windows-1252", "UTF-16", "US-ASCII" };
- public TestSerializableMetadata() {
- super();
+ public TestSerializableMetadata() {
+ super();
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ public void testSerialization() throws Exception {
+ for (int i = 0; i < encodings.length; i++) {
+ String encoding = encodings[i];
+ boolean useCDATA = false;
+ for (int j = 0; j < 2; j++, useCDATA = true) {
+ SerializableMetadata sm = new SerializableMetadata(encoding, useCDATA);
+ sm.addMetadata("key1", "val1");
+ sm.addMetadata("key2", "val2");
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(out);
+ oos.writeObject(sm);
+ oos.flush();
+
+ ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
+ out.toByteArray()));
+ SerializableMetadata sm2 = (SerializableMetadata) ois.readObject();
+ oos.close();
+ ois.close();
+
+ assertNotNull(sm2);
+ assertNotNull(sm2.getHashtable());
+ assertEquals(2, sm2.getHashtable().size());
+ assertNotNull(sm2.getMetadata("key1"));
+ assertEquals("val1", sm2.getMetadata("key1"));
+ assertNotNull(sm2.getMetadata("key2"));
+ assertEquals("val2", sm2.getMetadata("key2"));
+ assertNotNull(sm2.getEncoding());
+ assertEquals(encoding, sm2.getEncoding());
+ assertEquals(useCDATA, sm2.isUsingCDATA());
+ }
}
+ }
- protected void setUp() throws Exception {
- super.setUp();
+ public void testXmlStreaming() throws Exception {
+ for (int i = 0; i < encodings.length; i++) {
+ String encoding = encodings[i];
+ boolean useCDATA = false;
+ for (int j = 0; j < 2; j++, useCDATA = true) {
+ SerializableMetadata metadata1 = new SerializableMetadata(encoding,
+ useCDATA);
+ metadata1.addMetadata("Name1", "Value1");
+ metadata1.addMetadata("Name2", "Value/2");
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ metadata1.writeMetadataToXmlStream(out);
+
+ SerializableMetadata metadata2 = new SerializableMetadata(encoding,
+ useCDATA);
+ metadata2.loadMetadataFromXmlStream(new ByteArrayInputStream(out
+ .toByteArray()));
+ out.close();
+
+ assertNotNull(metadata2);
+ assertNotNull(metadata2.getHashtable());
+ assertEquals(2, metadata2.getHashtable().size());
+ assertNotNull(metadata2.getMetadata("Name1"));
+ assertEquals("Value1", metadata2.getMetadata("Name1"));
+ assertNotNull(metadata2.getMetadata("Name2"));
+ assertEquals("Value/2", metadata2.getMetadata("Name2"));
+ }
}
+ }
- public void testSerialization() throws Exception {
- for (int i = 0; i < encodings.length; i++) {
- String encoding = encodings[i];
- boolean useCDATA = false;
- for (int j = 0; j < 2; j++, useCDATA = true) {
- SerializableMetadata sm = new SerializableMetadata(encoding,
- useCDATA);
- sm.addMetadata("key1", "val1");
- sm.addMetadata("key2", "val2");
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(out);
- oos.writeObject(sm);
- oos.flush();
-
- ObjectInputStream ois = new ObjectInputStream(
- new ByteArrayInputStream(out.toByteArray()));
- SerializableMetadata sm2 = (SerializableMetadata) ois
- .readObject();
- oos.close();
- ois.close();
-
- assertNotNull(sm2);
- assertNotNull(sm2.getHashtable());
- assertEquals(2, sm2.getHashtable().size());
- assertNotNull(sm2.getMetadata("key1"));
- assertEquals("val1", sm2.getMetadata("key1"));
- assertNotNull(sm2.getMetadata("key2"));
- assertEquals("val2", sm2.getMetadata("key2"));
- assertNotNull(sm2.getEncoding());
- assertEquals(encoding, sm2.getEncoding());
- assertEquals(useCDATA, sm2.isUsingCDATA());
- }
- }
+ public void testMetadataConversion() throws Exception {
+ for (int i = 0; i < encodings.length; i++) {
+ String encoding = encodings[i];
+ Metadata m = new Metadata();
+ m.addMetadata("key1", "val1");
+ m.addMetadata("key2", "val2");
+
+ SerializableMetadata sm = new SerializableMetadata(m, encoding, false);
+ Metadata mConv = sm.getMetadata();
+
+ assertNotNull(mConv);
+ assertNotNull(mConv.getHashtable());
+ assertEquals(2, mConv.getHashtable().size());
+ assertNotNull(mConv.getMetadata("key1"));
+ assertEquals("val1", mConv.getMetadata("key1"));
+ assertNotNull(mConv.getMetadata("key2"));
+ assertEquals("val2", mConv.getMetadata("key2"));
}
+ }
- public void testXmlStreaming() throws Exception {
- for (int i = 0; i < encodings.length; i++) {
- String encoding = encodings[i];
- boolean useCDATA = false;
- for (int j = 0; j < 2; j++, useCDATA = true) {
- SerializableMetadata metadata1 = new SerializableMetadata(
- encoding, useCDATA);
- metadata1.addMetadata("Name1", "Value1");
- metadata1.addMetadata("Name2", "Value/2");
-
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- metadata1.writeMetadataToXmlStream(out);
-
- SerializableMetadata metadata2 = new SerializableMetadata(
- encoding, useCDATA);
- metadata2.loadMetadataFromXmlStream(new ByteArrayInputStream(
- out.toByteArray()));
- out.close();
-
- assertNotNull(metadata2);
- assertNotNull(metadata2.getHashtable());
- assertEquals(2, metadata2.getHashtable().size());
- assertNotNull(metadata2.getMetadata("Name1"));
- assertEquals("Value1", metadata2.getMetadata("Name1"));
- assertNotNull(metadata2.getMetadata("Name2"));
- assertEquals("Value/2", metadata2.getMetadata("Name2"));
- }
- }
+ public void testWriteRead() {
+ SerializableMetadata metadata = new SerializableMetadata();
+ metadata.addMetadata("Name1", "Value1");
+ metadata.addMetadata("Name2", "Value2");
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ try {
+ XMLUtils.writeXmlToStream(metadata.toXML(), out);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
}
- public void testMetadataConversion() throws Exception {
- for (int i = 0; i < encodings.length; i++) {
- String encoding = encodings[i];
- Metadata m = new Metadata();
- m.addMetadata("key1", "val1");
- m.addMetadata("key2", "val2");
-
- SerializableMetadata sm = new SerializableMetadata(m, encoding,
- false);
- Metadata mConv = sm.getMetadata();
-
- assertNotNull(mConv);
- assertNotNull(mConv.getHashtable());
- assertEquals(2, mConv.getHashtable().size());
- assertNotNull(mConv.getMetadata("key1"));
- assertEquals("val1", mConv.getMetadata("key1"));
- assertNotNull(mConv.getMetadata("key2"));
- assertEquals("val2", mConv.getMetadata("key2"));
- }
+ SerializableMetadata metadata2 = null;
+ try {
+ metadata2 = new SerializableMetadata(new ByteArrayInputStream(out
+ .toByteArray()));
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
}
+
+ assertNotNull(metadata2);
+ assertNotNull(metadata2.getHashtable());
+
+ assertEquals(2, metadata2.getHashtable().size());
+ assertNotNull(metadata2.getMetadata("Name1"));
+ assertEquals("Value1", metadata2.getMetadata("Name1"));
+ assertNotNull(metadata2.getMetadata("Name2"));
+ assertEquals("Value2", metadata2.getMetadata("Name2"));
+ }
}