1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.configuration;
21
22 import org.apache.commons.collections.map.MultiValueMap;
23 import org.apache.directory.server.core.DirectoryService;
24 import org.apache.directory.shared.ldap.entry.Entry;
25 import org.apache.directory.shared.ldap.entry.EntryAttribute;
26 import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
27 import org.apache.directory.shared.ldap.ldif.LdifComposer;
28 import org.apache.directory.shared.ldap.ldif.LdifComposerImpl;
29 import org.apache.directory.shared.ldap.ldif.LdifReader;
30 import org.apache.directory.shared.ldap.util.AttributeUtils;
31 import org.apache.directory.shared.ldap.util.StringTools;
32
33 import javax.naming.NamingEnumeration;
34 import javax.naming.NamingException;
35 import javax.naming.directory.Attribute;
36 import javax.naming.directory.Attributes;
37 import java.beans.PropertyEditor;
38 import java.beans.PropertyEditorSupport;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.StringReader;
42 import java.util.Map;
43
44
45
46
47
48
49
50
51
52
53
54 public class AttributesPropertyEditor extends PropertyEditorSupport
55 {
56
57
58
59
60 public AttributesPropertyEditor()
61 {
62 super();
63 }
64
65
66
67
68 public AttributesPropertyEditor( Object source )
69 {
70 super( source );
71 }
72
73
74
75
76 @SuppressWarnings("deprecation")
77 public String getAsText()
78 {
79 LdifComposer composer = new LdifComposerImpl();
80 Map<String, Object> map = new MultiValueMap();
81
82 Attributes attrs = (Attributes) getValue();
83 try
84 {
85 NamingEnumeration<? extends Attribute> e = attrs.getAll();
86 while ( e.hasMore() )
87 {
88 Attribute attr = e.next();
89 NamingEnumeration<? extends Object> e2 = attr.getAll();
90 while ( e2.hasMoreElements() )
91 {
92 Object value = e2.next();
93 map.put( attr.getID(), value );
94 }
95 }
96
97 return composer.compose( map );
98 }
99 catch ( Exception e )
100 {
101 e.printStackTrace();
102 return null;
103 }
104 }
105
106
107
108
109
110
111
112 private Entry readEntry( String text )
113 {
114 StringReader strIn = new StringReader( text );
115 BufferedReader in = new BufferedReader( strIn );
116
117 String line = null;
118 Entry entry = new DefaultClientEntry();
119
120 try
121 {
122 while ( ( line = in.readLine() ) != null )
123 {
124 if ( line.length() == 0 )
125 {
126 continue;
127 }
128
129 String addedLine = line.trim();
130
131 if ( StringTools.isEmpty( addedLine ) )
132 {
133 continue;
134 }
135
136 EntryAttribute attribute = AttributeUtils.toClientAttribute( LdifReader.parseAttributeValue( addedLine ) );
137 EntryAttribute oldAttribute = entry.get( attribute.getId() );
138
139 if ( oldAttribute != null )
140 {
141 try
142 {
143 oldAttribute.add( attribute.get() );
144 entry.put( oldAttribute );
145 }
146 catch (NamingException ne)
147 {
148
149 }
150 }
151 else
152 {
153 try
154 {
155 entry.put( attribute );
156 }
157 catch ( NamingException ne )
158 {
159
160 }
161 }
162 }
163 }
164 catch (IOException ioe)
165 {
166
167 }
168
169 return entry;
170 }
171
172
173
174
175 public void setAsText( String text ) throws IllegalArgumentException
176 {
177 if ( text == null )
178 {
179 text = "";
180 }
181
182 Entry entry = readEntry( text );
183 setValue( entry );
184 }
185 }