8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster/**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * opensso/legal/CDDLv1.0.txt
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * at opensso/legal/CDDLv1.0.txt.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: PrintUtils.java,v 1.2 2008/06/25 05:41:27 qcheng Exp $
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpackage com.iplanet.am.util;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.io.PrintWriter;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Iterator;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Map;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterimport java.util.Set;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Fosterpublic class PrintUtils {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /** Each indentation will be 2 spaces wide. */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public static final int INDENT_WIDTH = 2;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private PrintWriter writer;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /*
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Creates a new PrintUtils object and sets the PrintWriter object. @param
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * writer the PrintWriter
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public PrintUtils(PrintWriter writer) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster super();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster this.writer = writer;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Prints AV Pairs with no indentation. this method calls the toString
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * method of the objects which are in the Map.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param avPairs
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * which contains a Map of attribute value pairs that should be
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * printed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void printAVPairs(Map avPairs) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster printAVPairs(avPairs, 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Prints AV Pairs with the specified indent level. Actual indentation will
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * be (indentLevel * INDENT_WIDTH). this method calls the toString method of
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the objects which are in the Map.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param avPairs
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * which contains a Map of attribute value pairs that should be
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * printed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param indentLevel
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the int value which specifies the width of the indent.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void printAVPairs(Map avPairs, int indentLevel) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set set = avPairs.keySet();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator itr = set.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object objAttribute;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Object objValue;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (itr.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objAttribute = itr.next();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster objValue = avPairs.get(objAttribute);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster printIndent(indentLevel);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writer.println(objAttribute.toString() + " = "
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster + objValue.toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writer.flush();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Prints the contents of a Set with no indentation this method calls the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * toString method of the objects which are in the set.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * which contains a set of objects that should be printed.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void printSet(Set set) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster printSet(set, 0);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Prints the contents of a Set with the specified indent level. Actual
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * indentation will be (indentLevel * INDENT_WIDTH). this method calls the
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * toString method of the objects which are in the set.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param set
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * which contains a set of objects that should be printed
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param indentLevel
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the int value which specifies the width of the indent.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void printSet(Set set, int indentLevel) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Iterator itr = set.iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster while (itr.hasNext()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster printIndent(indentLevel);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writer.println(itr.next().toString());
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writer.flush();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster /**
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * This method prints the indent based on the value of the indentLevel.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster *
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * @param indentLevel
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * the int value which specifies the size of the indent.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster */
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster public void printIndent(int indentLevel) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String indent = "";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster indentLevel = indentLevel * INDENT_WIDTH;
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (int i = 0; i < indentLevel; i++) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster indent = indent + " ";
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster writer.print(indent);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster }
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster}