001: /*
002: * UserMeta.java
003: *
004: * Version: $Revision: 1.7 $
005: *
006: * Date: $Date: 2006/08/08 21:59:25 $
007: *
008: * Copyright (c) 2002-2005, 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.element;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import org.dspace.app.xmlui.wing.AttributeMap;
047: import org.dspace.app.xmlui.wing.WingConstants;
048: import org.dspace.app.xmlui.wing.WingContext;
049: import org.dspace.app.xmlui.wing.WingException;
050: import org.xml.sax.Attributes;
051: import org.xml.sax.ContentHandler;
052: import org.xml.sax.SAXException;
053: import org.xml.sax.ext.LexicalHandler;
054: import org.xml.sax.helpers.AttributesImpl;
055: import org.xml.sax.helpers.NamespaceSupport;
056:
057: /**
058: * A class representing a set of metadata about the user generating this page.
059: *
060: * @author Scott Phillips
061: */
062: public class UserMeta extends AbstractWingElement implements
063: WingMergeableElement, MetadataElement {
064: /** The name of the userMeta element */
065: public static final String E_USER_META = "userMeta";
066:
067: /** The name of the authenticated attribute */
068: public static final String A_AUTHENTICATED = "authenticated";
069:
070: /** Has this UserMeta element been merged? */
071: private boolean merged = false;
072:
073: /** Has this user been authenticated? */
074: private boolean authenticated = false;
075:
076: /** The metadata contents of this UserMeta element */
077: private List<Metadata> metadatum = new ArrayList<Metadata>();
078:
079: /**
080: * Construct a new userMeta
081: *
082: * @param context
083: * (Required) The context this element is contained in, such as
084: * where to route SAX events and what i18n catalogue to use.
085: * @param authenticated
086: * (Required) Weather the user has been authenticated.
087: */
088: protected UserMeta(WingContext context) throws WingException {
089: super (context);
090: }
091:
092: /**
093: * Set the user described in the meta object as authenticated.
094: *
095: * @param authenticated
096: * (Required) True if the user is authenticated, false otherwise.
097: */
098: public void setAuthenticated(boolean authenticated) {
099: this .authenticated = authenticated;
100: }
101:
102: /**
103: * Add metadata about the requesting user to the document.
104: *
105: * @param element
106: * (Required) The metadata element.
107: * @param qualifier
108: * (May be null) The metadata qualifier.
109: * @param language
110: * (May be null) The metadata's language
111: * @param allowMultiple
112: * (Required) determine if multipe metadata element with the same
113: * element, qualifier and language are allowed.
114: * @return A new metadata
115: */
116: public Metadata addMetadata(String element, String qualifier,
117: String language, boolean allowMultiple)
118: throws WingException {
119: Metadata metadata = new Metadata(context, element, qualifier,
120: language, allowMultiple);
121: metadatum.add(metadata);
122: return metadata;
123: }
124:
125: /**
126: * Add metadata about the requesting user to the document.
127: *
128: * @param element
129: * (Required) The metadata element.
130: * @param qualifier
131: * (May be null) The metadata qualifier.
132: * @param language
133: * (May be null) The metadata's language
134: * @return A new metadata
135: */
136: public Metadata addMetadata(String element, String qualifier,
137: String language) throws WingException {
138: return addMetadata(element, qualifier, null, false);
139: }
140:
141: /**
142: * Add metadata about the requesting user to the document.
143: *
144: * @param element
145: * (Required) The metadata element.
146: * @param qualifier
147: * (May be null) The metadata qualifier.
148: * @return A new metadata
149: */
150: public Metadata addMetadata(String element, String qualifier)
151: throws WingException {
152: return addMetadata(element, qualifier, null, false);
153: }
154:
155: /**
156: * Add metadata about the requesting user to the document.
157: *
158: * @param element
159: * (Required) The metadata element.
160: * @return A new metadata
161: */
162: public Metadata addMetadata(String element) throws WingException {
163: return addMetadata(element, null, null, false);
164: }
165:
166: /**
167: * Determine if the given SAX event is a UserMeta element.
168: *
169: * @param namespace
170: * The element's name space
171: * @param localName
172: * The local, unqualified, name for this element
173: * @param qName
174: * The qualified name for this element
175: * @param attributes
176: * The element's attributes
177: * @return True if this WingElement is equivalent to the given SAX Event.
178: */
179: public boolean mergeEqual(String namespace, String localName,
180: String qName, Attributes attributes) throws SAXException,
181: WingException {
182:
183: if (!WingConstants.DRI.URI.equals(namespace))
184: return false;
185:
186: if (!E_USER_META.equals(localName))
187: return false;
188: return true;
189: }
190:
191: /**
192: * Since metadata can not be merged there are no mergeable children. This
193: * just return's null.
194: *
195: * @param namespace
196: * The element's name space
197: * @param localName
198: * The local, unqualified, name for this element *
199: * @param qName
200: * The qualified name for this element
201: * @param attributes
202: * The element's attributes
203: * @return The child element
204: */
205: public WingMergeableElement mergeChild(String namespace,
206: String localName, String qName, Attributes attributes)
207: throws SAXException, WingException {
208: // We don't merge our children but we do have one special optimization,
209: // if a metadata is allready in the document and it is taged as not allowing
210: // multiples then we do not add the new metadata to the document.
211:
212: if (WingConstants.DRI.URI.equals(namespace)
213: && Metadata.E_METADATA.equals(localName)) {
214: String element = attributes.getValue(Metadata.A_ELEMENT);
215: String qualifier = attributes
216: .getValue(Metadata.A_QUALIFIER);
217: String language = attributes.getValue(Metadata.A_LANGUAGE);
218:
219: List<Metadata> remove = new ArrayList<Metadata>();
220: for (Metadata metadata : metadatum) {
221: if (metadata.equals(element, qualifier, language)
222: && !metadata.allowMultiple()) {
223: remove.add(metadata);
224: }
225: }
226:
227: // Remove all the metadata elements we found.
228: for (Metadata metadata : remove) {
229: metadata.dispose();
230: metadatum.remove(metadata);
231: }
232: }
233:
234: return null;
235: }
236:
237: /**
238: * Inform this element that it is being merged with an existing element.
239: */
240: public Attributes merge(Attributes attributes) throws SAXException,
241: WingException {
242: this .merged = true;
243:
244: String mergedAuthenticated = attributes
245: .getValue(A_AUTHENTICATED);
246:
247: if ("yes".equals(mergedAuthenticated)) {
248: // The user has allready been set to authenticated.
249: // Do nothing.
250: } else if ("no".equals(mergedAuthenticated)) {
251: // No authenticated user yet.
252: if (this .authenticated) {
253: // Original no, but we've been told that the user is
254: // authenticated.
255: AttributesImpl attributesImpl = new AttributesImpl(
256: attributes);
257: int index = attributesImpl.getIndex(A_AUTHENTICATED);
258: if (index >= 0) {
259: attributesImpl.setValue(index, "yes");
260: } else {
261: attributesImpl.addAttribute("", A_AUTHENTICATED,
262: A_AUTHENTICATED, "CDATA", "yes");
263: }
264: attributes = attributesImpl;
265: }
266: } else {
267: // Authenticated value does not conform to the schema.
268: AttributesImpl attributesImpl = new AttributesImpl(
269: attributes);
270: attributesImpl.addAttribute("", A_AUTHENTICATED,
271: A_AUTHENTICATED, "CDATA",
272: (this .authenticated ? "yes" : "no"));
273: attributes = attributesImpl;
274:
275: }
276:
277: return attributes;
278: }
279:
280: /**
281: * Translate this element into SAX events.
282: *
283: * @param contentHandler
284: * (Required) The registered contentHandler where SAX events
285: * should be routed too.
286: * @param lexicalHandler
287: * (Required) The registered lexicalHandler where lexical
288: * events (such as CDATA, DTD, etc) should be routed too.
289: * @param namespaces
290: * (Required) SAX Helper class to keep track of namespaces able
291: * to determine the correct prefix for a given namespace URI.
292: */
293: public void toSAX(ContentHandler contentHandler,
294: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
295: throws SAXException {
296: if (!merged) {
297: AttributeMap attributes = new AttributeMap();
298: if (authenticated)
299: attributes.put(A_AUTHENTICATED, "yes");
300: else
301: attributes.put(A_AUTHENTICATED, "no");
302: startElement(contentHandler, namespaces, E_USER_META,
303: attributes);
304: }
305:
306: for (Metadata metadata : metadatum)
307: metadata.toSAX(contentHandler, lexicalHandler, namespaces);
308:
309: if (!merged)
310: endElement(contentHandler, namespaces, E_USER_META);
311: }
312:
313: /**
314: * dispose
315: */
316: public void dispose() {
317: for (AbstractWingElement content : metadatum)
318: content.dispose();
319: metadatum.clear();
320: metadatum = null;
321: super.dispose();
322: }
323: }
|