📄 abstractbeanwriter.java
字号:
log.trace( "Is " + descriptor + " empty?" );
}
// an element which has attributes is not empty
if ( descriptor.hasAttributes() ) {
log.trace( "Element has attributes." );
return false;
}
// an element is not empty if it has a non-empty body
Expression expression = descriptor.getTextExpression();
if ( expression != null ) {
Object value = expression.evaluate( context );
String text = convertToString( value, descriptor, context );
if ( text != null && text.length() > 0 ) {
log.trace( "Element has body text which isn't empty." );
return false;
}
}
// always write out loops - even when they have no elements
if ( XMLIntrospectorHelper.isLoopType( descriptor.getPropertyType() ) ) {
log.trace("Loop type so not empty.");
return false;
}
// now test child elements
// an element is empty if it has no non-empty child elements
if ( descriptor.hasChildren() ) {
for ( int i=0, size=descriptor.getElementDescriptors().length; i<size; i++ ) {
if ( ! isEmptyElement( descriptor.getElementDescriptors()[i], context ) ) {
log.trace( "Element has child which isn't empty." );
return false;
}
}
}
log.trace( "Element is empty." );
return true;
}
/**
* Attributes backed by attribute descriptors.
* ID/IDREFs not set.
*/
private class ElementAttributes implements Attributes {
/** Attribute descriptors backing the <code>Attributes</code> */
private AttributeDescriptor[] attributes;
/** Context to be evaluated when finding values */
private Context context;
/**
* Construct attributes for element and context.
*
* @param descriptor the <code>ElementDescriptor</code> describing the element
* @param context evaluate against this context
*/
ElementAttributes( ElementDescriptor descriptor, Context context ) {
attributes = descriptor.getAttributeDescriptors();
this.context = context;
}
/**
* Gets the index of an attribute by qualified name.
*
* @param qName the qualified name of the attribute
* @return the index of the attribute - or -1 if there is no matching attribute
*/
public int getIndex( String qName ) {
for ( int i=0; i<attributes.length; i++ ) {
if (attributes[i].getQualifiedName() != null
&& attributes[i].getQualifiedName().equals( qName )) {
return i;
}
}
return -1;
}
/**
* Gets the index of an attribute by namespace name.
*
* @param uri the namespace uri of the attribute
* @param localName the local name of the attribute
* @return the index of the attribute - or -1 if there is no matching attribute
*/
public int getIndex( String uri, String localName ) {
for ( int i=0; i<attributes.length; i++ ) {
if (
attributes[i].getURI() != null
&& attributes[i].getURI().equals(uri)
&& attributes[i].getLocalName() != null
&& attributes[i].getURI().equals(localName)) {
return i;
}
}
return -1;
}
/**
* Gets the number of attributes in the list.
*
* @return the number of attributes in this list
*/
public int getLength() {
return attributes.length;
}
/**
* Gets the local name by index.
*
* @param index the attribute index (zero based)
* @return the attribute local name - or null if the index is out of range
*/
public String getLocalName( int index ) {
if ( indexInRange( index ) ) {
return attributes[index].getLocalName();
}
return null;
}
/**
* Gets the qualified name by index.
*
* @param index the attribute index (zero based)
* @return the qualified name of the element - or null if the index is our of range
*/
public String getQName( int index ) {
if ( indexInRange( index ) ) {
return attributes[index].getQualifiedName();
}
return null;
}
/**
* Gets the attribute SAX type by namespace name.
*
* @param index the attribute index (zero based)
* @return the attribute type (as a string) or null if the index is out of range
*/
public String getType( int index ) {
if ( indexInRange( index ) ) {
return "CDATA";
}
return null;
}
/**
* Gets the attribute SAX type by qualified name.
*
* @param qName the qualified name of the attribute
* @return the attribute type (as a string) or null if the attribute is not in the list
*/
public String getType( String qName ) {
return getType( getIndex( qName ) );
}
/**
* Gets the attribute SAX type by namespace name.
*
* @param uri the namespace uri of the attribute
* @param localName the local name of the attribute
* @return the attribute type (as a string) or null if the attribute is not in the list
*/
public String getType( String uri, String localName ) {
return getType( getIndex( uri, localName ));
}
/**
* Gets the namespace URI for attribute at the given index.
*
* @param index the attribute index (zero-based)
* @return the namespace URI (empty string if none is available)
* or null if the index is out of range
*/
public String getURI( int index ) {
if ( indexInRange( index ) ) {
return attributes[index].getURI();
}
return null;
}
/**
* Gets the value for the attribute at given index.
*
* @param index the attribute index (zero based)
* @return the attribute value or null if the index is out of range
* @todo add value caching
*/
public String getValue( int index ) {
if ( indexInRange( index )) {
Expression expression = attributes[index].getTextExpression();
if ( expression != null ) {
Object value = expression.evaluate( context );
return convertToString( value, attributes[index], context );
}
return "";
}
return null;
}
/**
* Gets the value for the attribute by qualified name.
*
* @param qName the qualified name
* @return the attribute value or null if there are no attributes
* with the given qualified name
* @todo add value caching
*/
public String getValue( String qName ) {
return getValue( getIndex( qName ) );
}
/**
* Gets the value for the attribute by namespace name.
*
* @param uri the namespace URI of the attribute
* @param localName the local name of the attribute
* @return the attribute value or null if there are not attributes
* with the given namespace and local name
* @todo add value caching
*/
public String getValue( String uri, String localName ) {
return getValue( getIndex( uri, localName ) );
}
/**
* Is the given index within the range of the attribute list
*
* @param index the index whose range will be checked
* @return true if the index with within the range of the attribute list
*/
private boolean indexInRange( int index ) {
return ( index >= 0 && index < attributes.length );
}
}
/**
* Attributes with generate ID/IDREF attributes
* //TODO: refactor the ID/REF generation so that it's fixed at introspection
* and the generators are placed into the Context.
* @author <a href='http://jakarta.apache.org/'>Jakarta Commons Team</a>
* @version $Revision: 1.28 $
*/
private class IDElementAttributes extends ElementAttributes {
/** ID attribute value */
private String idValue;
/** ID attribute name */
private String idAttributeName;
private boolean matchingAttribute = false;
private int length;
private int idIndex;
/**
* Construct attributes for element and context.
*
* @param descriptor the <code>ElementDescriptor</code> describing the element
* @param context evaluate against this context
* @param idAttributeName the name of the id attribute
* @param idValue the ID attribute value
*/
IDElementAttributes(
ElementDescriptor descriptor,
Context context,
String idAttributeName,
String idValue) {
super(descriptor, context);
this.idValue = idValue;
this.idAttributeName = idAttributeName;
// see if we have already have a matching attribute descriptor
AttributeDescriptor[] attributeDescriptors = descriptor.getAttributeDescriptors();
length = attributeDescriptors.length;
for (int i=0; i<length; i++) {
if (idAttributeName.equals(attributeDescriptors[i])) {
matchingAttribute = true;
idIndex = i;
break;
}
}
if (!matchingAttribute) {
length += 1;
idIndex = length-1;
}
}
public int getIndex(String uri, String localName) {
if (localName.equals(idAttributeName)) {
return idIndex;
}
return super.getIndex(uri, localName);
}
public int getIndex(String qName) {
if (qName.equals(idAttributeName)) {
return idIndex;
}
return super.getIndex(qName);
}
public int getLength() {
return length;
}
public String getLocalName(int index) {
if (index == idIndex) {
return idAttributeName;
}
return super.getLocalName(index);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -