| 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.IOException; |
| 41 | import java.io.PrintWriter; |
| 42 | import java.io.Serializable; |
| 43 | import java.util.Iterator; |
| 44 | |
| 45 | import org.ietf.ldap.LDAPAttribute; |
| 46 | import org.ietf.ldap.LDAPAttributeSet; |
| 47 | import org.ietf.ldap.LDAPEntry; |
| 48 | |
| 49 | /** |
| 50 | * Abstract class for outputting LDAP entries to a stream. |
| 51 | * |
| 52 | * @version 1.0 |
| 53 | */ |
| 54 | public abstract class LDAPWriter implements Serializable { |
| 55 | |
| 56 | // static final long serialVersionUID = -2710382547996750924L; |
| 57 | protected PrintWriter m_pw; |
| 58 | private static MimeBase64Encoder m_encoder = new MimeBase64Encoder(); |
| 59 | |
| 60 | /** |
| 61 | * Constructs an <CODE>LDAPWriter</CODE> object to output entries |
| 62 | * to a stream. |
| 63 | * |
| 64 | * @param pw output stream |
| 65 | */ |
| 66 | public LDAPWriter( PrintWriter pw ) { |
| 67 | m_pw = pw; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * The main method of LDAPWriter. It calls printEntryIntro, |
| 72 | * printAttribute, and printEntryEnd of derived classes. |
| 73 | * |
| 74 | * @param entry an LDAPEntry to be formatted to the output |
| 75 | * stream |
| 76 | */ |
| 77 | public void printEntry( LDAPEntry entry ) throws IOException { |
| 78 | printEntryStart( entry.getDN() ); |
| 79 | /* Get the attributes of the entry */ |
| 80 | LDAPAttributeSet attrs = entry.getAttributeSet(); |
| 81 | Iterator<LDAPAttribute> enumAttrs = attrs.iterator(); |
| 82 | /* Loop on attributes */ |
| 83 | while ( enumAttrs.hasNext() ) { |
| 84 | LDAPAttribute anAttr = enumAttrs.next(); |
| 85 | printAttribute( anAttr ); |
| 86 | } |
| 87 | printEntryEnd( entry.getDN() ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Default schema writer - assumes an ordinary entry |
| 92 | * |
| 93 | * @param entry an LDAPEntry containing schema to be formatted |
| 94 | * to the output stream |
| 95 | */ |
| 96 | public void printSchema( LDAPEntry entry ) throws IOException { |
| 97 | printEntry( entry ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Print an attribute of an entry |
| 102 | * |
| 103 | * @param attr the attribute to format to the output stream |
| 104 | */ |
| 105 | protected abstract void printAttribute( LDAPAttribute attr ); |
| 106 | |
| 107 | /** |
| 108 | * Print prologue to entry |
| 109 | * |
| 110 | * @param dn the DN of the entry |
| 111 | */ |
| 112 | protected abstract void printEntryStart( String dn ); |
| 113 | |
| 114 | /** |
| 115 | * Print epilogue to entry |
| 116 | * |
| 117 | * @param dn the DN of the entry |
| 118 | */ |
| 119 | protected abstract void printEntryEnd( String dn ); |
| 120 | |
| 121 | protected String getPrintableValue( byte[] b ) { |
| 122 | String s = ""; |
| 123 | ByteBuf inBuf = new ByteBuf( b, 0, b.length ); |
| 124 | ByteBuf encodedBuf = new ByteBuf(); |
| 125 | // Translate to base 64 |
| 126 | m_encoder.translate( inBuf, encodedBuf ); |
| 127 | int nBytes = encodedBuf.length(); |
| 128 | if ( nBytes > 0 ) { |
| 129 | s = new String(encodedBuf.toBytes(), |
| 130 | 0, nBytes); |
| 131 | } |
| 132 | return s; |
| 133 | } |
| 134 | } |