001 /* 002 // $Id: //open/util/resgen/src/org/eigenbase/xom/XMLAttrVector.java#3 $ 003 // Package org.eigenbase.xom is an XML Object Mapper. 004 // Copyright (C) 2005-2005 The Eigenbase Project 005 // Copyright (C) 2005-2005 Disruptive Tech 006 // Copyright (C) 2005-2005 LucidEra, Inc. 007 // Portions Copyright (C) 2000-2005 Kana Software, Inc. and others. 008 // 009 // This library is free software; you can redistribute it and/or modify it 010 // under the terms of the GNU Lesser General Public License as published by the 011 // Free Software Foundation; either version 2 of the License, or (at your 012 // option) any later version approved by The Eigenbase Project. 013 // 014 // This library is distributed in the hope that it will be useful, 015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 // GNU Lesser General Public License for more details. 018 // 019 // You should have received a copy of the GNU Lesser General Public License 020 // along with this library; if not, write to the Free Software 021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 // 023 // dsommerfield, 12 December, 2000 024 */ 025 026 package org.eigenbase.xom; 027 import java.io.PrintWriter; 028 import java.util.Vector; 029 030 /** 031 * XMLAttrVector is an class which assists in writing XML attributes to a 032 * stream. 033 */ 034 public class XMLAttrVector { 035 036 // Vector to hold all attributes and their values. 037 private Vector attrs; 038 039 /** 040 * This private helper class holds an attribute-value pair. It is used 041 * as the element of the vector attrs. 042 */ 043 private static class AttrVal 044 { 045 public AttrVal(String attr, String val) 046 { 047 this.attr = attr; 048 this.val = val; 049 } 050 051 public String attr; 052 public String val; 053 } 054 055 /** 056 * Construct an empty XMLAttrVector. Attribute/value pairs may be added 057 * with the add() functions below. 058 */ 059 public XMLAttrVector() 060 { 061 attrs = new Vector(); 062 } 063 064 /** 065 * Returns the number of attributes. 066 **/ 067 public int size() 068 { 069 return attrs.size(); 070 } 071 072 /** 073 * Add a new attribute/value pair based on a String value. Note that 074 * attrVal may be null, in which case no attribute/value pair is added. 075 * @param attrName the name of the attribute. 076 * @param attrVal the String value of the attribute. 077 * @return this (to allow chaining) 078 */ 079 public XMLAttrVector add(String attrName, Object attrVal) 080 { 081 if(attrVal != null) 082 attrs.addElement(new AttrVal(attrName, attrVal.toString())); 083 return this; 084 } 085 086 /** 087 * Add a new attribute/value pair based on an int value. 088 * @param attrName the name of the attribute. 089 * @param attrVal the int value of the attribute. 090 * @return this (to allow chaining) 091 */ 092 public XMLAttrVector add(String attrName, int attrVal) 093 { 094 attrs.addElement(new AttrVal(attrName, ""+attrVal)); 095 return this; 096 } 097 098 /** 099 * Add a new attribute/value pair based on a double value. 100 * @param attrName the name of the attribute. 101 * @param attrVal the double value of the attribute. 102 * @return this (to allow chaining) 103 */ 104 public XMLAttrVector add(String attrName, double attrVal) 105 { 106 attrs.addElement(new AttrVal(attrName, ""+attrVal)); 107 return this; 108 } 109 110 /** 111 * Add a new attribute/value pair based on a boolean value. 112 * True is represented as "true", and false as "false". 113 * @param attrName the name of the attribute. 114 * @param attrVal the boolean value of the attribute. 115 * @return this (to allow chaining) 116 */ 117 public XMLAttrVector add(String attrName, boolean attrVal) 118 { 119 if(attrVal) 120 attrs.addElement(new AttrVal(attrName, "true")); 121 else 122 attrs.addElement(new AttrVal(attrName, "false")); 123 return this; 124 } 125 126 /** 127 * Displays the entire attribute/value pair list, given a PrintWriter 128 * to which to display and an indentation level. 129 * This function is typically called from XMLOutput. 130 * @param out PrintWriter to which to write output. 131 * @param indent indentation level. 132 */ 133 public void display(PrintWriter out, int indent) 134 { 135 // The indentation level is not used; all attribute/value pairs 136 // are rendered on the same line. 137 for(int i=0; i<attrs.size(); i++) { 138 AttrVal av = (AttrVal)(attrs.elementAt(i)); 139 if (av.val != null) { 140 out.print(" "); 141 out.print(av.attr); 142 out.print("=\""); 143 out.print(StringEscaper.xmlNumericEscaper.escapeString(av.val)); 144 out.print("\""); 145 } 146 } 147 } 148 } 149 150 151 // End XMLAttrVector.java