View Javadoc

1   // 
2   // Copyright (c) 2003, 2004, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   //   
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //   
8   // * Redistributions of source code must retain the above copyright notice,  
9   // this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  // this list of conditions and the following disclaimer in the documentation  
12  // and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  // nor the names of its contributors may be used to endorse or promote products  
15  // derived from this software without specific prior written permission. 
16  //
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  //
28  package org.objectledge.datatype;
29  
30  import java.util.ArrayList;
31  import java.util.Hashtable;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
35  
36  import org.objectledge.datatype.xml.XMLSchemaDatatypeSetFactory;
37  import org.objectledge.xml.XMLGrammarCache;
38  
39  /***
40   * The Datatype set manager component. 
41   * 
42   * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a>
43   * @version $Id: DatatypeSetManager.java,v 1.5 2006/02/08 18:20:56 zwierzem Exp $
44   */
45  public class DatatypeSetManager
46  {
47  	/*** the provided datatype sets. */
48  	protected Map<String,DatatypeSet> datatypeSets = new Hashtable<String,DatatypeSet>();
49  
50  	/*** the datatype sets' factories. */
51  	protected List<DatatypeSetFactory> factories = new ArrayList<DatatypeSetFactory>();
52  	
53  	/***
54  	 * Creates an abstract datatype set manager.
55       * 
56  	 * @param grammarCache XML grammar cache for retrieval of XML schema datatypes
57  	 */
58  	public DatatypeSetManager(XMLGrammarCache grammarCache)
59  	{
60  		// TODO Register factories in a "dynamic" or configurable way using PicoContainer
61  		factories.add(new XMLSchemaDatatypeSetFactory(grammarCache));
62  	}
63  	
64  	/***
65  	 * Returns a named set of datatypes.
66  	 * 
67  	 * @param name name of the datatype set
68  	 * @return the datatype set or null if the datatype set does not exist
69  	 * @throws DatatypeSetCreationException thrown when datatype set name is known but 
70  	 * 		the set cannot be created.
71  	 */
72  	public synchronized DatatypeSet getDatatypeSet(String name)
73  		throws DatatypeSetCreationException
74  	{
75  		DatatypeSet set = (DatatypeSet) datatypeSets.get(name);
76  		if(set == null)
77  		{
78  			for (Iterator iter = factories.iterator(); iter.hasNext();)
79          	{
80              	DatatypeSetFactory element = (DatatypeSetFactory) iter.next();
81  				set = element.createDatatypeSet(name);
82  				if(set != null)
83  				{
84  					break;
85  				}
86          	}
87  			datatypeSets.put(name, set);
88  		}
89  		return set;
90  	}
91  	
92  	/***
93  	 * Set a parent - child relation between sets of datatypes. This relation is used to inherit
94  	 * datatypes between sets. Child datatype set inherits datatypes from parent one.
95  	 * 
96  	 * @param child child datatype set
97  	 * @param parent parent datatype set 
98  	 * @throws CircularDependencyException on dependency cycle creation
99  	 */
100     public synchronized void setParent(DatatypeSet child, DatatypeSet parent)
101 		throws CircularDependencyException
102     {
103     	DatatypeSet set = parent;
104     	while(set != null && set != child)
105     	{
106     		set = ((AbstractDatatypeSet) set).getParent();
107     	}
108     	if(set == child)
109     	{
110     		throw new CircularDependencyException("datatype set '"+parent.getName()
111     			+"' is a child of datatype set '"+child.getName()+"'");
112     	}
113     	((AbstractDatatypeSet) child).setParent(parent);
114     }
115 
116 	/***
117 	 * Clears a parent - child relation on a child set of datatypes.
118 	 * 
119 	 * @param child child datatype set
120 	 */
121 	public void clearParent(DatatypeSet child)
122 	{
123 		((AbstractDatatypeSet) child).setParent(null);
124 	}
125 
126 	/*	
127 	public void addDatatype(DatatypeSet set, Datatype datatype);
128 	public void removeDatatype(DatatypeSet set, Datatype datatype);
129 	public void clearDatatypes(DatatypeSet set);
130 	*/
131 }