001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2001-2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: JAnnotatedElementHelper
044: */package org.exolab.javasource;
045:
046: import java.util.Iterator;
047:
048: import org.exolab.castor.util.OrderedHashMap;
049:
050: /**
051: * Implements JAnnotatedElement interface on behalf of other classes in this
052: * package that implement this interface.
053: *
054: * @author <a href="mailto:andrew DOT fawcett AT coda DOTcom">Andrew Fawcett</a>
055: * @version $Revision: 6669 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
056: */
057: public class JAnnotatedElementHelper implements JAnnotatedElement {
058: //--------------------------------------------------------------------------
059:
060: /** Stores annotations associated with the source element containing this helper. */
061: private OrderedHashMap _annotations;
062:
063: //--------------------------------------------------------------------------
064:
065: /**
066: * Creates a JAnnodatedElementHelper.
067: */
068: public JAnnotatedElementHelper() {
069: super ();
070: }
071:
072: //--------------------------------------------------------------------------
073:
074: /**
075: * {@inheritDoc}
076: */
077: public final JAnnotation getAnnotation(
078: final JAnnotationType annotationType) {
079: if (_annotations == null) {
080: return null;
081: }
082: return (JAnnotation) _annotations.get(annotationType.getName());
083: }
084:
085: /**
086: * {@inheritDoc}
087: */
088: public final JAnnotation[] getAnnotations() {
089: if (_annotations == null) {
090: return new JAnnotation[0];
091: }
092: return (JAnnotation[]) _annotations.values().toArray(
093: new JAnnotation[_annotations.size()]);
094: }
095:
096: /**
097: * {@inheritDoc}
098: */
099: public final boolean isAnnotationPresent(
100: final JAnnotationType annotationType) {
101: if (_annotations != null) {
102: return _annotations.containsKey(annotationType.getName());
103: }
104: return false;
105: }
106:
107: /**
108: * {@inheritDoc}
109: */
110: public final void addAnnotation(final JAnnotation annotation) {
111: if (isAnnotationPresent(annotation.getAnnotationType())) {
112: throw new IllegalArgumentException("Annotation for '"
113: + annotation.getAnnotationType().getName()
114: + "' already added.");
115: }
116: String annotationType = annotation.getAnnotationType()
117: .getName();
118: if (_annotations == null) {
119: _annotations = new OrderedHashMap();
120: }
121: _annotations.put(annotationType, annotation);
122: }
123:
124: /**
125: * {@inheritDoc}
126: */
127: public final JAnnotation removeAnnotation(
128: final JAnnotationType annotationType) {
129: if (!isAnnotationPresent(annotationType)) {
130: throw new IllegalArgumentException("Annotation for '"
131: + annotationType.getName() + "' not present.");
132: }
133: return (JAnnotation) _annotations.remove(annotationType
134: .getName());
135: }
136:
137: /**
138: * {@inheritDoc}
139: */
140: public final boolean hasAnnotations() {
141: if (_annotations != null) {
142: return _annotations.size() > 0;
143: }
144: return false;
145: }
146:
147: /**
148: * Outputs the list of annotations maintained by this object.
149: *
150: * @param jsw the JSourceWriter to print the annotations to
151: * @return true if at least one annotation was printed, false otherwise.
152: */
153: public final boolean printAnnotations(final JSourceWriter jsw) {
154: boolean printed = false;
155: if (_annotations != null) {
156: Iterator annotations = _annotations.values().iterator();
157: while (annotations.hasNext()) {
158: JAnnotation annotation = (JAnnotation) annotations
159: .next();
160: annotation.print(jsw);
161: jsw.writeln();
162: printed = true;
163: }
164: }
165: return printed;
166: }
167:
168: //--------------------------------------------------------------------------
169: }
|