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
47 package org.exolab.castor.dsml.mozilla;
48
49
50 import java.util.Enumeration;
51 import java.util.Vector;
52 import org.xml.sax.SAXException;
53 import org.xml.sax.AttributeList;
54 import org.xml.sax.HandlerBase;
55 import netscape.ldap.LDAPEntry;
56 import netscape.ldap.LDAPAttribute;
57 import netscape.ldap.LDAPAttributeSet;
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
68
69
70 class MozillaEntryConsumer
71 extends HandlerBase
72 {
73
74
75 private String _entryDN;
76
77
78 private LDAPAttributeSet _attrSet;
79
80
81 private LDAPAttribute _attr;
82
83
84 private StringBuffer _value;
85
86
87 private Base64Decoder _decoder;
88
89
90 private Vector _entries = new Vector();
91
92
93 MozillaEntryConsumer()
94 {
95 }
96
97
98 public Enumeration getEntries()
99 {
100 return _entries.elements();
101 }
102
103
104 public void startElement( String tagName, AttributeList attr )
105 throws SAXException
106 {
107 if ( tagName.equals( XML.Entries.ELEMENT ) ) {
108
109 } else if ( tagName.equals( XML.Entries.Elements.ENTRY ) ) {
110 if ( _attrSet != null )
111 throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
112 _attrSet = new LDAPAttributeSet();
113 _entryDN = attr.getValue( XML.Entries.Attributes.DN );
114 } else if ( tagName.equals( XML.Entries.Elements.OBJECT_CLASS ) ) {
115 if ( _attrSet == null || _attr != null )
116 throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
117 _attr = new LDAPAttribute( "objectclass" );
118 } else if ( tagName.equals( XML.Entries.Elements.ATTRIBUTE ) ) {
119 if ( _attrSet == null || _attr != null )
120 throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
121 _attr = new LDAPAttribute( attr.getValue( XML.Entries.Attributes.NAME ) );
122 } else if ( tagName.equals( XML.Entries.Elements.VALUE ) ||
123 tagName.equals( XML.Entries.Elements.OBJECT_CLASS_VALUE ) ) {
124 if ( _attrSet == null || _attr == null || _value != null )
125 throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
126 if ( XML.Entries.Attributes.Encodings.BASE64.equals(
127 attr.getValue( XML.Entries.Attributes.ENCODING ) ) ) {
128 _decoder = new Base64Decoder();
129 } else {
130 _value = new StringBuffer();
131 }
132 } else {
133 throw new SAXException( Messages.format( "dsml.openingTagNotRecognized", tagName ) );
134 }
135 }
136
137
138 public void endElement( String tagName )
139 throws SAXException
140 {
141 if ( tagName.equals( XML.Entries.ELEMENT ) ) {
142 if ( _attrSet != null )
143 throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
144 } else if ( tagName.equals( XML.Entries.Elements.ENTRY ) ) {
145 if ( _attrSet == null || _attr != null )
146 throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
147 _entries.addElement( new LDAPEntry( _entryDN, _attrSet ) );
148 _entryDN = null;
149 _attrSet = null;
150 } else if ( tagName.equals( XML.Entries.Elements.OBJECT_CLASS ) ||
151 tagName.equals( XML.Entries.Elements.ATTRIBUTE ) ) {
152 if ( _attrSet == null || _attr == null || _value != null )
153 throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
154 _attrSet.add( _attr );
155 _attr = null;
156 } else if ( tagName.equals( XML.Entries.Elements.VALUE ) ||
157 tagName.equals( XML.Entries.Elements.OBJECT_CLASS_VALUE ) ) {
158 if ( _attrSet == null || _attr == null || ( _value == null && _decoder == null ) )
159 throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
160 if ( _decoder != null ) {
161 _attr.addValue( _decoder.getByteArray() );
162 _decoder = null;
163 } else {
164 _attr.addValue( _value.toString() );
165 _value = null;
166 }
167 } else {
168 throw new SAXException( Messages.format( "dsml.closingTagNotRecognized", tagName ) );
169 }
170 }
171
172 public void characters(final char[] chars, final int offset, final int length) {
173 if (_decoder != null) {
174 _decoder.translate(new String(chars, offset, length));
175 } else if (_value != null) {
176 _value.append(chars, offset, length);
177 }
178 }
179 }