001: /*
002: * SAXFilter.java
003: *
004: * Version: $Revision: 1.3 $
005: *
006: * Date: $Date: 2006/04/26 18:26:46 $
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.objectmanager;
042:
043: import org.xml.sax.Attributes;
044: import org.xml.sax.ContentHandler;
045: import org.xml.sax.Locator;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.ext.LexicalHandler;
048: import org.xml.sax.helpers.NamespaceSupport;
049:
050: /**
051: * This is a swiss army like SAX Filter, it's purpose is to filter out
052: * undesierable SAX events from the stream. The primary application of
053: * this is for inserting SAX fragment into an existing SAX pipeline,
054: * under this senario you would not want new startDocument or
055: * endDocument events interfering with the existing pipeline thus
056: * this class can filter those out.
057: *
058: * The swiss army part comes in because it's configurable. Instead of
059: * defining a static set of events that are filled out by default all
060: * events are filled out and must be turned on to allow each type
061: * individualy.
062: *
063: * Primarily you can filter events based upon their type, i.e. start/end
064: * elements or start/end documents. However there is one special control,
065: * and that is to only allow elements below a minimum level.
066: * .
067: *
068: * @author Scott Phillips
069: */
070: public class SAXFilter implements ContentHandler, LexicalHandler {
071:
072: /** Control for which type of SAX events to allow */
073: private boolean allowDocuments = false;
074: private boolean allowDocumentLocators = false;
075: private boolean allowProcessingInstructions = false;
076: private boolean allowPrefixMappings = false;
077: private boolean allowElements = false;
078: private boolean allowIgnorableWhitespace = false;
079: private boolean allowSkippedEntities = false;
080: private boolean allowCharacters = false;
081:
082: private boolean allowDTDs = false;
083: private boolean allowEntities = false;
084: private boolean allowCDATA = false;
085: private boolean allowComments = false;
086:
087: /** The minimum level an element must be before it will be allowed */
088: private int minimumElementLevel = -1;
089:
090: /**
091: * The current XML level, each time start element is encountered this
092: * is increased, and each time an end element is encountered it is
093: * decressed.
094: */
095: private int currentElementLevel = 0;
096:
097: /**
098: * If no uri is provided then substitute this default prefix and URI:
099: */
100: private String defaultURI;
101:
102: /** The sax handlers and namespace support */
103: private ContentHandler contentHandler;
104: private LexicalHandler lexicalHandler;
105: private NamespaceSupport namespaces;
106:
107: /**
108: * Construct a new SAXFilter such that the allowed events will be routed
109: * to the corresponding content and lexcial handlers.
110: *
111: * @param contentHandler The SAX content handler.
112: * @param lexicalHandler The SAX lexical handler.
113: * @param namespaces Namespace support which records what prefixes have been defined.
114: */
115: public SAXFilter(ContentHandler contentHandler,
116: LexicalHandler lexicalHandler, NamespaceSupport namespaces) {
117: this .contentHandler = contentHandler;
118: this .lexicalHandler = lexicalHandler;
119: this .namespaces = namespaces;
120: }
121:
122: /** Allow start/end document events */
123: public SAXFilter allowDocuments() {
124: this .allowDocuments = true;
125: return this ;
126: }
127:
128: /** Allow document locator events */
129: public SAXFilter allowDocumentLocators() {
130: this .allowDocumentLocators = true;
131: return this ;
132: }
133:
134: /** Allow processing instruction events */
135: public SAXFilter allowProcessingInstructions() {
136: this .allowProcessingInstructions = true;
137: return this ;
138: }
139:
140: /** allow start/end prefix mapping events */
141: public SAXFilter allowPrefixMappings() {
142: this .allowPrefixMappings = true;
143: return this ;
144: }
145:
146: /** allow start/end element events */
147: public SAXFilter allowElements() {
148: this .allowElements = true;
149: return this ;
150: }
151:
152: /**
153: * Allow start/end element events.
154: *
155: * However only allow those start / end events if
156: * they are below the given XML level. I.e. each nested
157: * element is a new level.
158: *
159: * @param belowElementLevel
160: * the minumum level required.
161: * @return
162: */
163: public SAXFilter allowElements(int minimumElementLevel) {
164: this .allowElements = true;
165: this .minimumElementLevel = minimumElementLevel;
166: return this ;
167: }
168:
169: /** Allow ignorable whitespace events */
170: public SAXFilter allowIgnorableWhitespace() {
171: this .allowIgnorableWhitespace = true;
172: return this ;
173: }
174:
175: /** Allow start / end events for skipped entities */
176: public SAXFilter allowSkippedEntities() {
177: this .allowSkippedEntities = true;
178: return this ;
179: }
180:
181: /** Allow character events */
182: public SAXFilter allowCharacters() {
183: this .allowCharacters = true;
184: return this ;
185: }
186:
187: /** Allow DTD events */
188: public SAXFilter allowDTDs() {
189: this .allowDTDs = true;
190: return this ;
191: }
192:
193: /** Allow XML entities events */
194: public SAXFilter allowEntities() {
195: this .allowEntities = true;
196: return this ;
197: }
198:
199: /** Allow CDATA events */
200: public SAXFilter allowCDATA() {
201: this .allowCDATA = true;
202: return this ;
203: }
204:
205: /** Allow comment events */
206: public SAXFilter allowComments() {
207: this .allowComments = true;
208: return this ;
209: }
210:
211: /**
212: * Add a default namespace is none is provided. The namespace
213: * should have allready been declared (add added to the
214: * namespace support object
215: *
216: * @param uri the default namespace uri.
217: */
218: public SAXFilter setDefaultNamespace(String uri) {
219: this .defaultURI = uri;
220: return this ;
221: }
222:
223: /**
224: * SAX Content events
225: */
226:
227: public void startDocument() throws SAXException {
228: if (allowDocuments)
229: contentHandler.startDocument();
230: }
231:
232: public void endDocument() throws SAXException {
233: if (allowDocuments)
234: contentHandler.endDocument();
235: }
236:
237: public void setDocumentLocator(Locator locator) {
238: if (allowDocumentLocators)
239: contentHandler.setDocumentLocator(locator);
240: }
241:
242: public void processingInstruction(String target, String data)
243: throws SAXException {
244: if (allowProcessingInstructions)
245: contentHandler.processingInstruction(target, data);
246: }
247:
248: public void startPrefixMapping(String prefix, String uri)
249: throws SAXException {
250: if (allowPrefixMappings)
251: contentHandler.startPrefixMapping(prefix, uri);
252: }
253:
254: public void endPrefixMapping(String prefix) throws SAXException {
255: if (allowPrefixMappings)
256: contentHandler.endPrefixMapping(prefix);
257: }
258:
259: public void startElement(String uri, String localName,
260: String qName, Attributes atts) throws SAXException {
261: if (allowElements) {
262: currentElementLevel++;
263: // check if we are past the minimum level requirement
264: if (minimumElementLevel < currentElementLevel) {
265: if (defaultURI != null
266: && (uri == null || "".equals(uri))) {
267: // No namespace provided, use the default namespace.
268: String prefix = namespaces.getPrefix(defaultURI);
269:
270: if (!(prefix == null || "".equals(prefix))) {
271: qName = prefix + ":" + localName;
272: }
273:
274: contentHandler.startElement(defaultURI, localName,
275: qName, atts);
276: } else {
277: // let the event pass through unmodified.
278: contentHandler.startElement(uri, localName,
279: localName, atts);
280: }
281: }
282: }
283: }
284:
285: public void endElement(String uri, String localName, String qName)
286: throws SAXException {
287: if (allowElements) {
288: // check if we are past the minimum level requirements
289: if (minimumElementLevel < currentElementLevel) {
290: if (defaultURI != null
291: && (uri == null || "".equals(uri))) {
292: // No namespace provided, use the default namespace.
293: String prefix = namespaces.getPrefix(defaultURI);
294:
295: if (!(prefix == null || "".equals(prefix))) {
296: qName = prefix + ":" + localName;
297: }
298:
299: contentHandler.endElement(defaultURI, localName,
300: qName);
301: } else {
302: // Let the event pass through unmodified.
303: contentHandler
304: .endElement(uri, localName, localName);
305: }
306: }
307: currentElementLevel--;
308: }
309: }
310:
311: public void ignorableWhitespace(char[] ch, int start, int length)
312: throws SAXException {
313: if (allowIgnorableWhitespace)
314: contentHandler.ignorableWhitespace(ch, start, length);
315: }
316:
317: public void skippedEntity(String name) throws SAXException {
318: if (allowSkippedEntities)
319: contentHandler.skippedEntity(name);
320: }
321:
322: public void characters(char[] ch, int start, int length)
323: throws SAXException {
324: if (allowCharacters)
325: contentHandler.characters(ch, start, length);
326: }
327:
328: /**
329: * SAX Lexical events
330: */
331:
332: public void startDTD(String name, String publicId, String systemId)
333: throws SAXException {
334: if (allowDTDs)
335: lexicalHandler.startDTD(name, publicId, systemId);
336: }
337:
338: public void endDTD() throws SAXException {
339: if (allowDTDs)
340: lexicalHandler.endDTD();
341: }
342:
343: public void startEntity(String name) throws SAXException {
344: if (allowEntities)
345: lexicalHandler.startEntity(name);
346: }
347:
348: public void endEntity(String name) throws SAXException {
349: if (allowEntities)
350: lexicalHandler.endEntity(name);
351: }
352:
353: public void startCDATA() throws SAXException {
354: if (allowCDATA)
355: lexicalHandler.startCDATA();
356: }
357:
358: public void endCDATA() throws SAXException {
359: if (allowCDATA)
360: lexicalHandler.endCDATA();
361: }
362:
363: public void comment(char[] ch, int start, int length)
364: throws SAXException {
365: if (allowComments)
366: lexicalHandler.comment(ch, start, length);
367: }
368: }
|