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: SearchDescriptor.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.dsml;
045:
046: import java.io.Serializable;
047: import java.util.Vector;
048: import java.util.Enumeration;
049: import org.xml.sax.DocumentHandler;
050: import org.xml.sax.AttributeList;
051: import org.xml.sax.SAXException;
052: import org.xml.sax.HandlerBase;
053: import org.xml.sax.helpers.AttributeListImpl;
054: import org.castor.util.Messages;
055:
056: /**
057: *
058: *
059: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
060: * @version $Revision: 5951 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
061: */
062: public class SearchDescriptor extends HandlerBase implements
063: Serializable {
064: /** SerialVersionUID */
065: private static final long serialVersionUID = -6614367393322175115L;
066:
067: public static class Scope {
068:
069: public static final int OneLevel = 0;
070: public static final int Base = 1;
071: public static final int SubTree = 3;
072:
073: }
074:
075: static class Names {
076: static class Element {
077: public static final String Search = "search";
078: public static final String ReturnAttr = "return-attr";
079: }
080:
081: static class Attribute {
082: public static final String AttrName = "name";
083: public static final String BaseDN = "base";
084: public static final String Scope = "scope";
085: public static final String Filter = "filter";
086: public static final String ScopeOneLevel = "onelevel";
087: public static final String ScopeBase = "base";
088: public static final String ScopeSubTree = "subtree";
089: }
090: }
091:
092: private int _scope = Scope.Base;
093:
094: private String _baseDN;
095:
096: private String _filter;
097:
098: private Vector _returnAttrs;
099:
100: private StringBuffer _attrName;
101:
102: private boolean _insideRoot;
103:
104: public SearchDescriptor() {
105: }
106:
107: public int getScope() {
108: return _scope;
109: }
110:
111: public void setScope(int scope) {
112: _scope = scope;
113: }
114:
115: public String getBaseDN() {
116: return _baseDN;
117: }
118:
119: public void setBaseDN(String baseDN) {
120: _baseDN = baseDN;
121: }
122:
123: public String getFilter() {
124: return _filter;
125: }
126:
127: public void setFilter(String filter) {
128: _filter = filter;
129: }
130:
131: public String[] getReturnAttrs() {
132: if (_returnAttrs == null)
133: return null;
134: String[] array;
135:
136: array = new String[_returnAttrs.size()];
137: _returnAttrs.copyInto(array);
138: return array;
139: }
140:
141: public Enumeration listReturnAttrs() {
142: if (_returnAttrs == null)
143: return new Vector().elements();
144:
145: return _returnAttrs.elements();
146: }
147:
148: public void addReturnAttr(String attrName) {
149: if (_returnAttrs == null)
150: _returnAttrs = new Vector();
151: if (!_returnAttrs.contains(attrName))
152: _returnAttrs.addElement(attrName);
153: }
154:
155: public void produce(DocumentHandler docHandler) throws SAXException {
156: AttributeListImpl attrList;
157: Enumeration enumeration;
158:
159: attrList = new AttributeListImpl();
160: docHandler.startElement(XML.Namespace.Root, attrList);
161:
162: attrList = new AttributeListImpl();
163: if (_baseDN != null)
164: attrList.addAttribute(Names.Attribute.BaseDN, "CDATA",
165: _baseDN);
166: if (_filter != null)
167: attrList.addAttribute(Names.Attribute.Filter, "CDATA",
168: _filter);
169: switch (_scope) {
170: case Scope.OneLevel:
171: attrList.addAttribute(Names.Attribute.Scope, null,
172: Names.Attribute.ScopeOneLevel);
173: break;
174: case Scope.Base:
175: attrList.addAttribute(Names.Attribute.Scope, null,
176: Names.Attribute.ScopeBase);
177: break;
178: case Scope.SubTree:
179: attrList.addAttribute(Names.Attribute.Scope, null,
180: Names.Attribute.ScopeSubTree);
181: break;
182: }
183: docHandler.startElement(Names.Element.Search, attrList);
184:
185: if (_returnAttrs != null) {
186: enumeration = _returnAttrs.elements();
187: while (enumeration.hasMoreElements()) {
188: attrList = new AttributeListImpl();
189: attrList.addAttribute(Names.Attribute.AttrName,
190: "NMTOKEN", (String) enumeration.nextElement());
191: docHandler.startElement(Names.Element.ReturnAttr,
192: attrList);
193: docHandler.endElement(Names.Element.ReturnAttr);
194: }
195: }
196:
197: docHandler.endElement(Names.Element.Search);
198: docHandler.endElement(XML.Namespace.Root);
199: }
200:
201: public void startElement(String tagName, AttributeList attr)
202: throws SAXException {
203: String value;
204:
205: if (tagName.equals(XML.Namespace.Root)) {
206: // Flag when entering (and leaving) the root element.
207: if (_insideRoot)
208: throw new SAXException(Messages.format(
209: "dsml.elementNested", XML.Namespace.Root));
210: _insideRoot = true;
211: } else {
212: if (!_insideRoot)
213: throw new SAXException(Messages.format(
214: "dsml.expectingOpeningTag", XML.Namespace.Root,
215: tagName));
216:
217: if (tagName.equals(Names.Element.Search)) {
218: _baseDN = attr.getValue(Names.Attribute.BaseDN);
219: if (_baseDN == null)
220: throw new SAXException(Messages.format(
221: "dsml.missingAttribute",
222: Names.Element.Search,
223: Names.Attribute.BaseDN));
224: _filter = attr.getValue(Names.Attribute.Filter);
225: value = attr.getValue(Names.Attribute.Scope);
226: if (value != null) {
227: if (value.equals(Names.Attribute.ScopeOneLevel))
228: _scope = Scope.OneLevel;
229: else if (value.equals(Names.Attribute.ScopeBase))
230: _scope = Scope.Base;
231: else if (value.equals(Names.Attribute.ScopeSubTree))
232: _scope = Scope.SubTree;
233: else
234: throw new SAXException(Messages.format(
235: "dsml.invalidValue",
236: Names.Attribute.Scope, value));
237: }
238: } else if (tagName.equals(Names.Element.ReturnAttr)) {
239: if (_baseDN == null) {
240: throw new SAXException(Messages.format(
241: "dsml.expectingOpeningTag",
242: Names.Element.Search, tagName));
243: }
244: // Create a string buffer, characters() will fill it up,
245: // endElement() will add it to the list.
246: _attrName = new StringBuffer();
247: } else {
248: throw new SAXException(Messages.format(
249: "dsml.expectingOpeningTag",
250: Names.Element.Search, tagName));
251: }
252: }
253: }
254:
255: public void endElement(String tagName) throws SAXException {
256: if (tagName.equals(XML.Namespace.Root)) {
257: if (_insideRoot)
258: _insideRoot = false;
259: else
260: throw new SAXException(Messages.format(
261: "dsml.closingOutsideRoot", tagName));
262: } else {
263: if (!_insideRoot)
264: throw new SAXException(Messages.format(
265: "dsml.closingOutsideRoot", tagName));
266: if (tagName.equals(Names.Element.Search)) {
267: // Nothing to do hare
268: } else if (tagName.equals(Names.Element.ReturnAttr)) {
269: if (_attrName.length() > 0) {
270: addReturnAttr(_attrName.toString());
271: _attrName = null;
272: }
273: } else {
274: throw new SAXException(Messages.format(
275: "dsml.expectingClosingTag",
276: Names.Element.Search, tagName));
277: }
278: }
279: }
280:
281: public void characters(char[] ch, int offset, int length) {
282: if (_attrName != null) {
283: _attrName.append(ch, offset, length);
284: }
285: }
286:
287: }
|