001: /*
002: * $Id: FormattedParameters.java,v 1.2 2007/09/18 08:45:10 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common;
008:
009: import java.util.Iterator;
010:
011: import org.xins.common.collections.PropertyReader;
012: import org.xins.common.text.URLEncoding;
013: import org.xins.common.xml.Element;
014:
015: /**
016: * Convert parameters to (semi) human readable text.
017: *
018: * @version $Revision: 1.2 $ $Date: 2007/09/18 08:45:10 $
019: * @author <a href="mailto:ernst@ernstdehaan.com">Ernst de Haan</a>
020: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
021: *
022: * @since XINS 2.0
023: */
024: public class FormattedParameters {
025:
026: /**
027: * The parameters to serialize. This field can be <code>null</code>.
028: */
029: private final PropertyReader _parameters;
030:
031: /**
032: * The data section.
033: */
034: private final Element _dataSection;
035:
036: /**
037: * The value if there is no parameters and data section.
038: */
039: private final String _valueIfEmpty;
040:
041: /**
042: * The prefix if there is at least a parameter or a data section.
043: */
044: private final String _prefixIfNotEmpty;
045:
046: /**
047: * The maximum limit of serialized value characters.
048: */
049: private final int _maxValueLength;
050:
051: /**
052: * The String representation of the parameters.
053: */
054: private String _asString;
055:
056: /**
057: * Constructs a new <code>FormattedParameters</code> object.
058: *
059: * @param parameters
060: * the parameters, can be <code>null</code>.
061: */
062: public FormattedParameters(PropertyReader parameters) {
063: this (parameters, null);
064: }
065:
066: /**
067: * Constructs a new <code>FormattedParameters</code> object.
068: *
069: * @param parameters
070: * the parameters, can be <code>null</code>.
071: *
072: * @param dataSection
073: * the data section, can be <code>null</code>.
074: */
075: public FormattedParameters(PropertyReader parameters,
076: Element dataSection) {
077: this (parameters, dataSection, "-", null, -1);
078: }
079:
080: /**
081: * Constructs a new <code>FormattedParameters</code> object.
082: *
083: * @param parameters
084: * the parameters, can be <code>null</code>.
085: *
086: * @param dataSection
087: * the data section, can be <code>null</code>.
088: *
089: * @param valueIfEmpty
090: * the value to return if there is no parameter and no data section, can be <code>null</code>.
091: *
092: * @param prefixIfNotEmpty
093: * the prefix to add if there is a parameter or a data section, can be <code>null</code>.
094: *
095: * @param maxValueLength
096: * the maximum of characters to set for the value, if the value is longer
097: * than this limit '...' will be added after the limit.
098: * If the value is -1, no limit will be set.
099: */
100: public FormattedParameters(PropertyReader parameters,
101: Element dataSection, String valueIfEmpty,
102: String prefixIfNotEmpty, int maxValueLength) {
103: _parameters = parameters;
104: _dataSection = dataSection;
105: _valueIfEmpty = valueIfEmpty;
106: _prefixIfNotEmpty = prefixIfNotEmpty;
107: _maxValueLength = maxValueLength;
108: }
109:
110: /**
111: * String representation of the parameters including the data section.
112: *
113: * @return
114: * the String representation of the request.
115: */
116: public String toString() {
117:
118: // The String representation has already been created.
119: if (_asString != null) {
120: return _asString;
121: }
122:
123: Iterator names = (_parameters == null) ? null : _parameters
124: .getNames();
125:
126: // If there are no parameters, then just return a hyphen
127: if ((names == null || !names.hasNext()) && _dataSection == null) {
128: _asString = _valueIfEmpty;
129: return _asString;
130: }
131:
132: StringBuffer buffer = new StringBuffer(
133: 80 + _parameters.size() * 40);
134: if (_prefixIfNotEmpty != null) {
135: buffer.append(_prefixIfNotEmpty);
136: }
137:
138: boolean first = true;
139: while (names != null && names.hasNext()) {
140:
141: // Get the name and value
142: String name = (String) names.next();
143: String value = _parameters.get(name);
144:
145: // If the value is null or an empty string, then output nothing
146: if (value == null || value.length() == 0) {
147: continue;
148: }
149:
150: // Append an ampersand, except for the first entry
151: if (!first) {
152: buffer.append('&');
153: } else {
154: first = false;
155: }
156:
157: // Append the key and the value, separated by an equals sign
158: buffer.append(URLEncoding.encode(name));
159: buffer.append('=');
160: String encodedValue;
161: if (_maxValueLength == -1
162: || value.length() <= _maxValueLength) {
163: encodedValue = URLEncoding.encode(value);
164: } else {
165: encodedValue = URLEncoding.encode(value.substring(0,
166: _maxValueLength))
167: + "...";
168: }
169: buffer.append(encodedValue);
170: }
171:
172: if (_dataSection != null) {
173: if (!first) {
174: buffer.append('&');
175: }
176: buffer.append("_data=");
177: buffer.append(URLEncoding.encode(_dataSection.toString()));
178: }
179:
180: _asString = buffer.toString();
181: return _asString;
182: }
183: }
|