1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.exolab.castor.dsml.jndi;
47
48 import java.util.Enumeration;
49 import java.util.Vector;
50 import org.xml.sax.SAXException;
51 import org.xml.sax.AttributeList;
52 import org.xml.sax.HandlerBase;
53 import javax.naming.directory.Attribute;
54 import javax.naming.directory.Attributes;
55 import javax.naming.directory.BasicAttribute;
56 import javax.naming.directory.BasicAttributes;
57 import javax.naming.directory.SearchResult;
58
59 import org.castor.core.util.Base64Decoder;
60 import org.castor.core.util.Messages;
61 import org.exolab.castor.dsml.XML;
62
63
64
65
66
67 class JNDIEntryConsumer extends HandlerBase {
68 private String _entryDN;
69 private Attributes _attrSet;
70 private Attribute _attr;
71 private StringBuffer _value;
72 private Base64Decoder _decoder;
73 private Vector<SearchResult> _entries = new Vector<SearchResult>();
74
75 JNDIEntryConsumer() {
76 }
77
78 public Enumeration<SearchResult> getSearchResults() {
79 return _entries.elements();
80 }
81
82 public void startElement(final String tagName, final AttributeList attr) throws SAXException {
83 if (tagName.equals(XML.Entries.ELEMENT)) {
84
85 } else if (tagName.equals(XML.Entries.Elements.ENTRY)) {
86 if (_attrSet != null) {
87 throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
88 }
89 _attrSet = new BasicAttributes();
90 _entryDN = attr.getValue(XML.Entries.Attributes.DN);
91 } else if (tagName.equals(XML.Entries.Elements.OBJECT_CLASS)) {
92 if (_attrSet == null || _attr != null) {
93 throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
94 }
95 _attr = new BasicAttribute("objectclass");
96 } else if (tagName.equals(XML.Entries.Elements.ATTRIBUTE)) {
97 if (_attrSet == null || _attr != null) {
98 throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
99 }
100 _attr = new BasicAttribute(attr.getValue(XML.Entries.Attributes.NAME));
101 } else if (tagName.equals(XML.Entries.Elements.VALUE)
102 || tagName.equals(XML.Entries.Elements.OBJECT_CLASS_VALUE)) {
103 if (_attrSet == null || _attr == null || _value != null) {
104 throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
105 }
106 if (XML.Entries.Attributes.Encodings.BASE64.equals(
107 attr.getValue(XML.Entries.Attributes.ENCODING))) {
108 _decoder = new Base64Decoder();
109 } else {
110 _value = new StringBuffer();
111 }
112 } else {
113 throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
114 }
115 }
116
117 public void endElement(final String tagName) throws SAXException {
118 if (tagName.equals(XML.Entries.ELEMENT)) {
119 if (_attrSet != null) {
120 throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
121 }
122 } else if (tagName.equals(XML.Entries.Elements.ENTRY)) {
123 if (_attrSet == null || _attr != null) {
124 throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
125 }
126 _entries.addElement(new SearchResult(_entryDN, null, _attrSet));
127 _entryDN = null;
128 _attrSet = null;
129 } else if (tagName.equals(XML.Entries.Elements.OBJECT_CLASS)
130 || tagName.equals(XML.Entries.Elements.ATTRIBUTE)) {
131 if (_attrSet == null || _attr == null || _value != null) {
132 throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
133 }
134 _attrSet.put(_attr);
135 _attr = null;
136 } else if (tagName.equals(XML.Entries.Elements.VALUE)
137 || tagName.equals(XML.Entries.Elements.OBJECT_CLASS_VALUE)) {
138 if (_attrSet == null || _attr == null || (_value == null && _decoder == null)) {
139 throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
140 }
141 if (_decoder != null) {
142 _attr.add(_decoder.getByteArray());
143 _decoder = null;
144 } else {
145 _attr.add(_value.toString());
146 _value = null;
147 }
148 } else {
149 throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
150 }
151 }
152
153 public void characters(final char[] chars, final int offset, final int length) {
154 if (_decoder != null) {
155 _decoder.translate(new String(chars, offset, length));
156 } else if (_value != null) {
157 _value.append(chars, offset, length);
158 }
159 }
160 }