01: /**********************************************************************
02: Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
03: Licensed under the Apache License, Version 2.0 (the "License");
04: you may not use this file except in compliance with the License.
05: You may obtain a copy of the License at
06:
07: http://www.apache.org/licenses/LICENSE-2.0
08:
09: Unless required by applicable law or agreed to in writing, software
10: distributed under the License is distributed on an "AS IS" BASIS,
11: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: See the License for the specific language governing permissions and
13: limitations under the License.
14:
15: Contributors:
16: ...
17: **********************************************************************/package org.jpox.util;
18:
19: import java.util.ArrayList;
20: import java.util.Iterator;
21: import java.util.List;
22: import java.util.Map;
23: import java.util.Set;
24:
25: import org.jpox.exceptions.JPOXUserException;
26:
27: /**
28: * Utilities for handling Views.
29: *
30: * @version $Revision: 1.4 $
31: **/
32: public class ViewUtils {
33: protected static final Localiser LOCALISER = Localiser
34: .getInstance("org.jpox.Localisation");
35:
36: /**
37: * Check for any circular view references between referencer and referencee.
38: * If one is found, throw a JPOXUserException with the chain of references.
39: * @param viewReferences The Map of view references to check.
40: * @param referencer_name Name of the class that has the reference.
41: * @param referencee_name Name of the class that is being referenced.
42: * @param referenceChain The List of class names that have been referenced
43: * @throws JPOXUserException If a circular reference is found in the view definitions.
44: */
45: public static void checkForCircularViewReferences(
46: Map viewReferences, String referencer_name,
47: String referencee_name, List referenceChain) {
48: Set class_names = (Set) viewReferences.get(referencee_name);
49: if (class_names != null) {
50: // Initialize the chain of references if needed. Add the referencee
51: // to the chain.
52: if (referenceChain == null) {
53: referenceChain = new ArrayList();
54: referenceChain.add(referencer_name);
55: }
56: referenceChain.add(referencee_name);
57:
58: // Iterate through all referenced classes from the referencee. If
59: // any reference the referencer, throw an exception.
60: for (Iterator it = class_names.iterator(); it.hasNext();) {
61: String current_name = (String) it.next();
62: if (current_name.equals(referencer_name)) {
63: StringBuffer error = new StringBuffer(LOCALISER
64: .msg("031003"));
65:
66: for (Iterator chainIter = referenceChain.iterator(); chainIter
67: .hasNext();) {
68: error.append(chainIter.next());
69: if (chainIter.hasNext()) {
70: error.append(" -> ");
71: }
72: }
73:
74: throw new JPOXUserException(error.toString())
75: .setFatal();
76: } else {
77: // Make recursive call to check for any nested dependencies.
78: // e.g A references B, B references C, C references A.
79: checkForCircularViewReferences(viewReferences,
80: referencer_name, current_name,
81: referenceChain);
82: }
83: }
84: }
85: }
86: }
|