/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.model.elements;

import org.eclipse.birt.report.model.activity.ReadOnlyActivityStack;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.LibraryHandle;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.birt.report.model.core.DesignSession;
import org.eclipse.birt.report.model.core.Module;
import org.eclipse.birt.report.model.elements.interfaces.ILibraryModel;
import org.eclipse.birt.report.model.writer.LibraryWriter;
import org.eclipse.birt.report.model.writer.ModuleWriter;

/**
 * Represents the library module. The library is the container of reusable
 * report items , data sources, styles and so on. One library has its own
 * namespace, which is used to identify which library the element reference
 * refers.
 */

public class Library extends Module implements ILibraryModel
{

	/**
	 * Namespace of the library.
	 */
	
	private String namespace;

	/**
	 * The host module which includes this module.
	 */

	protected Module host = null;

	/**
	 * Constructor for loading library from design file.
	 * 
	 * @param theSession
	 *            the session in which this library is involved
	 * @param host
	 *            the host module which includes this library
	 */

	public Library( DesignSession theSession, Module host )
	{
		super( theSession );
		this.host = host;
		initSlots( );
		onCreate( );
	}

	/**
	 * Constructos for opening library directly.
	 * 
	 * @param theSession
	 *            the session in which this library is involved
	 */

	public Library( DesignSession theSession )
	{
		this( theSession, null );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.core.DesignElement#apply(org.eclipse.birt.report.model.elements.ElementVisitor)
	 */

	public void apply( ElementVisitor visitor )
	{
		visitor.visitLibrary( this );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.core.DesignElement#getElementName()
	 */

	public String getElementName( )
	{
		return ReportDesignConstants.LIBRARY_ELEMENT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.api.core.IDesignElement#getHandle(org.eclipse.birt.report.model.elements.ReportDesign)
	 */

	public DesignElementHandle getHandle( Module module )
	{
		return handle( );
	}

	/**
	 * Returns an API handle for this element.
	 * 
	 * @return an API handle for this element
	 */

	public LibraryHandle handle( )
	{
		if ( handle == null )
		{
			handle = new LibraryHandle( this );
		}
		return (LibraryHandle) handle;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.core.module#getSlotCount()
	 */
	protected int getSlotCount( )
	{
		return SLOT_COUNT;
	}

	/**
	 * Returns the library namespace.
	 * 
	 * @return the library namespace
	 */

	public String getNamespace( )
	{
		return namespace;
	}

	/**
	 * Sets the library namespace.
	 * 
	 * @param namespace
	 *            The namespace to set.
	 */

	public void setNamespace( String namespace )
	{
		this.namespace = namespace;
	}

	/**
	 * Sets the library is read-only one. That means any operation on it will
	 * throw runtime exception.
	 */

	public void setReadOnly( )
	{
		activityStack = new ReadOnlyActivityStack( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.core.Module#getWriter()
	 */

	public ModuleWriter getWriter( )
	{
		return new LibraryWriter( this );
	}

	/**
	 * Returns the host module. If this module is not included by any module,
	 * return null.
	 * 
	 * @return the host module.
	 */

	public Module getHost( )
	{
		return host;
	}

	/**
	 * Sets the host module.
	 * 
	 * @param theHost
	 *            the host module to set
	 */

	public void setHost( Module theHost )
	{
		this.host = theHost;
	}

	/**
	 * Returns whether the library with the given namespace can be included in
	 * this module.
	 * 
	 * @param namespace
	 *            the library namespace
	 * @return true, if the library with the given namespace can be included.
	 */

	public boolean isRecursiveNamespace( String namespace )
	{
		Module module = this;
		while ( module instanceof Library )
		{
			Library library = (Library) module;

			if ( library.getNamespace( ).equals( namespace ) )
				return true;

			module = library.getHost( );
		}

		return false;
	}

	/**
	 * Finds a theme in this module itself.
	 * 
	 * @param name
	 *            Name of the theme to find.
	 * @return The style, or null if the theme is not found.
	 */

	public Theme findNativeTheme( String name )
	{
		return (Theme) nameSpaces[THEME_NAME_SPACE].getElement( name );
	}

}
@@ -1479,17 +1479,16 @@
 y.%0A%09 */%0A
-%09
 %0A%09privat
@@ -4736,16 +4736,34 @@
 %0A%09%09%09if (
+ namespace.equals(
  library
@@ -4782,26 +4782,8 @@
 e( )
-.equals( namespace
  ) )

resolve NPE problem when open a library which include another library