001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: Producer.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.dsml;
045:
046: import org.xml.sax.DocumentHandler;
047: import org.xml.sax.SAXException;
048: import org.xml.sax.helpers.AttributeListImpl;
049:
050: /**
051: *
052: *
053: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
054: * @version $Revision: 5951 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
055: */
056: public abstract class Producer {
057:
058: protected DocumentHandler _docHandler;
059:
060: private boolean _namespace;
061:
062: private boolean _insideDirectory;
063:
064: private boolean _insideSchema;
065:
066: public Producer(DocumentHandler docHandler, boolean namespace) {
067: _docHandler = docHandler;
068: _namespace = namespace;
069: }
070:
071: public void startDocument() throws SAXException {
072: AttributeListImpl attrList;
073:
074: attrList = new AttributeListImpl();
075: if (_namespace)
076: attrList.addAttribute("xmlns:" + XML.Namespace.Prefix,
077: "CDATA", XML.Namespace.URI);
078: else
079: attrList.addAttribute("xmlns", "CDATA", XML.Namespace.URI);
080: _docHandler.startElement(prefix(XML.Namespace.Root), attrList);
081:
082: }
083:
084: public void endDocument() throws SAXException {
085: leaveDirectory();
086: leaveSchema();
087: _docHandler.endElement(prefix(XML.Namespace.Root));
088: }
089:
090: public void produce(ImportDescriptor importDesc)
091: throws SAXException {
092: leaveDirectory();
093: leaveSchema();
094: importDesc.produce(_docHandler);
095: }
096:
097: public void produce(SearchDescriptor searchDesc)
098: throws SAXException {
099: leaveDirectory();
100: leaveSchema();
101: searchDesc.produce(_docHandler);
102: }
103:
104: protected void enterDirectory() throws SAXException {
105: // If not inside directory element, start it
106: if (!_insideDirectory) {
107: _insideDirectory = true;
108: _docHandler.startElement(prefix(XML.Entries.Element),
109: new AttributeListImpl());
110: }
111: }
112:
113: protected void leaveDirectory() throws SAXException {
114: // If inside directory element, end it
115: if (_insideDirectory) {
116: _insideDirectory = false;
117: _docHandler.endElement(prefix(XML.Entries.Element));
118: }
119: }
120:
121: protected void enterSchema() throws SAXException {
122: // If not inside schema element, start it
123: if (!_insideSchema) {
124: _insideSchema = true;
125: _docHandler.startElement(prefix(XML.Schema.Element),
126: new AttributeListImpl());
127: }
128: }
129:
130: protected void leaveSchema() throws SAXException {
131: // If not inside schema element, end it
132: if (_insideSchema) {
133: _insideSchema = false;
134: _docHandler.endElement(prefix(XML.Schema.Element));
135: }
136: }
137:
138: protected String prefix(String tagName) {
139: if (_namespace)
140: return XML.Namespace.Prefix + ":" + tagName;
141: return tagName;
142: }
143:
144: }
|