Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/util/LDIFWriter.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 * bugzilla 62700: Joe Rank ([email protected])
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39 package org.ietf.ldap.util;
40
41 import java.io.File;
42 import java.io.FileOutputStream;
43 import java.io.IOException;
44 import java.io.PrintWriter;
45 import java.io.UnsupportedEncodingException;
46 import java.util.Enumeration;
47
48 import org.ietf.ldap.LDAPAttribute;
49
50 /**
51 * Class for outputting LDAP entries to a stream as LDIF.
52 *
53 * @version 1.0
54 */
55 public class LDIFWriter extends LDAPWriter {
56
57 // static final long serialVersionUID = -2710382547996750924L;
58 private String m_sep;
59 private boolean m_foldLines;
60 private boolean m_attrsOnly;
61 private boolean m_toFiles;
62 private static final String DEFAULT_SEPARATOR = ":";
63 private static final int MAX_LINE = 77;
64
65
66 /**
67 * Constructs an <CODE>LDIFWriter</CODE> object to output entries
68 * to a stream as LDIF.
69 *
70 * @param pw output stream
71 */
72 public LDIFWriter( PrintWriter pw ) {
73 this( pw, false, DEFAULT_SEPARATOR, true, false );
74 }
75
76 /**
77 * Constructs an <CODE>LDIFWriter</CODE> object to output entries
78 * to a stream as LDIF.
79 *
80 * @param pw output stream
81 * @param attrsOnly <code>true</code> if only attribute names, not
82 * values, are to be printed
83 * @param separator String to use between attribute names and values;
84 * the default is ":"
85 * @param foldLines <code>true</code> to fold lines at 77 characters,
86 * <code>false</code> to not fold them; the default is <code>true</code>.
87 * @param toFiles <code>true</code> to write each attribute value to a
88 * file in the temp folder, <code>false</code> to write them to the
89 * output stream in printable format; the default is <code>false</code>.
90 */
91 public LDIFWriter( PrintWriter pw, boolean attrsOnly,
92 String separator, boolean foldLines,
93 boolean toFiles ) {
94 super( pw );
95 m_attrsOnly = attrsOnly;
96 m_sep = separator;
97 m_foldLines = foldLines;
98 m_toFiles = toFiles;
99 }
100
101 /**
102 * Print an attribute of an entry
103 *
104 * @param attr the attribute to format to the output stream
105 */
106 protected void printAttribute( LDAPAttribute attr ) {
107 String attrName = attr.getName();
108
109 if ( m_attrsOnly ) {
110 printString( attrName + m_sep );
111 return;
112 }
113
114 /* Loop on values for this attribute */
115 Enumeration<byte[]> enumVals = attr.getByteValues();
116
117 if ( enumVals != null ) {
118 while (enumVals.hasMoreElements()) {
119 if ( m_toFiles ) {
120 try {
121 FileOutputStream f = getTempFile( attrName );
122 f.write( enumVals.nextElement() );
123 } catch ( Exception e ) {
124 System.err.println( "Error writing values " +
125 "of " + attrName + ", " +
126 e.toString() );
127 System.exit(1);
128 }
129 } else {
130 byte[] b = enumVals.nextElement();
131 String s;
132 if ( LDIF.isPrintable(b) ) {
133 try {
134 s = new String( b, "UTF8" );
135 } catch ( UnsupportedEncodingException e ) {
136 s = "";
137 }
138 printString( attrName + m_sep + " " + s );
139 } else {
140 s = getPrintableValue( b );
141 if ( s.length() > 0 ) {
142 printString( attrName + ":: " + s );
143 } else {
144 printString( attrName + m_sep + ' ' );
145 }
146 }
147 }
148 }
149 } else {
150 printString( attrName + m_sep + ' ' );
151 }
152 }
153
154 /**
155 * Print prologue to entry
156 *
157 * @param dn the DN of the entry
158 */
159 protected void printEntryStart( String dn ) {
160 if ( dn == null ) {
161 printString( "dn" + m_sep + " ");
162 } else {
163 byte[] b = null;
164 try {
165 b = dn.getBytes( "UTF8" );
166 } catch ( UnsupportedEncodingException ex ) {
167 }
168 if ( LDIF.isPrintable(b) ) {
169 printString( "dn" + m_sep + " " + dn );
170 } else {
171 dn = getPrintableValue( b );
172 printString( "dn" + m_sep + m_sep + " " + dn );
173 }
174 }
175 }
176
177 /**
178 * Print epilogue to entry
179 *
180 * @param dn the DN of the entry
181 */
182 protected void printEntryEnd( String dn ) {
183 m_pw.println();
184 }
185
186 protected void printString( String value ) {
187 if ( m_foldLines ) {
188 LDIF.breakString( m_pw, value, MAX_LINE );
189 } else {
190 m_pw.print( value );
191 m_pw.print( '\n' );
192 }
193 }
194
195 /**
196 * Create a unique file name in the temp folder and open an
197 * output stream to the file
198 *
199 * @param name base name of file; an extension is appended which
200 * consists of a number that makes the name unique
201 * @return an open output stream to the file
202 * @exception IOException if the file couldn't be opened for output
203 */
204 protected FileOutputStream getTempFile( String name )
205 throws IOException {
206 int num = 0;
207 File f;
208 String filename;
209 do {
210 filename = name + '.' + num;
211 f = new File( filename );
212 num++;
213 } while ( f.exists() );
214 printString(name + m_sep + " " + filename);
215 return new FileOutputStream( f );
216 }
217 }