Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/util/DSMLWriter.java

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38 package org.ietf.ldap.util;
39
40 import java.io.PrintWriter;
41 import java.io.UnsupportedEncodingException;
42 import java.util.Enumeration;
43
44 import org.ietf.ldap.LDAPAttribute;
45 import org.ietf.ldap.LDAPAttributeSchema;
46 import org.ietf.ldap.LDAPEntry;
47 import org.ietf.ldap.LDAPObjectClassSchema;
48 import org.ietf.ldap.LDAPSchema;
49
50 /**
51 * Class for outputting LDAP entries to a stream as DSML.
52 *
53 * @version 1.0
54 */
55 public class DSMLWriter extends LDAPWriter {
56
57 // static final long serialVersionUID = -2710382547996750924L;
58
59 /**
60 * Constructs a <CODE>DSMLWriter</CODE> object to output entries
61 * to a stream as DSML.
62 *
63 * @param pw output stream
64 */
65 public DSMLWriter( PrintWriter pw ) {
66 super( pw );
67 }
68
69 /**
70 * Prints the schema from an entry containing subschema
71 *
72 * entry entry containing schema definitions
73 */
74 public void printSchema( LDAPEntry entry ) {
75 LDAPSchema schema = new LDAPSchema( entry );
76 printString( " <dsml:directory-schema>" );
77 printObjectClassSchema( schema );
78 printAttributeSchema( schema );
79 printString( " </dsml:directory-schema>" );
80 }
81
82
83 /**
84 * Prints the object class schema from a schema object
85 *
86 * schema schema elements
87 */
88 protected void printObjectClassSchema( LDAPSchema schema ) {
89 Enumeration<LDAPObjectClassSchema> en = schema.getObjectClassSchemas();
90 while( en.hasMoreElements() ) {
91 LDAPObjectClassSchema s = en.nextElement();
92 printString( " <dsml:class" );
93 printString( " id=\"" + s.getNames()[0] + "\"" );
94 printString( " oid=\"" + s.getID() + "\"" );
95 String[] superiors = s.getSuperiors();
96 if ( superiors != null ) {
97 for( int i = 0; i < superiors.length; i++ ) {
98 printString( " superior=\"#" + superiors[i] + "\"" );
99 }
100 }
101 String classType = "structural";
102 switch( s.getType() ) {
103 case LDAPObjectClassSchema.ABSTRACT: classType = "abstract";
104 break;
105 case LDAPObjectClassSchema.AUXILIARY: classType = "auxiliary";
106 break;
107 }
108 printString( " type=\"" + classType + "\">" );
109 if ( s.isObsolete() ) {
110 printString( " obsolete=true" );
111 }
112 printString( " <dsml:name>" + s.getNames()[0] + "</dsml:name>" );
113 printString( " <dsml:description>" + s.getDescription() +
114 "</dsml:description>" );
115 Enumeration<String> attrs = s.getRequiredAttributes();
116 while( attrs.hasMoreElements() ) {
117 printString( " <dsml:attribute ref=\"#" +
118 attrs.nextElement() +
119 "\" required=\"true\"/>" );
120 }
121 attrs = s.getOptionalAttributes();
122 while( attrs.hasMoreElements() ) {
123 printString( " <dsml:attribute ref=\"#" +
124 attrs.nextElement() +
125 "\" required=\"false\"/>" );
126 }
127 printString( " </dsml:class>" );
128 }
129 }
130
131
132 /**
133 * Prints the attribute schema from a schema object
134 *
135 * schema schema elements
136 */
137 protected void printAttributeSchema( LDAPSchema schema ) {
138 Enumeration<LDAPAttributeSchema> en = schema.getAttributeSchemas();
139 while( en.hasMoreElements() ) {
140 LDAPAttributeSchema s = en.nextElement();
141 printString( " <dsml:attribute-type" );
142 printString( " id=\"" + s.getNames()[0] + "\"" );
143 printString( " oid=\"" + s.getID() + "\"" );
144 String superior = s.getSuperior();
145 if ( superior != null ) {
146 printString( " superior=\"#" + superior + "\"" );
147 }
148 if ( s.isSingleValued() ) {
149 printString( " single-value=true" );
150 }
151 if ( s.isObsolete() ) {
152 printString( " obsolete=true" );
153 }
154 if ( s.getQualifier( LDAPAttributeSchema.NO_USER_MODIFICATION ) != null ) {
155 printString( " user-modification=false" );
156 }
157 String[] vals = s.getQualifier( LDAPAttributeSchema.EQUALITY );
158 if ( (vals != null) && (vals.length > 0) ) {
159 printString( " equality=" + vals[0] );
160 }
161 vals = s.getQualifier( LDAPAttributeSchema.ORDERING );
162 if ( (vals != null) && (vals.length > 0) ) {
163 printString( " ordering=" + vals[0] );
164 }
165 vals = s.getQualifier( LDAPAttributeSchema.SUBSTR );
166 if ( (vals != null) && (vals.length > 0) ) {
167 printString( " substring=" + vals[0] );
168 }
169 printString( " <dsml:name>" + s.getNames()[0] + "</dsml:name>" );
170 printString( " <dsml:description>" + s.getDescription() +
171 "</dsml:description>" );
172 printString( " <dsml:syntax>" + s.getSyntaxString() +
173 "</dsml:syntax>" );
174 printString( " </dsml:attribute-type>" );
175 }
176 }
177
178
179 /**
180 * Print an attribute of an entry
181 *
182 * @param attr the attribute to format to the output stream
183 */
184 protected void printAttribute( LDAPAttribute attr ) {
185 String attrName = attr.getName();
186
187 // Object classes are treated differently in DSML. Also, they
188 // are always String-valued
189 if ( attrName.equalsIgnoreCase( "objectclass" ) ) {
190 Enumeration<String> enumVals = attr.getStringValues();
191 if ( enumVals != null ) {
192 while ( enumVals.hasMoreElements() ) {
193 String s = enumVals.nextElement();
194 printString( " <dsml:objectclass>" + s +
195 "</dsml:objectclass>" );
196 }
197 }
198 return;
199 }
200
201 printString( " <dsml:attr name=\"" + attrName + "\">" );
202
203 /* Loop on values for this attribute */
204 Enumeration<byte[]> enumVals = attr.getByteValues();
205
206 if ( enumVals != null ) {
207 while ( enumVals.hasMoreElements() ) {
208 byte[] b = enumVals.nextElement();
209 String s;
210 if ( LDIF.isPrintable(b) ) {
211 try {
212 s = new String( b, "UTF8" );
213 } catch ( UnsupportedEncodingException e ) {
214 s = "";
215 }
216 printEscapedValue( " <dsml:value>", s,
217 "</dsml:value>" );
218 } else {
219 s = getPrintableValue( b );
220 if ( s.length() > 0 ) {
221 printString( " " +
222 "<dsml:value encoding=\"base64\">" );
223 printString( " " + s );
224 printString( " </dsml:value>" );
225 }
226 }
227 }
228 }
229 printString( " </dsml:attr>" );
230 }
231
232 /**
233 * Print prologue to entry
234 *
235 * @param dn the DN of the entry
236 */
237 protected void printEntryStart( String dn ) {
238 if ( dn == null ) {
239 dn = "";
240 }
241 printString( " <dsml:entry dn=\"" + dn + "\">" );
242 }
243
244 /**
245 * Print epilogue to entry
246 *
247 * @param dn the DN of the entry
248 */
249 protected void printEntryEnd( String dn ) {
250 printString( " </dsml:entry>" );
251 }
252
253 /**
254 * Print the element start, the value with escaping of special
255 * characters, and the element end
256 *
257 * @param prolog element start
258 * @param value value to be escaped
259 * @param epilog element end
260 */
261 protected void printEscapedValue( String prolog, String value,
262 String epilog ) {
263 m_pw.print( prolog );
264 int l = value.length();
265 char[] text = new char[l];
266 value.getChars( 0, l, text, 0 );
267 for ( int i = 0; i < l; i++ ) {
268 char c = text[i];
269 switch (c) {
270 case '<' :
271 m_pw.print( "&lt;" );
272 break;
273 case '&' :
274 m_pw.print( "&amp;" );
275 break;
276 default :
277 m_pw.print( c );
278 }
279 }
280 m_pw.print( epilog);
281 m_pw.print( '\n' );
282 }
283
284 protected void printString( String value ) {
285 m_pw.print( value );
286 m_pw.print( '\n' );
287 }
288 }