001: /*
002: * WingContext.java
003: *
004: * Version: $Revision: 1.5 $
005: *
006: * Date: $Date: 2006/03/20 22:41:02 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.wing;
042:
043: /**
044: * A class representing the framework's context, what component is generationg
045: * content, where should we log. It's basicaly a grab bag for framework wide
046: * communication, if all elements need to know about something then it should go
047: * here.
048: *
049: * @author Scott Phillips
050: */
051:
052: import org.apache.avalon.framework.logger.Logger;
053:
054: public class WingContext {
055: /** The nameing divider */
056: public static final char DIVIDER = '.';
057:
058: /**
059: * What is the component name for this transformer, this is used to generate
060: * unique ids.
061: */
062: private String componentName;
063:
064: /** Cocoon logger for debugging Wing elements */
065: private Logger logger;
066:
067: /**
068: * The repository specefic object manager.
069: */
070: private ObjectManager objectManager;
071:
072: /**
073: * Set the current transformer's name
074: *
075: * @param componentName
076: * the current transformer's name.
077: */
078: public void setComponentName(String componentName) {
079: this .componentName = componentName;
080: }
081:
082: /**
083: * Return the current transformer's name.
084: *
085: * @return
086: */
087: public String getComponentName() {
088: return this .componentName;
089: }
090:
091: /**
092: * Return the cocoon logger
093: *
094: * @return
095: */
096: public Logger getLogger() {
097: return this .logger;
098: }
099:
100: /**
101: * Set the cocoon logger
102: *
103: * @param contentHandler
104: * A new content handler.
105: */
106: public void setLogger(Logger logger) {
107: this .logger = logger;
108: }
109:
110: /**
111: * Generate a unique id based upon the locally unique name and the
112: * application.
113: *
114: * The format of the unique id typically is:
115: *
116: * <componentName> dot <application> dot <unique name>
117: *
118: * typically the componentName is the Java class path of the Wing component.
119: *
120: * @param application
121: * The application of this element, typically this is the element
122: * type that is being identified. Such as p, div, table, field,
123: * etc...
124: * @param name
125: * A locally unique name that distinguished this element from
126: * among its siblings.
127: * @return A globally unique identifier.
128: */
129: public String generateID(String application, String name) {
130: return combine(componentName, application, name);
131: }
132:
133: /**
134: * Generate a unique id with a sub name. Like the other form of generateID
135: * this will append another sub-name. This is typically used for when
136: * duplicate names are used but further identification is needed for
137: * globally unique names.
138: *
139: * @param application
140: * The application of this element, typically this is the element
141: * type that is being identified. Such as p, div, table, field,
142: * etc...
143: * @param name
144: * A locally unique name that distinguished this element from
145: * among its siblings.
146: * @param subName
147: * An additional name to the original name to further identify it
148: * in cases when just the name alone does not accomplish this.
149: * @return
150: */
151: public String generateID(String application, String name,
152: String subName) {
153: return combine(componentName, application, name, subName);
154: }
155:
156: /**
157: * Generate a specialized name based on the callers name. This is typically
158: * used in situations where two namespaces are being merged together so that
159: * each namespace is prepended with the current scope's name.
160: *
161: * @param name
162: * A locally unique name that distinguished this element from
163: * among it's siblings.
164: */
165: public String generateName(String name) {
166: return combine(componentName, name);
167: }
168:
169: /**
170: * Simple utility function to join the parts into one string separated by a
171: * DOT.
172: *
173: * @param parts
174: * The input parts.
175: * @return The combined string.
176: */
177: private String combine(String... parts) {
178:
179: String combine = "";
180: boolean skipDivider = true;
181: for (String part : parts) {
182: if (part == null) {
183: skipDivider = true;
184: continue;
185: }
186:
187: if (skipDivider)
188: skipDivider = false;
189: else
190: combine += DIVIDER;
191:
192: combine += part;
193:
194: }
195:
196: return combine;
197: }
198:
199: /**
200: * Set the objectManager which is repsonsible for managing the object store in the DRI document.
201: *
202: * @param objectManager The new objectManager
203: */
204: public void setObjectManager(ObjectManager objectManager) {
205: this .objectManager = objectManager;
206: }
207:
208: /**
209: * Get the objectManager.
210: *
211: * @return Return the objectManager associated with this component.
212: */
213: public ObjectManager getObjectManager() {
214: return this .objectManager;
215: }
216:
217: /**
218: * Check that the context is valid, and able to be used. An error should be
219: * thrown if it is not in a valid sate.
220: */
221: public void checkValidity() throws WingException {
222: // FIXME: There use to be invalid states for wing contexts however they
223: // were removed. I think in the future however additions could require
224: // this check so I do not want to remove it just yet.
225: }
226:
227: /**
228: * Recycle this context for future use by another wing element
229: */
230: public void dispose() {
231: this.componentName = null;
232: this.objectManager = null;
233: }
234: }
|