001: /*
002: * Metadata.java
003: *
004: * Version: $Revision: 1.6 $
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 org.dspace.app.xmlui.wing.AttributeMap;
044: import org.dspace.app.xmlui.wing.WingContext;
045: import org.dspace.app.xmlui.wing.WingException;
046: import org.xml.sax.ContentHandler;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.ext.LexicalHandler;
049: import org.xml.sax.helpers.NamespaceSupport;
050:
051: /**
052: * This is a class representing an individual metadata field in sudo-dublin core
053: * format. The metadata elements carries generic metadata information in the
054: * form of an attribute-value pair.
055: *
056: * @author Scott Phillips
057: */
058:
059: public class Metadata extends TextContainer implements MetadataElement {
060: /** The name of the metadata element */
061: public static final String E_METADATA = "metadata";
062:
063: /** The name of the element attribute */
064: public static final String A_ELEMENT = "element";
065:
066: /** The name of the qualifier attribute */
067: public static final String A_QUALIFIER = "qualifier";
068:
069: /** The name of the language attribute */
070: public static final String A_LANGUAGE = "lang";
071:
072: /** The metadata's element */
073: private String element;
074:
075: /** The metadata's qualifier */
076: private String qualifier;
077:
078: /** The metadata's language */
079: private String language;
080:
081: /**
082: * Determine the addititive model for the metadata, should
083: * the metadata allways be added to the document or only if
084: * it does not allready exist?
085: */
086: private boolean allowMultiple;
087:
088: /**
089: * Construct a new metadata.
090: *
091: * @param element
092: * (Required) The element of this metadata
093: * @param qualifier
094: * (May be null) The qualifier of this metadata
095: * @param language
096: * (May be null) the language of this metadata
097: * @param allowMultiple
098: * (Required) Are multipe metadata elements with the same element,
099: * qualifier, and language allowed?
100: */
101: protected Metadata(WingContext context, String element,
102: String qualifier, String language, boolean allowMultiple)
103: throws WingException {
104: super (context);
105: this .element = element;
106: this .qualifier = qualifier;
107: this .language = language;
108: this .allowMultiple = allowMultiple;
109: }
110:
111: /**
112: * If an metadata with the same element, qualifier, and language exist
113: * within the document should this metadata element be added into the
114: * document or should only one metadata be allowed.
115: */
116: protected boolean allowMultiple() {
117: return this .allowMultiple;
118: }
119:
120: /**
121: * Determine if the given element, qualifier, and lang are equal to this
122: * metadata.
123: *
124: * @param element
125: * (Required) The element of this metadata
126: * @param qualifier
127: * (May be null) The qualifier of this metadata
128: * @param language
129: * (May be null) the language of this metadata
130: * @return True if the given parameters are equal to this metadata.
131: */
132: protected boolean equals(String element, String qualifier,
133: String language) {
134: // Element should never be null.
135: if (this .element == null || element == null)
136: return false;
137:
138: if (stringEqualsWithNulls(this .element, element) == false)
139: return false;
140: if (stringEqualsWithNulls(this .qualifier, qualifier) == false)
141: return false;
142: if (stringEqualsWithNulls(this .language, language) == false)
143: return false;
144:
145: // Element, qualifier, and language are equal.
146: return true;
147: }
148:
149: /**
150: * This is just a silly private method to make the method above easier to
151: * read. Compare the two parameters to see if they are equal while taking
152: * into account nulls. So if both values are null that is considered an
153: * equals.
154: *
155: * The method is meant to replace the syntax current.equals(test) so that it
156: * works when current is null.
157: *
158: * @param current
159: * (May be null) The current value.
160: * @param test
161: * (May be null) The value to be compared to current
162: * @return If the strings are equal.
163: */
164: private boolean stringEqualsWithNulls(String current, String test) {
165: if (current == null) {
166: if (test == null)
167: return true;
168: else
169: return false;
170: } else {
171: return current.equals(test);
172: }
173: }
174:
175: /**
176: * Translate into SAX events.
177: *
178: * @param contentHandler
179: * (Required) The registered contentHandler where SAX events
180: * should be routed too.
181: * @param lexicalHandler
182: * (Required) The registered lexicalHandler where lexical
183: * events (such as CDATA, DTD, etc) should be routed too.
184: * @param namespaces
185: * (Required) SAX Helper class to keep track of namespaces able
186: * to determine the correct prefix for a given namespace URI.
187: */
188: public void toSAX(ContentHandler contentHandler,
189: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
190: throws SAXException {
191: AttributeMap attributes = new AttributeMap();
192: attributes.put(A_ELEMENT, element);
193: if (this .qualifier != null)
194: attributes.put(A_QUALIFIER, qualifier);
195: if (this .language != null)
196: attributes.put(A_LANGUAGE, language);
197:
198: startElement(contentHandler, namespaces, E_METADATA, attributes);
199: super .toSAX(contentHandler, lexicalHandler, namespaces);
200: endElement(contentHandler, namespaces, E_METADATA);
201: }
202:
203: /**
204: * dispose()
205: */
206: public void dispose() {
207: super.dispose();
208: }
209: }
|