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