001: /*
002: * PageMeta.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 java.util.ArrayList;
044: import java.util.List;
045:
046: import org.dspace.app.xmlui.wing.Message;
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.NamespaceSupport;
055:
056: /**
057: * A class representing a set of metadata about the page being generated.
058: *
059: * @author Scott Phillips
060: */
061: public class PageMeta extends AbstractWingElement implements
062: WingMergeableElement, MetadataElement {
063: /** Name of the pageMeta element */
064: public final static String E_PAGE_META = "pageMeta";
065:
066: /** Has this PageMeta element been merged? */
067: private boolean merged = false;
068:
069: /**
070: * A page meta may hold two types of elements, trails or
071: * metadata. Each of these types are seperated so that
072: * we can search through each time as we merge documents.
073: */
074: private List<Metadata> metadatum = new ArrayList<Metadata>();
075: private List<Trail> trails = new ArrayList<Trail>();
076:
077: /**
078: * Construct a new pageMeta
079: *
080: * @param context
081: * (Required) The context this element is contained in, such as
082: * where to route SAX events and what i18n catalogue to use.
083: */
084: protected PageMeta(WingContext context) throws WingException {
085: super (context);
086: }
087:
088: /**
089: * Add metadata about this page.
090: *
091: * @param element
092: * (Required) The metadata element.
093: * @param qualifier
094: * (May be null) The metadata qualifier.
095: * @param language
096: * (May be null) The metadata's language
097: * @param allowMultiple
098: * (Required) determine if multipe metadata element with the same
099: * element, qualifier and language are allowed.
100: * @return A new metadata
101: */
102: public Metadata addMetadata(String element, String qualifier,
103: String language, boolean allowMultiple)
104: throws WingException {
105: Metadata metadata = new Metadata(context, element, qualifier,
106: language, allowMultiple);
107: metadatum.add(metadata);
108: return metadata;
109: }
110:
111: /**
112: * Add metadata about this page.
113: *
114: * @param element
115: * (Required) The metadata element.
116: * @param qualifier
117: * (May be null) The metadata qualifier.
118: * @param language
119: * (May be null) The metadata's language
120: * @return A new metadata
121: */
122: public Metadata addMetadata(String element, String qualifier,
123: String language) throws WingException {
124: return addMetadata(element, qualifier, language, false);
125: }
126:
127: /**
128: * Add metadata about this page.
129: *
130: * @param element
131: * (Required) The metadata element.
132: * @param qualifier
133: * (May be null) The metadata qualifier.
134: * @return A new metadata
135: */
136: public Metadata addMetadata(String element, String qualifier)
137: throws WingException {
138: return addMetadata(element, qualifier, null, false);
139: }
140:
141: /**
142: * Add metadata about this page.
143: *
144: * @param element
145: * (Required) The metadata element.
146: * @return A new metadata
147: */
148: public Metadata addMetadata(String element) throws WingException {
149: return addMetadata(element, null, null, false);
150: }
151:
152: /**
153: * Add a new trail to the page.
154: *
155: * @param target
156: * (May be null) Target URL for this trail item.
157: * @param rend
158: * (May be null) special rendering instructions
159: * @return a new trail
160: */
161: public Trail addTrail(String target, String rend)
162: throws WingException {
163: Trail trail = new Trail(context, target, rend);
164: trails.add(trail);
165: return trail;
166: }
167:
168: /**
169: * Add a new trail to the page without a link or render attribute.
170: *
171: * @return a new trail
172: */
173: public Trail addTrail() throws WingException {
174: return addTrail(null, null);
175: }
176:
177: /**
178: * Add a new trail link to the page.
179: *
180: * @param target
181: * (May be null) The Target URL for this trail item.
182: * @param characters
183: * (May be null) The textual contents of this trail item.
184: */
185: public void addTrailLink(String target, String characters)
186: throws WingException {
187: Trail trail = addTrail(target, null);
188: trail.addContent(characters);
189: }
190:
191: /**
192: * Add a new trail link to the page.
193: *
194: * @param target
195: * (May be null) The Target URL for this trail item.
196: * @param message
197: * (Required) The textual contents of this trail item to be
198: * translated
199: */
200: public void addTrailLink(String target, Message message)
201: throws WingException {
202: Trail trail = addTrail(target, null);
203: trail.addContent(message);
204: }
205:
206: /**
207: * Determine if the given SAX event is a PageMeta element.
208: *
209: * @param namespace
210: * The element's name space
211: * @param localName
212: * The local, unqualified, name for this element
213: * @param qName
214: * The qualified name for this element
215: * @param attributes
216: * The element's attributes
217: * @return True if this WingElement is equivalent to the given SAX Event.
218: */
219: public boolean mergeEqual(String namespace, String localName,
220: String qName, Attributes attributes) throws SAXException,
221: WingException {
222:
223: if (!WingConstants.DRI.URI.equals(namespace))
224: return false;
225:
226: if (!E_PAGE_META.equals(localName))
227: return false;
228: return true;
229: }
230:
231: /**
232: * Since metadata can not be merged there are no mergeable children. This
233: * just return's null.
234: *
235: * @param namespace
236: * The element's name space
237: * @param localName
238: * The local, unqualified, name for this element *
239: * @param qName
240: * The qualified name for this element
241: * @param attributes
242: * The element's attributes
243: * @return The child element
244: */
245: public WingMergeableElement mergeChild(String namespace,
246: String localName, String qName, Attributes attributes)
247: throws SAXException, WingException {
248: // We don't merge our children but we do have one special optimization,
249: // if a metadata or trail is allready in the document then we do not add
250: // our own trail or metadata for that particular item.
251: if (WingConstants.DRI.URI.equals(namespace)
252: && Trail.E_TRAIL.equals(localName)) {
253: for (Trail trail : trails)
254: trail.dispose();
255: trails.clear();
256: }
257:
258: if (WingConstants.DRI.URI.equals(namespace)
259: && Metadata.E_METADATA.equals(localName)) {
260: String element = attributes.getValue(Metadata.A_ELEMENT);
261: String qualifier = attributes
262: .getValue(Metadata.A_QUALIFIER);
263: String language = attributes.getValue(Metadata.A_LANGUAGE);
264:
265: List<Metadata> remove = new ArrayList<Metadata>();
266: for (Metadata metadata : metadatum) {
267: if (metadata.equals(element, qualifier, language)
268: && !metadata.allowMultiple()) {
269: remove.add(metadata);
270: }
271: }
272:
273: // Remove all the metadata elements we found.
274: for (Metadata metadata : remove) {
275: metadata.dispose();
276: metadatum.remove(metadata);
277: }
278: }
279:
280: return null;
281: }
282:
283: /**
284: * Inform this element that it is being merged with an existing element.
285: */
286: public Attributes merge(Attributes attributes) throws SAXException,
287: WingException {
288: this .merged = true;
289: return attributes;
290: }
291:
292: /**
293: * Translate this element into SAX events.
294: *
295: * @param contentHandler
296: * (Required) The registered contentHandler where SAX events
297: * should be routed too.
298: * @param lexicalHandler
299: * (Required) The registered lexicalHandler where lexical
300: * events (such as CDATA, DTD, etc) should be routed too.
301: * @param namespaces
302: * (Required) SAX Helper class to keep track of namespaces able
303: * to determine the correct prefix for a given namespace URI.
304: */
305: public void toSAX(ContentHandler contentHandler,
306: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
307: throws SAXException {
308: if (!merged) {
309: startElement(contentHandler, namespaces, E_PAGE_META, null);
310: }
311:
312: for (Metadata metadata : metadatum)
313: metadata.toSAX(contentHandler, lexicalHandler, namespaces);
314:
315: for (Trail trail : trails)
316: trail.toSAX(contentHandler, lexicalHandler, namespaces);
317:
318: if (!merged)
319: endElement(contentHandler, namespaces, E_PAGE_META);
320: }
321:
322: /**
323: * dispose
324: */
325: public void dispose() {
326: for (Metadata metadata : metadatum)
327: metadata.dispose();
328:
329: for (Trail trail : trails)
330: trail.dispose();
331:
332: trails.clear();
333: trails = null;
334: metadatum.clear();
335: metadatum = null;
336:
337: super.dispose();
338: }
339: }
|