package org.apache.maven;

/* ====================================================================
 *   Copyright 2001-2005 The Apache Software Foundation.
 *
 *   Licensed 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.
 * ====================================================================
 */

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

/**
 * Some utility methods.
 * @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
 */
public class PomUtils
{
    private File file;
    private String parent;

    /** The parent.
     * @return The parent
     */
    public String getParent()
    {
      return parent;
    }

    /** The file.
     * @return The file
     */
    public File getFile()
    {
      return file;
    }

    /**
     * Sets the file.
     * @param newFile The file to set
     */
    public void setFile(File newFile)
    {
      this.file = newFile;
    }

    /** The file.
     * @return True if the file contains <extend>.
     */
    public boolean extend()
    {
        Document doc = parseXmlFile(file, false);
        NodeList list = doc.getElementsByTagName("extend");
        boolean extend = false;
        if ( list.getLength() > 0 )
        {
            extend = true;
            Element element = (Element) list.item(0);
            try
            {
            Node firstChild = element.getFirstChild();
            parent = firstChild.getNodeValue();
            }  catch (DOMException e) {
            }
        }
        return extend;
    }

    private Document parseXmlFile(File filename, boolean validating)
    {
        try
        {
            DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);
            Document doc = factory.newDocumentBuilder().parse(filename);
            return doc;
        } catch (SAXException e) {
        } catch (ParserConfigurationException e) {
        } catch (IOException e) {
        }
        return null;
    }
}
