/*
 * 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.
 */

import java.awt.Color;
import java.util.Iterator;
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.IlvXMLDataReader;
import ilog.views.chart.data.xml.IlvXMLDataSource;
import ilog.views.chart.data.xml.IlvXMLPropertyReader;
import ilog.views.util.IlvProductUtil;

/**
 * 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 ;.
 */
class HREFPropertyReader implements IlvXMLPropertyReader {
  /** The <code>hrefs</code> property element tag. */
  public static final 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);
  }

}

public class XMLExtension {
  public static void main(String[] args) throws Exception {
    // This sample uses JViews Charts features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Charts Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Charts_Deployment);

    // -- Read the XML data

    // Create the data source.
    IlvXMLDataSource ds = new IlvXMLDataSource();
    // Create the XML reader.
    IlvXMLDataReader reader = new IlvXMLDataReader();
    // Optional: Specify that the parser should validate the contents of the
    // file
    reader.setValidating(true);
    // Register our own XMLPropertyReader
    reader.setPropertyReader(HREFPropertyReader.HREFS_TAG, new HREFPropertyReader());
    // Load the data.
    ds.load(new org.xml.sax.InputSource("resources/data.xml"), reader);

    // -- Getting the properties values
    IlvDataSet dataSet = ds.getDataSet(0);
    // hrefs (read with a custom XMLPropertyReader)
    System.out.println("hrefs property:");
    SuppressWarnings("unchecked")
    List<String> hrefs = (List<String>) dataSet.getProperty(HREFPropertyReader.HREFS_TAG);
    if (hrefs != null) {
      for (Iterator<String> iter = hrefs.iterator(); iter.hasNext();)
        System.out.println(iter.next());
    }
    // color (read with the predefined reader; with a 'javaClass' attribute
    // equals to #924242
    Color color = (Color) dataSet.getProperty("color");
    if (color != null)
      System.out.println("color property: " + color.getRGB());

    // product (read with the predefined reader; no 'javaClass' attribute)
    String product = (String) dataSet.getProperty("product");
    System.out.println("Product property:" + product);
  }

}