/** * Write a properly escaped attribute name and the corresponding value. The value text will be * converted to a String if necessary. This method may only be called after a call to <code> * startElement()</code>, and before the opened element has been closed. * * @param name Attribute name to be added * @param value Attribute value to be added * @param componentPropertyName The name of the component property to which this attribute * argument applies. This argument may be <code>null</code>. * @throws IllegalStateException if this method is called when there is no currently open element * @throws IOException if an input/output error occurs * @throws NullPointerException if <code>name</code> is <code>null</code> */ public void writeAttribute(String name, Object value, String componentPropertyName) throws IOException { if (name == null) { throw new NullPointerException( MessageUtils.getExceptionMessageString( MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name")); } if (value == null) { return; } if (isCdata) { return; } if (name.equalsIgnoreCase("src") && isScriptOrStyle()) { scriptOrStyleSrc = true; } Class valueClass = value.getClass(); // Output Boolean values specially if (valueClass == Boolean.class) { if (Boolean.TRUE.equals(value)) { // NOTE: HTML 4.01 states that boolean attributes // may legally take a single value which is the // name of the attribute itself or appear using // minimization. // http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4.2 attributesBuffer.write(' '); attributesBuffer.write(name); attributesBuffer.write("=\""); attributesBuffer.write(name); attributesBuffer.write('"'); } } else { attributesBuffer.write(' '); attributesBuffer.write(name); attributesBuffer.write("=\""); // write the attribute value String val = value.toString(); ensureTextBufferCapacity(val); HtmlUtils.writeAttribute( attributesBuffer, escapeUnicode, escapeIso, buffer, val, textBuffer, isScriptInAttributeValueEnabled); attributesBuffer.write('"'); } }
/** * Write a properly encoded URI attribute name and the corresponding value. The value text will be * converted to a String if necessary). This method may only be called after a call to <code> * startElement()</code>, and before the opened element has been closed. * * @param name Attribute name to be added * @param value Attribute value to be added * @param componentPropertyName The name of the component property to which this attribute * argument applies. This argument may be <code>null</code>. * @throws IllegalStateException if this method is called when there is no currently open element * @throws IOException if an input/output error occurs * @throws NullPointerException if <code>name</code> or <code>value</code> is <code>null</code> */ public void writeURIAttribute(String name, Object value, String componentPropertyName) throws IOException { if (name == null) { throw new NullPointerException( MessageUtils.getExceptionMessageString( MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "name")); } if (value == null) { throw new NullPointerException( MessageUtils.getExceptionMessageString( MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "value")); } if (isCdata) { return; } if (name.equalsIgnoreCase("src") && isScriptOrStyle()) { scriptOrStyleSrc = true; } attributesBuffer.write(' '); attributesBuffer.write(name); attributesBuffer.write("=\""); String stringValue = value.toString(); ensureTextBufferCapacity(stringValue); // Javascript URLs should not be URL-encoded if (stringValue.startsWith("javascript:")) { HtmlUtils.writeAttribute( attributesBuffer, escapeUnicode, escapeIso, buffer, stringValue, textBuffer, isScriptInAttributeValueEnabled); } else { HtmlUtils.writeURL(attributesBuffer, stringValue, textBuffer, encoding); } attributesBuffer.write('"'); }