📄 xmlintrospectorhelper.java
字号:
if ( log.isDebugEnabled() ) {
log.debug( "!! " + method);
log.debug( "!! " + types[0]);
}
// is there a child element with no localName
ElementDescriptor[] children
= descriptor.getElementDescriptors();
if ( children != null && children.length > 0 ) {
ElementDescriptor child = children[0];
String localName = child.getLocalName();
if ( localName == null || localName.length() == 0 ) {
child.setLocalName(
introspector.getElementNameMapper()
.mapTypeToElementName( propertyName ) );
}
}
} else if ( isMapDescriptor && types.length == 2 ) {
// this may match a map
log.trace("Matching map");
ElementDescriptor[] children
= descriptor.getElementDescriptors();
// see if the descriptor's been set up properly
if ( children.length == 0 ) {
log.info(
"'entry' descriptor is missing for map. "
+ "Updaters cannot be set");
} else {
// loop through grandchildren
// adding updaters for key and value
ElementDescriptor[] grandchildren
= children[0].getElementDescriptors();
MapEntryAdder adder = new MapEntryAdder(method);
for (
int n=0,
noOfGrandChildren = grandchildren.length;
n < noOfGrandChildren;
n++ ) {
if ( "key".equals(
grandchildren[n].getLocalName() ) ) {
grandchildren[n].setUpdater(
adder.getKeyUpdater() );
grandchildren[n].setSingularPropertyType(
types[0] );
if ( log.isTraceEnabled() ) {
log.trace(
"Key descriptor: " + grandchildren[n]);
}
} else if (
"value".equals(
grandchildren[n].getLocalName() ) ) {
grandchildren[n].setUpdater(
adder.getValueUpdater() );
grandchildren[n].setSingularPropertyType(
types[1] );
if ( log.isTraceEnabled() ) {
log.trace(
"Value descriptor: " + grandchildren[n]);
}
}
}
}
}
} else {
if ( log.isDebugEnabled() ) {
log.debug(
"Could not find an ElementDescriptor with property name: "
+ propertyName + " to attach the add method: " + method
);
}
}
}
}
}
}
}
}
/**
* Is this a loop type class?
*
* @param type is this <code>Class</code> a loop type?
* @return true if the type is a loop type, or if type is null
*/
public static boolean isLoopType(Class type) {
// check for NPEs
if (type == null) {
log.trace("isLoopType: type is null");
return false;
}
return type.isArray()
|| Map.class.isAssignableFrom( type )
|| Collection.class.isAssignableFrom( type )
|| Enumeration.class.isAssignableFrom( type )
|| Iterator.class.isAssignableFrom( type );
}
/**
* Is this a primitive type?
*
* TODO: this method will probably be removed when primitive types
* are subsumed into the simple type concept.
* This needs moving into XMLIntrospector so that the list of simple
* type can be varied.
* @param type is this <code>Class<code> a primitive type?
* @return true for primitive types
* @deprecated 0.6 replaced by {@link org.apache.commons.betwixt.strategy.TypeBindingStrategy}
*/
public static boolean isPrimitiveType(Class type) {
if ( type == null ) {
return false;
} else if ( type.isPrimitive() ) {
return true;
} else if ( type.equals( Object.class ) ) {
return false;
}
return type.getName().startsWith( "java.lang." )
|| Number.class.isAssignableFrom( type )
|| String.class.isAssignableFrom( type )
|| Date.class.isAssignableFrom( type )
|| java.sql.Date.class.isAssignableFrom( type )
|| java.sql.Time.class.isAssignableFrom( type )
|| java.sql.Timestamp.class.isAssignableFrom( type )
|| java.math.BigDecimal.class.isAssignableFrom( type )
|| java.math.BigInteger.class.isAssignableFrom( type );
}
// Implementation methods
//-------------------------------------------------------------------------
/**
* Attempts to find the element descriptor for the getter property that
* typically matches a collection or array. The property name is used
* to match. e.g. if an addChild() method is detected the
* descriptor for the 'children' getter property should be returned.
*
* @param introspector use this <code>XMLIntrospector</code>
* @param rootDescriptor the <code>ElementDescriptor</code> whose child element will be
* searched for a match
* @param propertyName the name of the 'adder' method to match
* @return <code>ElementDescriptor</code> for the matching getter
* @deprecated 0.6 moved into XMLIntrospector
*/
protected static ElementDescriptor findGetCollectionDescriptor(
XMLIntrospector introspector,
ElementDescriptor rootDescriptor,
String propertyName ) {
// create the Map of propertyName -> descriptor that the PluralStemmer will choose
Map map = new HashMap();
//String propertyName = rootDescriptor.getPropertyName();
if ( log.isTraceEnabled() ) {
log.trace( "findPluralDescriptor( " + propertyName
+ " ):root property name=" + rootDescriptor.getPropertyName() );
}
if (rootDescriptor.getPropertyName() != null) {
map.put(propertyName, rootDescriptor);
}
makeElementDescriptorMap( rootDescriptor, map );
PluralStemmer stemmer = introspector.getPluralStemmer();
ElementDescriptor elementDescriptor = stemmer.findPluralDescriptor( propertyName, map );
if ( log.isTraceEnabled() ) {
log.trace(
"findPluralDescriptor( " + propertyName
+ " ):ElementDescriptor=" + elementDescriptor );
}
return elementDescriptor;
}
/**
* Creates a map where the keys are the property names and the values are the ElementDescriptors
*
* @param rootDescriptor the values of the maps are the children of this
* <code>ElementDescriptor</code> index by their property names
* @param map the map to which the elements will be added
* @deprecated 0.6 moved into XMLIntrospector
*/
protected static void makeElementDescriptorMap( ElementDescriptor rootDescriptor, Map map ) {
ElementDescriptor[] children = rootDescriptor.getElementDescriptors();
if ( children != null ) {
for ( int i = 0, size = children.length; i < size; i++ ) {
ElementDescriptor child = children[i];
String propertyName = child.getPropertyName();
if ( propertyName != null ) {
map.put( propertyName, child );
}
makeElementDescriptorMap( child, map );
}
}
}
/**
* Traverse the tree of element descriptors and find the oldValue and swap it with the newValue.
* This would be much easier to do if ElementDescriptor supported a parent relationship.
*
* @param rootDescriptor traverse child graph for this <code>ElementDescriptor</code>
* @param oldValue replace this <code>ElementDescriptor</code>
* @param newValue replace with this <code>ElementDescriptor</code>
* @deprecated 0.6 now unused
*/
protected static void swapDescriptor(
ElementDescriptor rootDescriptor,
ElementDescriptor oldValue,
ElementDescriptor newValue ) {
ElementDescriptor[] children = rootDescriptor.getElementDescriptors();
if ( children != null ) {
for ( int i = 0, size = children.length; i < size; i++ ) {
ElementDescriptor child = children[i];
if ( child == oldValue ) {
children[i] = newValue;
break;
}
swapDescriptor( child, oldValue, newValue );
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -