/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.util;

import java.util.*;
import org.apache.log4j.*;
import org.w3c.dom.*;

public final class StockMessages
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(StockMessages.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private Map msgmap;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  public StockMessages(Element elt)
  {
    HashMap tmp_msgmap = new HashMap();
    NodeList msgs = elt.getChildNodes();
    for (int i=0; i<msgs.getLength(); i++)
    { // check each sub-element, get its message, and load it
      Node n = msgs.item(i);
      if (n.getNodeType()==Node.ELEMENT_NODE)
      { // add this node to the hashmap by its tag name
	DOMElementHelper h = new DOMElementHelper((Element)n);
	String txt = h.getElementText();
	if (txt!=null)
	  tmp_msgmap.put(n.getNodeName(),txt.trim());

      } // end if

    } // end for

    if (tmp_msgmap.size()>0)
      msgmap = Collections.unmodifiableMap(tmp_msgmap);
    else
      msgmap = Collections.EMPTY_MAP;

    if (logger.isDebugEnabled())
      logger.debug(msgmap.size() + " stock message(s) loaded from <" + elt.getTagName() + "/> section");

  } // end constructor

  public final String get(String name)
  {
    return (String)(msgmap.get(name));

  } // end get

  public final String getReplace(String name, Map vars)
  {
    return StringUtil.replaceAllVariables((String)(msgmap.get(name)),vars);

  } // end getReplace

} // end class StockMessages
