/*
    PCS - A Framework For Java Web Applications
    Copyright (C) 2002 Patrick Carl, patrick.carl@web.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 
    $Id: XmlUtility.java,v 1.1.1.1 2002/12/27 23:49:48 pcs_org Exp $
 
 */

package de.spieleck.pcs.util;

/**
 * This class provides methods for easily accessing values in XML strings. It uses 
 * no DOM or SAX, just string methods. So there is hopefully no useless overhead.
 * @author Patrick Carl
 */
public class XmlUtility
{
    
    /**
     * returns the text within the first given occurrance of an XML element named 
     * tag in the string named xmlSource. 
     * @param tag the element which test is searched for
     * @param xmlSource the XML string used as source
     * @return a string containing the text of the first element named tag or null 
     * if so such tag was found or it has no text.
     */
    public static String extractXmlValue(String xmlSource, String tag)
    {
        String tagStart = "<" + tag + ">";
        String tagEnd = "</" + tag + ">";
        int start = xmlSource.indexOf(tagStart);
        int end = xmlSource.indexOf(tagEnd);
        String value = null;
        try{
            value = xmlSource.substring(start + tagStart.length(), end);
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println(xmlSource + ":" + start + ":" + end);
        }
        catch(StringIndexOutOfBoundsException e)
        {
            System.out.println(xmlSource + ":" + start + ":" + end);
        }
        return value;
    }


}