001: /*
002: * Body.java
003: *
004: * Version: $Revision: 1.8 $
005: *
006: * Date: $Date: 2006/03/13 17:19:39 $
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.WingConstants;
047: import org.dspace.app.xmlui.wing.WingContext;
048: import org.dspace.app.xmlui.wing.WingException;
049: import org.xml.sax.Attributes;
050: import org.xml.sax.ContentHandler;
051: import org.xml.sax.SAXException;
052: import org.xml.sax.ext.LexicalHandler;
053: import org.xml.sax.helpers.NamespaceSupport;
054:
055: /**
056: * A class representing the page division.
057: *
058: * The body contains any number of divisions (div elements) which group content
059: * into interactive and non interactive display blocks.
060: *
061: * @author Scott Phillips
062: */
063: public class Body extends AbstractWingElement implements
064: WingMergeableElement {
065: /** The name of the body element */
066: public static final String E_BODY = "body";
067:
068: /** Has this element been merged */
069: private boolean merged = false;
070:
071: /** The divisions contained within this body */
072: private List<Division> divisions = new ArrayList<Division>();
073:
074: /**
075: * Generate a new Body framework element. This method will NOT open or close
076: * a body element instead it expects that those events are being handled by
077: * the caller. It is important to note that other divisions (div elements)
078: * may precede or follow the divisions created through this object.
079: *
080: * @param context
081: * (Required) The context this element is contained in.
082: */
083: protected Body(WingContext context) throws WingException {
084: super (context);
085: }
086:
087: /**
088: * Append a new division (div element) to the document's body. The division
089: * created is not interactive meaning that it may not contain any form
090: * elements, to create an interactive division use addInteractiveDivision().
091: *
092: * @param name
093: * (Required) a local identifier used to differentiate the
094: * element from its siblings.
095: * @param rend
096: * (May be null) a rendering hint used to override the default
097: * display of the element.
098: * @return a new division.
099: */
100: public Division addDivision(String name, String rend)
101: throws WingException {
102: Division div = new Division(context, name, rend);
103: divisions.add(div);
104: return div;
105: }
106:
107: /**
108: * Append a new division (div element) to the document's body. This is a
109: * short cut method for divisions with out special rendering instructions.
110: * The division created is not interactive meaning that it may not contain
111: * any form elements, to create an interactive division use
112: * addInteractiveDivision().
113: *
114: * @param name
115: * a local identifier used to differentiate the element from its
116: * siblings
117: * @return A new division.
118: */
119: public Division addDivision(String name) throws WingException {
120: return this .addDivision(name, null);
121: }
122:
123: /**
124: * Append a new interactive division (div element) to the document's body.
125: * An interactive division is able to contain form elements as. The extra
126: * parameters required such as action and method dictate where and how the
127: * form data should be processed.
128: *
129: * @param name
130: * (Required) a local identifier used to differentiate the
131: * element from its siblings.
132: * @param action
133: * (Required) determines where the form information should be
134: * sent for processing.
135: * @param method
136: * (Required) determines the method used to pass gathered field
137: * values to the handler specified by the action attribute. The
138: * multipart method should be used if there are any file fields
139: * used within the division.
140: * @param rend
141: * (May be null) a rendering hint used to override the default
142: * display of the element.
143: * @return A new division.
144: */
145: public Division addInteractiveDivision(String name, String action,
146: String method, String rend) throws WingException {
147: Division div = new Division(context, name, action, method, rend);
148: divisions.add(div);
149: return div;
150:
151: }
152:
153: /**
154: * Is this SAX event equivalent to this body?
155: *
156: * @param namespace
157: * The element's name space
158: * @param localName
159: * The local, unqualified, name for this element
160: * @param qName
161: * The qualified name for this element
162: * @param attributes
163: * The element's attributes
164: * @return True if it is equivalent.
165: */
166: public boolean mergeEqual(String namespace, String localName,
167: String qName, Attributes attributes) throws SAXException,
168: WingException {
169: if (!WingConstants.DRI.URI.equals(namespace))
170: return false;
171: if (!E_BODY.equals(localName))
172: return false;
173: return true;
174: }
175:
176: /**
177: * Merge this SAX event into the body.
178: *
179: * @param namespace
180: * The element's name space
181: * @param localName
182: * The local, unqualified, name for this element
183: * @param qName
184: * The qualified name for this element
185: * @param attributes
186: * The element's attributes
187: * @return
188: */
189: public WingMergeableElement mergeChild(String namespace,
190: String localName, String qName, Attributes attributes)
191: throws SAXException, WingException {
192: Division found = null;
193: for (Division candidate : divisions) {
194: if (candidate.mergeEqual(namespace, localName, qName,
195: attributes))
196: found = candidate;
197:
198: }
199: divisions.remove(found);
200: return found;
201: }
202:
203: /**
204: * Inform this element that it is being merged with an existing body.
205: *
206: * @param attributes
207: * The to-be-merged attributes
208: */
209: public Attributes merge(Attributes attributes) throws SAXException,
210: WingException {
211: this .merged = true;
212: return attributes;
213: }
214:
215: /**
216: * Translate into SAX
217: *
218: * @param contentHandler
219: * (Required) The registered contentHandler where SAX events
220: * should be routed too.
221: * @param lexicalHandler
222: * (Required) The registered lexicalHandler where lexical events
223: * (such as CDATA, DTD, etc) should be routed too.
224: * @param namespaces
225: * (Required) SAX Helper class to keep track of namespaces able
226: * to determine the correct prefix for a given namespace URI.
227: */
228: public void toSAX(ContentHandler contentHandler,
229: LexicalHandler lexicalHandler, NamespaceSupport namespaces)
230: throws SAXException {
231: if (!merged)
232: startElement(contentHandler, namespaces, E_BODY, null);
233:
234: for (Division division : divisions)
235: division.toSAX(contentHandler, lexicalHandler, namespaces);
236:
237: if (!merged)
238: startElement(contentHandler, namespaces, E_BODY, null);
239: }
240:
241: /**
242: * dispose
243: */
244: public void dispose() {
245: for (Division division : divisions)
246: division.dispose();
247: super.dispose();
248: }
249:
250: }
|