/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

package imgmap;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import ilog.views.chart.data.IlvDataSet;
import ilog.views.chart.data.xml.IlvXMLPropertyReader;

/**
 * An <code>IlvXMLPropertyReader</code> implementation that reads the
 * <code>hrefs</code> property. The property should be expressed as a sequence
 * of hypertext references (hrefs) separated by a ;.
 */
public class HREFPropertyReader implements IlvXMLPropertyReader {
  /** The <code>hrefs</code> property element tag. */
  public static String HREFS_TAG = "hrefs";

  /**
   * Reads the specified property element. This method reads an
   * <code>hrefs</code> element associated with a series and stores its contents
   * in a <code>List</code>.
   */
  Override
  public Object readProperty(org.w3c.dom.Element propertyElt) {
    Node child = propertyElt.getFirstChild();
    while (child != null) {
      if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(HREFS_TAG)) {
        Element hrefElt = (Element) child;
        StringTokenizer tokenizer = new StringTokenizer(hrefElt.getFirstChild().getNodeValue(), ";\n\t ");
        List<String> hrefs = new LinkedList<String>();
        while (tokenizer.hasMoreTokens())
          hrefs.add(tokenizer.nextToken());
        return hrefs;
      }
      child = child.getNextSibling();
    }
    return null;
  }

  /**
   * Sets the property on the specified data set. This method sets the
   * <code>href</code> s <code>List</code> as a property of the specified data
   * set.
   */
  Override
  public void setProperty(IlvDataSet dataSet, String propertyName, Object value) {
    dataSet.putProperty(HREFPropertyReader.HREFS_TAG, value, false);
  }
}