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  package org.exolab.castor.dsml.mozilla;
38  
39  
40  import java.util.Enumeration;
41  import java.util.Vector;
42  import org.xml.sax.SAXException;
43  import org.xml.sax.AttributeList;
44  import org.xml.sax.HandlerBase;
45  import netscape.ldap.LDAPEntry;
46  import netscape.ldap.LDAPAttribute;
47  import netscape.ldap.LDAPAttributeSet;
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  
58  
59  
60  class MozillaEntryConsumer extends HandlerBase {
61  
62  
63    private String _entryDN;
64  
65  
66    private LDAPAttributeSet _attrSet;
67  
68  
69    private LDAPAttribute _attr;
70  
71  
72    private StringBuffer _value;
73  
74  
75    private Base64Decoder _decoder;
76  
77  
78    private Vector _entries = new Vector();
79  
80  
81    MozillaEntryConsumer() {}
82  
83  
84    public Enumeration getEntries() {
85      return _entries.elements();
86    }
87  
88  
89    public void startElement(String tagName, AttributeList attr) throws SAXException {
90      if (tagName.equals(XML.Entries.ELEMENT)) {
91        
92      } else if (tagName.equals(XML.Entries.Elements.ENTRY)) {
93        if (_attrSet != null)
94          throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
95        _attrSet = new LDAPAttributeSet();
96        _entryDN = attr.getValue(XML.Entries.Attributes.DN);
97      } else if (tagName.equals(XML.Entries.Elements.OBJECT_CLASS)) {
98        if (_attrSet == null || _attr != null)
99          throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
100       _attr = new LDAPAttribute("objectclass");
101     } else if (tagName.equals(XML.Entries.Elements.ATTRIBUTE)) {
102       if (_attrSet == null || _attr != null)
103         throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
104       _attr = new LDAPAttribute(attr.getValue(XML.Entries.Attributes.NAME));
105     } else if (tagName.equals(XML.Entries.Elements.VALUE)
106         || tagName.equals(XML.Entries.Elements.OBJECT_CLASS_VALUE)) {
107       if (_attrSet == null || _attr == null || _value != null)
108         throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
109       if (XML.Entries.Attributes.Encodings.BASE64
110           .equals(attr.getValue(XML.Entries.Attributes.ENCODING))) {
111         _decoder = new Base64Decoder();
112       } else {
113         _value = new StringBuffer();
114       }
115     } else {
116       throw new SAXException(Messages.format("dsml.openingTagNotRecognized", tagName));
117     }
118   }
119 
120 
121   public void endElement(String tagName) throws SAXException {
122     if (tagName.equals(XML.Entries.ELEMENT)) {
123       if (_attrSet != null)
124         throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
125     } else if (tagName.equals(XML.Entries.Elements.ENTRY)) {
126       if (_attrSet == null || _attr != null)
127         throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
128       _entries.add(new LDAPEntry(_entryDN, _attrSet));
129       _entryDN = null;
130       _attrSet = null;
131     } else if (tagName.equals(XML.Entries.Elements.OBJECT_CLASS)
132         || tagName.equals(XML.Entries.Elements.ATTRIBUTE)) {
133       if (_attrSet == null || _attr == null || _value != null)
134         throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
135       _attrSet.add(_attr);
136       _attr = null;
137     } else if (tagName.equals(XML.Entries.Elements.VALUE)
138         || tagName.equals(XML.Entries.Elements.OBJECT_CLASS_VALUE)) {
139       if (_attrSet == null || _attr == null || (_value == null && _decoder == null))
140         throw new SAXException(Messages.format("dsml.closingTagNotRecognized", tagName));
141       if (_decoder != null) {
142         _attr.addValue(_decoder.getByteArray());
143         _decoder = null;
144       } else {
145         _attr.addValue(_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 }